Skip to content

Commit

Permalink
Cosmos: Improvement to Find/FindAsync
Browse files Browse the repository at this point in the history
Allow ReadItem optimization when the Partition Key is known and Cosmos Resource Id is provided or Cosmos Resource Id can be generated based on the default generator or a custom generator.

Fixes #17310
  • Loading branch information
1iveowl authored Apr 19, 2020
1 parent 5526977 commit b549dc6
Show file tree
Hide file tree
Showing 20 changed files with 2,182 additions and 1,043 deletions.
22 changes: 22 additions & 0 deletions src/EFCore.Cosmos/Diagnostics/Internal/CosmosLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ public static void ExecutingSqlQuery(
cosmosSqlQuery.Query);
}

public static void ExecutingReadItem(
[NotNull] this IDiagnosticsLogger<DbLoggerCategory.Database.Command> diagnosticsLogger,
[NotNull] string partitionKey,
[NotNull] string resourceId)
{
var definition = new EventDefinition<string, string, string>(
diagnosticsLogger.Options,
CoreEventId.ProviderBaseId,
LogLevel.Debug,
"CoreEventId.ProviderBaseId",
level => LoggerMessage.Define<string, string, string>(
level,
CoreEventId.ProviderBaseId,
"Executing Read Item [Partition Key, Resource Id=[{parameters}]]{newLine}{commandText}"));

definition.Log(
diagnosticsLogger,
$"{partitionKey}, {resourceId}",
Environment.NewLine,
"Read Item");
}

private static string FormatParameters(IReadOnlyList<SqlParameter> parameters)
{
return parameters.Count == 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.EntityFrameworkCore.ValueGeneration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata;
using Microsoft.EntityFrameworkCore.Cosmos.ValueGeneration.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
Expand Down
26 changes: 22 additions & 4 deletions src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/EFCore.Cosmos/Properties/CosmosStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,13 @@
<data name="ReverseRequiresOffsetOrLimit" xml:space="preserve">
<value>Reverse is not supported without Limit or Offset.</value>
</data>
<data name="InvalidResourceId" xml:space="preserve">
<value>Invalid Resource id. Resource id cannot be null or empty and must be a string value.</value>
</data>
<data name="ParitionKeyMissing" xml:space="preserve">
<value>Partition key missing.</value>
</data>
<data name="ResourceIdMissing" xml:space="preserve">
<value>Resource id missing or cannot be generated.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
public class CosmosQueryCompilationContext : QueryCompilationContext
{
public virtual string PartitionKey { get; internal set; }
public virtual string PartitionKeyFromExtension { get; internal set; }

public CosmosQueryCompilationContext(
[NotNull] QueryCompilationContextDependencies dependencies, bool async)
Expand Down
10 changes: 10 additions & 0 deletions src/EFCore.Cosmos/Query/Internal/CosmosQueryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.EntityFrameworkCore.ValueGeneration;

namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
Expand All @@ -28,6 +29,7 @@ public CosmosQueryContext(
: base(dependencies)
{
Check.NotNull(cosmosClient, nameof(cosmosClient));

CosmosClient = cosmosClient;
}

Expand All @@ -38,5 +40,13 @@ public CosmosQueryContext(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual CosmosClientWrapper CosmosClient { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual IValueGeneratorSelector ValueGeneratorSelector { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
{
var innerQueryable = Visit(methodCallExpression.Arguments[0]);

_cosmosQueryCompilationContext.PartitionKey = (string)((ConstantExpression)methodCallExpression.Arguments[1]).Value;
_cosmosQueryCompilationContext.PartitionKeyFromExtension = (string)((ConstantExpression)methodCallExpression.Arguments[1]).Value;

return innerQueryable;
}
Expand Down
Loading

0 comments on commit b549dc6

Please sign in to comment.