Skip to content

Commit 7373fbc

Browse files
committed
Code changes to refactor serialization and de-serialization logic.
1 parent e732ff2 commit 7373fbc

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

Microsoft.Azure.Cosmos/src/RequestOptions/ItemRequestOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ internal override void PopulateRequestOptions(RequestMessage request)
150150
IndexingDirectiveStrings.FromIndexingDirective(this.IndexingDirective.Value));
151151
}
152152

153+
if (ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false))
154+
{
155+
request.Headers.Add(HttpConstants.HttpHeaders.SupportedSerializationFormats, SupportedSerializationFormats.CosmosBinary.ToString());
156+
request.Headers.Add(HttpConstants.HttpHeaders.ContentSerializationFormat, SupportedSerializationFormats.CosmosBinary.ToString());
157+
}
158+
153159
DedicatedGatewayRequestOptions.PopulateMaxIntegratedCacheStalenessOption(this.DedicatedGatewayRequestOptions, request);
154160
DedicatedGatewayRequestOptions.PopulateBypassIntegratedCacheOption(this.DedicatedGatewayRequestOptions, request);
155161

Microsoft.Azure.Cosmos/src/RequestOptions/RequestOptions.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
namespace Microsoft.Azure.Cosmos
66
{
77
using System;
8-
using System.Collections.Generic;
8+
using System.Collections.Generic;
99
using Microsoft.Azure.Documents;
10-
using Telemetry;
1110

1211
/// <summary>
1312
/// The default cosmos request options
@@ -69,7 +68,15 @@ public class RequestOptions
6968
/// This can be used to route a request to a specific region by excluding all other regions.
7069
/// If all regions are excluded, then the request will be routed to the primary/hub region.
7170
/// </summary>
72-
public List<string> ExcludeRegions { get; set; }
71+
public List<string> ExcludeRegions { get; set; }
72+
73+
/// <summary>
74+
/// Cosmos availability strategy.
75+
/// Availability strategy allows the SDK to send out additional cross region requests to help
76+
/// reduce latency and increase availability. Currently there is one type of availability strategy, parallel request hedging.
77+
/// If there is a globally enabled availability strategy, setting one in the request options will override the global one.
78+
/// </summary>
79+
internal AvailabilityStrategy AvailabilityStrategy { get; set; }
7380

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

9299
internal bool DisablePointOperationDiagnostics { get; set; }
93-
100+
94101
/// <summary>
95102
/// Fill the CosmosRequestMessage headers with the set properties
96103
/// </summary>
@@ -119,16 +126,7 @@ internal virtual void PopulateRequestOptions(RequestMessage request)
119126
{
120127
request.Headers.Add(HttpConstants.HttpHeaders.PriorityLevel, this.PriorityLevel.ToString());
121128
}
122-
123-
if (ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false))
124-
{
125-
request.Headers.Add(HttpConstants.HttpHeaders.SupportedSerializationFormats, SupportedSerializationFormats.CosmosBinary.ToString());
126-
request.Headers.Add(HttpConstants.HttpHeaders.ContentSerializationFormat, SupportedSerializationFormats.CosmosBinary.ToString());
127-
128-
//request.Headers.CosmosMessageHeaders.SupportedSerializationFormats = SupportedSerializationFormats.CosmosBinary.ToString();
129-
//request.Headers.CosmosMessageHeaders.ContentSerializationFormat = SupportedSerializationFormats.CosmosBinary.ToString();
130-
}
131-
129+
132130
this.AddRequestHeaders?.Invoke(request.Headers);
133131
}
134132

Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -841,12 +841,6 @@ private async Task<ResponseMessage> ExtractPartitionKeyAndProcessItemStreamAsync
841841
using (trace.StartChild("ItemSerialize"))
842842
{
843843
itemStream = this.ClientContext.SerializerCore.ToStream<T>(item);
844-
845-
// Add check for env variable.
846-
if (ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false))
847-
{
848-
itemStream = ContainerCore.ConvertTextStreamToBinaryStream(itemStream);
849-
}
850844
}
851845

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

918912
ContainerInternal.ValidatePartitionKey(partitionKey, requestOptions);
919-
string resourceUri = this.GetResourceUri(requestOptions, operationType, itemId);
913+
string resourceUri = this.GetResourceUri(requestOptions, operationType, itemId);
914+
915+
// Add check for env variable.
916+
if (ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false))
917+
{
918+
requestOptions ??= new ItemRequestOptions();
919+
if (streamPayload != null)
920+
{
921+
streamPayload = ContainerCore.ConvertTextStreamToBinaryStream(streamPayload);
922+
}
923+
}
920924

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

934938
// Add check for env variable.
935-
if (ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false)
939+
if (responseMessage != null
940+
&& ConfigurationManager.IsBinaryEncodingEnabled(defaultValue: false)
936941
&& ContainerCore.IsFirstBufferByteBinary(responseMessage, out byte[] content))
937942
{
938943
responseMessage.Content = ContainerCore.ConvertBinaryToTextByteStream(content);

0 commit comments

Comments
 (0)