Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No content on Response: Add the ability to have operation return no content from Cosmos DB #1439

Merged
merged 16 commits into from
May 8, 2020
Merged
Prev Previous commit
Next Next commit
Added separate request options for read vs write.
  • Loading branch information
Jake Willey committed May 4, 2020
commit ebab57eb3f4449800d1fcda5037b530f759209f8
5 changes: 4 additions & 1 deletion Microsoft.Azure.Cosmos/src/Batch/ItemBatchOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ internal static Result WriteOperation(ref RowWriter writer, TypeArgument typeArg
}
}

if (options.ReturnMinimalResponse)
if (ItemRequestOptions.ShouldSetNoContentHeader(
options.NoContentResponseOnWrite,
options.NoContentResponseOnRead,
operation.OperationType))
{
r = writer.WriteBool("minimalReturnPreference", true);
if (r != Result.Success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ public class TransactionalBatchItemRequestOptions : RequestOptions
/// <seealso cref="Microsoft.Azure.Cosmos.IndexingPolicy"/>
public IndexingDirective? IndexingDirective { get; set; }

/// <summary>
/// Gets or sets the boolean to only return the headers and status code in the Cosmos DB response.
/// This removes the resource from the response. This reduces networking and CPU load by not sending
/// the resource back over the network and serializing it on the client.
/// </summary>
/// <remarks>
/// This is optimal for workloads where the returned resource is not used.
/// </remarks>
public virtual bool? NoContentResponseOnWrite { get; set; }
j82w marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets or sets the boolean to only return the headers and status code in the Cosmos DB response.
/// This removes the resource from the response. This reduces networking and CPU load by not sending
/// the resource back over the network and serializing it on the client.
/// </summary>
/// <remarks>
/// This is optimal for workloads where the returned resource is not used.
/// </remarks>
public virtual bool? NoContentResponseOnRead { get; set; }

/// <summary>
/// Options to encrypt properties of the item.
/// </summary>
Expand All @@ -35,14 +55,14 @@ internal static TransactionalBatchItemRequestOptions FromItemRequestOptions(Item
return null;
}

RequestOptions requestOptions = itemRequestOptions as RequestOptions;
TransactionalBatchItemRequestOptions batchItemRequestOptions = new TransactionalBatchItemRequestOptions();
batchItemRequestOptions.IndexingDirective = itemRequestOptions.IndexingDirective;
batchItemRequestOptions.IfMatchEtag = requestOptions.IfMatchEtag;
batchItemRequestOptions.IfNoneMatchEtag = requestOptions.IfNoneMatchEtag;
batchItemRequestOptions.Properties = requestOptions.Properties;
batchItemRequestOptions.ReturnMinimalResponse = requestOptions.ReturnMinimalResponse;
batchItemRequestOptions.IsEffectivePartitionKeyRouting = requestOptions.IsEffectivePartitionKeyRouting;
batchItemRequestOptions.IfMatchEtag = itemRequestOptions.IfMatchEtag;
batchItemRequestOptions.IfNoneMatchEtag = itemRequestOptions.IfNoneMatchEtag;
batchItemRequestOptions.Properties = itemRequestOptions.Properties;
batchItemRequestOptions.NoContentResponseOnWrite = itemRequestOptions.NoContentResponseOnWrite;
batchItemRequestOptions.NoContentResponseOnRead = itemRequestOptions.NoContentResponseOnRead;
batchItemRequestOptions.IsEffectivePartitionKeyRouting = itemRequestOptions.IsEffectivePartitionKeyRouting;
return batchItemRequestOptions;
}
}
Expand Down
67 changes: 67 additions & 0 deletions Microsoft.Azure.Cosmos/src/RequestOptions/ItemRequestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,46 @@ public ConsistencyLevel? ConsistencyLevel
set => this.BaseConsistencyLevel = value;
}

/// <summary>
/// Gets or sets the boolean to only return the headers and status code in the Cosmos DB response.
/// This removes the resource from the response. This reduces networking and CPU load by not sending
/// the resource back over the network and serializing it on the client.
/// </summary>
/// <example>
/// <code language="c#">
/// <![CDATA[
/// ItemRequestOption requestOptions = new ItemRequestOptions() { NoContentResponseOnWrite = true };
/// ItemResponse itemResponse = await this.container.CreateItemAsync<ToDoActivity>(tests, new PartitionKey(test.status), requestOptions);
/// Assert.AreEqual(HttpStatusCode.Created, itemResponse.StatusCode);
/// Assert.IsNull(itemResponse.Resource);
/// ]]>
/// </code>
/// </example>
/// <remarks>
/// This is optimal for workloads where the returned resource is not used.
/// </remarks>
public virtual bool? NoContentResponseOnWrite { get; set; }
j82w marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets or sets the boolean to only return the headers and status code in the Cosmos DB response.
/// This removes the resource from the response. This reduces networking and CPU load by not sending
/// the resource back over the network and serializing it on the client.
/// </summary>
/// <example>
/// <code language="c#">
/// <![CDATA[
/// ItemRequestOption requestOptions = new ItemRequestOptions() { NoContentResponseOnWrite = true };
/// ItemResponse itemResponse = await this.container.ReadItemAsync<ToDoActivity>(tests, new PartitionKey(test.status), requestOptions);
/// Assert.AreEqual(HttpStatusCode.Created, itemResponse.StatusCode);
/// Assert.IsNull(itemResponse.Resource);
/// ]]>
/// </code>
/// </example>
/// <remarks>
/// This is optimal for workloads where the returned resource is not used.
/// </remarks>
public virtual bool? NoContentResponseOnRead { get; set; }

/// <summary>
/// Options to encrypt properties of the item.
/// </summary>
Expand Down Expand Up @@ -132,7 +172,34 @@ internal override void PopulateRequestOptions(RequestMessage request)

RequestOptions.SetSessionToken(request, this.SessionToken);

if (ItemRequestOptions.ShouldSetNoContentHeader(
this.NoContentResponseOnWrite,
this.NoContentResponseOnRead,
request.OperationType))
{
request.Headers.Add(HttpConstants.HttpHeaders.Prefer, HttpConstants.HttpHeaderValues.PreferReturnMinimal);
}

base.PopulateRequestOptions(request);
}

internal static bool ShouldSetNoContentHeader(
bool? onItemWrite,
bool? onItemRead,
OperationType operationType)
{
if (onItemRead.HasValue &&
onItemRead.Value &&
operationType == OperationType.Read)
{
return true;
}

return onItemWrite.HasValue &&
onItemWrite.Value &&
j82w marked this conversation as resolved.
Show resolved Hide resolved
(operationType == OperationType.Create ||
operationType == OperationType.Replace ||
operationType == OperationType.Upsert);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,6 @@ public ConsistencyLevel? ConsistencyLevel
/// </remarks>
public string SessionToken { get; set; }

/// <inheritdoc/>
public override bool ReturnMinimalResponse
{
get => base.ReturnMinimalResponse;
set => throw new NotSupportedException("ReturnMinimalResponse is not supported on query operations. Please use SQL to change the response.");
}

internal CosmosElement CosmosElementContinuationToken { get; set; }

internal string StartId { get; set; }
Expand Down
25 changes: 0 additions & 25 deletions Microsoft.Azure.Cosmos/src/RequestOptions/RequestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,6 @@ public class RequestOptions
/// </remarks>
public string IfNoneMatchEtag { get; set; }

/// <summary>
/// Gets or sets the boolean to only return the headers and status code in the Cosmos DB response.
/// This removes the resource from the response. This reduces networking and CPU load by not sending
/// the resource back over the network and serializing it on the client.
/// </summary>
/// <example>
/// <code language="c#">
/// <![CDATA[
/// ItemRequestOption requestOptions = new ItemRequestOptions() { ReturnMinimalResponse = true };
/// ItemResponse itemResponse = await this.container.CreateItemAsync<ToDoActivity>(tests, new PartitionKey(test.status), requestOptions);
/// Assert.AreEqual(HttpStatusCode.Created, itemResponse.StatusCode);
/// Assert.IsNull(itemResponse.Resource);
/// ]]>
/// </code>
/// </example>
/// <remarks>
/// This is optimal for workloads where the returned resource is not used.
/// </remarks>
public virtual bool ReturnMinimalResponse { get; set; }

/// <summary>
j82w marked this conversation as resolved.
Show resolved Hide resolved
/// Gets or sets the boolean to use effective partition key routing in the cosmos db request.
/// </summary>
Expand Down Expand Up @@ -100,11 +80,6 @@ internal virtual void PopulateRequestOptions(RequestMessage request)
{
request.Headers.Add(HttpConstants.HttpHeaders.IfNoneMatch, this.IfNoneMatchEtag);
}

if (this.ReturnMinimalResponse)
{
request.Headers.Add(HttpConstants.HttpHeaders.Prefer, HttpConstants.HttpHeaderValues.PreferReturnMinimal);
}
}

/// <summary>
Expand Down
Loading