Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlang authored Nov 27, 2019
2 parents f67c2ec + 4503949 commit 6b80c87
Show file tree
Hide file tree
Showing 33 changed files with 754 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ internal static CosmosArray ToCosmosElements(
// And you should execute the callback on each document in "Documents".

long responseLengthBytes = memoryStream.Length;
byte[] content = memoryStream.ToArray();
IJsonNavigator jsonNavigator = null;
ReadOnlyMemory<byte> content;
if (memoryStream.TryGetBuffer(out ArraySegment<byte> buffer))
{
content = buffer;
}
else
{
content = memoryStream.ToArray();
}

IJsonNavigator jsonNavigator;

// Use the users custom navigator
if (cosmosSerializationOptions != null)
Expand All @@ -68,7 +77,7 @@ internal static CosmosArray ToCosmosElements(
}
else
{
jsonNavigator = JsonNavigator.Create(new ArraySegment<byte>(content));
jsonNavigator = JsonNavigator.Create(content);
}

string resourceName = CosmosElementSerializer.GetRootNodeName(resourceType);
Expand Down
8 changes: 4 additions & 4 deletions Microsoft.Azure.Cosmos/src/Linq/ExpressionToSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private static SqlScalarExpression VisitMethodCallScalar(MethodCallExpression me
}
}

return SqlFunctionCallScalarExpression.Create(methodName, true, arguments.ToArray());
return SqlFunctionCallScalarExpression.Create(methodName, true, arguments);
}
else
{
Expand Down Expand Up @@ -706,7 +706,7 @@ public static SqlScalarExpression VisitConstant(ConstantExpression inputExpressi
arrayItems.Add(ExpressionToSql.VisitConstant(Expression.Constant(item), context));
}

return SqlArrayCreateScalarExpression.Create(arrayItems.ToArray());
return SqlArrayCreateScalarExpression.Create(arrayItems);
}

return ConvertCosmosElementToSqlScalarExpression(
Expand Down Expand Up @@ -1955,7 +1955,7 @@ private static SqlScalarExpression ConvertCosmosElementToSqlScalarExpression(Cos
properties.Add(property);
}

sqlScalarExpression = SqlObjectCreateScalarExpression.Create(properties.ToArray());
sqlScalarExpression = SqlObjectCreateScalarExpression.Create(properties);
}
else if (element is CosmosArray cosmosArray)
{
Expand All @@ -1965,7 +1965,7 @@ private static SqlScalarExpression ConvertCosmosElementToSqlScalarExpression(Cos
items.Add(ConvertCosmosElementToSqlScalarExpression(item));
}

sqlScalarExpression = SqlArrayCreateScalarExpression.Create(items.ToArray());
sqlScalarExpression = SqlArrayCreateScalarExpression.Create(items);
}
else if (element is CosmosNumber64 cosmosNumber)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ private bool AddStringValue(string value)
}
else
{
UInt128 uInt128Value = UInt128.FromByteArray(this.utf8Buffer, 0);
UInt128 uInt128Value = UInt128.FromByteArray(this.utf8Buffer);
added = this.stringsLength16.Add(uInt128Value);
}
}
Expand Down Expand Up @@ -571,8 +571,7 @@ private static HashSet<UInt128> Parse128BitHashes(CosmosObject hashDictionary, s
$"{nameof(UnorderdDistinctMap)} continuation token was malformed.");
}

// Todo have this method work with span<byte> instead to avoid the allocation.
UInt128 uint128 = UInt128.FromByteArray(binary.Value.ToArray());
UInt128 uint128 = UInt128.FromByteArray(binary.Value.Span);
hashSet.Add(uint128);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,15 @@ public override void Stop()
/// <param name="tryFilterAsync">The callback used to filter each partition.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task to await on.</returns>
protected async Task<TryCatch<bool>> TryInitializeAsync(
protected async Task<TryCatch> TryInitializeAsync(
string collectionRid,
IReadOnlyList<PartitionKeyRange> partitionKeyRanges,
int initialPageSize,
SqlQuerySpec querySpecForInit,
IReadOnlyDictionary<string, string> targetRangeToContinuationMap,
bool deferFirstPage,
string filter,
Func<ItemProducerTree, Task<TryCatch<bool>>> tryFilterAsync,
Func<ItemProducerTree, Task<TryCatch>> tryFilterAsync,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -380,7 +380,7 @@ protected async Task<TryCatch<bool>> TryInitializeAsync(

if (failureResponse.HasValue)
{
return TryCatch<bool>.FromException(
return TryCatch.FromException(
new CosmosException(
statusCode: failureResponse.Value.StatusCode,
subStatusCode: (int)failureResponse.Value.SubStatusCode.GetValueOrDefault(0),
Expand Down Expand Up @@ -408,7 +408,7 @@ protected async Task<TryCatch<bool>> TryInitializeAsync(

if (tryFilterAsync != null)
{
TryCatch<bool> tryFilter = await tryFilterAsync(itemProducerTree);
TryCatch tryFilter = await tryFilterAsync(itemProducerTree);
if (!tryFilter.Succeeded)
{
return tryFilter;
Expand All @@ -418,7 +418,7 @@ protected async Task<TryCatch<bool>> TryInitializeAsync(
this.itemProducerForest.Enqueue(itemProducerTree);
}

return TryCatch<bool>.FromResult(true);
return TryCatch.FromResult();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static async Task<TryCatch<CosmosOrderByItemQueryExecutionContext>> TryCr
sortOrders: initParams.PartitionedQueryExecutionInfo.QueryInfo.OrderBy,
orderByExpressions: initParams.PartitionedQueryExecutionInfo.QueryInfo.OrderByExpressions,
cancellationToken: cancellationToken))
.Try<CosmosOrderByItemQueryExecutionContext>((ignore) => context);
.Try<CosmosOrderByItemQueryExecutionContext>(() => context);
}

/// <summary>
Expand Down Expand Up @@ -321,7 +321,7 @@ private bool ShouldIncrementSkipCount(ItemProducer currentItemProducer)
StringComparison.Ordinal);
}

private async Task<TryCatch<bool>> TryInitializeAsync(
private async Task<TryCatch> TryInitializeAsync(
SqlQuerySpec sqlQuerySpec,
string requestContinuation,
string collectionRid,
Expand Down Expand Up @@ -364,7 +364,7 @@ private async Task<TryCatch<bool>> TryInitializeAsync(
sqlQuerySpec.QueryText.Replace(oldValue: FormatPlaceHolder, newValue: True),
sqlQuerySpec.Parameters);

TryCatch<bool> tryInitialize = await base.TryInitializeAsync(
TryCatch tryInitialize = await base.TryInitializeAsync(
collectionRid,
partitionKeyRanges,
initialPageSize,
Expand All @@ -387,7 +387,7 @@ private async Task<TryCatch<bool>> TryInitializeAsync(
orderByExpressions);
if (!tryExtractContinuationTokens.Succeeded)
{
return TryCatch<bool>.FromException(tryExtractContinuationTokens.Exception);
return TryCatch.FromException(tryExtractContinuationTokens.Exception);
}

TryCatch<OrderByInitInfo> tryGetOrderByInitInfo = CosmosOrderByItemQueryExecutionContext.TryGetOrderByPartitionKeyRangesInitializationInfo(
Expand All @@ -397,7 +397,7 @@ private async Task<TryCatch<bool>> TryInitializeAsync(
orderByExpressions);
if (!tryGetOrderByInitInfo.Succeeded)
{
return TryCatch<bool>.FromException(tryGetOrderByInitInfo.Exception);
return TryCatch.FromException(tryGetOrderByInitInfo.Exception);
}

OrderByInitInfo initiaizationInfo = tryGetOrderByInitInfo.Result;
Expand Down Expand Up @@ -427,7 +427,7 @@ private async Task<TryCatch<bool>> TryInitializeAsync(
sqlQuerySpec.QueryText.Replace(FormatPlaceHolder, info.Filter),
sqlQuerySpec.Parameters);

TryCatch<bool> tryInitialize = await base.TryInitializeAsync(
TryCatch tryInitialize = await base.TryInitializeAsync(
collectionRid,
partialRanges,
initialPageSize,
Expand All @@ -443,7 +443,7 @@ private async Task<TryCatch<bool>> TryInitializeAsync(
itemProducerTree.Root.PartitionKeyRange.Id,
out OrderByContinuationToken continuationToken))
{
TryCatch<bool> tryFilter = await this.TryFilterAsync(
TryCatch tryFilter = await this.TryFilterAsync(
itemProducerTree,
sortOrders,
continuationToken,
Expand All @@ -455,7 +455,7 @@ private async Task<TryCatch<bool>> TryInitializeAsync(
}
}
return TryCatch<bool>.FromResult(true);
return TryCatch.FromResult();
},
cancellationToken);
if (!tryInitialize.Succeeded)
Expand All @@ -465,7 +465,7 @@ private async Task<TryCatch<bool>> TryInitializeAsync(
}
}

return TryCatch<bool>.FromResult(true);
return TryCatch.FromResult();
}

private static TryCatch<OrderByContinuationToken[]> TryExtractContinuationTokens(
Expand Down Expand Up @@ -524,7 +524,7 @@ private static TryCatch<OrderByContinuationToken[]> TryExtractContinuationTokens
/// <param name="continuationToken">The continuation token.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task to await on.</returns>
private async Task<TryCatch<bool>> TryFilterAsync(
private async Task<TryCatch> TryFilterAsync(
ItemProducerTree producer,
SortOrder[] sortOrders,
OrderByContinuationToken continuationToken,
Expand All @@ -542,7 +542,7 @@ private async Task<TryCatch<bool>> TryFilterAsync(
{
if (!ResourceId.TryParse(continuationToken.Rid, out ResourceId continuationRid))
{
return TryCatch<bool>.FromException(
return TryCatch.FromException(
new MalformedContinuationTokenException($"Invalid Rid in the continuation token {continuationToken.CompositeContinuationToken.Token} for OrderBy~Context."));
}

Expand Down Expand Up @@ -586,7 +586,7 @@ private async Task<TryCatch<bool>> TryFilterAsync(
{
if (!ResourceId.TryParse(orderByResult.Rid, out rid))
{
return TryCatch<bool>.FromException(
return TryCatch.FromException(
new MalformedContinuationTokenException($"Invalid Rid in the continuation token {continuationToken.CompositeContinuationToken.Token} for OrderBy~Context~TryParse."));

}
Expand All @@ -598,7 +598,7 @@ private async Task<TryCatch<bool>> TryFilterAsync(
{
if (continuationRid.Database != rid.Database || continuationRid.DocumentCollection != rid.DocumentCollection)
{
return TryCatch<bool>.FromException(
return TryCatch.FromException(
new MalformedContinuationTokenException($"Invalid Rid in the continuation token {continuationToken.CompositeContinuationToken.Token} for OrderBy~Context."));
}

Expand Down Expand Up @@ -633,7 +633,7 @@ private async Task<TryCatch<bool>> TryFilterAsync(
{
if (failureResponse.HasValue)
{
return TryCatch<bool>.FromException(
return TryCatch.FromException(
new CosmosException(
statusCode: failureResponse.Value.StatusCode,
subStatusCode: (int)failureResponse.Value.SubStatusCode.GetValueOrDefault(0),
Expand All @@ -659,7 +659,7 @@ private async Task<TryCatch<bool>> TryFilterAsync(
}
}

return TryCatch<bool>.FromResult(true);
return TryCatch.FromResult();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private async Task<TryCatch<CosmosParallelItemQueryExecutionContext>> TryInitial
ParallelInitInfo initializationInfo = tryGetInitInfo.Result;
IReadOnlyList<PartitionKeyRange> filteredPartitionKeyRanges = initializationInfo.PartialRanges;
IReadOnlyDictionary<string, CompositeContinuationToken> targetIndicesForFullContinuation = initializationInfo.ContinuationTokens;
TryCatch<bool> tryInitialize = await base.TryInitializeAsync(
TryCatch tryInitialize = await base.TryInitializeAsync(
collectionRid,
filteredPartitionKeyRanges,
initialPageSize,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Query.Core.Monads
{
using System;
using System.Diagnostics;

internal sealed class ExceptionWithStackTraceException : Exception
{
private static readonly string EndOfInnerExceptionString = "--- End of inner exception stack trace ---";
private readonly StackTrace stackTrace;

public ExceptionWithStackTraceException(StackTrace stackTrace)
: this(message: null, innerException: null, stackTrace: stackTrace)
{
}

public ExceptionWithStackTraceException(string message, StackTrace stackTrace)
: this(message: message, innerException: null, stackTrace: stackTrace)
{
}

public ExceptionWithStackTraceException(
string message,
Exception innerException,
StackTrace stackTrace)
: base(
message: message,
innerException: innerException)
{
if (stackTrace == null)
{
throw new ArgumentNullException(nameof(stackTrace));
}

this.stackTrace = stackTrace;
}

public override string StackTrace => this.stackTrace.ToString();

public override string ToString()
{
// core2.x does not honor the StackTrace property in .ToString() (it uses the private internal stack trace).
// core3.x uses the property as it should
// For now just copying and pasting the 2.x implementation (this can be removed in 3.x)
string s;

if ((this.Message == null) || (this.Message.Length <= 0))
{
s = this.GetClassName();
}
else
{
s = this.GetClassName() + ": " + this.Message;
}

if (this.InnerException != null)
{
s = s
+ " ---> "
+ this.InnerException.ToString()
+ Environment.NewLine
+ " "
+ EndOfInnerExceptionString;

}

s += Environment.NewLine + this.StackTrace;
return s;
}

private string GetClassName()
{
return this.GetType().ToString();
}
}
}
Loading

0 comments on commit 6b80c87

Please sign in to comment.