コード例 #1
0
        public async Task FirstLastTest()
        {
            IDbStore columnFamilyDbStore = this.rocksDbStoreProvider.GetColumnStoreFamily("firstLastTest");

            string firstKey   = "firstKey";
            string firstValue = "firstValue";
            await columnFamilyDbStore.Put(firstKey.ToBytes(), firstValue.ToBytes());

            for (int i = 0; i < 10; i++)
            {
                string key   = $"key{i}";
                string value = "$value{i}";
                await columnFamilyDbStore.Put(key.ToBytes(), value.ToBytes());
            }

            string lastKey   = "lastKey";
            string lastValue = "lastValue";
            await columnFamilyDbStore.Put(lastKey.ToBytes(), lastValue.ToBytes());

            Option <(byte[], byte[])> firstEntryOption = await columnFamilyDbStore.GetFirstEntry();

            Assert.True(firstEntryOption.HasValue);
            (byte[] key, byte[] value)firstEntry = firstEntryOption.OrDefault();
            Assert.Equal(firstKey, firstEntry.key.FromBytes());
            Assert.Equal(firstValue, firstEntry.value.FromBytes());

            Option <(byte[], byte[])> lastEntryOption = await columnFamilyDbStore.GetLastEntry();

            Assert.True(lastEntryOption.HasValue);
            (byte[] key, byte[] value)lastEntry = lastEntryOption.OrDefault();
            Assert.Equal(lastKey, lastEntry.key.FromBytes());
            Assert.Equal(lastValue, lastEntry.value.FromBytes());
        }
コード例 #2
0
        public async Task GetRemoveDefaultPartitionTestAsync()
        {
            var options = new RocksDbOptionsProvider(new SystemEnvironment(), true);

            var partitionsList = new[]
            {
                "Partition1"
            };

            using (IDbStoreProvider rocksDbStoreProvider = DbStoreProvider.Create(options, this.rocksDbFolder, partitionsList))
            {
                Assert.NotNull(rocksDbStoreProvider);

                IDbStore store = rocksDbStoreProvider.GetDbStore();

                string key      = "key";
                string val      = "val";
                byte[] valBytes = val.ToBytes();
                await store.Put(key.ToBytes(), valBytes);

                Option <byte[]> valRetrieved = await store.Get("key".ToBytes());

                byte[] valRetrievedBytes = valRetrieved.GetOrElse(string.Empty.ToBytes());
                Assert.True(valRetrievedBytes.SequenceEqual(valBytes));
            }
        }
コード例 #3
0
        public async Task BasicTest()
        {
            IDbStore columnFamilyDbStore = this.rocksDbStoreProvider.GetColumnStoreFamily("test");

            string key   = "key1";
            string value = "value1";

            byte[] keyBytes   = key.ToBytes();
            byte[] valueBytes = value.ToBytes();
            await columnFamilyDbStore.Put(keyBytes, valueBytes);

            Option <byte[]> returnedValueBytesOption = await columnFamilyDbStore.Get(keyBytes);

            Assert.True(returnedValueBytesOption.HasValue);
            byte[] returnedValueBytes = returnedValueBytesOption.OrDefault();
            Assert.True(valueBytes.SequenceEqual(returnedValueBytes));
            Assert.Equal(value, returnedValueBytes.FromBytes());
            Assert.True(await columnFamilyDbStore.Contains(keyBytes));
            Assert.False(await columnFamilyDbStore.Contains("key2".ToBytes()));

            await columnFamilyDbStore.Remove(keyBytes);

            returnedValueBytesOption = await columnFamilyDbStore.Get(keyBytes);

            Assert.False(returnedValueBytesOption.HasValue);
        }
コード例 #4
0
        public async Task MessageCountTest()
        {
            using (IDbStore columnFamilyDbStore = this.rocksDbStoreProvider.GetDbStore("test"))
            {
                Assert.Equal(0ul, await columnFamilyDbStore.Count());

                for (int i = 0; i < 10; i++)
                {
                    string key   = $"key{i}";
                    string value = "$value{i}";
                    await columnFamilyDbStore.Put(key.ToBytes(), value.ToBytes());
                }

                Assert.Equal(10ul, await columnFamilyDbStore.Count());
            }

            using (IDbStore columnFamilyDbStore = this.rocksDbStoreProvider.GetDbStore("test"))
            {
                Assert.Equal(10ul, await columnFamilyDbStore.Count());

                for (int i = 0; i < 10; i++)
                {
                    string key = $"key{i}";
                    await columnFamilyDbStore.Remove(key.ToBytes());
                }

                Assert.Equal(0ul, await columnFamilyDbStore.Count());
            }
        }
コード例 #5
0
        async Task DbStoreRestoreAsync(string store, IDbStore dbStore, string latestBackupDirPath)
        {
            try
            {
                IList <Item> items = await this.dataBackupRestore.RestoreAsync <IList <Item> >(store, latestBackupDirPath);

                if (items != null)
                {
                    foreach (Item item in items)
                    {
                        await dbStore.Put(item.Key, item.Value);
                    }
                }
            }
            catch (IOException exception)
            {
                throw new IOException($"The restore operation for {latestBackupDirPath} failed with error.", exception);
            }
        }
コード例 #6
0
        public async Task SpaceCheckViolationsTest()
        {
            Option <long>        maxStorageBytes = Option.Some(90L);
            IStorageSpaceChecker checker         = new MemorySpaceChecker(() => 0L);

            checker.SetMaxSizeBytes(maxStorageBytes);
            InMemoryDbStoreProvider storeProvider = new InMemoryDbStoreProvider(Option.Some(checker));

            string   store1Name = "store1";
            IDbStore store1     = storeProvider.GetDbStore(store1Name);

            string   store2Name = "store2";
            IDbStore store2     = storeProvider.GetDbStore(store2Name);

            byte[] message1 = new byte[10];
            byte[] message2 = new byte[20];
            await store1.Put(message1, message1);

            await store2.Put(message2, message2);

            // Current sizes -
            // store1 -> (message1 * 2)
            // store2 -> (message2 * 2)
            // Aggregated size is less than limit, adding another item should succeed.
            byte[] message3 = new byte[30];
            await store1.Put(message3, message3, CancellationToken.None);

            // Current sizes -
            // store1 -> (message1 * 2) + (message3 * 2)
            // store2 -> (message2 * 2)
            // Aggregated size is greater than limit, adding another item should fail.
            byte[] message4 = new byte[40];
            await Assert.ThrowsAsync <StorageFullException>(() => store2.Put(message4, message4));

            await Assert.ThrowsAsync <StorageFullException>(() => store2.Put(message4, message4, CancellationToken.None));

            // Remove store2. The usage of store1 alone should be less than the max limit.
            // Current sizes -
            // store1 -> (message1 * 2) + (message3 * 2)
            // store2 -> X
            // Aggregated size is less than limit, adding another item should succeed.
            storeProvider.RemoveDbStore(store2Name);
            await store1.Put(message4, message4);

            // Current sizes -
            // store1 -> (message1 * 2) + (message3 * 2) + (message4 * 2)
            // store2 -> X
            // Aggregated size is greater than limit, adding another item should fail.
            byte[] message5 = new byte[45];
            await Assert.ThrowsAsync <StorageFullException>(() => store1.Put(message5, message5));

            // Re-add store2.
            store2 = storeProvider.GetDbStore(store2Name);

            await store1.Remove(message4);

            // Current sizes -
            // store1 -> (message1 * 2) + (message3 * 2)
            // store2 -> X
            // Aggregated size is less than limit, adding another item should succeed.
            await store2.Put(message1, message1);

            // Current sizes -
            // store1 -> (message1 * 2) + (message3 * 2)
            // store2 -> (message1 * 2)
            // Aggregated size is greater than limit, adding another item should fail.
            await Assert.ThrowsAsync <StorageFullException>(() => store2.Put(message5, message5));

            // Remove an item from store1 and then try adding a smaller item to store2 which should succeed.
            await store1.Remove(message3, CancellationToken.None);

            // Current sizes -
            // store1 -> (message1 * 2)
            // store2 -> (message1 * 2)
            // Aggregated size is less than limit, adding another item should succeed.
            await store2.Put(message3, message3, CancellationToken.None);

            // Current sizes -
            // store1 -> (message1 * 2)
            // store2 -> (message1 * 2) + (message3 * 2)
            // Aggregated size is greater than limit, adding another item should fail.
            await Assert.ThrowsAsync <StorageFullException>(() => store2.Put(message4, message4));

            // Set max storage size to be greater than the current db size.
            Option <long> newMaxStorageBytes = Option.Some((message1.Length * 2) * 2L + (message3.Length * 2) + 10);

            checker.SetMaxSizeBytes(newMaxStorageBytes);

            // Adding another item should now succeed after the limits have been increased.
            await store1.Put(message4, message4);

            // Adding a new item to store1 should fail.
            // Current sizes -
            // store1 -> (message1 * 2) + (message4 * 2)
            // store2 -> (message1 * 2) + (message3 * 2)
            // Aggregated size is greater than limit, adding another item should fail.
            await Assert.ThrowsAsync <StorageFullException>(() => store1.Put(message5, message5));

            // Updating the message4 item in store1 should succeed as the size difference between
            // the existing and updated value is zero bytes which doesn't lead to the limit being breached.
            await store1.Put(message4, message4);
        }