Beispiel #1
0
        public override XCollectionResult Write(Document document, Action onPreExecuteWrite)
        {
            var result = RetryPolicy.ExecuteAction(() =>
            {
                // 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)
                {
                    // Initialize the Version field
                    document.InitVersion();

                    // 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
                    document.SetVersion(VersionIncrementer.Increment(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();
                }

                return(new XCollectionResult());
            });

            // Send to the gate now!
            _gateQueueWriter.Write(DocdbGatewayMessage.Create(_gatewayKey, document));

            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);
        }
        protected virtual void OnError(Exception exception, DocdbGatewayMessage message, CloudQueueMessage cloudMessage)
        {
            // TODO you can extend it to save the poisonous message

            FaildedMessagesCount++;
        }