示例#1
0
        private async Task UpdateIndexes(InventorySnapshotEntity inventorySnapshotEntity)
        {
            var existingIndex = await _indexStorage.GetDataAsync(GetIndexPartitionKey(inventorySnapshotEntity.Time),
                                                                 IndexForLastRowKey);

            if (existingIndex == null || (await _storage.GetDataAsync(existingIndex)).Time < inventorySnapshotEntity.Time)
            {
                // Update index for current day, it now must point to this the last received snapshot
                await _indexStorage.InsertOrReplaceAsync(
                    AzureIndex.Create(GetIndexPartitionKey(inventorySnapshotEntity.Time), IndexForLastRowKey,
                                      inventorySnapshotEntity));
            }

            // If we have index entries for any future date, they are now are invalid, we can just remove them,
            // as they will be reconstructed if needed

            var filter = TableQuery.GenerateFilterCondition(nameof(AzureIndex.PartitionKey),
                                                            QueryComparisons.GreaterThan,
                                                            GetIndexPartitionKey(inventorySnapshotEntity.Time));

            var query = new TableQuery <AzureIndex>().Where(filter);

            var allNowInvalidIndexEntriesFromTheFuture = (await _indexStorage.WhereAsync(query)).ToList();

            foreach (var indexEntity in allNowInvalidIndexEntriesFromTheFuture)
            {
                await _indexStorage.DeleteAsync(indexEntity);
            }
        }
示例#2
0
        public async Task InsertAsync(InventorySnapshot inventorySnapshot)
        {
            Guid id = Guid.NewGuid();

            var entity = new InventorySnapshotEntity(GetPartitionKey(inventorySnapshot.Timestamp), GetRowKey(id))
            {
                Time = inventorySnapshot.Timestamp,
                Json = JsonConvert.SerializeObject(inventorySnapshot)
            };

            await _storage.InsertAsync(entity);

            await UpdateIndexes(entity);
        }