public void SetValueWorksSameTransaction([Values(0, 1, 256, 268, 269, 270, 512, 4364, 4365, 4366, 5000, 1200000, 1200012, 10000000)] int firstLength, [Values(0, 1, 256, 268, 269, 270, 512, 4364, 4365, 4366, 5000, 1200000, 1200012, 10000000)] int secondLength) { var valbuf = new byte[secondLength]; new Random(0).NextBytes(valbuf); using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr1 = db.StartTransaction()) { tr1.CreateKey(_key1); tr1.CreateKey(_key2); tr1.CreateKey(_key3); tr1.SetValueSize(firstLength); tr1.SetValue(valbuf, 0, valbuf.Length); tr1.Commit(); } using (var tr2 = db.StartTransaction()) { Assert.True(tr2.FindExactKey(_key1)); Assert.True(tr2.FindExactKey(_key2)); Assert.True(tr2.FindExactKey(_key3)); var valbuf2 = tr2.ReadValue(); for (int i = 0; i < secondLength; i++) { if (valbuf[i] != valbuf2[i]) { Assert.AreEqual(valbuf[i], valbuf2[i]); } } } } }
void DoWork4() { var key = new byte[4000]; using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); _sw.Restart(); for (int i = 0; i < 30000; i++) { using (var tr = db.StartTransaction()) { key[3] = (byte)(i % 256); key[2] = (byte)(i / 256 % 256); key[1] = (byte)(i / 256 / 256 % 256); key[0] = (byte)(i / 256 / 256 / 256); tr.CreateOrUpdateKeyValue(key, key); tr.Commit(); } } _sw.Stop(); using (var trStat = db.StartTransaction()) { Console.WriteLine(trStat.CalculateStats().ToString()); Console.WriteLine("Insert COU: {0,15}ms", _sw.Elapsed.TotalMilliseconds); } } }
public void BiggerKey([Values(0, 1, 268, 269, 270, 4364, 4365, 4366, 1200000)] int keyLength) { var key = new byte[keyLength]; for (int i = 0; i < keyLength; i++) { key[i] = (byte)i; } using (var stream = CreateTestStream()) { using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr1 = db.StartTransaction()) { tr1.CreateKey(key); tr1.Commit(); } using (var tr2 = db.StartTransaction()) { Assert.True(tr2.FindExactKey(key)); var buf = tr2.ReadKey(); Assert.AreEqual(key, buf); Debug.WriteLine(tr2.CalculateStats().ToString()); } } Debug.WriteLine("KeySize:{0,7} DataBaseSize:{1,7}", keyLength, stream.GetSize()); } }
public void TwoTransactions() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr1 = db.StartTransaction()) { tr1.CreateKey(_key1); tr1.Commit(); } using (var tr2 = db.StartTransaction()) { tr2.CreateKey(_key2); Assert.True(tr2.FindExactKey(_key1)); Assert.True(tr2.FindExactKey(_key2)); Assert.False(tr2.FindExactKey(_key3)); tr2.Commit(); } using (var tr3 = db.StartTransaction()) { Assert.True(tr3.FindExactKey(_key1)); Assert.True(tr3.FindExactKey(_key2)); Assert.False(tr3.FindExactKey(_key3)); } } }
public void RandomlyCreatedTest2() { using (var db = new KeyValueDB()) { db.Open(CreateTestStream(), true); using (var tr1 = db.StartTransaction()) { tr1.CreateKey(new byte[] { 1 }); tr1.Commit(); } using (var tr2 = db.StartTransaction()) { tr2.CreateKey(new byte[] { 2 }); tr2.Commit(); } using (var tr3 = db.StartTransaction()) { tr3.CreateKey(new byte[] { 3 }); } using (var tr4 = db.StartTransaction()) { tr4.CreateKey(new byte[] { 4 }); } } }
public void StartWritingTransactionWorks() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); var tr1 = db.StartWritingTransaction().Result; var tr2Task = db.StartWritingTransaction(); var task = Task.Factory.StartNew(() => { var tr2 = tr2Task.Result; Assert.True(tr2.FindExactKey(_key1)); tr2.CreateKey(_key2); tr2.Commit(); tr2.Dispose(); }); tr1.CreateKey(_key1); tr1.Commit(); tr1.Dispose(); task.Wait(1000); using (var tr = db.StartTransaction()) { Assert.True(tr.FindExactKey(_key1)); Assert.True(tr.FindExactKey(_key2)); } } }
void DoWork5ReadCheck() { _sw.Restart(); using (var stream = new PositionLessStreamProxy("data.btdb")) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var trStat = db.StartTransaction()) { Console.WriteLine(trStat.CalculateStats().ToString()); } using (var trCheck = db.StartTransaction()) { long pureDataLengthCheck = 0; if (trCheck.FindFirstKey()) { do { pureDataLengthCheck += trCheck.ReadKey().Length + trCheck.ReadValue().Length; } while (trCheck.FindNextKey()); } if (pureDataLengthCheck != 396130000) { throw new Exception("Bug"); } } using (var trStat = db.StartTransaction()) { Console.WriteLine(trStat.CalculateStats().ToString()); } } _sw.Stop(); Console.WriteLine("Time: {0,15}ms", _sw.Elapsed.TotalMilliseconds); }
public void MultipleTransactions2([Values(1000)] int transactionCount) { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); var key = new byte[2]; for (int i = 0; i < transactionCount; i++) { key[0] = (byte)((transactionCount - i) / 256); key[1] = (byte)((transactionCount - i) % 256); using (var tr1 = db.StartTransaction()) { tr1.CreateKey(key); if (i % 100 == 0 || i == transactionCount - 1) { for (int j = 0; j < i; j++) { key[0] = (byte)((transactionCount - j) / 256); key[1] = (byte)((transactionCount - j) % 256); Assert.True(tr1.FindExactKey(key)); } } tr1.Commit(); } } } }
public void ValueStoreWorksSameTransaction([Values(0, 1, 256, 268, 269, 270, 512, 4364, 4365, 4366, 5000, 1200000, 1200012, 10000000)] int firstLength, [Values(0, 1, 256, 268, 269, 270, 512, 4364, 4365, 4366, 5000, 1200000, 1200012, 10000000)] int secondLength) { var valbuf = new byte[firstLength]; new Random(0).NextBytes(valbuf); using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr1 = db.StartTransaction()) { tr1.CreateKey(_key1); tr1.WriteValue(0, valbuf.Length, valbuf, 0); tr1.SetValueSize(secondLength); var valbuf2 = tr1.ReadValue(); var commonLength = Math.Min(firstLength, secondLength); for (int i = 0; i < commonLength; i++) { if (valbuf[i] != valbuf2[i]) { Assert.AreEqual(valbuf[i], valbuf2[i]); } } for (int i = commonLength; i < secondLength; i++) { if (valbuf2[i] != 0) { Assert.AreEqual(0, valbuf2[i]); } } tr1.Commit(); } } }
public void CreateEmptyDatabase() { using (var stream = CreateTestStream()) { using (IKeyValueDB db = new KeyValueDB()) { Assert.IsTrue(db.Open(stream, false)); } } }
public void FirstTransaction() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr = db.StartTransaction()) { Assert.True(tr.CreateKey(_key1)); tr.Commit(); } } }
void DoWork5(bool alsoDoReads) { _sw.Restart(); long pureDataLength = 0; using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.DurableTransactions = true; db.CacheSizeInMB = 40; db.Open(stream, false); for (int i = 0; i < 200; i++) { long pureDataLengthPrevTr = pureDataLength; using (var tr = db.StartTransaction()) { for (int j = 0; j < 200; j++) { tr.CreateOrUpdateKeyValue(new[] { (byte)j, (byte)i }, new byte[1 + i * j]); pureDataLength += 2 + 1 + i * j; } if (alsoDoReads) { using (var trCheck = db.StartTransaction()) { long pureDataLengthCheck = 0; if (trCheck.FindFirstKey()) { do { pureDataLengthCheck += trCheck.ReadKey().Length + trCheck.ReadValue().Length; } while (trCheck.FindNextKey()); } if (pureDataLengthCheck != pureDataLengthPrevTr) { throw new Exception("Transactions are not in serializable mode"); } } } tr.Commit(); } } using (var trStat = db.StartTransaction()) { Console.WriteLine(trStat.CalculateStats().ToString()); } } _sw.Stop(); Console.WriteLine("Pure data length: {0}", pureDataLength); Console.WriteLine("Time: {0,15}ms", _sw.Elapsed.TotalMilliseconds); }
public void RandomlyCreatedTest1() { using (IKeyValueDB db = new KeyValueDB()) { db.Open(CreateTestStream(), true); using (var tr = db.StartTransaction()) { tr.CreateKey(new byte[232]); tr.SetValue(new byte[93341]); tr.SetValue(new byte[15397]); tr.SetValue(new byte[46700]); tr.Commit(); } } }
void WarmUp() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr = db.StartTransaction()) { tr.CreateKey(new byte[10000]); tr.SetValueSize(10000); tr.SetValue(new byte[1000], 0, 1000); tr.Commit(); } } }
public void HumanReadableDescriptionInHeaderWorks() { using (var stream = CreateTestStream()) { using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); db.HumanReadableDescriptionInHeader = "Hello World"; Assert.AreEqual("Hello World", db.HumanReadableDescriptionInHeader); } using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); Assert.AreEqual("Hello World", db.HumanReadableDescriptionInHeader); } } }
public void OnlyOneWrittingTransactionPossible2() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); var tr1 = db.StartTransaction(); tr1.CreateKey(_key1); using (var tr2 = db.StartTransaction()) { tr1.Commit(); tr1.Dispose(); Assert.False(tr2.FindExactKey(_key1)); Assert.Throws <BTDBTransactionRetryException>(() => tr2.CreateKey(_key2)); } } }
public void RollbackWorks() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr1 = db.StartTransaction()) { tr1.CreateKey(_key1); // Rollback because of missing commit } using (var tr2 = db.StartTransaction()) { Assert.False(tr2.FindExactKey(_key1)); } } }
public void FindLastKeyWorks() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr = db.StartTransaction()) { Assert.False(tr.FindLastKey()); tr.CreateKey(_key1); tr.CreateKey(_key2); tr.CreateKey(_key3); Assert.True(tr.FindLastKey()); Assert.AreEqual(_key2, tr.ReadKey()); tr.Commit(); } } }
public void PrefixWithFindPrevKeyWorks() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr = db.StartTransaction()) { tr.CreateKey(_key1); tr.CreateKey(_key2); tr.SetKeyPrefix(_key2, 0, 1); Assert.True(tr.FindFirstKey()); Assert.False(tr.FindPreviousKey()); Assert.AreEqual(_key1.Skip(1).ToArray(), tr.ReadKey()); tr.Commit(); } } }
public void MoreComplexTransaction() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr = db.StartTransaction()) { Assert.True(tr.CreateKey(_key1)); Assert.False(tr.CreateKey(_key1)); Assert.False(tr.FindExactKey(_key2)); Assert.True(tr.CreateKey(_key2)); Assert.True(tr.FindExactKey(_key1)); Assert.True(tr.FindExactKey(_key2)); Assert.False(tr.FindExactKey(_key3)); tr.Commit(); } } }
public void AdvancedEraseRangeWorks(int createKeys, int removeStart, int removeCount) { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); var key = new byte[2]; using (var tr = db.StartTransaction()) { for (int i = 0; i < createKeys; i++) { key[0] = (byte)(i / 256); key[1] = (byte)(i % 256); tr.CreateKey(key); } tr.Commit(); } using (var tr = db.StartTransaction()) { tr.EraseRange(removeStart, removeStart + removeCount - 1); Assert.AreEqual(createKeys - removeCount, tr.GetKeyValueCount()); tr.Commit(); } using (var tr = db.StartTransaction()) { Assert.AreEqual(createKeys - removeCount, tr.GetKeyValueCount()); for (int i = 0; i < createKeys; i++) { key[0] = (byte)(i / 256); key[1] = (byte)(i % 256); if (i >= removeStart && i < removeStart + removeCount) { Assert.False(tr.FindExactKey(key)); } else { Assert.True(tr.FindExactKey(key)); } } } } }
void DoParallelTest() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr = db.StartTransaction()) { var key = new byte[4000]; for (int i = 0; i < 30000; i++) { key[3] = (byte)(i % 256); key[2] = (byte)(i / 256 % 256); key[1] = (byte)(i / 256 / 256 % 256); key[0] = (byte)(i / 256 / 256 / 256); tr.CreateOrUpdateKeyValue(key, key); } tr.Commit(); } Parallel.For(0, 4, iter => { using (var tr = db.StartTransaction()) { var key = new byte[4000]; for (int i = 0; i < 30000; i++) { key[3] = (byte)(i % 256); key[2] = (byte)(i / 256 % 256); key[1] = (byte)(i / 256 / 256 % 256); key[0] = (byte)(i / 256 / 256 / 256); if (!tr.FindExactKey(key)) { Trace.Assert(false); } //var val = tr.ReadValue(); //Trace.Assert(key.SequenceEqual(val)); } } }); } }
public void SimpleEraseCurrentWorks() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr = db.StartTransaction()) { tr.CreateKey(_key1); tr.CreateKey(_key2); tr.CreateKey(_key3); tr.EraseCurrent(); Assert.True(tr.FindFirstKey()); Assert.AreEqual(_key1, tr.ReadKey()); Assert.True(tr.FindNextKey()); Assert.AreEqual(_key2, tr.ReadKey()); Assert.False(tr.FindNextKey()); Assert.AreEqual(2, tr.CalculateStats().KeyValuePairCount); } } }
public void AdvancedFindPreviousAndNextKeyWorks() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); var key = new byte[2]; const int keysCreated = 10000; using (var tr = db.StartTransaction()) { for (int i = 0; i < keysCreated; i++) { key[0] = (byte)(i / 256); key[1] = (byte)(i % 256); tr.CreateKey(key); } tr.Commit(); } using (var tr = db.StartTransaction()) { Assert.AreEqual(-1, tr.GetKeyIndex()); tr.FindExactKey(key); Assert.AreEqual(keysCreated - 1, tr.GetKeyIndex()); for (int i = 1; i < keysCreated; i++) { Assert.True(tr.FindPreviousKey()); Assert.AreEqual(keysCreated - 1 - i, tr.GetKeyIndex()); } Assert.False(tr.FindPreviousKey()); Assert.AreEqual(0, tr.GetKeyIndex()); for (int i = 1; i < keysCreated; i++) { Assert.True(tr.FindNextKey()); Assert.AreEqual(i, tr.GetKeyIndex()); } Assert.False(tr.FindNextKey()); Assert.AreEqual(keysCreated - 1, tr.GetKeyIndex()); } } }
public void SimplePrefixWorks() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr = db.StartTransaction()) { tr.CreateKey(_key1); tr.CreateKey(_key2); tr.CreateKey(_key3); Assert.AreEqual(3, tr.GetKeyValueCount()); tr.SetKeyPrefix(_key1, 0, 3); Assert.AreEqual(2, tr.GetKeyValueCount()); tr.FindFirstKey(); Assert.AreEqual(new byte[0], tr.ReadKey()); tr.FindLastKey(); Assert.AreEqual(_key3.Skip(3).ToArray(), tr.ReadKey()); tr.Commit(); } } }
public void CreateOrUpdateKeyValueWorks([Values(0, 1, 256, 268, 269, 270, 512, 4364, 4365, 4366, 5000, 1200000, 1200012, 10000000)] int length) { var valbuf = new byte[length]; new Random(0).NextBytes(valbuf); using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr1 = db.StartTransaction()) { Assert.True(tr1.CreateOrUpdateKeyValue(_key1, valbuf)); Assert.False(tr1.CreateOrUpdateKeyValue(_key1, valbuf)); Assert.True(tr1.CreateOrUpdateKeyValue(_key2, valbuf)); tr1.Commit(); } using (var tr2 = db.StartTransaction()) { Assert.True(tr2.FindExactKey(_key1)); var valbuf2 = tr2.ReadValue(); for (int i = 0; i < length; i++) { if (valbuf[i] != valbuf2[i]) { Assert.AreEqual(valbuf[i], valbuf2[i]); } } Assert.True(tr2.FindExactKey(_key2)); valbuf2 = tr2.ReadValue(); for (int i = 0; i < length; i++) { if (valbuf[i] != valbuf2[i]) { Assert.AreEqual(valbuf[i], valbuf2[i]); } } } } }
public void RepairsOnReopen([Values(false, true)] bool durable) { using (var stream = CreateTestStream()) { using (IKeyValueDB db = new KeyValueDB()) { db.DurableTransactions = durable; db.Open(stream, false); using (var tr = db.StartTransaction()) { tr.CreateKey(_key1); tr.Commit(); } using (var tr = db.StartTransaction()) { tr.CreateKey(_key2); tr.Commit(); } using (IKeyValueDB db2 = new KeyValueDB()) { db2.Open(stream, false); using (var tr = db2.StartTransaction()) { Assert.True(tr.FindExactKey(_key1)); Assert.True(tr.FindExactKey(_key2)); } } } using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr = db.StartTransaction()) { Assert.True(tr.FindExactKey(_key1)); Assert.True(tr.FindExactKey(_key2)); } } } }
public void SimpleFindNextKeyWorks() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); using (var tr1 = db.StartTransaction()) { tr1.CreateKey(_key1); tr1.CreateKey(_key2); tr1.CreateKey(_key3); tr1.Commit(); } using (var tr2 = db.StartTransaction()) { Assert.True(tr2.FindExactKey(_key3)); Assert.True(tr2.FindNextKey()); Assert.AreEqual(_key2, tr2.ReadKey()); Assert.False(tr2.FindNextKey()); } } }
public void ALotOfLargeKeysWorks() { using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); for (int i = 0; i < 10; i++) { using (var tr = db.StartTransaction()) { for (int j = 0; j < 10; j++) { var key = new byte[5000]; key[0] = (byte)i; key[key.Length - 1] = (byte)j; Assert.True(tr.CreateKey(key)); tr.SetValueSize(4000 + i * 100 + j); } tr.Commit(); } } using (var tr = db.StartTransaction()) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { var key = new byte[5000]; key[0] = (byte)i; key[key.Length - 1] = (byte)j; Assert.True(tr.FindExactKey(key)); Assert.AreEqual(4000 + i * 100 + j, tr.GetValueSize()); } } Debug.WriteLine(tr.CalculateStats().ToString()); } } }
void DoWork() { var key = new byte[1000]; using (var stream = CreateTestStream()) using (IKeyValueDB db = new KeyValueDB()) { db.Open(stream, false); _sw.Restart(); for (int i = 0; i < 100000; i++) { key[504] = (byte)(i % 256); key[503] = (byte)(i / 256 % 256); key[502] = (byte)(i / 256 / 256 % 256); key[501] = (byte)(i / 256 / 256 / 256); using (var tr = db.StartTransaction()) { tr.CreateKey(key); tr.SetValueSize(10000); tr.Commit(); } _sw.Stop(); _results.Add(_sw.Elapsed.TotalMilliseconds); if (i % 1000 == 0) { Console.WriteLine("{0} {1}", i, _sw.Elapsed.TotalSeconds); } _sw.Start(); } _sw.Stop(); using (var trStat = db.StartTransaction()) { Console.WriteLine(trStat.CalculateStats().ToString()); Console.WriteLine("Total miliseconds: {0,15}", _sw.Elapsed.TotalMilliseconds); } } }