Skip to content

Commit

Permalink
Code changes to refactor serialization and de-serialization logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
kundadebdatta committed Aug 23, 2024
1 parent e732ff2 commit 7373fbc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ internal override void PopulateRequestOptions(RequestMessage request)
IndexingDirectiveStrings.FromIndexingDirective(this.IndexingDirective.Value));
}

if (ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false))
{
request.Headers.Add(HttpConstants.HttpHeaders.SupportedSerializationFormats, SupportedSerializationFormats.CosmosBinary.ToString());
request.Headers.Add(HttpConstants.HttpHeaders.ContentSerializationFormat, SupportedSerializationFormats.CosmosBinary.ToString());
}

DedicatedGatewayRequestOptions.PopulateMaxIntegratedCacheStalenessOption(this.DedicatedGatewayRequestOptions, request);
DedicatedGatewayRequestOptions.PopulateBypassIntegratedCacheOption(this.DedicatedGatewayRequestOptions, request);

Expand Down
26 changes: 12 additions & 14 deletions Microsoft.Azure.Cosmos/src/RequestOptions/RequestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Microsoft.Azure.Documents;
using Telemetry;

/// <summary>
/// The default cosmos request options
Expand Down Expand Up @@ -69,7 +68,15 @@ public class RequestOptions
/// This can be used to route a request to a specific region by excluding all other regions.
/// If all regions are excluded, then the request will be routed to the primary/hub region.
/// </summary>
public List<string> ExcludeRegions { get; set; }
public List<string> ExcludeRegions { get; set; }

/// <summary>
/// Cosmos availability strategy.
/// Availability strategy allows the SDK to send out additional cross region requests to help
/// reduce latency and increase availability. Currently there is one type of availability strategy, parallel request hedging.
/// If there is a globally enabled availability strategy, setting one in the request options will override the global one.
/// </summary>
internal AvailabilityStrategy AvailabilityStrategy { get; set; }

/// <summary>
/// Gets or sets the boolean to use effective partition key routing in the cosmos db request.
Expand All @@ -90,7 +97,7 @@ public class RequestOptions
internal virtual ConsistencyLevel? BaseConsistencyLevel { get; set; }

internal bool DisablePointOperationDiagnostics { get; set; }


/// <summary>
/// Fill the CosmosRequestMessage headers with the set properties
/// </summary>
Expand Down Expand Up @@ -119,16 +126,7 @@ internal virtual void PopulateRequestOptions(RequestMessage request)
{
request.Headers.Add(HttpConstants.HttpHeaders.PriorityLevel, this.PriorityLevel.ToString());
}

if (ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false))
{
request.Headers.Add(HttpConstants.HttpHeaders.SupportedSerializationFormats, SupportedSerializationFormats.CosmosBinary.ToString());
request.Headers.Add(HttpConstants.HttpHeaders.ContentSerializationFormat, SupportedSerializationFormats.CosmosBinary.ToString());

//request.Headers.CosmosMessageHeaders.SupportedSerializationFormats = SupportedSerializationFormats.CosmosBinary.ToString();
//request.Headers.CosmosMessageHeaders.ContentSerializationFormat = SupportedSerializationFormats.CosmosBinary.ToString();
}


this.AddRequestHeaders?.Invoke(request.Headers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,12 +841,6 @@ private async Task<ResponseMessage> ExtractPartitionKeyAndProcessItemStreamAsync
using (trace.StartChild("ItemSerialize"))
{
itemStream = this.ClientContext.SerializerCore.ToStream<T>(item);

// Add check for env variable.
if (ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false))
{
itemStream = ContainerCore.ConvertTextStreamToBinaryStream(itemStream);
}
}

// User specified PK value, no need to extract it
Expand Down Expand Up @@ -916,7 +910,17 @@ private async Task<ResponseMessage> ProcessItemStreamAsync(
}

ContainerInternal.ValidatePartitionKey(partitionKey, requestOptions);
string resourceUri = this.GetResourceUri(requestOptions, operationType, itemId);
string resourceUri = this.GetResourceUri(requestOptions, operationType, itemId);

// Add check for env variable.
if (ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false))
{
requestOptions ??= new ItemRequestOptions();
if (streamPayload != null)
{
streamPayload = ContainerCore.ConvertTextStreamToBinaryStream(streamPayload);
}
}

ResponseMessage responseMessage = await this.ClientContext.ProcessResourceOperationStreamAsync(
resourceUri: resourceUri,
Expand All @@ -932,7 +936,8 @@ private async Task<ResponseMessage> ProcessItemStreamAsync(
cancellationToken: cancellationToken);

// Add check for env variable.
if (ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false)
if (responseMessage != null
&& ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false)
&& ContainerCore.IsFirstBufferByteBinary(responseMessage, out byte[] content))
{
responseMessage.Content = ContainerCore.ConvertBinaryToTextByteStream(content);
Expand Down

0 comments on commit 7373fbc

Please sign in to comment.