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

[Internal] Engineering: Refactors code to reduce technical debt #2470

Merged
merged 6 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,7 @@ protected byte[] EncryptData(byte[] plainText, bool hasAuthenticationTag)
}
catch (Exception)
{
if (aesAlg != null)
{
aesAlg.Dispose();
}
aesAlg?.Dispose();

throw;
}
Expand Down Expand Up @@ -352,10 +349,7 @@ private byte[] DecryptData(byte[] iv, byte[] cipherText, int offset, int count)
}
catch (Exception)
{
if (aesAlg != null)
{
aesAlg.Dispose();
}
aesAlg?.Dispose();

throw;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ private static JObject RetrieveEncryptionProperties(
{
JProperty encryptionPropertiesJProp = item.Property(Constants.EncryptedInfo);
JObject encryptionPropertiesJObj = null;
if (encryptionPropertiesJProp != null && encryptionPropertiesJProp.Value != null && encryptionPropertiesJProp.Value.Type == JTokenType.Object)
if (encryptionPropertiesJProp?.Value != null && encryptionPropertiesJProp.Value.Type == JTokenType.Object)
{
encryptionPropertiesJObj = (JObject)encryptionPropertiesJProp.Value;
}
Expand Down
22 changes: 8 additions & 14 deletions Microsoft.Azure.Cosmos/src/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,10 +1353,7 @@ internal async Task<DocumentServiceResponse> ProcessRequestAsync(
{
await this.EnsureValidClientAsync(NoOpTrace.Singleton);

if (retryPolicyInstance != null)
{
retryPolicyInstance.OnBeforeSendRequest(request);
}
retryPolicyInstance?.OnBeforeSendRequest(request);

using (new ActivityScope(Guid.NewGuid()))
{
Expand Down Expand Up @@ -1728,7 +1725,7 @@ public Task<ResourceResponse<Document>> CreateDocumentAsync(string documentsFeed
private async Task<ResourceResponse<Document>> CreateDocumentInlineAsync(string documentsFeedOrDatabaseLink, object document, Documents.Client.RequestOptions options, bool disableAutomaticIdGeneration, CancellationToken cancellationToken)
{
IDocumentClientRetryPolicy requestRetryPolicy = this.ResetSessionTokenRetryPolicy.GetRequestPolicy();
if (options == null || options.PartitionKey == null)
if (options?.PartitionKey == null)
{
requestRetryPolicy = new PartitionKeyMismatchRetryPolicy(
await this.GetCollectionCacheAsync(NoOpTrace.Singleton),
Expand Down Expand Up @@ -5480,17 +5477,14 @@ private async Task<StoredProcedureResponse<TValue>> ExecuteStoredProcedurePrivat
headers))
{
request.Headers[HttpConstants.HttpHeaders.XDate] = DateTime.UtcNow.ToString("r");
if (options == null || options.PartitionKeyRangeId == null)
if (options?.PartitionKeyRangeId == null)
{
await this.AddPartitionKeyInformationAsync(
request,
options);
}

if (retryPolicyInstance != null)
{
retryPolicyInstance.OnBeforeSendRequest(request);
}
retryPolicyInstance?.OnBeforeSendRequest(request);

request.SerializerSettings = this.GetSerializerSettingsForRequest(options);
return new StoredProcedureResponse<TValue>(await this.ExecuteProcedureAsync(
Expand Down Expand Up @@ -5683,7 +5677,7 @@ public Task<ResourceResponse<Document>> UpsertDocumentAsync(string documentsFeed
private async Task<ResourceResponse<Document>> UpsertDocumentInlineAsync(string documentsFeedOrDatabaseLink, object document, Documents.Client.RequestOptions options, bool disableAutomaticIdGeneration, CancellationToken cancellationToken)
{
IDocumentClientRetryPolicy requestRetryPolicy = this.ResetSessionTokenRetryPolicy.GetRequestPolicy();
if (options == null || options.PartitionKey == null)
if (options?.PartitionKey == null)
{
requestRetryPolicy = new PartitionKeyMismatchRetryPolicy(
await this.GetCollectionCacheAsync(NoOpTrace.Singleton),
Expand Down Expand Up @@ -6646,11 +6640,11 @@ private async Task AddPartitionKeyInformationAsync(DocumentServiceRequest reques
PartitionKeyDefinition partitionKeyDefinition = collection.PartitionKey;

PartitionKeyInternal partitionKey;
if (options != null && options.PartitionKey != null && options.PartitionKey.Equals(Documents.PartitionKey.None))
if (options?.PartitionKey != null && options.PartitionKey.Equals(Documents.PartitionKey.None))
{
partitionKey = collection.GetNoneValue();
}
else if (options != null && options.PartitionKey != null)
else if (options?.PartitionKey != null)
{
partitionKey = options.PartitionKey.InternalKey;
}
Expand All @@ -6671,7 +6665,7 @@ internal async Task AddPartitionKeyInformationAsync(DocumentServiceRequest reque
// For backward compatibility, if collection doesn't have partition key defined, we assume all documents
// have empty value for it and user doesn't need to specify it explicitly.
PartitionKeyInternal partitionKey;
if (options == null || options.PartitionKey == null)
if (options?.PartitionKey == null)
{
if (partitionKeyDefinition == null || partitionKeyDefinition.Paths.Count == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static async Task<IDocumentQueryExecutionContext> CreateDocumentQueryExec
collection = await collectionCache.ResolveCollectionAsync(request, token, NoOpTrace.Singleton);
}

if (feedOptions != null && feedOptions.PartitionKey != null && feedOptions.PartitionKey.Equals(Documents.PartitionKey.None))
if (feedOptions?.PartitionKey != null && feedOptions.PartitionKey.Equals(Documents.PartitionKey.None))
{
feedOptions.PartitionKey = Documents.PartitionKey.FromInternalKey(collection.GetNoneValue());
}
Expand Down
3 changes: 1 addition & 2 deletions Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ public Task<ShouldRetryResult> ShouldRetryAsync(
Exception exception,
CancellationToken cancellationToken)
{
if (exception is DocumentClientException)
if (exception is DocumentClientException dce)
ealsur marked this conversation as resolved.
Show resolved Hide resolved
{
DocumentClientException dce = (DocumentClientException)exception;
if (!this.IsValidThrottleStatusCode(dce.StatusCode))
{
DefaultTrace.TraceError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ private async Task<ContainerProperties> ReadCollectionAsync(string collectionLin

using (new ActivityScope(Guid.NewGuid()))
{
if (retryPolicyInstance != null)
{
retryPolicyInstance.OnBeforeSendRequest(request);
}
retryPolicyInstance?.OnBeforeSendRequest(request);

try
{
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Routing/CollectionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private async Task<ContainerProperties> ResolveByPartitionKeyRangeIdentityAsync(
{
// if request is targeted at specific partition using x-ms-documentd-partitionkeyrangeid header,
// which contains value "<collectionrid>,<partitionkeyrangeid>", then resolve to collection rid in this header.
if (partitionKeyRangeIdentity != null && partitionKeyRangeIdentity.CollectionRid != null)
if (partitionKeyRangeIdentity?.CollectionRid != null)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public static IReadOnlyList<Range<string>> GetProvidedPartitionKeyRanges(
}

PartitionedQueryExecutionInfo queryExecutionInfo = tryGetPartitionQueryExecutionInfo.Result;
if (queryExecutionInfo == null ||
queryExecutionInfo.QueryRanges == null ||
if (queryExecutionInfo?.QueryRanges == null ||
queryExecutionInfo.QueryInfo == null ||
queryExecutionInfo.QueryRanges.Any(range => range.Min == null || range.Max == null))
{
Expand Down Expand Up @@ -435,7 +434,7 @@ public virtual Range<string> ExtractPartitionKeyRangeFromContinuationToken(IName
}
}

if (initialContinuationToken != null && initialContinuationToken.Range != null)
if (initialContinuationToken?.Range != null)
{
range = initialContinuationToken.Range;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3413,7 +3413,7 @@ public static string DumpFullExceptionMessage(Exception e)
while (e != null)
{
DocumentClientException docException = e as DocumentClientException;
if (docException != null && docException.Error != null)
if (docException?.Error != null)
{
exceptionMessage.Append("Code : " + docException.Error.Code);
if (docException.Error.ErrorDetails != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public Query(string strFilter, string rootName = "r", FieldComparer comparer = n
if (filterBuffer.Length > 0)
this.query += " WHERE " + filterBuffer.ToString();

if (comparer != null && comparer.field != null && comparer.order != 0)
if (comparer?.field != null && comparer.order != 0)
{
this.query += " ORDER BY r." + comparer.field.Name + (comparer.order > 0 ? " ASC" : " DESC");
this.Comparer = comparer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ internal static void TestForEachClient(
}
}

if (requestChargeHelper != null)
{
requestChargeHelper.CompareRequestCharge(testName);
}
requestChargeHelper?.CompareRequestCharge(testName);
}

/// <summary>
Expand Down Expand Up @@ -162,10 +159,7 @@ internal static void TestForAnyClient(
client.Dispose();
}

if (requestChargeHelper != null)
{
requestChargeHelper.CompareRequestCharge(testName);
}
requestChargeHelper?.CompareRequestCharge(testName);
}

private static void RunTestForClient(
Expand Down