Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public IDbStore GetDbStore(string partitionName)
        {
            Preconditions.CheckNonWhiteSpace(partitionName, nameof(partitionName));
            IDbStore dbStore = this.partitionDbStoreDictionary.GetOrAdd(partitionName, new InMemoryDbStore());

            return(dbStore);
        }
Ejemplo n.º 3
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());
        }
Ejemplo n.º 4
0
 public UnitOfWork(Container container, IsolationLevel isolationLevel)
 {
     _scope       = AsyncScopedLifestyle.BeginScope(container);
     _dbStore     = _scope.GetInstance <IDbStore>();
     _session     = GetSession(_dbStore);
     _transaction = _session.BeginTransaction(isolationLevel);
 }
Ejemplo n.º 5
0
        public static CheckpointStore Create(IDbStoreProvider dbStoreProvider)
        {
            IDbStore dbStore = Preconditions.CheckNotNull(dbStoreProvider, nameof(dbStoreProvider)).GetDbStore(Constants.CheckpointStorePartitionKey);
            IEntityStore <string, CheckpointEntity> underlyingStore = new EntityStore <string, CheckpointEntity>(dbStore, nameof(CheckpointEntity), 12);

            return(new CheckpointStore(underlyingStore));
        }
Ejemplo n.º 6
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));
            }
        }
Ejemplo n.º 7
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());
            }
        }
Ejemplo n.º 8
0
        public IEntityStore <TK, TV> GetEntityStore <TK, TV>(string entityName)
        {
            IDbStore entityDbStore            = this.dbStoreProvider.GetDbStore(Preconditions.CheckNonWhiteSpace(entityName, nameof(entityName)));
            IEntityStore <TK, TV> entityStore = new EntityStore <TK, TV>(entityDbStore, entityName, 12);

            return(entityStore);
        }
Ejemplo n.º 9
0
        IEntityStore <TK, TV> GetEntityStore <TK, TV>(string entityName, IDbStore entityDbStore)
        {
            IKeyValueStore <TK, TV> dbStoreMapper    = new KeyValueStoreMapper <TK, byte[], TV, byte[]>(entityDbStore, new BytesMapper <TK>(), new BytesMapper <TV>());
            IEntityStore <TK, TV>   entityStore      = new EntityStore <TK, TV>(dbStoreMapper, entityName, 12);
            IEntityStore <TK, TV>   timedEntityStore = new TimedEntityStore <TK, TV>(entityStore, this.operationTimeout);

            return(timedEntityStore);
        }
Ejemplo n.º 10
0
        public IEntityStore <TK, TV> GetEntityStore <TK, TV>(string entityName)
        {
            IDbStore entityDbStore = this.dbStoreProvider.GetDbStore(Preconditions.CheckNonWhiteSpace(entityName, nameof(entityName)));
            IKeyValueStore <TK, TV> dbStoreMapper    = new KeyValueStoreMapper <TK, byte[], TV, byte[]>(entityDbStore, new BytesMapper <TK>(), new BytesMapper <TV>());
            IEntityStore <TK, TV>   entityStore      = new EntityStore <TK, TV>(dbStoreMapper, entityName, 12);
            IEntityStore <TK, TV>   timedEntityStore = new TimedEntityStore <TK, TV>(entityStore, this.operationTimeout);

            return(timedEntityStore);
        }
        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);
            }
        }
        async Task DbStoreBackupAsync(string store, IDbStore dbStore, string latestBackupDirPath)
        {
            try
            {
                IList <Item> items = new List <Item>();
                await dbStore.IterateBatch(
                    int.MaxValue,
                    (key, value) =>
                {
                    items.Add(new Item(key, value));
                    return(Task.CompletedTask);
                });

                await this.dataBackupRestore.BackupAsync(store, latestBackupDirPath, items);
            }
            catch (IOException exception)
            {
                throw new IOException($"The backup operation for {store} failed with error.", exception);
            }
        }
        async Task BackupAsync()
        {
            this.events.StartingBackup();
            Guid   backupId          = Guid.NewGuid();
            string dbBackupDirectory = Path.Combine(this.backupPath, backupId.ToString());

            BackupMetadata     newBackupMetadata  = new BackupMetadata(backupId, this.dataBackupRestore.DataBackupFormat, DateTime.UtcNow, this.dbStores.Keys.ToList());
            BackupMetadataList backupMetadataList = new BackupMetadataList(new List <BackupMetadata> {
                newBackupMetadata
            });

            try
            {
                Directory.CreateDirectory(dbBackupDirectory);

                // Backup other stores.
                foreach (string store in this.dbStores.Keys)
                {
                    IDbStore dbStore = this.dbStoreProvider.GetDbStore(store);
                    await this.DbStoreBackupAsync(store, dbStore, dbBackupDirectory);
                }

                using (StreamWriter file = File.CreateText(Path.Combine(this.backupPath, BackupMetadataFileName)))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Serialize(file, backupMetadataList);
                }

                this.events.BackupComplete();

                // Clean any old backups.
                this.CleanupUnknownBackups(this.backupPath, backupMetadataList);
            }
            catch (Exception exception)
            {
                this.events.BackupFailure($"The backup operation failed with error ${exception}.");

                // Clean up any artifacts of the attempted backup.
                this.CleanupKnownBackups(this.backupPath, backupMetadataList);
            }
        }
Ejemplo n.º 14
0
 public ChooseCourseModel(IDbStore store)
 {
     this.store = store;
 }
Ejemplo n.º 15
0
 public EntityStore(IDbStore dbStore, string entityName, int keyShardCount = 1)
 {
     this.dbStore         = Preconditions.CheckNotNull(dbStore, nameof(dbStore));
     this.keyLockProvider = new AsyncLockProvider <TK>(Preconditions.CheckRange(keyShardCount, 1, nameof(keyShardCount)));
     this.EntityName      = Preconditions.CheckNonWhiteSpace(entityName, nameof(entityName));
 }
Ejemplo n.º 16
0
 public RecordModel(IDbStore store)
 {
     this.store = store;
 }
Ejemplo n.º 17
0
 public static T LoadIf <T>(this IDbStore dbStore, object id)
     where T : IEntity
 {
     return(id == null ? default(T) : dbStore.Load <T>(id));
 }
Ejemplo n.º 18
0
 public static ISession GetSession(this IDbStore dbStore)
 {
     return(dbStore.GetMemberValue("_session") as ISession);
 }
Ejemplo n.º 19
0
 private static ISession GetSession(IDbStore dbStore)
 {
     return(dbStore.GetMemberValue("_session") as ISession);
 }
Ejemplo n.º 20
0
 public ChooseStudentModel(IDbStore store)
 {
     this.store = store;
 }
Ejemplo n.º 21
0
 public RoleStore(IDbStore dbStore)
 {
     _dbStore = dbStore;
 }
Ejemplo n.º 22
0
 public StorageSpaceAwareDbStore(IDbStore dbStore, IStorageSpaceChecker diskSpaceChecker)
     : base(dbStore)
 {
     this.storageSpaceChecker = Preconditions.CheckNotNull(diskSpaceChecker, nameof(diskSpaceChecker));
 }
Ejemplo n.º 23
0
 public StudentRecordModel(IDbStore store)
 {
     this.store = store;
 }
Ejemplo n.º 24
0
 public TeacherModel(IDbStore store)
 {
     this.store = store;
 }
Ejemplo n.º 25
0
 public DbStoreDecorator(IDbStore dbStore)
 {
     this.dbStore = Preconditions.CheckNotNull(dbStore, nameof(dbStore));
 }
Ejemplo n.º 26
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);
        }
Ejemplo n.º 27
0
 public ClassModel(IDbStore store)
 {
     this.store = store;
 }