Esempio n. 1
0
        private static async Task LoadSnapshotsAsync()
        {
            await ServiceLocator.SnapshotStorage.LoadSnapshotsAsync(tables =>
            {
                foreach (var table in tables)
                {
                    DbInstance.CreateTableIfNotExists(table);
                }
            },

                                                                    snapshot =>
            {
                try
                {
                    var table     = DbInstance.CreateTableIfNotExists(snapshot.TableName);
                    var partition = table.InitPartitionFromSnapshot(snapshot.Snapshot.AsMyMemory());

                    if (partition != null)
                    {
                        ServiceLocator.DataSynchronizer.PublishInitPartition(table, partition);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(
                        $"Snapshots {snapshot.TableName}/{snapshot.PartitionKey} could not be loaded: " +
                        e.Message);
                }
            });
        }
Esempio n. 2
0
        public async ValueTask <IActionResult> InsertEntity([Required][FromQuery] string tableName,
                                                            [FromQuery] string syncPeriod)
        {
            var shutDown = this.CheckOnShuttingDown();

            if (shutDown != null)
            {
                return(shutDown);
            }

            if (string.IsNullOrEmpty(tableName))
            {
                return(this.TableNameIsNull());
            }

            var table = DbInstance.CreateTableIfNotExists(tableName);


            var body = await Request.BodyAsIMemoryAsync();

            var fields = body.ParseFirstLevelOfJson();

            var entityInfo = fields.GetEntityInfo();

            if (string.IsNullOrEmpty(entityInfo.PartitionKey))
            {
                return(this.PartitionKeyIsNull());
            }

            if (string.IsNullOrEmpty(entityInfo.RowKey))
            {
                return(this.RowKeyIsNull());
            }

            if (table.HasRecord(entityInfo))
            {
                this.ResponseConflict("Record with the same PartitionKey and RowKey is already exists");
            }

            var(dbPartition, dbRow) = table.Insert(entityInfo, fields);

            if (dbPartition == null)
            {
                return(this.ResponseConflict("Can not insert entity"));
            }

            ServiceLocator.DataSynchronizer.SynchronizeUpdate(table, new[] { dbRow });

            return(await this.ResponseOk()
                   .SynchronizePartitionAsync(table, dbPartition, syncPeriod.ParseSynchronizationPeriod()));
        }
Esempio n. 3
0
        public IActionResult CreateIfNotExists([Required][FromQuery] string tableName)
        {
            var shutDown = this.CheckOnShuttingDown();

            if (shutDown != null)
            {
                return(shutDown);
            }

            if (string.IsNullOrEmpty(tableName))
            {
                return(this.ResponseConflict("Please specify table name"));
            }

            DbInstance.CreateTableIfNotExists(tableName);
            return(this.ResponseOk());
        }
Esempio n. 4
0
        public async ValueTask <IActionResult> InsertOrReplaceEntity([Required][FromQuery] string tableName,
                                                                     [FromQuery] string syncPeriod)
        {
            var shutDown = this.CheckOnShuttingDown();

            if (shutDown != null)
            {
                return(shutDown);
            }

            if (string.IsNullOrEmpty(tableName))
            {
                return(this.TableNameIsNull());
            }

            var table = DbInstance.CreateTableIfNotExists(tableName);

            var data = await Request.BodyReader.ReadAsync();

            var fields = data.Buffer.Enumerate().AsMyMemory().ParseFirstLevelOfJson();

            var entityInfo = fields.GetEntityInfo();


            if (string.IsNullOrEmpty(entityInfo.PartitionKey))
            {
                return(this.PartitionKeyIsNull());
            }

            if (string.IsNullOrEmpty(entityInfo.RowKey))
            {
                return(this.RowKeyIsNull());
            }

            var(dbPartition, dbRow) = table.InsertOrReplace(entityInfo, fields);

            ServiceLocator.DataSynchronizer
            .SynchronizeUpdate(table, new[] { dbRow });


            return(await this.ResponseOk()
                   .SynchronizePartitionAsync(table, dbPartition, syncPeriod.ParseSynchronizationPeriod()));
        }
Esempio n. 5
0
        public async ValueTask <IActionResult> CleanAndBulkInsert([Required][FromQuery] string tableName,
                                                                  [FromQuery] string partitionKey,
                                                                  [FromQuery] string syncPeriod)
        {
            var shutDown = this.CheckOnShuttingDown();

            if (shutDown != null)
            {
                return(shutDown);
            }

            if (string.IsNullOrEmpty(tableName))
            {
                return(this.TableNameIsNull());
            }

            var theSyncPeriod = syncPeriod.ParseSynchronizationPeriod();

            if (theSyncPeriod == DataSynchronizationPeriod.Immediately)
            {
                return(Conflict("CleanAndBulkInsert insert does not support immediate persistence"));
            }


            var table = DbInstance.CreateTableIfNotExists(tableName);

            var body = await Request.BodyReader.ReadAsync();

            var entitiesToInsert = body.Buffer.Enumerate().SplitJsonArrayToObjects();

            if (string.IsNullOrEmpty(partitionKey))
            {
                CleanTableAndBulkInsert(table, entitiesToInsert, theSyncPeriod);
            }
            else
            {
                CleanPartitionAndBulkInsert(table, entitiesToInsert, partitionKey, theSyncPeriod);
            }

            return(this.ResponseOk());
        }
Esempio n. 6
0
        public async ValueTask <IActionResult> InsertOrReplace([Required][FromQuery] string tableName,
                                                               [FromQuery] string syncPeriod)
        {
            var shutDown = this.CheckOnShuttingDown();

            if (shutDown != null)
            {
                return(shutDown);
            }

            if (string.IsNullOrEmpty(tableName))
            {
                return(this.TableNameIsNull());
            }

            var theSyncPeriod = syncPeriod.ParseSynchronizationPeriod();

            if (theSyncPeriod == DataSynchronizationPeriod.Immediately)
            {
                return(Conflict("Bulk insert does not support immediate persistence"));
            }

            var table = DbInstance.CreateTableIfNotExists(tableName);


            var body = await Request.BodyReader.ReadAsync();

            var entitiesToInsert = body.Buffer.Enumerate().SplitJsonArrayToObjects();

            var(dbPartitions, dbRows) = table.BulkInsertOrReplace(entitiesToInsert);

            ServiceLocator.DataSynchronizer.SynchronizeUpdate(table, dbRows);

            foreach (var dbPartition in dbPartitions)
            {
                ServiceLocator.SnapshotSaverScheduler.SynchronizePartition(table, dbPartition, theSyncPeriod);
            }

            return(this.ResponseOk());
        }