public override XCollectionResult Write(Document document, Action onPreExecuteWrite) { return(RetryPolicy.ExecuteAction(() => { XCollectionResult result = new XCollectionResult(); // check if the document exists in the database var existingDocument = Client.CreateDocumentQuery <VersionedDocument>(Collection.SelfLink) .Where(x => x.Id == document.Id) .Select(x => new { x.Version, x.ETag, x.SelfLink }) .AsEnumerable() .FirstOrDefault(); if (existingDocument == null) { // document does not exist onPreExecuteWrite(); var task = Client.CreateDocumentAsync(Collection.SelfLink, document); task.Wait(); } else { // document does exist and we're going to check the version and replace if needed if (document.GetVersion() > existingDocument.Version) { onPreExecuteWrite(); RequestOptions options = new RequestOptions { AccessCondition = new AccessCondition { Type = AccessConditionType.IfMatch, Condition = existingDocument.ETag } }; var task = Client.ReplaceDocumentAsync(existingDocument.SelfLink, document, options); task.Wait(); } else { result.Discarded = true; } } return result; })); }
protected virtual bool DispatchMessage(DocdbGatewayMessage message) { Guard.NotNull(message, "message"); Guard.NotNullOrEmpty(message.Key, "message.Key"); Guard.NotNull(message.Documents, "message.Documents"); // resolve key string key = message.Key; // Get target table and its storage account if (!_cachedTargetCollections.ContainsKey(key)) { _cachedTargetCollections[key] = _targetCollectionResolver.Resolve(key); } // TODO inserting multiple documents is not supported currently // TODO or: a simple for-loop may be introduced here XCollectionResult result = _cachedTargetCollections[key].Write(message.Documents[0]); ProcessedMessagesCount++; return(result.Discarded); }