本文整理汇总了C#中WriteBatch类的典型用法代码示例。如果您正苦于以下问题:C# WriteBatch类的具体用法?C# WriteBatch怎么用?C# WriteBatch使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WriteBatch类属于命名空间,在下文中一共展示了WriteBatch类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Read
public ReadResult Read(string treeName, Slice key, WriteBatch writeBatch = null)
{
Tree tree = null;
if (writeBatch != null)
{
WriteBatch.BatchOperationType operationType;
Stream stream;
ushort? version;
if (writeBatch.TryGetValue(treeName, key, out stream, out version, out operationType))
{
if (!version.HasValue)
tree = GetTree(treeName);
switch (operationType)
{
case WriteBatch.BatchOperationType.Add:
{
var reader = new ValueReader(stream);
return new ReadResult(reader, version.HasValue ? (ushort)(version.Value + 1) : tree.ReadVersion(key));
}
case WriteBatch.BatchOperationType.Delete:
return null;
}
}
}
if (tree == null)
tree = GetTree(treeName);
return tree.Read(key);
}
示例2: CanGetLatestVersion
public async Task CanGetLatestVersion()
{
var storage = await NewStorageAsync();
var name = storage.Name;
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 65; i++)
{
for (int k = 0; k < 4; k++)
{
var writeBatch = new WriteBatch();
writeBatch.Put("A" + k, new MemoryStream(new[] { (byte)i, (byte)k }));
await storage.Writer.WriteAsync(writeBatch);
}
}
}
var fileSystem = storage.StorageState.FileSystem;
storage.Dispose();
using (var newStorage = new Storage(new StorageState(name, new StorageOptions())
{
FileSystem = fileSystem
}))
{
await newStorage.InitAsync();
var stream = newStorage.Reader.Read("A2");
Assert.Equal(64, stream.ReadByte());
}
}
示例3: Accept
public override void Accept(Disk d)
{
var ms = new MemoryStream();
_serializer.Serialize(new JsonTextWriter(new StreamWriter(ms)), d);
ms.Position = 0;
var key = new Slice(EndianBitConverter.Big.GetBytes(counter++));
_currentBatch.Add(key, ms, "albums");
foreach (var diskId in d.DiskIds)
{
_currentBatch.MultiAdd(diskId, key, "ix_diskids");
}
if(d.Artist != null)
_currentBatch.MultiAdd(d.Artist.ToLower(), key, "ix_artists");
if (d.Title != null)
_currentBatch.MultiAdd(d.Title.ToLower(), key, "ix_titles");
if (counter%1000 == 0)
{
_storageEnvironment.Writer.Write(_currentBatch);
_currentBatch = new WriteBatch();
}
}
示例4: CanOpenAndCloseWithUpdate
public async Task CanOpenAndCloseWithUpdate()
{
var storage = await NewStorageAsync();
var name = storage.Name;
var str1 = "test1";
var str2 = "test2";
var s1 = new MemoryStream(Encoding.UTF8.GetBytes(str1));
var s2 = new MemoryStream(Encoding.UTF8.GetBytes(str2));
var writeBatch = new WriteBatch();
writeBatch.Put("A", s1);
await storage.Writer.WriteAsync(writeBatch);
writeBatch = new WriteBatch();
writeBatch.Put("A", s2);
await storage.Writer.WriteAsync(writeBatch);
var fileSystem = storage.StorageState.FileSystem;
storage.Dispose();
using (var newStorage = new Storage(new StorageState(name, new StorageOptions())
{
FileSystem = fileSystem
}))
{
await newStorage.InitAsync();
AssertEqual(str2, newStorage.Reader.Read("A"));
}
}
示例5: ReadVersion_Items_From_Both_WriteBatch_And_Snapshot_WithoutVersionNumber
public void ReadVersion_Items_From_Both_WriteBatch_And_Snapshot_WithoutVersionNumber()
{
using (var tx = Env.NewTransaction(TransactionFlags.ReadWrite))
{
Env.CreateTree(tx, "tree");
tx.Environment.CreateTree(tx,"tree").Add("foo1", StreamFor("foo1"));
tx.Commit();
}
using (var writeBatch = new WriteBatch())
using (var snapshot = Env.CreateSnapshot())
{
writeBatch.Add("foo2", StreamFor("foo2"), "tree");
var foor1Version = snapshot.ReadVersion("tree", "foo1", writeBatch);
var foo2Version = snapshot.ReadVersion("tree", "foo2", writeBatch);
var foo2VersionThatShouldBe0 = snapshot.ReadVersion("tree", "foo2");
Assert.Equal(1, foor1Version);
Assert.Equal(0, foo2Version); //added to write batch without version number, so 0 is version number that is fetched
Assert.Equal(0, foo2VersionThatShouldBe0);
}
}
示例6: Read_Items_From_Both_WriteBatch_And_Snapshot
public void Read_Items_From_Both_WriteBatch_And_Snapshot()
{
using (var tx = Env.NewTransaction(TransactionFlags.ReadWrite))
{
Env.CreateTree(tx, "tree");
tx.Environment.CreateTree(tx,"tree").Add("foo1", StreamFor("foo1"));
tx.Commit();
}
using (var writeBatch = new WriteBatch())
using (var snapshot = Env.CreateSnapshot())
{
writeBatch.Add("foo2", StreamFor("foo2"), "tree");
var foo1ReadResult = snapshot.Read("tree", "foo1", writeBatch);
var foo2ReadResult = snapshot.Read("tree", "foo2", writeBatch);
var foo2ReadResultThatShouldBeNull = snapshot.Read("tree", "foo2");
Assert.NotNull(foo1ReadResult);
Assert.NotNull(foo2ReadResult);
Assert.Null(foo2ReadResultThatShouldBeNull);
Assert.Equal(foo1ReadResult.Reader.ToStringValue(), "foo1");
Assert.Equal(foo2ReadResult.Reader.ToStringValue(), "foo2");
}
}
示例7: CanAddMultiValuesUnderTheSameKeyToBatch
public void CanAddMultiValuesUnderTheSameKeyToBatch()
{
using (var env = new StorageEnvironment(StorageEnvironmentOptions.CreateMemoryOnly()))
{
var rand = new Random();
var testBuffer = new byte[168];
rand.NextBytes(testBuffer);
CreateTrees(env, 1, "multitree");
var batch = new WriteBatch();
batch.MultiAdd("key", "value1", "multitree0");
batch.MultiAdd("key", "value2", "multitree0");
env.Writer.Write(batch);
using (var tx = env.NewTransaction(TransactionFlags.Read))
{
var tree = tx.Environment.CreateTree(tx,"multitree0");
using (var it = tree.MultiRead("key"))
{
Assert.True(it.Seek(Slice.BeforeAllKeys));
Assert.Equal("value1", it.CurrentKey.ToString());
Assert.True(it.MoveNext());
Assert.Equal("value2", it.CurrentKey.ToString());
}
}
}
}
示例8: ReadVersion_Items_From_Both_WriteBatch_And_Snapshot
public void ReadVersion_Items_From_Both_WriteBatch_And_Snapshot()
{
using (var tx = Env.NewTransaction(TransactionFlags.ReadWrite))
{
Env.CreateTree(tx, "tree");
tx.Environment.CreateTree(tx,"tree").Add("foo1", StreamFor("foo1"));
tx.Commit();
}
using (var writeBatch = new WriteBatch())
using (var snapshot = Env.CreateSnapshot())
{
writeBatch.Add("foo2", StreamFor("foo2"), "tree", 1);
var foor1Version = snapshot.ReadVersion("tree", "foo1", writeBatch);
var foo2Version = snapshot.ReadVersion("tree", "foo2", writeBatch);
var foo2VersionThatShouldBe0 = snapshot.ReadVersion("tree", "foo2");
Assert.Equal(1, foor1Version);
Assert.Equal(2, foo2Version); //is not committed yet
Assert.Equal(0, foo2VersionThatShouldBe0);
}
}
示例9: SnapshotTest
public async Task SnapshotTest()
{
using (var storage = await NewStorageAsync())
{
var str1 = "test1";
var str2 = "test2";
var s1 = new MemoryStream(Encoding.UTF8.GetBytes(str1));
var s2 = new MemoryStream(Encoding.UTF8.GetBytes(str2));
var writeBatch = new WriteBatch();
writeBatch.Put("key1", s1);
await storage.Writer.WriteAsync(writeBatch);
var snapshot = storage.Commands.CreateSnapshot();
writeBatch = new WriteBatch();
writeBatch.Put("key1", s2);
await storage.Writer.WriteAsync(writeBatch);
AssertEqual(str2, storage.Reader.Read("key1"));
AssertEqual(str1, storage.Reader.Read("key1", new ReadOptions
{
Snapshot = snapshot
}));
storage.Commands.ReleaseSnapshot(snapshot);
}
}
示例10: Should_be_able_to_read_and_write_lots_of_data
public void Should_be_able_to_read_and_write_lots_of_data()
{
CreatTestSchema();
var writeBatch = new WriteBatch();
var testData = GenerateTestData().ToList();
foreach (var dataPair in testData)
writeBatch.Add(dataPair.Key, StreamFor(dataPair.Value), TestTreeName);
Env.Writer.Write(writeBatch);
using (var snapshot = Env.CreateSnapshot())
{
using (var iterator = snapshot.Iterate(TestTreeName))
{
Assert.True(iterator.Seek(Slice.BeforeAllKeys));
do
{
var value = iterator.CreateReaderForCurrent().ToStringValue();
var extractedDataPair = new KeyValuePair<string, string>(iterator.CurrentKey.ToString(), value);
Assert.Contains(extractedDataPair,testData);
} while (iterator.MoveNext());
}
}
}
示例11: ShouldRecoverDataFromLogFile
public async Task ShouldRecoverDataFromLogFile()
{
var storage = await NewStorageAsync();
var name = storage.Name;
var writeBatch = new WriteBatch();
writeBatch.Put("A", new MemoryStream(Encoding.UTF8.GetBytes("123")));
writeBatch.Put("B", new MemoryStream(Encoding.UTF8.GetBytes("123")));
writeBatch.Put("D", new MemoryStream(Encoding.UTF8.GetBytes("123")));
storage.Writer.WriteAsync(writeBatch).Wait();
var fileSystem = storage.StorageState.FileSystem;
storage.Dispose();
using (var newStorage = new Storage(new StorageState(name, new StorageOptions())
{
FileSystem = fileSystem
}))
{
await newStorage.InitAsync();
using(var it = newStorage.Reader.NewIterator(new ReadOptions()))
{
it.Seek("C");
Assert.True(it.IsValid);
it.Prev();
Assert.True(it.IsValid);
Assert.Equal("B", it.Key);
}
}
}
示例12: 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);
}
}
}
示例13: SimpleIncrementShouldWorkUsingWriteBatch
public void SimpleIncrementShouldWorkUsingWriteBatch()
{
CreateTrees(Env, 1, "tree");
var writeBatch = new WriteBatch();
writeBatch.Increment("key/1", 10, "tree0");
Env.Writer.Write(writeBatch);
writeBatch = new WriteBatch();
writeBatch.Increment("key/1", 5, "tree0");
Env.Writer.Write(writeBatch);
writeBatch = new WriteBatch();
writeBatch.Increment("key/1", -3, "tree0");
Env.Writer.Write(writeBatch);
using (var tx = Env.NewTransaction(TransactionFlags.Read))
{
var read = tx.ReadTree("tree0").Read("key/1");
Assert.NotNull(read);
Assert.Equal(3, read.Version);
Assert.Equal(12, read.Reader.ReadLittleEndianInt64());
}
}
示例14: Contains
public bool Contains(string treeName, Slice key, out ushort? version, WriteBatch writeBatch = null)
{
if (writeBatch != null)
{
WriteBatch.BatchOperationType operationType;
Stream stream;
if (writeBatch.TryGetValue(treeName, key, out stream, out version, out operationType))
{
switch (operationType)
{
case WriteBatch.BatchOperationType.Add:
return true;
case WriteBatch.BatchOperationType.Delete:
return false;
default:
throw new ArgumentOutOfRangeException(operationType.ToString());
}
}
}
var tree = GetTree(treeName);
var readVersion = tree.ReadVersion(key);
var exists = readVersion > 0;
version = exists ? (ushort?)readVersion : null;
return exists;
}
示例15: Add
public virtual void Add(WriteBatch writeBatch, string key, byte[] value, ushort? expectedVersion = null)
{
var stream = new BufferPoolMemoryStream(BufferPool);
stream.Write(value, 0, value.Length);
stream.Position = 0;
writeBatch.Add(key, stream, TableName, expectedVersion);
}