public static Bundle Map(CosmosBundleDocument bundleDocument, IBundleContent?bundleContent = null) { var notificationIds = new List <Uuid>(); var fromBase64 = Convert.FromBase64String(bundleDocument.NotificationIdsBase64); using var stream = new MemoryStream(fromBase64); using var binaryReader = new BinaryReader(stream); while (stream.Position < stream.Length) { var guidBytes = binaryReader.ReadBytes(16); notificationIds.Add(new Uuid(new Guid(guidBytes))); } var recipient = Guid.TryParse(bundleDocument.Recipient, out var actorId) ? new ActorId(actorId) : new LegacyActorId(new GlobalLocationNumber(bundleDocument.Recipient)); var bundle = new Bundle( new Uuid(bundleDocument.Id), recipient, Enum.Parse <DomainOrigin>(bundleDocument.Origin), new ContentType(bundleDocument.ContentType), notificationIds, bundleContent, bundleDocument.DocumentTypes == null ? Enumerable.Empty <string>() : bundleDocument.DocumentTypes.Split(',', StringSplitOptions.RemoveEmptyEntries)); if (bundleDocument.Dequeued) { bundle.Dequeue(); } return(bundle); }
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); } }