private async Task UpdateCatalogAsync(CosmosBundleDocument bundle, CosmosCabinetDrawerChanges changes) { var updatedCatalogEntry = changes.UpdatedCatalogEntry; if (updatedCatalogEntry != null) { await _repositoryContainer .Catalog .UpsertItemAsync(updatedCatalogEntry) .ConfigureAwait(false); return; } if (changes.UpdatedDrawer.Position == MaximumCabinetDrawerItemCount) { return; } var asLinq = _repositoryContainer .Cabinet .GetItemLinqQueryable <CosmosDataAvailable>(); var query = from dataAvailable in asLinq where dataAvailable.PartitionKey == changes.UpdatedDrawer.Id orderby dataAvailable.SequenceNumber select(long?) dataAvailable.SequenceNumber; var nextSequenceNumber = await query .Skip(changes.UpdatedDrawer.Position) .Take(1) .AsCosmosIteratorAsync() .FirstOrDefaultAsync() .ConfigureAwait(false); if (!nextSequenceNumber.HasValue) { return; } var partitionKey = string.Join('_', bundle.Recipient, bundle.Origin); var contentType = bundle.ContentType; var nextCatalogEntry = new CosmosCatalogEntry { Id = Guid.NewGuid().ToString(), PartitionKey = partitionKey, ContentType = contentType, NextSequenceNumber = nextSequenceNumber.Value, }; await _repositoryContainer .Catalog .UpsertItemAsync(nextCatalogEntry) .ConfigureAwait(false); }
private async Task DeleteOldCatalogEntryAsync(CosmosBundleDocument bundle, CosmosCabinetDrawerChanges changes) { var asLinq = _repositoryContainer .Catalog .GetItemLinqQueryable <CosmosCatalogEntry>(); var partitionKey = string.Join('_', bundle.Recipient, bundle.Origin); var contentType = bundle.ContentType; var query = from catalogEntry in asLinq where catalogEntry.PartitionKey == partitionKey && catalogEntry.ContentType == contentType && catalogEntry.NextSequenceNumber == changes.InitialCatalogEntrySequenceNumber select catalogEntry; await foreach (var entry in query.AsCosmosIteratorAsync()) { await DeleteOldCatalogEntryAsync(entry).ConfigureAwait(false); } }
private async Task UpdateDrawerAsync(CosmosCabinetDrawerChanges changes) { var options = new ItemRequestOptions { IfMatchEtag = changes.UpdatedDrawer.ETag }; var updatedDrawer = changes.UpdatedDrawer; try { await _repositoryContainer .Cabinet .ReplaceItemAsync(updatedDrawer, updatedDrawer.Id, requestOptions : options) .ConfigureAwait(false); } catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.PreconditionFailed) { // When two Acknowledge are executing, they must not overwrite each others values. // The failed ReplaceItemAsync is discarded. } }