/// <summary> /// <para>Check if a database exists, and if it doesn't, create it. /// Only the database id is used to verify if there is an existing database. Other database properties /// such as throughput are not validated and can be different then the passed properties.</para> /// /// <para>A database manages users, permissions and a set of containers. /// Each Azure Cosmos DB Database Account is able to support multiple independent named databases, /// with the database being the logical container for data.</para> /// /// <para>Each Database consists of one or more containers, each of which in turn contain one or more /// documents. Since databases are an administrative resource, the Service Master Key will be /// required in order to access and successfully complete any action using the User APIs.</para> /// </summary> /// <param name="id">The database id.</param> /// <param name="throughput">(Optional) The throughput provisioned for a database in measurement of Request Units per second in the Azure Cosmos DB service.</param> /// <param name="requestOptions">(Optional) A set of additional options that can be set.</param> /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param> /// <returns>A <see cref="Task"/> containing a <see cref="DatabaseResponse"/> which wraps a <see cref="DatabaseProperties"/> containing the resource record. /// <list type="table"> /// <listheader> /// <term>StatusCode</term><description>Common success StatusCodes for the CreateDatabaseIfNotExistsAsync operation</description> /// </listheader> /// <item> /// <term>201</term><description>Created - New database is created.</description> /// </item> /// <item> /// <term>200</term><description>Accepted - This means the database already exists.</description> /// </item> /// </list> /// </returns> /// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units">Request Units</seealso> public virtual async Task <DatabaseResponse> CreateDatabaseIfNotExistsAsync( string id, int?throughput = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrEmpty(id)) { throw new ArgumentNullException(nameof(id)); } // Doing a Read before Create will give us better latency for existing databases DatabaseProperties databaseProperties = this.PrepareDatabaseProperties(id); Database database = this.GetDatabase(id); ResponseMessage response = await database.ReadStreamAsync(requestOptions : requestOptions, cancellationToken : cancellationToken); if (response.StatusCode != HttpStatusCode.NotFound) { return(await this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(database, Task.FromResult(response))); } response = await this.CreateDatabaseStreamAsync(databaseProperties, throughput, requestOptions, cancellationToken); if (response.StatusCode != HttpStatusCode.Conflict) { return(await this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(this.GetDatabase(databaseProperties.Id), Task.FromResult(response))); } // This second Read is to handle the race condition when 2 or more threads have Read the database and only one succeeds with Create // so for the remaining ones we should do a Read instead of throwing Conflict exception return(await database.ReadAsync(cancellationToken : cancellationToken)); }
/// <summary> /// Send a request for creating a database. /// /// A database manages users, permissions and a set of containers. /// Each Azure Cosmos DB Database Account is able to support multiple independent named databases, /// with the database being the logical container for data. /// /// Each Database consists of one or more containers, each of which in turn contain one or more /// documents. Since databases are an administrative resource, the Service Master Key will be /// required in order to access and successfully complete any action using the User APIs. /// </summary> /// <param name="databaseProperties">The database properties</param> /// <param name="throughputProperties">(Optional) The throughput provisioned for a database in measurement of Request Units per second in the Azure Cosmos DB service.</param> /// <param name="requestOptions">(Optional) A set of options that can be set.</param> /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param> /// <returns>A <see cref="Task"/> containing a <see cref="DatabaseResponse"/> which wraps a <see cref="DatabaseProperties"/> containing the resource record.</returns> /// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units">Request Units</seealso> internal virtual Task <ResponseMessage> CreateDatabaseStreamAsync( DatabaseProperties databaseProperties, ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { if (databaseProperties == null) { throw new ArgumentNullException(nameof(databaseProperties)); } return(this.ClientContext.OperationHelperAsync( nameof(CreateDatabaseIfNotExistsAsync), requestOptions, (diagnostics) => { this.ClientContext.ValidateResource(databaseProperties.Id); return this.CreateDatabaseStreamInternalAsync( diagnostics, databaseProperties, throughputProperties, requestOptions, cancellationToken); })); }
/// <summary> /// Sends a request for creating a database. /// /// A database manages users, permissions and a set of containers. /// Each Azure Cosmos DB Database Account is able to support multiple independent named databases, /// with the database being the logical container for data. /// /// Each Database consists of one or more containers, each of which in turn contain one or more /// documents. Since databases are an administrative resource, the Service Master Key will be /// required in order to access and successfully complete any action using the User APIs. /// </summary> /// <param name="id">The database id.</param> /// <param name="throughputProperties">(Optional) The throughput provisioned for a database in measurement of Request Units per second in the Azure Cosmos DB service.</param> /// <param name="requestOptions">(Optional) A set of options that can be set.</param> /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param> /// <returns>A <see cref="Task"/> containing a <see cref="DatabaseResponse"/> which wraps a <see cref="DatabaseProperties"/> containing the resource record.</returns> /// <exception>https://aka.ms/cosmosdb-dot-net-exceptions</exception> /// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units">Request Units</seealso> public virtual Task <DatabaseResponse> CreateDatabaseAsync( string id, ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrEmpty(id)) { throw new ArgumentNullException(nameof(id)); } return(this.ClientContext.OperationHelperAsync( nameof(CreateDatabaseAsync), requestOptions, (diagnostics) => { DatabaseProperties databaseProperties = this.PrepareDatabaseProperties(id); return this.CreateDatabaseInternalAsync( diagnosticsContext: diagnostics, databaseProperties: databaseProperties, throughputProperties: throughputProperties, requestOptions: requestOptions, cancellationToken: cancellationToken); })); }
/// <summary> /// <para>Check if a database exists, and if it doesn't, create it. /// Only the database id is used to verify if there is an existing database. Other database properties /// such as throughput are not validated and can be different then the passed properties.</para> /// /// <para>A database manages users, permissions and a set of containers. /// Each Azure Cosmos DB Database Account is able to support multiple independent named databases, /// with the database being the logical container for data.</para> /// /// <para>Each Database consists of one or more containers, each of which in turn contain one or more /// documents. Since databases are an administrative resource, the Service Master Key will be /// required in order to access and successfully complete any action using the User APIs.</para> /// </summary> /// <param name="id">The database id.</param> /// <param name="throughputProperties">The throughput provisioned for a database in measurement of Request Units per second in the Azure Cosmos DB service.</param> /// <param name="requestOptions">(Optional) A set of additional options that can be set.</param> /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param> /// <returns>A <see cref="Task"/> containing a <see cref="DatabaseResponse"/> which wraps a <see cref="DatabaseProperties"/> containing the resource record. /// <list type="table"> /// <listheader> /// <term>StatusCode</term><description>Common success StatusCodes for the CreateDatabaseIfNotExistsAsync operation</description> /// </listheader> /// <item> /// <term>201</term><description>Created - New database is created.</description> /// </item> /// <item> /// <term>200</term><description>Accepted - This means the database already exists.</description> /// </item> /// </list> /// </returns> /// <exception>https://aka.ms/cosmosdb-dot-net-exceptions</exception> /// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units">Request Units</seealso> public virtual Task <DatabaseResponse> CreateDatabaseIfNotExistsAsync( string id, ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrEmpty(id)) { throw new ArgumentNullException(nameof(id)); } return(this.ClientContext.OperationHelperAsync( nameof(CreateDatabaseIfNotExistsAsync), requestOptions, async(diagnostics) => { // Doing a Read before Create will give us better latency for existing databases DatabaseProperties databaseProperties = this.PrepareDatabaseProperties(id); DatabaseCore database = (DatabaseCore)this.GetDatabase(id); using (ResponseMessage readResponse = await database.ReadStreamAsync( diagnosticsContext: diagnostics, requestOptions: requestOptions, cancellationToken: cancellationToken)) { if (readResponse.StatusCode != HttpStatusCode.NotFound) { return this.ClientContext.ResponseFactory.CreateDatabaseResponse(database, readResponse); } } using (ResponseMessage createResponse = await this.CreateDatabaseStreamInternalAsync( diagnostics, databaseProperties, throughputProperties, requestOptions, cancellationToken)) { if (createResponse.StatusCode != HttpStatusCode.Conflict) { return this.ClientContext.ResponseFactory.CreateDatabaseResponse(this.GetDatabase(databaseProperties.Id), createResponse); } } // This second Read is to handle the race condition when 2 or more threads have Read the database and only one succeeds with Create // so for the remaining ones we should do a Read instead of throwing Conflict exception ResponseMessage readResponseAfterConflict = await database.ReadStreamAsync( diagnosticsContext: diagnostics, requestOptions: requestOptions, cancellationToken: cancellationToken); return this.ClientContext.ResponseFactory.CreateDatabaseResponse(this.GetDatabase(databaseProperties.Id), readResponseAfterConflict); })); }
internal Task <DatabaseResponse> CreateDatabaseAsync( DatabaseProperties databaseProperties, int?throughput = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { Task <ResponseMessage> response = this.CreateDatabaseStreamInternalAsync( streamPayload: this.ClientContext.PropertiesSerializer.ToStream <DatabaseProperties>(databaseProperties), throughput: throughput, requestOptions: requestOptions, cancellationToken: cancellationToken); return(this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(this.GetDatabase(databaseProperties.Id), response)); }
internal Task <DatabaseResponse> CreateDatabaseResponseAsync( Database database, Task <ResponseMessage> cosmosResponseMessageTask) { return(this.ProcessMessageAsync(cosmosResponseMessageTask, (cosmosResponseMessage) => { DatabaseProperties databaseProperties = this.ToObjectInternal <DatabaseProperties>(cosmosResponseMessage, this.propertiesSerializer); return new DatabaseResponse( cosmosResponseMessage.StatusCode, cosmosResponseMessage.Headers, databaseProperties, database); })); }
internal DatabaseProperties PrepareDatabaseProperties(string id) { if (string.IsNullOrWhiteSpace(id)) { throw new ArgumentNullException(nameof(id)); } DatabaseProperties databaseProperties = new DatabaseProperties() { Id = id }; this.ClientContext.ValidateResource(databaseProperties.Id); return(databaseProperties); }
/// <summary> /// Send a request for creating a database. /// /// A database manages users, permissions and a set of containers. /// Each Azure Cosmos DB Database Account is able to support multiple independent named databases, /// with the database being the logical container for data. /// /// Each Database consists of one or more containers, each of which in turn contain one or more /// documents. Since databases are an administrative resource, the Service Master Key will be /// required in order to access and successfully complete any action using the User APIs. /// </summary> /// <param name="databaseProperties">The database properties</param> /// <param name="throughput">(Optional) The throughput provisioned for a database in measurement of Request Units per second in the Azure Cosmos DB service.</param> /// <param name="requestOptions">(Optional) A set of options that can be set.</param> /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param> /// <returns>A <see cref="Task"/> containing a <see cref="DatabaseResponse"/> which wraps a <see cref="DatabaseProperties"/> containing the resource record.</returns> /// <remarks> /// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units"/> for details on provision throughput. /// </remarks> public virtual Task <ResponseMessage> CreateDatabaseStreamAsync( DatabaseProperties databaseProperties, int?throughput = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { if (databaseProperties == null) { throw new ArgumentNullException(nameof(databaseProperties)); } this.ClientContext.ValidateResource(databaseProperties.Id); Stream streamPayload = this.ClientContext.PropertiesSerializer.ToStream <DatabaseProperties>(databaseProperties); return(this.CreateDatabaseStreamInternalAsync(streamPayload, throughput, requestOptions, cancellationToken)); }
public override DatabaseResponse CreateDatabaseResponse( Database database, ResponseMessage responseMessage) { return(this.ProcessMessage(responseMessage, (cosmosResponseMessage) => { DatabaseProperties databaseProperties = this.ToObjectpublic <DatabaseProperties>(cosmosResponseMessage); return new DatabaseResponse( cosmosResponseMessage.StatusCode, cosmosResponseMessage.Headers, databaseProperties, database, cosmosResponseMessage.Diagnostics); })); }
/// <summary> /// <para>Check if a database exists, and if it doesn't, create it. /// Only the database id is used to verify if there is an existing database. Other database properties /// such as throughput are not validated and can be different then the passed properties.</para> /// /// <para>A database manages users, permissions and a set of containers. /// Each Azure Cosmos DB Database Account is able to support multiple independent named databases, /// with the database being the logical container for data.</para> /// /// <para>Each Database consists of one or more containers, each of which in turn contain one or more /// documents. Since databases are an administrative resource, the Service Master Key will be /// required in order to access and successfully complete any action using the User APIs.</para> /// </summary> /// <param name="id">The database id.</param> /// <param name="throughput">(Optional) The throughput provisioned for a database in measurement of Request Units per second in the Azure Cosmos DB service.</param> /// <param name="requestOptions">(Optional) A set of additional options that can be set.</param> /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param> /// <returns>A <see cref="Task"/> containing a <see cref="DatabaseResponse"/> which wraps a <see cref="DatabaseProperties"/> containing the resource record. /// <list type="table"> /// <listheader> /// <term>StatusCode</term><description>Common success StatusCodes for the CreateDatabaseIfNotExistsAsync operation</description> /// </listheader> /// <item> /// <term>201</term><description>Created - New database is created.</description> /// </item> /// <item> /// <term>200</term><description>Accepted - This means the database already exists.</description> /// </item> /// </list> /// </returns> /// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units">Request Units</seealso> public virtual Task <DatabaseResponse> CreateDatabaseIfNotExistsAsync( string id, int?throughput = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { this.ThrowIfDisposed(); if (string.IsNullOrEmpty(id)) { throw new ArgumentNullException(nameof(id)); } return(TaskHelper.RunInlineIfNeededAsync(async() => { // Doing a Read before Create will give us better latency for existing databases DatabaseProperties databaseProperties = this.PrepareDatabaseProperties(id); Database database = this.GetDatabase(id); ResponseMessage readResponse = await database.ReadStreamAsync( requestOptions: requestOptions, cancellationToken: cancellationToken); if (readResponse.StatusCode != HttpStatusCode.NotFound) { return await this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(database, Task.FromResult(readResponse)); } ResponseMessage createResponse = await this.CreateDatabaseStreamAsync(databaseProperties, throughput, requestOptions, cancellationToken); // Merge the diagnostics with the first read request. createResponse.DiagnosticsContext.AddDiagnosticsInternal(readResponse.DiagnosticsContext); if (createResponse.StatusCode != HttpStatusCode.Conflict) { return await this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(this.GetDatabase(databaseProperties.Id), Task.FromResult(createResponse)); } // This second Read is to handle the race condition when 2 or more threads have Read the database and only one succeeds with Create // so for the remaining ones we should do a Read instead of throwing Conflict exception ResponseMessage readResponseAfterConflict = await database.ReadStreamAsync( requestOptions: requestOptions, cancellationToken: cancellationToken); readResponseAfterConflict.DiagnosticsContext.AddDiagnosticsInternal(readResponse.DiagnosticsContext); return await this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(this.GetDatabase(databaseProperties.Id), Task.FromResult(readResponseAfterConflict)); })); }
/// <summary> /// Send a request for creating a database. /// /// A database manages users, permissions and a set of containers. /// Each Azure Cosmos DB Database Account is able to support multiple independent named databases, /// with the database being the logical container for data. /// /// Each Database consists of one or more containers, each of which in turn contain one or more /// documents. Since databases are an administrative resource, the Service Master Key will be /// required in order to access and successfully complete any action using the User APIs. /// </summary> /// <param name="id">The database id.</param> /// <param name="throughput">(Optional) The throughput provisioned for a database in measurement of Request Units per second in the Azure Cosmos DB service.</param> /// <param name="requestOptions">(Optional) A set of options that can be set.</param> /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param> /// <returns>A <see cref="Task"/> containing a <see cref="DatabaseResponse"/> which wraps a <see cref="DatabaseProperties"/> containing the resource record.</returns> /// <remarks> /// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units"/> for details on provision throughput. /// </remarks> public virtual Task <DatabaseResponse> CreateDatabaseAsync( string id, int?throughput = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrEmpty(id)) { throw new ArgumentNullException(nameof(id)); } DatabaseProperties databaseProperties = this.PrepareDatabaseProperties(id); return(this.CreateDatabaseAsync( databaseProperties: databaseProperties, throughput: throughput, requestOptions: requestOptions, cancellationToken: cancellationToken)); }
virtual Task <DatabaseResponse> CreateDatabaseAsync( string id, ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrEmpty(id)) { throw new ArgumentNullException(nameof(id)); } DatabaseProperties databaseProperties = this.PrepareDatabaseProperties(id); return(TaskHelper.RunInlineIfNeededAsync(() => this.CreateDatabaseAsync( databaseProperties: databaseProperties, throughputProperties: throughputProperties, requestOptions: requestOptions, cancellationToken: cancellationToken))); }
private Task <ResponseMessage> CreateDatabaseStreamInternalAsync( CosmosDiagnosticsContext diagnosticsContext, DatabaseProperties databaseProperties, ThroughputProperties throughputProperties, RequestOptions requestOptions, CancellationToken cancellationToken) { return(this.ClientContext.ProcessResourceOperationAsync( resourceUri: this.DatabaseRootUri, resourceType: ResourceType.Database, operationType: OperationType.Create, requestOptions: requestOptions, containerInternal: null, partitionKey: null, streamPayload: this.ClientContext.SerializerCore.ToStream <DatabaseProperties>(databaseProperties), requestEnricher: (httpRequestMessage) => httpRequestMessage.AddThroughputPropertiesHeader(throughputProperties), responseCreator: (response) => response, diagnosticsContext: diagnosticsContext, cancellationToken: cancellationToken)); }
internal Task <DatabaseResponse> CreateDatabaseAsync( DatabaseProperties databaseProperties, ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { Task <ResponseMessage> response = this.ClientContext.ProcessResourceOperationStreamAsync( resourceUri: this.DatabaseRootUri, resourceType: ResourceType.Database, operationType: OperationType.Create, requestOptions: requestOptions, cosmosContainerCore: null, partitionKey: null, streamPayload: this.ClientContext.SerializerCore.ToStream <DatabaseProperties>(databaseProperties), requestEnricher: (httpRequestMessage) => httpRequestMessage.AddThroughputPropertiesHeader(throughputProperties), diagnosticsContext: null, cancellationToken: cancellationToken); return(this.ClientContext.ResponseFactory.CreateDatabaseResponseAsync(this.GetDatabase(databaseProperties.Id), response)); }
/// <summary> /// Send a request for creating a database. /// /// A database manages users, permissions and a set of containers. /// Each Azure Cosmos DB Database Account is able to support multiple independent named databases, /// with the database being the logical container for data. /// /// Each Database consists of one or more containers, each of which in turn contain one or more /// documents. Since databases are an administrative resource, the Service Master Key will be /// required in order to access and successfully complete any action using the User APIs. /// </summary> /// <param name="databaseProperties">The database properties</param> /// <param name="throughputProperties">(Optional) The throughput provisioned for a database in measurement of Request Units per second in the Azure Cosmos DB service.</param> /// <param name="requestOptions">(Optional) A set of options that can be set.</param> /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param> /// <returns>A <see cref="Task"/> containing a <see cref="DatabaseResponse"/> which wraps a <see cref="DatabaseProperties"/> containing the resource record.</returns> /// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units">Request Units</seealso> internal virtual Task <ResponseMessage> CreateDatabaseStreamAsync( DatabaseProperties databaseProperties, ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { if (databaseProperties == null) { throw new ArgumentNullException(nameof(databaseProperties)); } this.ClientContext.ValidateResource(databaseProperties.Id); Stream streamPayload = this.ClientContext.SerializerCore.ToStream <DatabaseProperties>(databaseProperties); return(TaskHelper.RunInlineIfNeededAsync(() => this.CreateDatabaseStreamInternalAsync( streamPayload, throughputProperties, requestOptions, cancellationToken))); }