Пример #1
0
        public static async Task <DocumentCollection> GetOrCreateCollectionAsync(this IDocumentClient client, string databaseId, string collectionId, RequestOptions requestOptions = null)
        {
            try
            {
                var response = await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseId, collectionId), requestOptions);

                return(response.Resource);
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    var docCollection = new DocumentCollection {
                        Id = collectionId
                    };
                    docCollection.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String)
                    {
                        Precision = -1
                    });
                    docCollection.IndexingPolicy.IndexingMode = IndexingMode.Consistent;

                    var response = await client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri(databaseId), docCollection, requestOptions);

                    response.Resource.IndexingPolicy = new IndexingPolicy();
                    return(response.Resource);
                }
                else
                {
                    throw e.InnerException;
                }
            }
        }
Пример #2
0
        public static Task <DocumentCollection> GetOrAddDocumentCollection(string collectionId, IDocumentClient client, Database database, CancellationToken token, int throughput, int?timeToLive, Func <string, DocumentCollection> factory = null)
        {
            return(Policy
                   .Handle <DocumentClientException>(e => { return true; })
                   .WaitAndRetryForeverAsync(attempt => TimeSpan.FromSeconds(1 << Math.Min(attempt, 3)))
                   .ExecuteAsync(async() =>
            {
                token.ThrowIfCancellationRequested();
                var query = client.CreateDocumentCollectionQuery(database.SelfLink).Where(c => c.Id == collectionId).AsDocumentQuery();

                if (query.HasMoreResults)
                {
                    var res = await query.ExecuteNextAsync <DocumentCollection>();
                    if (res.Any())
                    {
                        return res.First();
                    }
                }

                var collection = factory == null ? new DocumentCollection {
                    Id = collectionId
                } : factory(collectionId);


                collection.DefaultTimeToLive = timeToLive <= 0 ? -1 : timeToLive * 60 * 60 * 24;     // expire all documents after timeToLive no of days. -1 indicates never expire

                throughput = throughput == 0 ? 400 : throughput;

                return (await client.CreateDocumentCollectionAsync(database.SelfLink, collection, new RequestOptions {
                    OfferThroughput = throughput
                }).ConfigureAwait(false)).Resource;
            }));
        }
Пример #3
0
        private async Task CreateDocumentCollectionIfNotExists(string databaseName, string collectionName, IDocumentClient documentClient)
        {
            try
            {
                await documentClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName));
            }
            catch (DocumentClientException de)
            {
                // If the document collection does not exist, create a new collection
                if (de.StatusCode == HttpStatusCode.NotFound)
                {
                    //DocumentCollection collectionInfo = new DocumentCollection();
                    //collectionInfo.Id = collectionName;

                    // Optionally, you can configure the indexing policy of a collection. Here we configure collections for maximum query flexibility
                    // including string range queries.
                    //collectionInfo.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });

                    // DocumentDB collections can be reserved with throughput specified in request units/second. 1 RU is a normalized request equivalent to the read
                    // of a 1KB document.  Here we create a collection with 400 RU/s.
                    await documentClient.CreateDocumentCollectionAsync(
                        UriFactory.CreateDatabaseUri(databaseName),
                        new DocumentCollection { Id = collectionName },
                        new RequestOptions { OfferThroughput = 400 });
                }
                else
                {
                    throw;
                }
            }
        }
Пример #4
0
        private async Task CreateColltionsIfNotExists()
        {
            foreach (var collectionName in _collectionNames)
            {
                try
                {
                    await _documentClient.ReadDocumentCollectionAsync(
                        UriFactory.CreateDocumentCollectionUri(_databaseName, collectionName.Key));
                }
                catch (DocumentClientException e)
                {
                    if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
                    {
                        var docCollection = new DocumentCollection {
                            Id = collectionName.Key
                        };
                        string partionKey = collectionName.Value;

                        if (!string.IsNullOrEmpty(partionKey))
                        {
                            docCollection.PartitionKey.Paths.Add($"/{partionKey}");
                        }

                        await _documentClient.CreateDocumentCollectionAsync(
                            UriFactory.CreateDatabaseUri(_databaseName),
                            docCollection,
                            new RequestOptions { OfferThroughput = DefaultRUs });
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Пример #5
0
        public static async Task <DocumentCollection> EnsureDatabaseAndCollection(this IDocumentClient client, string dbId, string collectionId, string[] partitionKeys = null)
        {
            var db = client.CreateDatabaseQuery().Where(d => d.Id == dbId).AsEnumerable().FirstOrDefault()
                     ?? await client.CreateDatabaseAsync(new Database()
            {
                Id = dbId
            });

            var collection = client.CreateDocumentCollectionQuery(db.SelfLink)
                             .Where(c => c.Id == collectionId).AsEnumerable().FirstOrDefault();

            if (collection == null)
            {
                var collectionDefinition = new DocumentCollection()
                {
                    Id = collectionId
                };
                if (partitionKeys != null && partitionKeys.Length > 0)
                {
                    foreach (var partitionKey in partitionKeys)
                    {
                        collectionDefinition.PartitionKey.Paths.Add(partitionKey);
                    }
                }

                collection = await client.CreateDocumentCollectionAsync(db.SelfLink, collectionDefinition);
            }

            return(collection);
        }
Пример #6
0
        private static async Task CreateCollection(IDocumentClient client, string collectionId, int reservedRUs = 1000, string partitionKey = "/partitionKey")
        {
            Console.WriteLine($">>> Create Collection {collectionId} in mydb <<<");
            Console.WriteLine();
            Console.WriteLine($" Throughput: {reservedRUs} RU/sec");
            Console.WriteLine($" Partition key: {partitionKey}");
            Console.WriteLine();

            PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition();

            partitionKeyDefinition.Paths.Add(partitionKey);

            DocumentCollection collectionDefinition = new DocumentCollection
            {
                Id           = collectionId,
                PartitionKey = partitionKeyDefinition
            };
            RequestOptions options = new RequestOptions {
                OfferThroughput = reservedRUs
            };

            ResourceResponse <DocumentCollection> result = await client.CreateDocumentCollectionAsync(MyDbDatabaseUri, collectionDefinition, options);

            DocumentCollection collection = result.Resource;

            Console.WriteLine("Created new collection");
            PrintCollection(collection);
        }
        /// <summary>
        /// Creates the new collection.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="database">The database.</param>
        /// <param name="collectionId">The collection identifier.</param>
        /// <param name="collectionSpec">The collection spec.</param>
        /// <returns></returns>
        public static async Task <DocumentCollection> CreateNewCollection(
            IDocumentClient client,
            Database database,
            string collectionId,
            DocumentCollectionSpec collectionSpec)
        {
            DocumentCollection collectionDefinition = new DocumentCollection {
                Id = collectionId
            };

            int throughput = 400;

            if (collectionSpec != null)
            {
                //Partitions
                if (collectionSpec != null && collectionSpec.CollectionIsPartitioned)
                {
                    collectionDefinition.PartitionKey.Paths.Add(collectionSpec.PartitionPath);
                }

                //Set throughput for new collection
                throughput = collectionSpec.Throughput;
            }

            //Create the Collection
            DocumentCollection collection = await
                                            client.CreateDocumentCollectionAsync(
                database.SelfLink,
                collectionDefinition,
                new RequestOptions { OfferThroughput = throughput }
                );

            return(collection);
        }
        private static async Task CreateCollectionIfNotExistsAsync(IDocumentClient client)
        {
            try
            {
                await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId));

                Console.WriteLine($"Already exists collection {CollectionId}");
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    Console.WriteLine($"**** Creating collection: {CollectionId} in {DatabaseId} ****");

                    DocumentCollection collection = new DocumentCollection {
                        Id = CollectionId
                    };

                    //collection.PartitionKey.Paths.Add("/station");

                    ResourceResponse <DocumentCollection> result =
                        await client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri(DatabaseId), collection);

                    Console.WriteLine($"Created collection {result.Resource.Id}");

                    ViewCollection(result.Resource);
                }
                else
                {
                    throw;
                }
            }
        }
Пример #9
0
        private async Task CreateCollectionIfNotExistsAsync()
        {
            try
            {
                await documentClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(cosmosDbConnection.DatabaseId, cosmosDbConnection.CollectionId)).ConfigureAwait(false);
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    var pkDef = new PartitionKeyDefinition
                    {
                        Paths = new Collection <string>()
                        {
                            cosmosDbConnection.PartitionKey
                        },
                    };

                    await documentClient.CreateDocumentCollectionAsync(
                        UriFactory.CreateDatabaseUri(cosmosDbConnection.DatabaseId),
                        new DocumentCollection { Id = cosmosDbConnection.CollectionId, PartitionKey = pkDef },
                        new RequestOptions { OfferThroughput = 1000 }).ConfigureAwait(false);
                }
                else
                {
                    throw;
                }
            }
        }
Пример #10
0
        private async Task SetupUserSettingsCollection()
        {
            var collectionName = typeof(Models.UserSettings).Name;
            var databaseUri    = UriFactory.CreateDatabaseUri(_configuration.DatabaseId);

            var collection = _client.CreateDocumentCollectionQuery(databaseUri)
                             .Where(c => c.Id == collectionName)
                             .AsEnumerable()
                             .FirstOrDefault();

            if (collection == null)
            {
                collection = new DocumentCollection
                {
                    Id             = collectionName,
                    IndexingPolicy = { IndexingMode = IndexingMode.Consistent }
                };

                collection.IndexingPolicy.IncludedPaths.Add(
                    new IncludedPath
                {
                    Path    = "/",
                    Indexes = new Collection <Index>
                    {
                        Index.Hash(DataType.String, -1)
                    }
                });

                var codeProperty = nameof(Models.UserSettings.UserId);
                collection.IndexingPolicy.IncludedPaths.Add(
                    new IncludedPath
                {
                    Path    = $"/\"{codeProperty}\"/?",
                    Indexes = new Collection <Index>
                    {
                        Index.Hash(DataType.String, -1)
                    }
                });

                var requestOptions = new RequestOptions
                {
                    OfferThroughput = _configuration.DefaultRUs
                };
                await _client.CreateDocumentCollectionAsync(databaseUri, collection, requestOptions);
            }
        }
 private async Task CreateCollectionIfNotExistsAsync()
 {
     await CreateResourceIfNotExistsAsync
     (
         () => _client.ReadDocumentCollectionAsync(CollectionUri),
         () => _client.CreateDocumentCollectionAsync(DatabaseUri,
                                                     new DocumentCollection {
         Id = CollectionId
     })
     );
 }
Пример #12
0
        public async Task EnsureInitialized()
        {
            try
            {
                await _documentClient.ReadDocumentCollectionAsync(_collectionUri);
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode != HttpStatusCode.NotFound)
                {
                    throw;
                }

                var collection = new DocumentCollection
                {
                    Id = _collectionId,
                    DefaultTimeToLive = -1,
                    IndexingPolicy    =
                    {
                        IndexingMode  = IndexingMode.Consistent,
                        Automatic     = true,
                        IncludedPaths =
                        {
                            new IncludedPath
                            {
                                Path    = "/*",
                                Indexes =
                                {
                                    new RangeIndex(DataType.Number, -1),
                                    new RangeIndex(DataType.String, -1) // Necessary for OrderBy
                                }
                            },
                        }
                    },
                    PartitionKey = new PartitionKeyDefinition {
                        Paths = { _partitionKeyPath }
                    }
                };

                try
                {
                    await _documentClient.CreateDocumentCollectionAsync(
                        UriFactory.CreateDatabaseUri(_databaseId),
                        collection);
                }
                catch (DocumentClientException dce)
                {
                    _logger.Warn($"Creation of {GetType().Name} collection failed, it might have been created by another instance.", dce);
                }
            }
        }
 public static async Task CreateDocumentCollectionIfNotExistsAsync(this IDocumentClient client, Uri databaseId, DocumentCollection documentCollection)
 {
     try
     {
         await client.CreateDocumentCollectionAsync(databaseId, documentCollection);
     }
     catch (DocumentClientException ex)
     {
         if (!(ex.StatusCode == HttpStatusCode.Conflict && ex.Message.Contains("Resource with specified id or name already exists")))
         {
             throw;
         }
     }
 }
Пример #14
0
        private async Task <DocumentCollection> GetOrCreateCollection()
        {
            var collectionName = typeof(T).Name;

            var collection = _client
                             .CreateDocumentCollectionQuery(_database.SelfLink)
                             .AsEnumerable()
                             .FirstOrDefault(c => c.Id == collectionName);

            return(collection ??
                   await _client.CreateDocumentCollectionAsync(
                       _database.SelfLink,
                       new DocumentCollection { Id = collectionName }));
        }
        public async Task <DocumentCollection> CreateOrGetCollection()
        {
            var collection =
                _documentClient.CreateDocumentCollectionQuery(
                    databaseLink: await _databaseProvider.GetDbSelfLink()
                    )
                .Where(c => c.Id == GetCollectionId())
                .AsEnumerable()
                .FirstOrDefault();

            return(collection ??
                   await _documentClient.CreateDocumentCollectionAsync(
                       await _databaseProvider.GetDbSelfLink(),
                       new DocumentCollection { Id = GetCollectionId() }));
        }
Пример #16
0
        private async Task <DocumentCollection> GetOrCreateCollectionAsync()
        {
            DocumentCollection collection = _client.CreateDocumentCollectionQuery((await _database).SelfLink).Where(c => c.Id == _collectionName).ToArray().FirstOrDefault();

            if (collection == null)
            {
                collection = new DocumentCollection {
                    Id = _collectionName
                };

                collection = await _client.CreateDocumentCollectionAsync((await _database).SelfLink, collection);
            }

            return(collection);
        }
        private async Task CreateColltionsIfNotExists()
        {
            foreach (var collectionName in _collectionNames)
            {
                try
                {
                    await _documentClient.ReadDocumentCollectionAsync(
                        UriFactory.CreateDocumentCollectionUri(_databaseName, collectionName));
                }
                catch (DocumentClientException e)
                {
                    if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
                    {
                        var docCollection = new DocumentCollection {
                            Id = collectionName
                        };
                        string partionKey = "";

                        if (collectionName == AppConstants.DbCognitiveFilesContainer)
                        {
                            partionKey = "/ownerId";
                        }
                        else if (collectionName == AppConstants.DbUserAccountsContainer)
                        {
                            partionKey = "/accountId";
                        }
                        else
                        {
                            partionKey = "/id";
                        }

                        if (string.IsNullOrEmpty(partionKey))
                        {
                            docCollection.PartitionKey.Paths.Add($"/{partionKey}");
                        }

                        await _documentClient.CreateDocumentCollectionAsync(
                            UriFactory.CreateDatabaseUri(_databaseName),
                            docCollection,
                            new RequestOptions { OfferThroughput = 400 });
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Пример #18
0
        private async Task CreateCollectionIfNotExistsAsync(
            IDocumentClient client,
            string dbName,
            string collName,
            RequestOptions options)
        {
            try
            {
                this.log.Info("Creating DocumentDb collection",
                              () => new { dbName, collName });
                var coll = new DocumentCollection {
                    Id = collName
                };

                var index    = Index.Range(DataType.String, -1);
                var indexing = new IndexingPolicy(index)
                {
                    IndexingMode = IndexingMode.Consistent
                };
                coll.IndexingPolicy = indexing;

                var dbUri = "/dbs/" + dbName;
                await client.CreateDocumentCollectionAsync(dbUri, coll, options);
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == HttpStatusCode.Conflict)
                {
                    this.log.Warn("Another process already created the collection",
                                  () => new { dbName, collName });
                    // Don't throw exception because it's fine if the collection was created somewhere else
                    return;
                }

                this.log.Error("Error while creating DocumentDb collection",
                               () => new { dbName, collName, e });

                throw new ExternalDependencyException("Error while creating DocumentDb collection", e);
            }
            catch (Exception e)
            {
                this.log.Error("Error while creating DocumentDb collection",
                               () => new { dbName, collName, e });
                throw;
            }
        }
Пример #19
0
 private async Task CreateCollectionIfNotExistsAsync(string databaseId, string collectionId)
 {
     try
     {
         await documentClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseId, collectionId));
     }
     catch (DocumentClientException documentClientException)
     {
         if (documentClientException.StatusCode == HttpStatusCode.NotFound)
         {
             await documentClient.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri(databaseId), new DocumentCollection { Id = collectionId }, new RequestOptions { OfferThroughput = 1000 });
         }
         else
         {
             throw;
         }
     }
 }
        private async Task <ResourceResponse <DocumentCollection> > CreateDocumentCollectionIfNotExists(string databaseName, string collectionName)
        {
            ResourceResponse <DocumentCollection> result;

            try
            {
                result = await _documentClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName)).ConfigureAwait(false);

                Logger.Info("Found {0}", collectionName);
            }
            catch (DocumentClientException de)
            {
                // If the document collection does not exist, create a new collection
                if (de.StatusCode == HttpStatusCode.NotFound)
                {
                    var collectionInfo = new DocumentCollection
                    {
                        Id             = collectionName,
                        IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String)
                        {
                            Precision = -1
                        })
                    };

                    // Optionally, you can configure the indexing policy of a collection. Here we configure collections for maximum query flexibility
                    // including string range queries.

                    // DocumentDB collections can be reserved with throughput specified in request units/second. 1 RU is a normalized request equivalent to the read
                    // of a 1KB document.  Here we create a collection with 400 RU/s.
                    result = await _documentClient.CreateDocumentCollectionAsync(
                        UriFactory.CreateDatabaseUri(databaseName),
                        new DocumentCollection { Id = collectionName },
                        new RequestOptions { OfferThroughput = 400 }).ConfigureAwait(false);

                    Logger.Info("Created {0}", collectionName);
                }
                else
                {
                    throw;
                }
            }

            return(result);
        }
        /// <summary>
        /// Creates a collection if it does not exist. This functionality is defined in DocumentClient, but not IDocumentClient.
        /// </summary>
        /// <param name="documentClient">The document client</param>
        /// <param name="databaseUri">The database URI</param>
        /// <param name="collectionUri">The collection URI</param>
        /// <param name="documentCollection">The collection to create</param>
        /// <param name="options">The request options</param>
        /// <returns>The result</returns>
        public static async Task <ResourceResponse <DocumentCollection> > CreateDocumentCollectionIfNotExistsAsync(
            this IDocumentClient documentClient,
            Uri databaseUri,
            Uri collectionUri,
            DocumentCollection documentCollection,
            RequestOptions options = null)
        {
            return(await CreateIfNotExists(
                       async() =>
            {
                DocumentCollection existingDocumentCollection = await documentClient.ReadDocumentCollectionAsync(collectionUri, options);

                existingDocumentCollection.IndexingPolicy = documentCollection.IndexingPolicy;
                existingDocumentCollection.DefaultTimeToLive = documentCollection.DefaultTimeToLive;

                return await documentClient.ReplaceDocumentCollectionAsync(existingDocumentCollection, options);
            },
                       () => documentClient.CreateDocumentCollectionAsync(databaseUri, documentCollection, options)));
        }
        private static async Task CreateCollectionAsync(IDocumentClient client, CollectionProperties properties)
        {
            var newCollection = new DocumentCollection
            {
                Id = properties.CollectionName,
                DefaultTimeToLive = -1,
                IndexingPolicy    = new IndexingPolicy(
                    new RangeIndex(DataType.String)
                {
                    Precision = -1
                },
                    new RangeIndex(DataType.Number)
                {
                    Precision = -1
                }
                    )
            };

            if (properties.PartitionKeys.Any())
            {
                newCollection.PartitionKey.Paths = new Collection <string>(properties.PartitionKeys);
            }

            if (!string.IsNullOrEmpty(properties.ExcludedPaths))
            {
                newCollection.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath {
                    Path = properties.ExcludedPaths
                });
            }

            if (properties.UniqueKeys.Any())
            {
                newCollection.UniqueKeyPolicy.UniqueKeys.Add(new UniqueKey {
                    Paths = new Collection <string>(properties.UniqueKeys)
                });
            }

            await client.CreateDocumentCollectionAsync(
                UriFactory.CreateDatabaseUri(properties.DatabaseName),
                newCollection,
                new RequestOptions { OfferThroughput = properties.OfferThroughput });
        }
 private async Task CreateCollectionIfNotExistsAsync()
 {
     try
     {
         await documentClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseId, collectionId));
     }
     catch (DocumentClientException e)
     {
         if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
         {
             await documentClient.CreateDocumentCollectionAsync(
                 UriFactory.CreateDatabaseUri(databaseId),
                 new DocumentCollection { Id = collectionId });
         }
         else
         {
             throw;
         }
     }
 }
Пример #24
0
        private static async Task <ResourceResponse <DocumentCollection> > CreateDocumentCollectionAsync(
            string collectionName, IDocumentClient client, IndexingPolicy indexingPolicy, RequestOptions requestOption)
        {
            Uri uri = UriFactory.CreateDatabaseUri("TablesDB");

            return(await client.CreateDocumentCollectionAsync(uri.ToString(), new DocumentCollection
            {
                Id = collectionName,
                PartitionKey = new PartitionKeyDefinition
                {
                    Paths =
                    {
                        "/'$pk'"
                    }
                },
                IndexingPolicy = (indexingPolicy
                                  ?? new IndexingPolicy(
                                      Index.Range(DataType.Number, -1), Index.Range(DataType.String, -1)))
            }, requestOption));
        }
Пример #25
0
 private static async Task CreateCollectionIfNotExistsAsync(this IDocumentClient documentClient, string databaseId, string collectionId)
 {
     try
     {
         await documentClient.ReadDocumentCollectionAsync(
             UriFactory.CreateDocumentCollectionUri(databaseId, collectionId));
     }
     catch (DocumentClientException ex)
     {
         if (ex.StatusCode == HttpStatusCode.NotFound)
         {
             await documentClient.CreateDocumentCollectionAsync(
                 UriFactory.CreateDatabaseUri(databaseId),
                 new DocumentCollection { Id = collectionId });
         }
         else
         {
             throw;
         }
     }
 }
Пример #26
0
        public static async Task CreateCollectionIfNotExistsAsync(this IDocumentClient client, string databaseId, string collectionId)
        {
            try {
                await CreateDatabaseIfNotExistsAsync(client, databaseId);

                await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseId, collectionId));
            }
            catch (DocumentClientException e) {
                if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    await client.CreateDocumentCollectionAsync(
                        UriFactory.CreateDatabaseUri(databaseId),
                        new DocumentCollection { Id = collectionId },
                        new RequestOptions { OfferThroughput = 400 });
                }
                else
                {
                    throw;
                }
            }
        }
 private static async Task CreateCollectionIfNotExistsAsync(IDocumentClient client)
 {
     try
     {
         await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseSettings.DatabaseId, DatabaseSettings.CollectionId));
     }
     catch (DocumentClientException e)
     {
         if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
         {
             await client.CreateDocumentCollectionAsync(
                 UriFactory.CreateDatabaseUri(DatabaseSettings.DatabaseId),
                 new DocumentCollection { Id = DatabaseSettings.CollectionId },
                 new RequestOptions { OfferThroughput = 400 });
         }
         else
         {
             throw;
         }
     }
 }
Пример #28
0
        private static async Task <DocumentCollection> CreateDocumentCollectionIfNotExistsAsync(this IDocumentClient client, string databaseLink, DocumentCollection collection)
        {
            try
            {
                var databaseId = databaseLink.Replace("dbs/", "").Replace("/", "");
                collection = (await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseId, collection.Id))).Resource;
            }
            catch (DocumentClientException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    collection = (await client.CreateDocumentCollectionAsync(databaseLink, collection)).Resource;
                }
                else
                {
                    throw;
                }
            }

            return(collection);
        }
 internal async Task CreateCollectionIfNotExistsAsync()
 {
     try
     {
         await _client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId));
     }
     catch (DocumentClientException e)
     {
         if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
         {
             await _client.CreateDocumentCollectionAsync(
                 UriFactory.CreateDatabaseUri(DatabaseId),
                 new DocumentCollection { Id = CollectionId },
                 new RequestOptions { OfferThroughput = 1000 });
         }
         else
         {
             throw;
         }
     }
 }
Пример #30
0
 private static async Task CreateCollectionIfNotExistsAsync(IDocumentClient client, string databaseName, string collectionName)
 {
     try
     {
         await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName));
     }
     catch (DocumentClientException e)
     {
         if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
         {
             await client.CreateDocumentCollectionAsync(
                 UriFactory.CreateDatabaseUri(databaseName),
                 new DocumentCollection { Id = collectionName },
                 new RequestOptions { OfferThroughput = 1000 });
         }
         else
         {
             throw;
         }
     }
 }