Exemple #1
0
 public void CreateEmptyDatabase()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (new KeyValueDB(fileCollection))
     {
     }
 }
Exemple #2
0
 public void AddingContinueToNewFileAfterReopenWithCorruption()
 {
     using (var fileCollection = new InMemoryFileCollection())
     {
         using (IKeyValueDB db = new KeyValueDB(fileCollection))
         {
             using (var tr = db.StartTransaction())
             {
                 tr.CreateOrUpdateKeyValue(_key1, _key1);
                 tr.Commit();
             }
         }
         fileCollection.SimulateCorruptionBySetSize(20 + 16);
         using (IKeyValueDB db = new KeyValueDB(fileCollection))
         {
             using (var tr = db.StartTransaction())
             {
                 Assert.Equal(0, tr.GetKeyValueCount());
                 tr.CreateOrUpdateKeyValue(Key2, Key2);
                 tr.Commit();
             }
             Console.WriteLine(db.CalcStats());
         }
         Assert.True(2 <= fileCollection.GetCount());
     }
 }
Exemple #3
0
 public void CreateEmptyCache()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (new DiskChunkCache(fileCollection, 20, 1000))
     {
     }
 }
Exemple #4
0
 public void GetFromEmptyCacheReturnsEmptyByteBuffer()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (var cache = new DiskChunkCache(fileCollection, 20, 1000))
     {
         Assert.AreEqual(0, cache.Get(CalcHash(new byte[] { 0 })).Result.Length);
     }
 }
Exemple #5
0
 public void EmptyWritingTransaction()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         using (var tr = db.StartWritingTransaction().Result)
         {
             tr.Commit();
         }
     }
 }
Exemple #6
0
 public void FirstTransaction()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         using (var tr = db.StartTransaction())
         {
             Assert.True(tr.CreateOrUpdateKeyValue(ByteBuffer.NewAsync(_key1), ByteBuffer.NewAsync(new byte[0])));
             tr.Commit();
         }
     }
 }
Exemple #7
0
 public void CanGetSizeOfPair()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         using (var tr = db.StartTransaction())
         {
             tr.CreateOrUpdateKeyValue(ByteBuffer.NewAsync(_key1), ByteBuffer.NewAsync(new byte[1]));
             var s = tr.GetStorageSizeOfCurrentKey();
             Assert.Equal((uint)_key1.Length, s.Key);
             Assert.Equal(1u, s.Value);
         }
     }
 }
Exemple #8
0
 public void GettingContentMakesItStayLongerDecreasingRate()
 {
     using (var fileCollection = new InMemoryFileCollection())
     {
         const int cacheCapacity = 50000;
         using (var cache = new DiskChunkCache(fileCollection, 20, cacheCapacity))
         {
             for (var i = 0; i < 80; i++)
             {
                 Put(cache, i);
                 for (var j = 0; j < 79 - i; j++)
                     Get(cache, i);
                 Assert.LessOrEqual(fileCollection.Enumerate().Sum(f => (long)f.GetSize()), cacheCapacity);
             }
             Console.WriteLine(cache.CalcStats());
             Assert.True(Get(cache, 0));
             Assert.False(Get(cache, 60));
         }
     }
 }
Exemple #9
0
 public void AccessEveryTenthTenTimesMoreMakesItStay()
 {
     using (var fileCollection = new InMemoryFileCollection())
     {
         const int cacheCapacity = 50000;
         using (var cache = new DiskChunkCache(fileCollection, 20, cacheCapacity))
         {
             for (var i = 0; i < 46; i++)
             {
                 Put(cache, i);
                 for (var j = 0; j < (i % 5 == 0 ? 10 + i : 1); j++)
                     Get(cache, i);
                 if (i==42) Thread.Sleep(500);
                 Assert.LessOrEqual(fileCollection.Enumerate().Sum(f => (long)f.GetSize()), cacheCapacity);
             }
             Console.WriteLine(cache.CalcStats());
             Assert.True(Get(cache, 0));
             Assert.False(Get(cache, 1));
         }
     }
 }
Exemple #10
0
 public void Run()
 {
     int cnt = 500000;
     using (var fc = new InMemoryFileCollection())
     using (var db = CreateDb(fc))
     {
         Measure("Relation: ", new RelationPersonTest(db, cnt));
     }
     using (var fc = new InMemoryFileCollection())
     using (var db = CreateDb(fc))
     {
         Measure("2 Maps: ", new SingletonPersonTest(db, cnt));
     }
     using (var db = CreateInMemoryDb())
     {
         Measure("Relation (mem): ", new RelationPersonTest(db, cnt));
     }
     using (var db = CreateInMemoryDb())
     {
         Measure("2 Maps (mem): ", new SingletonPersonTest(db, cnt));
     }
 }
Exemple #11
0
 public void AddingContinueToSameFileAfterReopen()
 {
     using (var fileCollection = new InMemoryFileCollection())
     {
         using (IKeyValueDB db = new KeyValueDB(fileCollection))
         {
             using (var tr = db.StartTransaction())
             {
                 tr.CreateOrUpdateKeyValue(_key1, _key1);
                 tr.Commit();
             }
         }
         using (IKeyValueDB db = new KeyValueDB(fileCollection))
         {
             using (var tr = db.StartTransaction())
             {
                 tr.CreateOrUpdateKeyValue(Key2, Key2);
                 tr.Commit();
             }
             Console.WriteLine(db.CalcStats());
         }
         Assert.Equal(2u, fileCollection.GetCount()); // Log + Index
     }
 }
Exemple #12
0
 public void CompressibleValueLoad()
 {
     using (var fileCollection = new InMemoryFileCollection())
     {
         using (IKeyValueDB db = new KeyValueDB(fileCollection))
         {
             using (var tr = db.StartTransaction())
             {
                 tr.CreateOrUpdateKeyValue(_key1, new byte[1000]);
                 Assert.Equal(new byte[1000], tr.GetValueAsByteArray());
                 tr.Commit();
             }
         }
     }
 }
Exemple #13
0
 public void SetKeyPrefixInOneTransaction()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         var key = new byte[5];
         var value = new byte[100];
         var rnd = new Random();
         using (var tr = db.StartTransaction())
         {
             for (byte i = 0; i < 100; i++)
             {
                 key[0] = i;
                 for (byte j = 0; j < 100; j++)
                 {
                     key[4] = j;
                     rnd.NextBytes(value);
                     tr.CreateOrUpdateKeyValue(key, value);
                 }
             }
             tr.Commit();
         }
         using (var tr = db.StartTransaction())
         {
             for (byte i = 0; i < 100; i++)
             {
                 key[0] = i;
                 tr.SetKeyPrefix(ByteBuffer.NewSync(key, 0, 4));
                 Assert.Equal(100, tr.GetKeyValueCount());
             }
         }
     }
 }
Exemple #14
0
 public void ALotOf5KbTransactionsWorks()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         for (int i = 0; i < 5000; i++)
         {
             var key = new byte[5000];
             using (var tr = db.StartTransaction())
             {
                 key[0] = (byte)(i / 256);
                 key[1] = (byte)(i % 256);
                 Assert.True(tr.CreateKey(key));
                 tr.Commit();
             }
         }
     }
 }
Exemple #15
0
 void AdvancedEraseRangeWorks(int createKeys, int removeStart, int removeCount)
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         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.Equal(createKeys - removeCount, tr.GetKeyValueCount());
             tr.Commit();
         }
         using (var tr = db.StartTransaction())
         {
             Assert.Equal(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), $"{i} should be removed");
                 }
                 else
                 {
                     Assert.True(tr.FindExactKey(key), $"{i} should be found");
                 }
             }
         }
     }
 }
Exemple #16
0
 public void SimpleEraseCurrentWorks()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         using (var tr = db.StartTransaction())
         {
             tr.CreateKey(_key1);
             tr.CreateKey(Key2);
             tr.CreateKey(_key3);
             tr.EraseCurrent();
             Assert.True(tr.FindFirstKey());
             Assert.Equal(_key1, tr.GetKeyAsByteArray());
             Assert.True(tr.FindNextKey());
             Assert.Equal(Key2, tr.GetKeyAsByteArray());
             Assert.False(tr.FindNextKey());
             Assert.Equal(2, tr.GetKeyValueCount());
         }
     }
 }
Exemple #17
0
 public void FindKeyWithPreferPreviousKeyWorks()
 {
     const int keyCount = 10000;
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         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.Find(ByteBuffer.NewSync(key));
                 Assert.Equal(FindResult.Previous, findKeyResult);
                 Assert.Equal(i, 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.Find(ByteBuffer.NewSync(key));
                 if (i == 0)
                 {
                     Assert.Equal(FindResult.Next, findKeyResult);
                     Assert.Equal(i, tr.GetKeyIndex());
                 }
                 else
                 {
                     Assert.Equal(FindResult.Previous, findKeyResult);
                     Assert.Equal(i - 1, tr.GetKeyIndex());
                 }
             }
         }
     }
 }
Exemple #18
0
 public void SimplePrefixWorks()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         using (var tr = db.StartTransaction())
         {
             tr.CreateKey(_key1);
             tr.CreateKey(Key2);
             tr.CreateKey(_key3);
             Assert.Equal(3, tr.GetKeyValueCount());
             tr.SetKeyPrefix(ByteBuffer.NewAsync(_key1, 0, 3));
             Assert.Equal(2, tr.GetKeyValueCount());
             tr.FindFirstKey();
             Assert.Equal(new byte[0], tr.GetKeyAsByteArray());
             tr.FindLastKey();
             Assert.Equal(_key3.Skip(3).ToArray(), tr.GetKeyAsByteArray());
             tr.Commit();
         }
     }
 }
Exemple #19
0
 public void FindLastKeyWorks()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         using (var tr = db.StartTransaction())
         {
             Assert.False(tr.FindLastKey());
             tr.CreateKey(_key1);
             tr.CreateKey(Key2);
             tr.CreateKey(_key3);
             Assert.True(tr.FindLastKey());
             Assert.Equal(Key2, tr.GetKeyAsByteArray());
             tr.Commit();
         }
     }
 }
Exemple #20
0
 public File(InMemoryFileCollection owner, uint index)
 {
     _owner = owner;
     _index = index;
     _writer = new Writer(this);
 }
Exemple #21
0
 public void MoreComplexReopen()
 {
     using (var fileCollection = new InMemoryFileCollection())
     {
         using (IKeyValueDB db = new KeyValueDB(fileCollection))
         {
             for (int i = 0; i < 100; i++)
             {
                 var key = new byte[100];
                 using (var tr = db.StartTransaction())
                 {
                     key[0] = (byte)(i / 256);
                     key[1] = (byte)(i % 256);
                     Assert.True(tr.CreateOrUpdateKeyValue(key, key));
                     tr.Commit();
                 }
             }
             using (var tr = db.StartTransaction())
             {
                 tr.SetKeyIndex(0);
                 tr.EraseCurrent();
                 tr.EraseRange(1, 3);
                 tr.Commit();
             }
         }
         using (IKeyValueDB db = new KeyValueDB(fileCollection))
         {
             using (var tr = db.StartTransaction())
             {
                 var key = new byte[100];
                 key[1] = 1;
                 Assert.True(tr.FindExactKey(key));
                 tr.FindNextKey();
                 Assert.Equal(5, tr.GetKeyAsByteArray()[1]);
                 Assert.Equal(96, tr.GetKeyValueCount());
             }
         }
     }
 }
Exemple #22
0
 public void AdvancedFindPreviousAndNextKeyWorks()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         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.Equal(-1, tr.GetKeyIndex());
             tr.FindExactKey(key);
             Assert.Equal(keysCreated - 1, tr.GetKeyIndex());
             for (int i = 1; i < keysCreated; i++)
             {
                 Assert.True(tr.FindPreviousKey());
                 Assert.Equal(keysCreated - 1 - i, tr.GetKeyIndex());
             }
             Assert.False(tr.FindPreviousKey());
             Assert.Equal(-1, tr.GetKeyIndex());
             for (int i = 0; i < keysCreated; i++)
             {
                 Assert.True(tr.FindNextKey());
                 Assert.Equal(i, tr.GetKeyIndex());
             }
             Assert.False(tr.FindNextKey());
             Assert.Equal(-1, tr.GetKeyIndex());
         }
     }
 }
Exemple #23
0
 public void SimpleFindNextKeyWorks()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         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.Equal(Key2, tr2.GetKeyAsByteArray());
             Assert.False(tr2.FindNextKey());
         }
     }
 }
Exemple #24
0
 public void StartWritingTransactionWorks()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         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 #25
0
 public void PrefixWithFindPrevKeyWorks()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         using (var tr = db.StartTransaction())
         {
             tr.CreateKey(_key1);
             tr.CreateKey(Key2);
             tr.SetKeyPrefix(ByteBuffer.NewAsync(Key2, 0, 1));
             Assert.True(tr.FindFirstKey());
             Assert.False(tr.FindPreviousKey());
             tr.Commit();
         }
     }
 }
Exemple #26
0
 public void RepairsOnReopen()
 {
     using (var fileCollection = new InMemoryFileCollection())
     {
         using (IKeyValueDB db = new KeyValueDB(fileCollection))
         {
             using (var tr = db.StartTransaction())
             {
                 tr.CreateKey(_key1);
                 tr.Commit();
             }
             using (var tr = db.StartTransaction())
             {
                 tr.CreateKey(Key2);
                 tr.Commit();
             }
             using (var tr = db.StartTransaction())
             {
                 tr.CreateKey(_key3);
                 // rollback
             }
             using (IKeyValueDB db2 = new KeyValueDB(fileCollection))
             {
                 using (var tr = db2.StartTransaction())
                 {
                     Assert.True(tr.FindExactKey(_key1));
                     Assert.True(tr.FindExactKey(Key2));
                     Assert.False(tr.FindExactKey(_key3));
                 }
             }
         }
         using (IKeyValueDB db = new KeyValueDB(fileCollection))
         {
             using (var tr = db.StartTransaction())
             {
                 Assert.True(tr.FindExactKey(_key1));
                 Assert.True(tr.FindExactKey(Key2));
                 Assert.False(tr.FindExactKey(_key3));
             }
         }
     }
 }
Exemple #27
0
 public void MultipleTransactions2(int transactionCount)
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         var key = new byte[2 + transactionCount * 10];
         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.CreateOrUpdateKeyValue(ByteBuffer.NewSync(key, 0, 2 + i * 10), ByteBuffer.NewEmpty());
                 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.Equal(FindResult.Exact, tr1.Find(ByteBuffer.NewSync(key, 0, 2 + j * 10)));
                     }
                 }
                 tr1.Commit();
             }
         }
     }
 }
Exemple #28
0
        public void ReadOnlyTransactionThrowsOnWriteAccess()
        {
            using (var fileCollection = new InMemoryFileCollection())
            using (IKeyValueDB db = new KeyValueDB(fileCollection))
            {
                using (var tr = db.StartReadOnlyTransaction())
                {
                    Assert.Throws<BTDBTransactionRetryException>(() => tr.CreateKey(new byte[1]));
                }
            }

        }
Exemple #29
0
 public void SetKeyIndexWorks()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         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.False(tr.SetKeyIndex(keysCreated));
             for (int i = 0; i < keysCreated; i += 5)
             {
                 Assert.True(tr.SetKeyIndex(i));
                 key = tr.GetKeyAsByteArray();
                 Assert.Equal((byte)(i / 256), key[0]);
                 Assert.Equal((byte)(i % 256), key[1]);
                 Assert.Equal(i, tr.GetKeyIndex());
             }
         }
     }
 }
 public File(InMemoryFileCollection owner, uint index)
 {
     _owner  = owner;
     _index  = index;
     _writer = new Writer(this);
 }
Exemple #31
0
 public void CreateOrUpdateKeyValueWorks(int length)
 {
     var valbuf = new byte[length];
     new Random(0).NextBytes(valbuf);
     using (var fileCollection = new InMemoryFileCollection())
     using (IKeyValueDB db = new KeyValueDB(fileCollection))
     {
         using (var tr1 = db.StartTransaction())
         {
             Assert.True(tr1.CreateOrUpdateKeyValueUnsafe(_key1, valbuf));
             Assert.False(tr1.CreateOrUpdateKeyValueUnsafe(_key1, valbuf));
             Assert.True(tr1.CreateOrUpdateKeyValueUnsafe(Key2, valbuf));
             tr1.Commit();
         }
         using (var tr2 = db.StartTransaction())
         {
             Assert.True(tr2.FindExactKey(_key1));
             var valbuf2 = tr2.GetValueAsByteArray();
             for (int i = 0; i < length; i++)
             {
                 if (valbuf[i] != valbuf2[i])
                     Assert.Equal(valbuf[i], valbuf2[i]);
             }
             Assert.True(tr2.FindExactKey(Key2));
             valbuf2 = tr2.GetValueAsByteArray();
             for (int i = 0; i < length; i++)
             {
                 if (valbuf[i] != valbuf2[i])
                     Assert.Equal(valbuf[i], valbuf2[i]);
             }
         }
     }
 }