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; } } }
private static async Task CreateCollectionIfNotExistsAsync(IDocumentClient documentClient, string collectionId, IOptions <CosmosDbOptions> settings) { try { await documentClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(settings.Value.DatabaseId, collectionId)); } catch (DocumentClientException e) { if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { await documentClient.CreateDocumentCollectionIfNotExistsAsync( UriFactory.CreateDatabaseUri(settings.Value.DatabaseId), new DocumentCollection { Id = collectionId, PartitionKey = new PartitionKeyDefinition { Paths = new Collection <string> { "/type" } } }, new RequestOptions { OfferThroughput = settings.Value.OfferThroughput, ConsistencyLevel = settings.Value.ConsistencyLevel }); } else { throw; } } }
public DocumentSession(IDocumentClient client, DocumentSessionConnectionString connectionString) { _client = client ?? throw new ArgumentNullException(nameof(client)); _databaseId = connectionString.Database; _documentCollection = client .ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(connectionString.Database, connectionString.Collection)) .Result.Resource; }
public async Task EnsureDbSetupAsync() { await _documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(_databaseName)); foreach (var collectionName in _collectionNames) { await _documentClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(_databaseName, collectionName)); } }
List <My.PartitionKeyRangeStatistics> partitionInfoList)> GetCollectionInfoAsync() { ResourceResponse <DocumentCollection> collectionInfo = await _documentClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(_database, _collection), new RequestOptions() { PopulateQuotaInfo = true, PopulatePartitionKeyRangeStatistics = true }); var partitionInfoList = new List <My.PartitionKeyRangeStatistics>(); foreach (var partitionKeyRangeStatistics in collectionInfo.Resource.PartitionKeyRangeStatistics) { var partitionInfo = new My.PartitionKeyRangeStatistics { PartitionKeyRangeId = partitionKeyRangeStatistics.PartitionKeyRangeId, DocumentCount = partitionKeyRangeStatistics.DocumentCount, SizeInKB = partitionKeyRangeStatistics.SizeInKB, PartitionKeyStatistics = new List <My.PartitionKeyStatistics>() }; foreach (var partitionKeyStatistics in partitionKeyRangeStatistics.PartitionKeyStatistics) { partitionInfo.PartitionKeyStatistics.Add(new My.PartitionKeyStatistics() { PartitionKey = partitionKeyStatistics.PartitionKey.ToString(), SizeInKB = partitionKeyStatistics.SizeInKB }); } partitionInfoList.Add(partitionInfo); } return(collectionInfo.CollectionQuota, collectionInfo.CollectionSizeQuota, collectionInfo.CollectionSizeUsage, collectionInfo.CollectionUsage, collectionInfo.DatabaseQuota, collectionInfo.DatabaseUsage, collectionInfo.DocumentQuota, collectionInfo.DocumentUsage, collectionInfo.UserQuota, collectionInfo.UserUsage, collectionInfo.CurrentResourceQuotaUsage, partitionInfoList); }
private async Task CreateCollectionIfNotExistsAsync() { await CreateResourceIfNotExistsAsync ( () => _client.ReadDocumentCollectionAsync(CollectionUri), () => _client.CreateDocumentCollectionAsync(DatabaseUri, new DocumentCollection { Id = CollectionId }) ); }
private async Task <Offer> GetCurrentOfferAsync() { // this proccess's RU is 1 DocumentCollection collection = await _client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId)); var currentOffer = _client.CreateOfferQuery() .AsEnumerable() .Single(o => o.ResourceLink == collection.SelfLink); return(currentOffer); }
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 <DocumentCollection> TryGetDocumentCollectionAsync( this IDocumentClient documentClient, Uri collectionUri, RequestOptions options = null) { try { return(await documentClient.ReadDocumentCollectionAsync(collectionUri, options)); } catch (DocumentClientException readException) when(readException.StatusCode == HttpStatusCode.NotFound) { return(null); } }
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; } } } }
public static async Task DeleteDocumentCollectionIfExistsAsync(this IDocumentClient client, string databaseId, string collectionId, RequestOptions requestOptions = null) { try { var response = await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseId, collectionId), requestOptions); await client.DeleteDocumentCollectionAsync(response.Resource.SelfLink, requestOptions); } catch (DocumentClientException e) { if (e.StatusCode != System.Net.HttpStatusCode.NotFound) { throw e.InnerException; } } }
private async Task UpdateCollectionConfigAsync(IDocumentClient client, DocumentDbConfig dbConfig, List <DocumentStoreConfig> storeConfigs) { DocumentCollection collection = await client.ReadDocumentCollectionAsync(GetDocumentCollectionUri(dbConfig)); RemoveIndexes(collection); AddIndexes(collection, storeConfigs); var requestOptions = new RequestOptions(); requestOptions.AccessCondition = new AccessCondition { Type = AccessConditionType.IfMatch, Condition = collection.ETag }; await client.ReplaceDocumentCollectionAsync(collection, requestOptions); }
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 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; } } }
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; } } }
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; } } }
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; } } }
private async Task EnsureCollectionExistsAsync( IDocumentClient client, RequestOptions options, string dbName, string collName) { try { var uri = $"/dbs/{dbName}/colls/{collName}"; await client.ReadDocumentCollectionAsync(uri, options); } catch (DocumentClientException e) when(e.StatusCode == HttpStatusCode.NotFound) { await this.CreateCollectionIfNotExistsAsync(client, dbName, collName, options); } catch (Exception e) { this.log.Error("Error while getting DocumentDb collection", e); throw; } }
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; } } }
private async Task CreateCollectionIfNotExistsAsync( IDocumentClient client, RequestOptions options, string dbName, string collName) { try { var uri = $"/dbs/{dbName}/colls/{collName}"; this.log.Info("Checking if the collection exists", () => new { dbName, collName }); await client.ReadDocumentCollectionAsync(uri, options); } catch (DocumentClientException e) when(e.StatusCode == HttpStatusCode.NotFound) { await this.CreateCollectionAsync(client, dbName, collName, options); } catch (Exception e) { this.log.Error("Error while getting Cosmos DB SQL collection", e); throw; } }
private async Task CreateCollectionIfNotExistsAsync() { try { await client.ReadDocumentCollectionAsync(collectionLink, options); } catch (DocumentClientException e) { if (e.StatusCode != HttpStatusCode.NotFound) { logger.Error("Error while getting DocumentDb collection", () => new { e }); throw; } await CreateCollectionAsync(); } catch (Exception e) { logger.Error("Error while getting DocumentDb collection", () => new { e }); throw; } }
/// <summary> /// Gets the document collection. /// </summary> /// <param name="dbId">Database id.</param> /// <param name="collectionId">Collection id.</param> /// <param name="requestOptions">Request options</param> /// <returns>DocumentCollection with specified id.</returns> public async Task <ResourceResponse <DocumentCollection> > GetCollectionAsync( string dbId, string collectionId, RequestOptions requestOptions = null) { Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b1d4 /* tag_961hu */)); Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b1d5 /* tag_961hv */)); IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false); return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b1d6 /* tag_961hw */), async() => { try { return await client.ReadDocumentCollectionAsync( UriFactory.CreateDocumentCollectionUri(dbId, collectionId), requestOptions); } catch (DocumentClientException e) when(e.StatusCode == HttpStatusCode.NotFound) { return default(ResourceResponse <DocumentCollection>); } }).ConfigureAwait(false)); }
public static async Task Initialize(IDocumentDbClientFactory documentDbClientFactory) { DatabaseId = ConfigurationManager.AppSettings.GetRefValue <string>(DatabaseIdConfigName); CollectionId = ConfigurationManager.AppSettings.GetRefValue <string>(CollectionNameConfigName); IDocumentClient client = documentDbClientFactory.GetClient(); try { await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId)); } catch (DocumentClientException ex) { if (ex.StatusCode == HttpStatusCode.NotFound) { await client.CreateDatabaseAsync(new Database { Id = DatabaseId }); } } try { await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)); } catch (DocumentClientException ex) { if (ex.StatusCode == HttpStatusCode.NotFound) { var documentCollection = new DocumentCollection { Id = CollectionId, IndexingPolicy = CollectionIndexingPolicy, }; await client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri(DatabaseId), documentCollection, CollectionRequestOptions); } } }
private async Task CreateCollectionIfNotExistsAsync(string CollectionId) { try { await _documentClient.ReadDocumentCollectionAsync(CollectionUri); } catch (DocumentClientException e) { if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { await _documentClient.CreateDocumentCollectionAsync( UriFactory.CreateDatabaseUri(_applicationConfig.Database), new DocumentCollection { Id = _applicationConfig.Collection, IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 }) }, new RequestOptions { OfferThroughput = 1000 }); } else { throw; } } }
public async Task <DocumentCollection> ReadDocumentCollectionAsync(RequestOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { return(await _documentClient.ReadDocumentCollectionAsync( UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName), options)); }
public async void EnsureConfigCurrentWithChangedSignature() { var dbConfig = CreateDbConfig(); string oldSig = "old_sig"; string newSig = "new_sig"; var configDoc = CreateConfigDoc("Test", oldSig); DocumentStoreConfigBuilder storeABuilder = new DocumentStoreConfigBuilder("StoreA"); storeABuilder.AddDocument("DocA"); DocumentStoreConfigBuilder storeBBuilder = new DocumentStoreConfigBuilder("StoreB"); storeBBuilder.AddDocument("DocA"); storeBBuilder.AddDocument("DocB"); var configSourceA = CreateConfigSource(storeABuilder); var configSourceB = CreateConfigSource(storeBBuilder); _signatureGenerator.CreateSignature(Arg.Any <IList <DocumentStoreConfig> >()).Returns(newSig); _documentClient.ReadDocumentAsync(Arg.Any <Uri>(), Arg.Is <RequestOptions>(x => x.PartitionKey != null)).Returns(WrapResource(configDoc)); // Existing index that should remain. var includeIdx1 = new IncludedPath(); includeIdx1.Path = "/content_Test_StoreA_DocA/*"; includeIdx1.Indexes.Add(new HashIndex(DataType.String, -1)); // Existing index that should be removed. It is no longer present. var includeIdx2 = new IncludedPath(); includeIdx2.Path = "/content_Test_StoreB_DocA/PropA/*"; includeIdx2.Indexes.Add(new RangeIndex(DataType.String)); var col1 = new DocumentCollection(); col1.IndexingPolicy.IncludedPaths.Add(includeIdx1); col1.IndexingPolicy.IncludedPaths.Add(includeIdx2); _documentClient.ReadDocumentCollectionAsync(Arg.Any <Uri>()).Returns(WrapResource(col1)); var manager = new ServiceDbConfigManager("Test", _signatureGenerator); var foo = WrapResource(col1); _documentClient.ReplaceDocumentCollectionAsync(Arg.Any <DocumentCollection>(), Arg.Any <RequestOptions>()).Returns(foo); _documentClient.UpsertDocumentAsync( Arg.Any <Uri>(), Arg.Is <object>(r => ((ServiceDbConfigManager.ServiceConfigRecord)r).Signature == newSig), Arg.Any <RequestOptions>()) .Returns(WrapResource(CreateConfigDoc(configDoc.Id, newSig))); manager.RegisterStoreConfigSource(configSourceA); manager.RegisterStoreConfigSource(configSourceB); manager.EnsureConfigCurrent(_documentClient, dbConfig); await _documentClient.Received(1).UpsertDocumentAsync(Arg.Any <Uri>(), Arg.Any <object>(), Arg.Any <RequestOptions>()); await _documentClient.Received().ReplaceDocumentCollectionAsync( Arg.Is <DocumentCollection>(c => IncludedPathCheck( c.IndexingPolicy.IncludedPaths, "/content_Test_StoreA_DocA/*", "/content_Test_StoreB_DocA/*", "/content_Test_StoreB_DocB/*")), Arg.Any <RequestOptions>()); }