本文整理汇总了C#中DB.Write方法的典型用法代码示例。如果您正苦于以下问题:C# DB.Write方法的具体用法?C# DB.Write怎么用?C# DB.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DB
的用法示例。
在下文中一共展示了DB.Write方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestIteratorWithSnapshot
public void TestIteratorWithSnapshot()
{
using (var db = new DB(new Options { CreateIfMissing = true }, "test2.db"))
{
using (var batch = new WriteBatch())
{
for (int i = 0; i < 100; i++)
{
batch.Put(Slice.FromString($"key::{i,20:D20}"), Slice.FromString($"{i,32}"));
}
db.Write(new WriteOptions { Sync = true }, batch);
}
using (var snapshot = db.GetSnapshot())
using (var batch = new WriteBatch())
using (var itr = db.NewIterator(new ReadOptions { Snapshot = snapshot }))
{
itr.Seek(Slice.FromString("key::"));
Assert.IsTrue(itr.Valid());
int entriesDeleted = 0;
while (itr.Valid())
{
batch.Delete(itr.Key());
itr.Next();
entriesDeleted++;
}
db.Write(new WriteOptions(), batch);
Assert.AreEqual(100, entriesDeleted);
}
}
}
示例2: LevelDBBlockchain
public LevelDBBlockchain()
{
Slice value;
db = DB.Open(Settings.Default.DataDirectoryPath);
if (db.TryGet(ReadOptions.Default, SliceBuilder.Begin(DataEntryPrefix.CFG_Initialized), out value) && value.ToBoolean())
{
value = db.Get(ReadOptions.Default, SliceBuilder.Begin(DataEntryPrefix.SYS_CurrentBlock));
this.current_block = new UInt256(value.ToArray().Take(32).ToArray());
this.current_height = BitConverter.ToUInt32(value.ToArray(), 32);
}
else
{
WriteBatch batch = new WriteBatch();
ReadOptions options = new ReadOptions { FillCache = false };
using (Iterator it = db.NewIterator(options))
{
for (it.SeekToFirst(); it.Valid(); it.Next())
{
batch.Delete(it.Key());
}
}
batch.Put(SliceBuilder.Begin(DataEntryPrefix.CFG_Version), 0);
db.Write(WriteOptions.Default, batch);
AddBlockToChain(GenesisBlock);
db.Put(WriteOptions.Default, SliceBuilder.Begin(DataEntryPrefix.CFG_Initialized), true);
}
thread_persistence = new Thread(PersistBlocks);
thread_persistence.Name = "LevelDBBlockchain.PersistBlocks";
thread_persistence.Start();
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
}
示例3: BatchWriteRangeInTask
private static Task BatchWriteRangeInTask(string dbName, int offset, int limit)
{
return Task.Run(() =>
{
using (var db = new DB(new Options { CreateIfMissing = true }, dbName))
using (var batch = new WriteBatch())
{
for (int i = offset; i < limit; i++)
{
batch.Put(Slice.FromString($"key::{i,20:D20}"), Slice.FromString($"{i,32}"));
}
db.Write(new WriteOptions { Sync = true }, batch);
}
});
}
示例4: LevelDBBlockchain
public LevelDBBlockchain(string path)
{
header_index.Add(GenesisBlock.Hash);
Version version;
Slice value;
db = DB.Open(path, new Options { CreateIfMissing = true });
if (db.TryGet(ReadOptions.Default, SliceBuilder.Begin(DataEntryPrefix.CFG_Version), out value) && Version.TryParse(value.ToString(), out version) && version >= Version.Parse("0.4"))
{
ReadOptions options = new ReadOptions { FillCache = false };
value = db.Get(options, SliceBuilder.Begin(DataEntryPrefix.SYS_CurrentBlock));
this.current_block_hash = new UInt256(value.ToArray().Take(32).ToArray());
this.current_block_height = BitConverter.ToUInt32(value.ToArray(), 32);
foreach (Block header in db.Find(options, SliceBuilder.Begin(DataEntryPrefix.DATA_HeaderList), (k, v) =>
{
using (MemoryStream ms = new MemoryStream(v.ToArray(), false))
using (BinaryReader r = new BinaryReader(ms))
{
return new
{
Index = BitConverter.ToUInt32(k.ToArray(), 1),
Headers = r.ReadSerializableArray<Block>()
};
}
}).OrderBy(p => p.Index).SelectMany(p => p.Headers).ToArray())
{
if (header.Hash != GenesisBlock.Hash)
{
header_chain.Add(header.Hash, header, header.PrevBlock);
header_index.Add(header.Hash);
}
stored_header_count++;
}
if (stored_header_count == 0)
{
Dictionary<UInt256, Block> table = db.Find(options, SliceBuilder.Begin(DataEntryPrefix.DATA_Block), (k, v) => Block.FromTrimmedData(v.ToArray(), 0)).ToDictionary(p => p.PrevBlock);
for (UInt256 hash = GenesisBlock.Hash; hash != current_block_hash;)
{
Block header = table[hash];
header_chain.Add(header.Hash, header, header.PrevBlock);
header_index.Add(header.Hash);
hash = header.Hash;
}
}
else if (current_block_height >= stored_header_count)
{
List<Block> list = new List<Block>();
for (UInt256 hash = current_block_hash; hash != header_index[(int)stored_header_count - 1];)
{
Block header = Block.FromTrimmedData(db.Get(options, SliceBuilder.Begin(DataEntryPrefix.DATA_Block).Add(hash)).ToArray(), 0);
list.Add(header);
header_index.Insert((int)stored_header_count, hash);
hash = header.PrevBlock;
}
for (int i = list.Count - 1; i >= 0; i--)
{
header_chain.Add(list[i].Hash, list[i], list[i].PrevBlock);
}
}
this.current_header_hash = header_index[header_index.Count - 1];
}
else
{
WriteBatch batch = new WriteBatch();
ReadOptions options = new ReadOptions { FillCache = false };
using (Iterator it = db.NewIterator(options))
{
for (it.SeekToFirst(); it.Valid(); it.Next())
{
batch.Delete(it.Key());
}
}
db.Write(WriteOptions.Default, batch);
Persist(GenesisBlock);
db.Put(WriteOptions.Default, SliceBuilder.Begin(DataEntryPrefix.CFG_Version), Assembly.GetExecutingAssembly().GetName().Version.ToString());
}
thread_persistence = new Thread(PersistBlocks);
thread_persistence.Name = "LevelDBBlockchain.PersistBlocks";
thread_persistence.Start();
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
}
示例5: TestWriteBatch
public void TestWriteBatch()
{
var path = CleanTestDB();
using (var db = new DB(path, new Options { CreateIfMissing = true }))
{
db.Put("NA", "Na");
using (var batch = new WriteBatch())
{
batch.Delete("NA")
.Put("Tampa", "Green")
.Put("London", "red")
.Put("New York", "blue");
db.Write(batch);
}
var expected = new[] { "London", "New York", "Tampa" };
var actual = from kv in db as IEnumerable<KeyValuePair<string, string>>
select kv.Key;
CollectionAssert.AreEqual(expected, actual.ToArray());
}
}