internal Task <ThroughputResponse> CreateThroughputResponseAsync( Task <ResponseMessage> cosmosResponseMessageTask) { return(this.ProcessMessageAsync(cosmosResponseMessageTask, (cosmosResponseMessage) => { ThroughputProperties throughputProperties = this.ToObjectInternal <ThroughputProperties>(cosmosResponseMessage); return new ThroughputResponse( cosmosResponseMessage.StatusCode, cosmosResponseMessage.Headers, throughputProperties, cosmosResponseMessage.Diagnostics); })); }
public override ThroughputResponse CreateThroughputResponse( ResponseMessage responseMessage) { return(this.ProcessMessage(responseMessage, (cosmosResponseMessage) => { ThroughputProperties throughputProperties = this.ToObjectpublic <ThroughputProperties>(cosmosResponseMessage); return new ThroughputResponse( cosmosResponseMessage.StatusCode, cosmosResponseMessage.Headers, throughputProperties, 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> /// <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, int?throughput = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { ThroughputProperties throughputProperties = ThroughputProperties.CreateManualThroughput(throughput); return(this.CreateDatabaseIfNotExistsAsync( id, throughputProperties, requestOptions, cancellationToken)); }
public Task <ThroughputResponse> ReplaceThroughputAsync( CosmosDiagnosticsContext diagnosticsContext, int throughput, ITrace trace, RequestOptions requestOptions = null, CancellationToken cancellationToken = default) { return(this.ReplaceThroughputAsync( diagnosticsContext: diagnosticsContext, throughputProperties: ThroughputProperties.CreateManualThroughput(throughput), requestOptions: requestOptions, trace: trace, cancellationToken: cancellationToken)); }
internal async Task <ThroughputResponse> ReplaceThroughputPropertiesIfExistsAsync( string targetRID, ThroughputProperties throughputProperties, RequestOptions requestOptions, CancellationToken cancellationToken = default(CancellationToken)) { try { ThroughputProperties currentProperty = await this.GetOfferV2Async <ThroughputProperties>(targetRID, failIfNotConfigured : false, cancellationToken : cancellationToken); if (currentProperty == null) { CosmosException notFound = CosmosExceptionFactory.CreateNotFoundException( $"Throughput is not configured for {targetRID}"); return(new ThroughputResponse( httpStatusCode: notFound.StatusCode, headers: notFound.Headers, throughputProperties: null, diagnostics: notFound.Diagnostics)); } currentProperty.Content = throughputProperties.Content; return(await this.GetThroughputResponseAsync( streamPayload : this.ClientContext.SerializerCore.ToStream(currentProperty), operationType : OperationType.Replace, linkUri : new Uri(currentProperty.SelfLink, UriKind.Relative), resourceType : ResourceType.Offer, requestOptions : requestOptions, cancellationToken : cancellationToken)); } catch (DocumentClientException dce) { ResponseMessage responseMessage = dce.ToCosmosResponseMessage(null); return(new ThroughputResponse( responseMessage.StatusCode, headers: responseMessage.Headers, throughputProperties: null, diagnostics: responseMessage.Diagnostics)); } catch (AggregateException ex) { ResponseMessage responseMessage = TransportHandler.AggregateExceptionConverter(ex, null); return(new ThroughputResponse( responseMessage.StatusCode, headers: responseMessage.Headers, throughputProperties: null, diagnostics: responseMessage.Diagnostics)); } }
internal override async Task <ThroughputResponse> ReplaceThroughputIfExistsAsync( ThroughputProperties throughput, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) { string rid = await this.GetRIDAsync(cancellationToken); CosmosOffers cosmosOffers = new CosmosOffers(this.ClientContext); return(await cosmosOffers.ReplaceThroughputPropertiesIfExistsAsync( targetRID : rid, throughputProperties : throughput, requestOptions : requestOptions, cancellationToken : cancellationToken)); }
public override async Task <ThroughputResponse> ReplaceThroughputAsync( ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default) { string rid = await this.GetRIDAsync(cancellationToken); CosmosOffers cosmosOffers = new CosmosOffers(this.ClientContext); return(await cosmosOffers.ReplaceThroughputPropertiesAsync( rid, throughputProperties, requestOptions, cancellationToken)); }
public async Task <ThroughputResponse> ReplaceThroughputAsync( CosmosDiagnosticsContext diagnosticsContext, ThroughputProperties throughputProperties, RequestOptions requestOptions, CancellationToken cancellationToken = default) { string rid = await this.GetRIDAsync(cancellationToken); CosmosOffers cosmosOffers = new CosmosOffers(this.ClientContext); return(await cosmosOffers.ReplaceThroughputPropertiesAsync( targetRID : rid, throughputProperties : throughputProperties, requestOptions : requestOptions, cancellationToken : cancellationToken)); }
public Task <ThroughputResponse> ReplaceThroughputIfExistsAsync( ThroughputProperties throughput, ITrace trace, RequestOptions requestOptions = null, CancellationToken cancellationToken = default) { CosmosOffers cosmosOffers = new CosmosOffers(this.ClientContext); return(this.OfferRetryHelperForStaleRidCacheAsync( (rid) => cosmosOffers.ReplaceThroughputPropertiesIfExistsAsync( targetRID: rid, throughputProperties: throughput, requestOptions: requestOptions, cancellationToken: cancellationToken), trace, cancellationToken)); }
internal Task <ResponseMessage> ProcessCollectionCreateAsync( Stream streamPayload, ThroughputProperties throughputProperties, RequestOptions requestOptions, CancellationToken cancellationToken) { return(this.ClientContext.ProcessResourceOperationStreamAsync( resourceUri: this.LinkUri, resourceType: ResourceType.Collection, operationType: OperationType.Create, cosmosContainerCore: null, partitionKey: null, streamPayload: streamPayload, requestOptions: requestOptions, requestEnricher: (httpRequestMessage) => httpRequestMessage.AddThroughputPropertiesHeader(throughputProperties), diagnosticsContext: null, cancellationToken: cancellationToken)); }
internal async Task <ThroughputResponse> ReplaceThroughputPropertiesAsync( string targetRID, ThroughputProperties throughputProperties, RequestOptions requestOptions, CancellationToken cancellationToken) { ThroughputProperties currentProperty = await this.GetOfferV2Async <ThroughputProperties>(targetRID, failIfNotConfigured : true, cancellationToken : cancellationToken); currentProperty.Content = throughputProperties.Content; return(await this.GetThroughputResponseAsync( streamPayload : this.ClientContext.SerializerCore.ToStream(currentProperty), operationType : OperationType.Replace, linkUri : new Uri(currentProperty.SelfLink, UriKind.Relative), resourceType : ResourceType.Offer, requestOptions : requestOptions, cancellationToken : cancellationToken)); }
public Task <ContainerResponse> CreateContainerIfNotExistsAsync( ContainerProperties containerProperties, int?throughput, RequestOptions requestOptions, ITrace trace, CancellationToken cancellationToken) { if (containerProperties == null) { throw new ArgumentNullException(nameof(containerProperties)); } return(this.CreateContainerIfNotExistsAsync( containerProperties, ThroughputProperties.CreateManualThroughput(throughput), requestOptions, trace, 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))); }
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(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, throughputProperties, 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)); })); }
private Task <ResponseMessage> CreateDatabaseStreamInternalAsync( Stream streamPayload, int?throughput, RequestOptions requestOptions, CancellationToken cancellationToken) { ThroughputProperties throughputProperties = null; if (throughput.HasValue) { throughputProperties = ThroughputProperties.CreateManualThroughput(throughput.Value); } return(this.CreateDatabaseStreamInternalAsync( streamPayload, throughputProperties, requestOptions, 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))); }
public Task <ResponseMessage> CreateContainerStreamAsync( ContainerProperties containerProperties, ThroughputProperties throughputProperties, RequestOptions requestOptions, ITrace trace, CancellationToken cancellationToken) { if (containerProperties == null) { throw new ArgumentNullException(nameof(containerProperties)); } this.ValidateContainerProperties(containerProperties); return(this.ProcessCollectionCreateAsync( streamPayload: this.ClientContext.SerializerCore.ToStream(containerProperties), throughputProperties: throughputProperties, requestOptions: requestOptions, trace: trace, cancellationToken: cancellationToken)); }
public async Task <ThroughputResponse> ReplaceThroughputAsync( ThroughputProperties throughputProperties, ITrace trace, RequestOptions requestOptions = null, CancellationToken cancellationToken = default) { ThroughputResponse throughputResponse = await this.ReplaceThroughputIfExistsAsync( throughputProperties, trace, requestOptions, cancellationToken); if (throughputResponse.StatusCode == HttpStatusCode.NotFound) { throw CosmosExceptionFactory.CreateNotFoundException( message: $"Throughput is not configured for {this.Id}", headers: throughputResponse.Headers); } return(throughputResponse); }
Task <ContainerResponse> CreateContainerAsync( ContainerProperties containerProperties, ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default) { if (containerProperties == null) { throw new ArgumentNullException(nameof(containerProperties)); } this.ValidateContainerProperties(containerProperties); Task <ResponseMessage> response = this.ProcessCollectionCreateAsync( streamPayload: this.ClientContext.SerializerCore.ToStream(containerProperties), throughputProperties: throughputProperties, requestOptions: requestOptions, cancellationToken: cancellationToken); return(this.ClientContext.ResponseFactory.CreateContainerResponseAsync(this.GetContainer(containerProperties.Id), response)); }
public abstract Task <ContainerResponse> CreateContainerIfNotExistsAsync( ContainerProperties containerProperties, ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken));
public abstract Task <ResponseMessage> CreateContainerStreamAsync( ContainerProperties containerProperties, ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken));
public async Task <ContainerResponse> CreateContainerIfNotExistsAsync( CosmosDiagnosticsContext diagnosticsContext, ContainerProperties containerProperties, ThroughputProperties throughputProperties, RequestOptions requestOptions, CancellationToken cancellationToken) { if (containerProperties == null) { throw new ArgumentNullException(nameof(containerProperties)); } this.ValidateContainerProperties(containerProperties); ContainerCore container = (ContainerCore)this.GetContainer(containerProperties.Id); using (ResponseMessage readResponse = await container.ReadContainerStreamAsync( diagnosticsContext: diagnosticsContext, cancellationToken: cancellationToken)) { if (readResponse.StatusCode != HttpStatusCode.NotFound) { ContainerResponse retrivedContainerResponse = this.ClientContext.ResponseFactory.CreateContainerResponse( container, readResponse); if (!retrivedContainerResponse.Resource.PartitionKeyPath.Equals(containerProperties.PartitionKeyPath)) { throw new ArgumentException( string.Format( ClientResources.PartitionKeyPathConflict, containerProperties.PartitionKeyPath, containerProperties.Id, retrivedContainerResponse.Resource.PartitionKeyPath), nameof(containerProperties.PartitionKey)); } return(retrivedContainerResponse); } } this.ValidateContainerProperties(containerProperties); using (ResponseMessage createResponse = await this.CreateContainerStreamAsync( diagnosticsContext, containerProperties, throughputProperties, requestOptions, cancellationToken)) { if (createResponse.StatusCode != HttpStatusCode.Conflict) { return(this.ClientContext.ResponseFactory.CreateContainerResponse(container, 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 using (ResponseMessage readResponseAfterCreate = await container.ReadContainerStreamAsync( diagnosticsContext: diagnosticsContext, cancellationToken: cancellationToken)) { return(this.ClientContext.ResponseFactory.CreateContainerResponse(container, readResponseAfterCreate)); } }
public async Task <ContainerResponse> CreateContainerIfNotExistsAsync( ContainerProperties containerProperties, ThroughputProperties throughputProperties, RequestOptions requestOptions, ITrace trace, CancellationToken cancellationToken) { if (containerProperties == null) { throw new ArgumentNullException(nameof(containerProperties)); } this.ValidateContainerProperties(containerProperties); double totalRequestCharge = 0; ContainerCore container = (ContainerCore)this.GetContainer(containerProperties.Id); using (ResponseMessage readResponse = await container.ReadContainerStreamAsync( requestOptions: requestOptions, trace: trace, cancellationToken: cancellationToken)) { totalRequestCharge = readResponse.Headers.RequestCharge; if (readResponse.StatusCode != HttpStatusCode.NotFound) { ContainerResponse retrivedContainerResponse = this.ClientContext.ResponseFactory.CreateContainerResponse( container, readResponse); if (containerProperties.PartitionKey.Kind != Documents.PartitionKind.MultiHash) { if (!retrivedContainerResponse.Resource.PartitionKeyPath.Equals(containerProperties.PartitionKeyPath)) { throw new ArgumentException( string.Format( ClientResources.PartitionKeyPathConflict, containerProperties.PartitionKeyPath, containerProperties.Id, retrivedContainerResponse.Resource.PartitionKeyPath), nameof(containerProperties.PartitionKey)); } } #if PREVIEW else { IReadOnlyList <string> retrivedPartitionKeyPaths = retrivedContainerResponse.Resource.PartitionKeyPaths; IReadOnlyList <string> receivedPartitionKeyPaths = containerProperties.PartitionKeyPaths; if (retrivedPartitionKeyPaths.Count != receivedPartitionKeyPaths.Count || !Enumerable.SequenceEqual(retrivedPartitionKeyPaths, receivedPartitionKeyPaths)) { throw new ArgumentException( string.Format( ClientResources.PartitionKeyPathConflict, string.Join(",", containerProperties.PartitionKeyPaths), containerProperties.Id, string.Join(",", retrivedContainerResponse.Resource.PartitionKeyPaths)), nameof(containerProperties.PartitionKey)); } } #endif return(retrivedContainerResponse); } } this.ValidateContainerProperties(containerProperties); using (ResponseMessage createResponse = await this.CreateContainerStreamAsync( containerProperties, throughputProperties, requestOptions, trace, cancellationToken)) { totalRequestCharge += createResponse.Headers.RequestCharge; createResponse.Headers.RequestCharge = totalRequestCharge; if (createResponse.StatusCode != HttpStatusCode.Conflict) { return(this.ClientContext.ResponseFactory.CreateContainerResponse(container, 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 using (ResponseMessage readResponseAfterCreate = await container.ReadContainerStreamAsync( requestOptions: requestOptions, trace: trace, cancellationToken: cancellationToken)) { totalRequestCharge += readResponseAfterCreate.Headers.RequestCharge; readResponseAfterCreate.Headers.RequestCharge = totalRequestCharge; return(this.ClientContext.ResponseFactory.CreateContainerResponse(container, readResponseAfterCreate)); } }
internal abstract Task <ThroughputResponse> ReplaceThroughputPropertiesIfExistsAsync( ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default);
Task <ThroughputResponse> ReplaceThroughputAsync(ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default) { return(TaskHelper.RunInlineIfNeededAsync(() => this.database.ReplaceThroughputAsync(throughputProperties, requestOptions, cancellationToken))); }
public abstract Task <ThroughputResponse> ReplaceThroughputAsync( ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken));
public abstract Task <ThroughputResponse> ReplaceThroughputIfExistsAsync( ThroughputProperties throughput, RequestOptions requestOptions, CancellationToken cancellationToken);
Task <ContainerResponse> CreateContainerAsync(ContainerProperties containerProperties, ThroughputProperties throughputProperties, RequestOptions requestOptions = null, CancellationToken cancellationToken = default) { return(TaskHelper.RunInlineIfNeededAsync(() => this.database.CreateContainerAsync(containerProperties, throughputProperties, requestOptions, cancellationToken))); }