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

Cosmos: Optimize Find without partition key #20698

Merged
merged 1 commit into from
Apr 21, 2020
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
56 changes: 56 additions & 0 deletions src/EFCore.Cosmos/Diagnostics/CosmosEventId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Diagnostics;
using Microsoft.Extensions.Logging;

namespace Microsoft.EntityFrameworkCore.Diagnostics
{
/// <summary>
/// <para>
/// Event IDs for Cosmos events that correspond to messages logged to an <see cref="ILogger" />
/// and events sent to a <see cref="DiagnosticSource" />.
/// </para>
/// <para>
/// These IDs are also used with <see cref="WarningsConfigurationBuilder" /> to configure the
/// behavior of warnings.
/// </para>
/// </summary>
public static class CosmosEventId
{
// Warning: These values must not change between releases.
// Only add new values to the end of sections, never in the middle.
// Try to use <Noun><Verb> naming and be consistent with existing names.
private enum Id
{
// Update events

// Query events
ExecutingSqlQuery = CoreEventId.ProviderBaseId + 100,
ExecutingReadItem
}

private static readonly string _queryPrefix = DbLoggerCategory.Query.Name + ".";
private static EventId MakeQueryId(Id id) => new EventId((int)id, _queryPrefix + id);

/// <summary>
/// <para>
/// A SQL query was executed.
/// </para>
/// <para>
/// This event is in the <see cref="DbLoggerCategory.Query" /> category.
/// </para>
/// </summary>
public static readonly EventId ExecutingSqlQuery = MakeQueryId(Id.ExecutingSqlQuery);

/// <summary>
/// <para>
/// ReadItem was executed.
/// </para>
/// <para>
/// This event is in the <see cref="DbLoggerCategory.Query" /> category.
/// </para>
/// </summary>
public static readonly EventId ExecutingReadItem = MakeQueryId(Id.ExecutingReadItem);
}
}
22 changes: 10 additions & 12 deletions src/EFCore.Cosmos/Diagnostics/Internal/CosmosLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public static void ExecutingSqlQuery(
{
var definition = new EventDefinition<string, string, string>(
diagnosticsLogger.Options,
CoreEventId.ProviderBaseId,
CosmosEventId.ExecutingSqlQuery,
LogLevel.Debug,
"CoreEventId.ProviderBaseId",
"CosmosEventId.ExecutingSqlQuery",
level => LoggerMessage.Define<string, string, string>(
level,
CoreEventId.ProviderBaseId,
CosmosEventId.ExecutingSqlQuery,
"Executing Sql Query [Parameters=[{parameters}]]{newLine}{commandText}"));

definition.Log(
Expand All @@ -56,21 +56,19 @@ public static void ExecutingReadItem(
[NotNull] string partitionKey,
[NotNull] string resourceId)
{
var definition = new EventDefinition<string, string, string>(
var definition = new EventDefinition<string>(
diagnosticsLogger.Options,
CoreEventId.ProviderBaseId,
CosmosEventId.ExecutingReadItem,
LogLevel.Debug,
"CoreEventId.ProviderBaseId",
level => LoggerMessage.Define<string, string, string>(
"CosmosEventId.ExecutingReadItem",
level => LoggerMessage.Define<string>(
level,
CoreEventId.ProviderBaseId,
"Executing Read Item [Partition Key, Resource Id=[{parameters}]]{newLine}{commandText}"));
CosmosEventId.ExecutingReadItem,
"Executing Read Item [Partition Key, Resource Id=[{parameters}]]"));

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

private static string FormatParameters(IReadOnlyList<SqlParameter> parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
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,7 +4,6 @@
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
6 changes: 3 additions & 3 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.

60 changes: 30 additions & 30 deletions src/EFCore.Cosmos/Properties/CosmosStrings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

<!--
Microsoft ResX Schema
Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -169,12 +169,12 @@
<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>
<value>Invalid id value. Specify a string value that's not null or empty.</value>
</data>
<data name="ParitionKeyMissing" xml:space="preserve">
<value>Partition key missing.</value>
<value>A ReadItem query was detected, but the partition key value is missing.</value>
</data>
<data name="ResourceIdMissing" xml:space="preserve">
<value>Resource id missing or cannot be generated.</value>
<value>A ReadItem query was detected, but the id value is missing and cannot be generated.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down Expand Up @@ -99,10 +100,15 @@ public override Expression Visit(Expression expression)
if (ProcessJoinCondition(lambdaBodyBinaryExpression, queryProperties, parameterNames))
{
var entityTypePrimaryKeyProperties = entityType.FindPrimaryKey().Properties;

var idProperty = entityType.GetProperties()
.First(p => p.GetJsonPropertyName() == StoreKeyConvention.IdPropertyName);

if (TryGetPartitionKeyProperty(out var partitionKeyProperty)
&& entityTypePrimaryKeyProperties.Contains(partitionKeyProperty)
&& entityTypePrimaryKeyProperties.SequenceEqual(queryProperties))
&& entityTypePrimaryKeyProperties.SequenceEqual(queryProperties)
&& (partitionKeyProperty == null
|| entityTypePrimaryKeyProperties.Contains(partitionKeyProperty))
&& (idProperty.GetValueGeneratorFactory() != null
|| entityTypePrimaryKeyProperties.Contains(idProperty)))
{
var propertyParameterList = queryProperties.Zip(parameterNames,
(property, parameter) => (property, parameter))
Expand Down Expand Up @@ -143,8 +149,12 @@ bool ProcessJoinCondition(
{
if (methodCallExpr.TryGetEFPropertyArguments(out _, out var propertyName))
{
#pragma warning disable EF1001
properties.Add(entityType.GetProperty(propertyName));
var property = entityType.FindProperty(propertyName);
if (property == null)
{
return false;
}
properties.Add(property);
paramNames.Add(parameterExpr.Name);
return true;
}
Expand All @@ -161,11 +171,10 @@ bool ProcessJoinCondition(
bool TryGetPartitionKeyProperty(out IProperty partitionKeyProperty)
{
var partitionKeyPropertyName = entityType.GetPartitionKeyPropertyName();

if (partitionKeyPropertyName is null)
{
partitionKeyProperty = null;
return false;
return true;
}

partitionKeyProperty = entityType.FindProperty(partitionKeyPropertyName);
Expand Down Expand Up @@ -234,7 +243,7 @@ protected override ShapedQueryExpression CreateShapedQueryExpression(Type elemen
protected override ShapedQueryExpression CreateShapedQueryExpression(IEntityType entityType)
{
Check.NotNull(entityType, nameof(entityType));

var selectExpression = _sqlExpressionFactory.Select(entityType);

return new ShapedQueryExpression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,9 @@ private static void IncludeReference<TIncludingEntity, TIncludedEntity>(
// For non-null relatedEntity StateManager will set the flag
else if (relatedEntity == null)
{
#pragma warning disable EF1001
#pragma warning disable EF1001 // Internal EF Core API usage.
entry.SetIsLoaded(navigation);
#pragma warning restore EF1001 // Internal EF Core API usage.
}
}

Expand Down Expand Up @@ -471,7 +472,9 @@ private static void IncludeCollection<TIncludingEntity, TIncludedEntity>(
}
else
{
#pragma warning disable EF1001 // Internal EF Core API usage.
entry.SetIsLoaded(navigation);
#pragma warning restore EF1001 // Internal EF Core API usage.
if (relatedEntities != null)
{
using var enumerator = relatedEntities.GetEnumerator();
Expand Down
Loading