public void CreateEmptyDatabase() { using (var fileCollection = new InMemoryFileCollection()) using (new KeyValueDB(fileCollection)) { } }
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()); } }
public void CreateEmptyCache() { using (var fileCollection = new InMemoryFileCollection()) using (new DiskChunkCache(fileCollection, 20, 1000)) { } }
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); } }
public void EmptyWritingTransaction() { using (var fileCollection = new InMemoryFileCollection()) using (IKeyValueDB db = new KeyValueDB(fileCollection)) { using (var tr = db.StartWritingTransaction().Result) { tr.Commit(); } } }
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(); } } }
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); } } }
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)); } } }
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)); } } }
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)); } }
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 } }
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(); } } } }
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()); } } } }
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(); } } } }
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"); } } } } }
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()); } } }
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()); } } } } }
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(); } } }
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(); } } }
public File(InMemoryFileCollection owner, uint index) { _owner = owner; _index = index; _writer = new Writer(this); }
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()); } } } }
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()); } } }
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()); } } }
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)); } } }
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(); } } }
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)); } } } }
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(); } } } }
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])); } } }
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); }
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]); } } } }