示例#1
0
        internal static async Task SetValueInternalAsync(JObject originalItem, T newItem, DocumentDBContext context)
        {
            JObject currentValue = JObject.FromObject(newItem);

            if (HasChanged(originalItem, currentValue))
            {
                // make sure it's not the id that has changed
                string originalId = null;
                string currentId  = null;
                if (TryGetId(currentValue, out currentId) &&
                    !string.IsNullOrEmpty(currentId) &&
                    TryGetId(originalItem, out originalId) &&
                    !string.IsNullOrEmpty(originalId))
                {
                    // make sure it's not the Id that has changed
                    if (!string.Equals(originalId, currentId, StringComparison.Ordinal))
                    {
                        throw new InvalidOperationException("Cannot update the 'Id' property.");
                    }
                }
                else
                {
                    // If the serialzied object does not have a lowercase 'id' property, DocDB will reject it.
                    // We'll just short-circuit here since we validate that the 'id' hasn't changed.
                    throw new InvalidOperationException(string.Format("The document must have an 'id' property."));
                }

                Uri documentUri = UriFactory.CreateDocumentUri(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName, originalId);
                await DocumentDBUtility.RetryAsync(() => context.Service.ReplaceDocumentAsync(documentUri, newItem),
                                                   context.MaxThrottleRetries);
            }
        }
        public async Task <IEnumerable <T> > ConvertAsync(DocumentDBAttribute attribute, CancellationToken cancellationToken)
        {
            DocumentDBContext context = _config.CreateContext(attribute);

            Uri collectionUri = UriFactory.CreateDocumentCollectionUri(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName);

            List <T> finalResults = new List <T>();

            string continuation = null;

            SqlQuerySpec sqlSpec = new SqlQuerySpec
            {
                QueryText  = context.ResolvedAttribute.SqlQuery,
                Parameters = context.ResolvedAttribute.SqlQueryParameters ?? new SqlParameterCollection()
            };

            do
            {
                DocumentQueryResponse <T> response = await DocumentDBUtility.RetryAsync(
                    () => context.Service.ExecuteNextAsync <T>(collectionUri, sqlSpec, continuation),
                    context.MaxThrottleRetries);

                finalResults.AddRange(response.Results);
                continuation = response.ResponseContinuation;
            }while (!string.IsNullOrEmpty(continuation));

            return(finalResults);
        }
示例#3
0
        public async Task AddAsync(T item, CancellationToken cancellationToken = default(CancellationToken))
        {
            bool create = false;

            try
            {
                await UpsertDocument(_docDBContext, item);
            }
            catch (Exception ex)
            {
                DocumentClientException de;
                if (_docDBContext.ResolvedAttribute.CreateIfNotExists &&
                    DocumentDBUtility.TryGetDocumentClientException(ex, out de) &&
                    de.StatusCode == HttpStatusCode.NotFound)
                {
                    create = true;
                }
                else
                {
                    throw;
                }
            }

            if (create)
            {
                await CreateIfNotExistAsync(_docDBContext);
                await UpsertDocument(_docDBContext, item);
            }
        }
示例#4
0
        internal static async Task CreateIfNotExistAsync(DocumentDBContext context)
        {
            await DocumentDBUtility.RetryAsync(() => CreateDatabaseIfNotExistsAsync(context.Service, context.ResolvedAttribute.DatabaseName),
                                               context.MaxThrottleRetries, codesToIgnore : HttpStatusCode.Conflict);

            await DocumentDBUtility.RetryAsync(() => CreateDocumentCollectionIfNotExistsAsync(context.Service, context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName, context.ResolvedAttribute.PartitionKey, context.ResolvedAttribute.CollectionThroughput),
                                               context.MaxThrottleRetries, codesToIgnore : HttpStatusCode.Conflict);
        }
示例#5
0
        public async Task AddAsync(T item, CancellationToken cancellationToken = default(CancellationToken))
        {
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri(_docDBContext.ResolvedDatabaseName, _docDBContext.ResolvedCollectionName);

            await DocumentDBUtility.ExecuteWithRetriesAsync(async() =>
            {
                return(await _docDBContext.Service.CreateDocumentAsync(collectionUri, item));
            });
        }
示例#6
0
        internal static async Task UpsertDocument(DocumentDBContext context, T item)
        {
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName);

            // DocumentClient does not accept strings directly.
            object convertedItem = item;

            if (item is string)
            {
                convertedItem = JObject.Parse(item.ToString());
            }

            await DocumentDBUtility.RetryAsync(() => context.Service.UpsertDocumentAsync(collectionUri, convertedItem), context.MaxThrottleRetries);
        }
        public static async Task CreateIfNotExistAsync(IDocumentDBService service, string databaseName, string documentCollectionName)
        {
            Uri      databaseUri = UriFactory.CreateDatabaseUri(databaseName);
            Database database    = new Database {
                Id = databaseName
            };
            DocumentCollection documentCollection = new DocumentCollection
            {
                Id = documentCollectionName
            };

            // If we queried for the Database or Collection before creation, we may hit a race condition
            // if multiple instances are running the same code. So let's just create and ignore a Conflict.
            await DocumentDBUtility.ExecuteAndIgnoreStatusCodeAsync(HttpStatusCode.Conflict,
                                                                    () => service.CreateDatabaseAsync(database));

            await DocumentDBUtility.ExecuteAndIgnoreStatusCodeAsync(HttpStatusCode.Conflict,
                                                                    () => service.CreateDocumentCollectionAsync(databaseUri, documentCollection));
        }
示例#8
0
        public object GetValue()
        {
            Uri            documentUri = UriFactory.CreateDocumentUri(_context.ResolvedAttribute.DatabaseName, _context.ResolvedAttribute.CollectionName, _context.ResolvedAttribute.Id);
            RequestOptions options     = null;

            if (!string.IsNullOrEmpty(_context.ResolvedAttribute.PartitionKey))
            {
                options = new RequestOptions
                {
                    PartitionKey = new PartitionKey(_context.ResolvedAttribute.PartitionKey)
                };
            }

            T document = DocumentDBUtility.RetryAsync(() => _context.Service.ReadDocumentAsync <T>(documentUri, options),
                                                      _context.MaxThrottleRetries, codesToIgnore: HttpStatusCode.NotFound).Result;

            if (document != null)
            {
                _originalItem = JObject.FromObject(document);
            }

            return(document);
        }
        public object GetValue()
        {
            Uri            documentUri = UriFactory.CreateDocumentUri(_context.ResolvedAttribute.DatabaseName, _context.ResolvedAttribute.CollectionName, _context.ResolvedAttribute.Id);
            RequestOptions options     = null;

            if (!string.IsNullOrEmpty(_context.ResolvedAttribute.PartitionKey))
            {
                options = new RequestOptions
                {
                    PartitionKey = new PartitionKey(_context.ResolvedAttribute.PartitionKey)
                };
            }

            Document document = DocumentDBUtility.RetryAsync(() => _context.Service.ReadDocumentAsync(documentUri, options),
                                                             _context.MaxThrottleRetries, codesToIgnore: HttpStatusCode.NotFound).Result;

            if (document == null)
            {
                return(document);
            }

            T item = null;

            _originalItem = JObject.FromObject(document);

            // Strings need to be handled differently.
            if (typeof(T) == typeof(string))
            {
                item = _originalItem.ToString(Formatting.None) as T;
            }
            else
            {
                item = (T)(dynamic)document;
            }

            return(item);
        }
示例#10
0
 internal static async Task UpsertDocument(DocumentDBContext context, T item)
 {
     Uri collectionUri = UriFactory.CreateDocumentCollectionUri(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName);
     await DocumentDBUtility.RetryAsync(() => context.Service.UpsertDocumentAsync(collectionUri, item), context.MaxThrottleRetries);
 }