Esempio n. 1
0
        protected override TableBatchOperation PrepareNextBatchOperation(IList <T> batch, string partitionKey, out List <XTableResult> results)
        {
            // Search for existing entities versions
            List <VersionedTableEntity> existingVersions = FindExistingVersions(batch, partitionKey);

            results = new List <XTableResult>(batch.Count);
            TableBatchOperation batchOperation = new TableBatchOperation();

            // Add data operations
            foreach (T entity in batch)
            {
                VersionedTableEntity existingEntity = existingVersions.FirstOrDefault(x => x.RowKey == entity.RowKey);

                results.Add(new XTableResult());

                // Insert entity if it doesn't exist
                if (existingEntity == null)
                {
                    batchOperation.Insert(entity);
                }
                else
                {
                    entity.Version = VersionIncrementer.Increment(existingEntity.Version);

                    // Set current ETag
                    entity.ETag = existingEntity.ETag;
                    batchOperation.Replace(entity);
                }
            }

            return(batchOperation);
        }
        public void CanIncrementMixed()
        {
            var versionIncrementer  = new VersionIncrementer();
            var parsedVersionString = new ParsedVersionString()
            {
                IsValid      = true,
                VersionParts = new List <ParsedPart>()
                {
                    new ParsedPart()
                    {
                        IsChar = false, Value = 1
                    },
                    new ParsedPart()
                    {
                        IsChar = true, Value = (int)'a'
                    },
                    new ParsedPart()
                    {
                        IsChar = false, Value = 1
                    },
                    new ParsedPart()
                    {
                        IsChar = true, Value = (int)'x'
                    },
                }
            };
            var incremented        = versionIncrementer.Increment(parsedVersionString, 1);
            var incrementedVersion =
                incremented.Aggregate("",
                                      (combined, part) => String.Concat(combined, ".",
                                                                        part.IsChar ? ((char)part.Value).ToString() : part.Value.ToString()));

            Assert.AreEqual(incrementedVersion.TrimStart('.'), "1.b.0.a");
        }
        public void WillThrowApplictionExcpetionIfIndexIsOutOfRange()
        {
            var versionIncrementer  = new VersionIncrementer();
            var parsedVersionString = new ParsedVersionString()
            {
                IsValid      = true,
                VersionParts = new List <ParsedPart>()
                {
                    new ParsedPart()
                    {
                        IsChar = false, Value = 1
                    },
                    new ParsedPart()
                    {
                        IsChar = true, Value = (int)'a'
                    },
                    new ParsedPart()
                    {
                        IsChar = false, Value = 1
                    },
                    new ParsedPart()
                    {
                        IsChar = true, Value = (int)'x'
                    },
                }
            };
            var ex = Assert.Throws <ApplicationException>(() =>
            {
                var incremented = versionIncrementer.Increment(parsedVersionString, 10);
            }
                                                          );

            Assert.AreEqual(ex.Message, "Index out of Range of parsed version parts");
        }
Esempio n. 4
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);
        }