internal KeyValueDBTransaction(KeyValueDB owner, KeyValueDB.ReadTrLink readLink)
 {
     _owner = owner;
     _readLink = readLink;
     _currentKeySector = null;
     _currentKeyIndexInLeaf = -1;
     _currentKeyIndex = -1;
     _prefix = EmptyByteArray;
     _prefixKeyStart = 0;
     _prefixKeyCount = (long)_readLink.KeyValuePairCount;
 }
Exemple #2
0
 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));
                 }
             }
         }
     }
 }
Exemple #3
0
 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));
                     }
                 }
             });
     }
 }
Exemple #4
0
 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();
         }
     }
 }
Exemple #5
0
 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();
         }
     }
 }
Exemple #6
0
 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();
         }
     }
 }
Exemple #7
0
 void DoRandomWork()
 {
     var opCounter = 0;
     var random = new Random();
     using (var stream = CreateTestStream())
     //using (IKeyValueDB db = new KeyValueDBReplayProxy(new KeyValueDB(), new PositionLessStreamWriter(new PositionLessStreamProxy("btdb.log"))))
     using (IKeyValueDB db = new KeyValueDB())
     {
         db.Open(stream, false);
         IKeyValueDBTransaction tr = db.StartTransaction();
         while (opCounter < 100000)
         {
             if (opCounter % 1000 == 0)
             {
                 Console.WriteLine(string.Format("Operation {0}", opCounter));
                 Console.WriteLine(tr.CalculateStats().ToString());
             }
             opCounter++;
             var action = random.Next(100);
             if (action < 10)
             {
                 if (action > 1)
                 {
                     //Console.WriteLine("Commit");
                     tr.Commit();
                 }
                 else
                 {
                     //Console.WriteLine("Rollback");
                 }
                 tr.Dispose();
                 tr = db.StartTransaction();
             }
             else if (action < 50 || tr.GetKeyIndex() < 0)
             {
                 var key = new byte[random.Next(1, 1000)];
                 random.NextBytes(key);
                 //Console.WriteLine(string.Format("CreateKey {0}", key.Length));
                 tr.CreateKey(key);
             }
             else if (action < 60)
             {
                 //Console.WriteLine("EraseCurrent");
                 tr.EraseCurrent();
             }
             else if (action < 65)
             {
                 //Console.WriteLine("FindNextKey");
                 tr.FindNextKey();
             }
             else if (action < 70)
             {
                 //Console.WriteLine("FindPreviousKey");
                 tr.FindPreviousKey();
             }
             else
             {
                 var value = new byte[random.Next(1, 100000)];
                 random.NextBytes(value);
                 //Console.WriteLine(string.Format("SetValue {0}", value.Length));
                 tr.SetValue(value);
             }
         }
         tr.Dispose();
     }
 }
Exemple #8
0
 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();
         }
     }
 }
Exemple #9
0
 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);
         }
     }
 }
Exemple #10
0
 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));
         }
     }
 }
Exemple #11
0
 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);
         }
     }
 }
Exemple #12
0
 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();
         }
     }
 }
Exemple #13
0
 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();
         }
     }
 }
Exemple #14
0
 public void FindKeyWithOnlyAndPreferNextKeyWorks()
 {
     const int keyCount = 10000;
     using (var stream = CreateTestStream())
     using (IKeyValueDB db = new KeyValueDB())
     {
         db.Open(stream, false);
         using (var tr = db.StartTransaction())
         {
             var key = new byte[100];
             for (int i = 0; i < keyCount; i++)
             {
                 key[0] = (byte)(i / 256);
                 key[1] = (byte)(i % 256);
                 tr.CreateKey(key);
             }
             tr.Commit();
         }
         using (var tr = db.StartTransaction())
         {
             var key = new byte[101];
             for (int i = 0; i < keyCount; i++)
             {
                 key[0] = (byte)(i / 256);
                 key[1] = (byte)(i % 256);
                 var findKeyResult = tr.FindKey(key, 0, key.Length, FindKeyStrategy.OnlyNext);
                 if (i == keyCount - 1)
                 {
                     Assert.AreEqual(FindKeyResult.NotFound, findKeyResult);
                     Assert.AreEqual(-1, tr.GetKeyIndex());
                 }
                 else
                 {
                     Assert.AreEqual(FindKeyResult.FoundNext, findKeyResult);
                     Assert.AreEqual(i + 1, tr.GetKeyIndex());
                 }
                 findKeyResult = tr.FindKey(key, 0, key.Length, FindKeyStrategy.PreferNext);
                 if (i == keyCount - 1)
                 {
                     Assert.AreEqual(FindKeyResult.FoundPrevious, findKeyResult);
                     Assert.AreEqual(i, tr.GetKeyIndex());
                 }
                 else
                 {
                     Assert.AreEqual(FindKeyResult.FoundNext, findKeyResult);
                     Assert.AreEqual(i + 1, tr.GetKeyIndex());
                 }
             }
         }
         using (var tr = db.StartTransaction())
         {
             var key = new byte[99];
             for (int i = 0; i < keyCount; i++)
             {
                 key[0] = (byte)(i / 256);
                 key[1] = (byte)(i % 256);
                 var findKeyResult = tr.FindKey(key, 0, key.Length, FindKeyStrategy.OnlyNext);
                 Assert.AreEqual(FindKeyResult.FoundNext, findKeyResult);
                 Assert.AreEqual(i, tr.GetKeyIndex());
                 findKeyResult = tr.FindKey(key, 0, key.Length, FindKeyStrategy.PreferNext);
                 Assert.AreEqual(FindKeyResult.FoundNext, findKeyResult);
                 Assert.AreEqual(i, tr.GetKeyIndex());
             }
         }
     }
 }
Exemple #15
0
 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]);
             }
         }
     }
 }
Exemple #16
0
 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());
     }
 }
Exemple #17
0
 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());
         }
     }
 }
Exemple #18
0
 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 });
         }
     }
 }
Exemple #19
0
 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));
             }
         }
     }
 }
Exemple #20
0
 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();
         }
     }
 }
Exemple #21
0
 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]);
             }
         }
     }
 }
Exemple #22
0
 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();
             }
         }
     }
 }
Exemple #23
0
 public void SimpleFindPreviousKeyWorks()
 {
     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.FindPreviousKey());
             Assert.AreEqual(_key1, tr2.ReadKey());
             Assert.False(tr2.FindPreviousKey());
         }
     }
 }
Exemple #24
0
 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));
         }
     }
 }
Exemple #25
0
 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));
         }
     }
 }
Exemple #26
0
 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();
         }
     }
 }
Exemple #27
0
 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());
         }
     }
 }
Exemple #28
0
 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));
         }
     }
 }
Exemple #29
0
 public void OpenEmptyDatabase()
 {
     using (var stream = CreateTestStream())
     {
         using (IKeyValueDB db = new KeyValueDB())
         {
             Assert.IsTrue(db.Open(stream, false));
         }
         using (IKeyValueDB db = new KeyValueDB())
         {
             Assert.IsFalse(db.Open(stream, false));
         }
     }
 }
Exemple #30
0
 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);
 }