Skip to content

Commit

Permalink
Query: Rename PerformIdentityResolution to AsNoTrackingWithIdentityRe…
Browse files Browse the repository at this point in the history
…solution

- Add another enum value in QueryTrackingBehavior.
- Change query pipeline to work with enum rather than bool flag for IsTracking

Part of #20409
Part of #19877
  • Loading branch information
smitpatel committed Jun 17, 2020
1 parent 60dc853 commit ba12446
Show file tree
Hide file tree
Showing 22 changed files with 196 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private sealed class QueryingEnumerable<T> : IEnumerable<T>, IAsyncEnumerable<T>
private readonly Type _contextType;
private readonly string _partitionKey;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _performIdentityResolution;
private readonly bool _standAloneStateManager;

public QueryingEnumerable(
CosmosQueryContext cosmosQueryContext,
Expand All @@ -43,7 +43,7 @@ public QueryingEnumerable(
Func<CosmosQueryContext, JObject, T> shaper,
Type contextType,
string partitionKeyFromExtension,
bool performIdentityResolution)
bool standAloneStateManager)
{
_cosmosQueryContext = cosmosQueryContext;
_sqlExpressionFactory = sqlExpressionFactory;
Expand All @@ -52,7 +52,7 @@ public QueryingEnumerable(
_shaper = shaper;
_contextType = contextType;
_queryLogger = cosmosQueryContext.QueryLogger;
_performIdentityResolution = performIdentityResolution;
_standAloneStateManager = standAloneStateManager;

var partitionKey = selectExpression.GetPartitionKey(cosmosQueryContext.ParameterValues);
if (partitionKey != null && partitionKeyFromExtension != null && partitionKeyFromExtension != partitionKey)
Expand Down Expand Up @@ -108,7 +108,7 @@ private sealed class Enumerator : IEnumerator<T>
private readonly Type _contextType;
private readonly string _partitionKey;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _performIdentityResolution;
private readonly bool _standAloneStateManager;

private IEnumerator<JObject> _enumerator;

Expand All @@ -121,7 +121,7 @@ public Enumerator(QueryingEnumerable<T> queryingEnumerable)
_contextType = queryingEnumerable._contextType;
_partitionKey = queryingEnumerable._partitionKey;
_queryLogger = queryingEnumerable._queryLogger;
_performIdentityResolution = queryingEnumerable._performIdentityResolution;
_standAloneStateManager = queryingEnumerable._standAloneStateManager;
}

public T Current { get; private set; }
Expand All @@ -144,7 +144,7 @@ public bool MoveNext()
_partitionKey,
sqlQuery)
.GetEnumerator();
_cosmosQueryContext.InitializeStateManager(_performIdentityResolution);
_cosmosQueryContext.InitializeStateManager(_standAloneStateManager);
}

var hasNext = _enumerator.MoveNext();
Expand Down Expand Up @@ -183,7 +183,7 @@ private sealed class AsyncEnumerator : IAsyncEnumerator<T>
private readonly Type _contextType;
private readonly string _partitionKey;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _performIdentityResolution;
private readonly bool _standAloneStateManager;
private readonly CancellationToken _cancellationToken;

private IAsyncEnumerator<JObject> _enumerator;
Expand All @@ -197,7 +197,7 @@ public AsyncEnumerator(QueryingEnumerable<T> queryingEnumerable, CancellationTok
_contextType = queryingEnumerable._contextType;
_partitionKey = queryingEnumerable._partitionKey;
_queryLogger = queryingEnumerable._queryLogger;
_performIdentityResolution = queryingEnumerable._performIdentityResolution;
_standAloneStateManager = queryingEnumerable._standAloneStateManager;
_cancellationToken = cancellationToken;
}

Expand All @@ -219,7 +219,7 @@ public async ValueTask<bool> MoveNextAsync()
_partitionKey,
sqlQuery)
.GetAsyncEnumerator(_cancellationToken);
_cosmosQueryContext.InitializeStateManager(_performIdentityResolution);
_cosmosQueryContext.InitializeStateManager(_standAloneStateManager);
}

var hasNext = await _enumerator.MoveNextAsync().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ private sealed class ReadItemQueryingEnumerable<T> : IEnumerable<T>, IAsyncEnume
private readonly Func<CosmosQueryContext, JObject, T> _shaper;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _performIdentityResolution;
private readonly bool _standAloneStateManager;

public ReadItemQueryingEnumerable(
CosmosQueryContext cosmosQueryContext,
ReadItemExpression readItemExpression,
Func<CosmosQueryContext, JObject, T> shaper,
Type contextType,
bool performIdentityResolution)
bool standAloneStateManager)
{
_cosmosQueryContext = cosmosQueryContext;
_readItemExpression = readItemExpression;
_shaper = shaper;
_contextType = contextType;
_queryLogger = _cosmosQueryContext.QueryLogger;
_performIdentityResolution = performIdentityResolution;
_standAloneStateManager = standAloneStateManager;
}

public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
Expand All @@ -71,7 +71,7 @@ private sealed class Enumerator : IEnumerator<T>, IAsyncEnumerator<T>
private readonly Func<CosmosQueryContext, JObject, T> _shaper;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _performIdentityResolution;
private readonly bool _standAloneStateManager;
private readonly CancellationToken _cancellationToken;

private JObject _item;
Expand All @@ -84,7 +84,7 @@ public Enumerator(ReadItemQueryingEnumerable<T> readItemEnumerable, Cancellation
_shaper = readItemEnumerable._shaper;
_contextType = readItemEnumerable._contextType;
_queryLogger = readItemEnumerable._queryLogger;
_performIdentityResolution = readItemEnumerable._performIdentityResolution;
_standAloneStateManager = readItemEnumerable._standAloneStateManager;
_cancellationToken = cancellationToken;
}

Expand Down Expand Up @@ -188,7 +188,7 @@ private bool ShapeResult()
{
var hasNext = !(_item is null);

_cosmosQueryContext.InitializeStateManager(_performIdentityResolution);
_cosmosQueryContext.InitializeStateManager(_standAloneStateManager);

Current
= hasNext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected override Expression VisitShapedQuery(ShapedQueryExpression shapedQuery
selectExpression.ApplyProjection();

shaperBody = new CosmosProjectionBindingRemovingExpressionVisitor(
selectExpression, jObjectParameter, QueryCompilationContext.IsTracking)
selectExpression, jObjectParameter, QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.TrackAll)
.Visit(shaperBody);

var shaperLambda = Expression.Lambda(
Expand All @@ -85,12 +85,12 @@ protected override Expression VisitShapedQuery(ShapedQueryExpression shapedQuery
Expression.Constant(shaperLambda.Compile()),
Expression.Constant(_contextType),
Expression.Constant(_partitionKeyFromExtension, typeof(string)),
Expression.Constant(QueryCompilationContext.PerformIdentityResolution));
Expression.Constant(QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.NoTrackingWithIdentityResolution));

case ReadItemExpression readItemExpression:

shaperBody = new CosmosProjectionBindingRemovingReadItemExpressionVisitor(
readItemExpression, jObjectParameter, QueryCompilationContext.IsTracking)
readItemExpression, jObjectParameter, QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.TrackAll)
.Visit(shaperBody);

var shaperReadItemLambda = Expression.Lambda(
Expand All @@ -106,7 +106,7 @@ protected override Expression VisitShapedQuery(ShapedQueryExpression shapedQuery
Expression.Constant(readItemExpression),
Expression.Constant(shaperReadItemLambda.Compile()),
Expression.Constant(_contextType),
Expression.Constant(QueryCompilationContext.PerformIdentityResolution));
Expression.Constant(QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.NoTrackingWithIdentityResolution));

default:
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ private sealed class QueryingEnumerable<T> : IAsyncEnumerable<T>, IEnumerable<T>
private readonly Func<QueryContext, ValueBuffer, T> _shaper;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _performIdentityResolution;
private readonly bool _standAloneStateManager;

public QueryingEnumerable(
QueryContext queryContext,
IEnumerable<ValueBuffer> innerEnumerable,
Func<QueryContext, ValueBuffer, T> shaper,
Type contextType,
bool performIdentityResolution)
bool standAloneStateManager)
{
_queryContext = queryContext;
_innerEnumerable = innerEnumerable;
_shaper = shaper;
_contextType = contextType;
_queryLogger = queryContext.QueryLogger;
_performIdentityResolution = performIdentityResolution;
_standAloneStateManager = standAloneStateManager;
}

public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
Expand All @@ -63,7 +63,7 @@ private sealed class Enumerator : IEnumerator<T>, IAsyncEnumerator<T>
private readonly Func<QueryContext, ValueBuffer, T> _shaper;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _performIdentityResolution;
private readonly bool _standAloneStateManager;
private readonly CancellationToken _cancellationToken;

public Enumerator(QueryingEnumerable<T> queryingEnumerable, CancellationToken cancellationToken = default)
Expand All @@ -73,7 +73,7 @@ public Enumerator(QueryingEnumerable<T> queryingEnumerable, CancellationToken ca
_shaper = queryingEnumerable._shaper;
_contextType = queryingEnumerable._contextType;
_queryLogger = queryingEnumerable._queryLogger;
_performIdentityResolution = queryingEnumerable._performIdentityResolution;
_standAloneStateManager = queryingEnumerable._standAloneStateManager;
_cancellationToken = cancellationToken;
}

Expand Down Expand Up @@ -122,7 +122,7 @@ private bool MoveNextHelper()
if (_enumerator == null)
{
_enumerator = _innerEnumerable.GetEnumerator();
_queryContext.InitializeStateManager(_performIdentityResolution);
_queryContext.InitializeStateManager(_standAloneStateManager);
}

var hasNext = _enumerator.MoveNext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ protected override Expression VisitShapedQuery(ShapedQueryExpression shapedQuery

shaper = new InMemoryProjectionBindingRemovingExpressionVisitor().Visit(shaper);

shaper = new CustomShaperCompilingExpressionVisitor(QueryCompilationContext.IsTracking).Visit(shaper);
shaper = new CustomShaperCompilingExpressionVisitor(
QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.TrackAll).Visit(shaper);

var shaperLambda = (LambdaExpression)shaper;

Expand All @@ -90,7 +91,7 @@ protected override Expression VisitShapedQuery(ShapedQueryExpression shapedQuery
innerEnumerable,
Expression.Constant(shaperLambda.Compile()),
Expression.Constant(_contextType),
Expression.Constant(QueryCompilationContext.PerformIdentityResolution));
Expression.Constant(QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.NoTrackingWithIdentityResolution));
}

private static readonly MethodInfo _tableMethodInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class FromSqlQueryingEnumerable<T> : IEnumerable<T>, IAsyncEnumerable<T>,
private readonly Func<QueryContext, DbDataReader, int[], T> _shaper;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _performIdentityResolution;
private readonly bool _standAloneStateManager;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -42,15 +42,15 @@ public FromSqlQueryingEnumerable(
[NotNull] IReadOnlyList<string> columnNames,
[NotNull] Func<QueryContext, DbDataReader, int[], T> shaper,
[NotNull] Type contextType,
bool performIdentityResolution)
bool standAloneStateManager)
{
_relationalQueryContext = relationalQueryContext;
_relationalCommandCache = relationalCommandCache;
_columnNames = columnNames;
_shaper = shaper;
_contextType = contextType;
_queryLogger = relationalQueryContext.QueryLogger;
_performIdentityResolution = performIdentityResolution;
_standAloneStateManager = standAloneStateManager;
}

/// <summary>
Expand Down Expand Up @@ -140,7 +140,7 @@ private sealed class Enumerator : IEnumerator<T>
private readonly Func<QueryContext, DbDataReader, int[], T> _shaper;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _performIdentityResolution;
private readonly bool _standAloneStateManager;

private RelationalDataReader _dataReader;
private int[] _indexMap;
Expand All @@ -154,7 +154,7 @@ public Enumerator(FromSqlQueryingEnumerable<T> queryingEnumerable)
_shaper = queryingEnumerable._shaper;
_contextType = queryingEnumerable._contextType;
_queryLogger = queryingEnumerable._queryLogger;
_performIdentityResolution = queryingEnumerable._performIdentityResolution;
_standAloneStateManager = queryingEnumerable._standAloneStateManager;
}

public T Current { get; private set; }
Expand Down Expand Up @@ -209,7 +209,7 @@ private bool InitializeReader(DbContext _, bool result)

_indexMap = BuildIndexMap(_columnNames, _dataReader.DbDataReader);

_relationalQueryContext.InitializeStateManager(_performIdentityResolution);
_relationalQueryContext.InitializeStateManager(_standAloneStateManager);

return result;
}
Expand All @@ -231,7 +231,7 @@ private sealed class AsyncEnumerator : IAsyncEnumerator<T>
private readonly Func<QueryContext, DbDataReader, int[], T> _shaper;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _performIdentityResolution;
private readonly bool _standAloneStateManager;
private readonly CancellationToken _cancellationToken;

private RelationalDataReader _dataReader;
Expand All @@ -248,7 +248,7 @@ public AsyncEnumerator(
_shaper = queryingEnumerable._shaper;
_contextType = queryingEnumerable._contextType;
_queryLogger = queryingEnumerable._queryLogger;
_performIdentityResolution = queryingEnumerable._performIdentityResolution;
_standAloneStateManager = queryingEnumerable._standAloneStateManager;
_cancellationToken = cancellationToken;
}

Expand Down Expand Up @@ -304,7 +304,7 @@ private async Task<bool> InitializeReaderAsync(DbContext _, bool result, Cancell

_indexMap = BuildIndexMap(_columnNames, _dataReader.DbDataReader);

_relationalQueryContext.InitializeStateManager(_performIdentityResolution);
_relationalQueryContext.InitializeStateManager(_standAloneStateManager);

return result;
}
Expand Down
Loading

0 comments on commit ba12446

Please sign in to comment.