Пример #1
0
        protected override IList <XTableResult> WriteNextBatch(string partitionKey, IList <T> entities, Action onPreExecuteBatch)
        {
            var results = base.WriteNextBatch(partitionKey, entities, onPreExecuteBatch);

            // Send to the gateway now
            _gateQueueWriter.Write(TableGatewayMessage.Create(_gatewayKey, entities));

            return(results);
        }
Пример #2
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);
        }