Skip to content

Commit

Permalink
Rename ValueIndex to EquatableKeyValue
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd committed Aug 24, 2022
1 parent e05609c commit 3d2757c
Show file tree
Hide file tree
Showing 24 changed files with 131 additions and 166 deletions.
22 changes: 11 additions & 11 deletions src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public MigrationsModelDiffer(
/// 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>
protected virtual IRowIdentityMapFactory RowIdentityMapFactory { get; }
protected virtual IRowIdentityMapFactory RowIdentityMapFactory { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -702,7 +702,7 @@ private static IEnumerable<IColumn> GetSortedColumns(ITable table)

Check.DebugAssert(columns.Count == 0, "columns is not empty");

// issue #28539
// issue #28539
// ideally we should inject JSON column in the place corresponding to the navigation that maps to it in the clr type
var jsonColumns = table.Columns.Where(x => x is JsonColumn).OrderBy(x => x.Name);

Expand Down Expand Up @@ -1743,7 +1743,7 @@ protected virtual void TrackData(
AddSeedData(sourceEntityType, _sourceIdentityMaps, EntityState.Deleted);
}
}

private void AddSeedData(IEntityType entityType, Dictionary<ITable, IRowIdentityMap> identityMaps, EntityState initialState)
{
var sensitiveLoggingEnabled = CommandBatchPreparerDependencies.LoggingOptions.IsSensitiveDataLoggingEnabled;
Expand Down Expand Up @@ -1816,21 +1816,21 @@ private void AddSeedData(IEntityType entityType, Dictionary<ITable, IRowIdentity
BuildValuesString(key),
table.SchemaQualifiedName));
}

throw new InvalidOperationException(
RelationalStrings.DuplicateSeedData(
entityType.DisplayName(),
table.SchemaQualifiedName));
}

command = existingCommand;
}
else
{
command = CommandBatchPreparerDependencies.ModificationCommandFactory.CreateNonTrackedModificationCommand(
new NonTrackedModificationCommandParameters(table, sensitiveLoggingEnabled));
command.EntityState = initialState;

identityMap.Add(key, command);
}

Expand Down Expand Up @@ -1914,7 +1914,7 @@ private void AddSeedData(IEntityType entityType, Dictionary<ITable, IRowIdentity
writeValue = writeValue
&& initialState != EntityState.Deleted
&& property.GetBeforeSaveBehavior() == PropertySaveBehavior.Save;

command.AddColumnModification(
new ColumnModificationParameters(
column, originalValue: value, value, property, columnMapping.TypeMapping,
Expand Down Expand Up @@ -1982,7 +1982,7 @@ protected virtual void DiffData(
{
return;
}

var tableMapping = new Dictionary<ITable, (ITable, IRowIdentityMap)?>();
var unchangedColumns = new List<IColumnModification>();
var overridenColumns = new List<IColumnModification>();
Expand Down Expand Up @@ -2049,10 +2049,10 @@ protected virtual void DiffData(
{
targetRow.EntityState = EntityState.Unchanged;
}

continue;
}

if (sourceTable.IsExcludedFromMigrations
|| targetTable.IsExcludedFromMigrations)
{
Expand Down Expand Up @@ -2162,7 +2162,7 @@ protected virtual IEnumerable<MigrationOperation> GetDataOperations(
DiffContext diffContext)
{
TrackData(source, target, diffContext);

DiffData(source, target, diffContext);

var dataOperations = GetDataOperations(forSource: true, diffContext)
Expand Down
39 changes: 19 additions & 20 deletions src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Microsoft.EntityFrameworkCore.Update.Internal;
Expand Down Expand Up @@ -527,7 +526,7 @@ private void Format(ITableIndex index, IReadOnlyModificationCommand source, IRea

var rowForeignKeyValueFactory = ((TableIndex)index).GetRowIndexValueFactory();
var dependentCommand = reverseDependency ? target : source;
var values = rowForeignKeyValueFactory.CreateValue(dependentCommand, fromOriginalValues: !reverseDependency)!;
var values = rowForeignKeyValueFactory.CreateIndexValue(dependentCommand, fromOriginalValues: !reverseDependency)!;
FormatValues(values, index.Columns, dependentCommand, builder);

builder.Append(" } ");
Expand Down Expand Up @@ -578,7 +577,7 @@ private void AddForeignKeyEdges(
}

var principalKeyValue = ((ForeignKeyConstraint)foreignKey).GetRowForeignKeyValueFactory()
.CreatePrincipalValueIndex(command);
.CreatePrincipalEquatableKeyValue(command);
Check.DebugAssert(principalKeyValue != null, "null principalKeyValue");

if (!predecessorsMap.TryGetValue(principalKeyValue, out var predecessorCommands))
Expand All @@ -598,14 +597,14 @@ private void AddForeignKeyEdges(
{
if (!CanCreateDependency(foreignKey, command, principal: true)
|| !IsModified(foreignKey.PrincipalKey.Properties, entry)
|| (command.Table != null
&& !HasTempKey(entry, foreignKey.PrincipalKey)))
|| command.Table != null
&& !HasTempKey(entry, foreignKey.PrincipalKey))
{
continue;
}

var principalKeyValue = foreignKey.GetDependentKeyValueFactory()
.CreatePrincipalValueIndex(entry);
.CreatePrincipalEquatableKey(entry);
Check.DebugAssert(principalKeyValue != null, "null principalKeyValue");

if (!predecessorsMap.TryGetValue(principalKeyValue, out var predecessorCommands))
Expand All @@ -631,7 +630,7 @@ private void AddForeignKeyEdges(
}

var dependentKeyValue = ((ForeignKeyConstraint)foreignKey).GetRowForeignKeyValueFactory()
.CreateDependentValueIndex(command, fromOriginalValues: true);
.CreateDependentEquatableKeyValue(command, fromOriginalValues: true);
if (dependentKeyValue != null)
{
if (!originalPredecessorsMap.TryGetValue(dependentKeyValue, out var predecessorCommands))
Expand All @@ -657,7 +656,7 @@ private void AddForeignKeyEdges(
}

var dependentKeyValue = foreignKey.GetDependentKeyValueFactory()
?.CreateDependentValueIndex(entry, fromOriginalValues: true);
?.CreateDependentEquatableKey(entry, fromOriginalValues: true);

if (dependentKeyValue != null)
{
Expand Down Expand Up @@ -689,7 +688,7 @@ private void AddForeignKeyEdges(
}

var dependentKeyValue = ((ForeignKeyConstraint)foreignKey).GetRowForeignKeyValueFactory()
.CreateDependentValueIndex(command);
.CreateDependentEquatableKeyValue(command);
if (dependentKeyValue is null)
{
continue;
Expand All @@ -713,7 +712,7 @@ private void AddForeignKeyEdges(
}

var dependentKeyValue = foreignKey.GetDependentKeyValueFactory()
?.CreateDependentValueIndex(entry);
?.CreateDependentEquatableKey(entry);
if (dependentKeyValue == null)
{
continue;
Expand All @@ -737,7 +736,7 @@ private void AddForeignKeyEdges(
}

var principalKeyValue = ((ForeignKeyConstraint)foreignKey).GetRowForeignKeyValueFactory()
.CreatePrincipalValueIndex(command, fromOriginalValues: true);
.CreatePrincipalEquatableKeyValue(command, fromOriginalValues: true);
Check.DebugAssert(principalKeyValue != null, "null principalKeyValue");
AddMatchingPredecessorEdge(
originalPredecessorsMap, principalKeyValue, commandGraph, command, foreignKey);
Expand All @@ -757,7 +756,7 @@ private void AddForeignKeyEdges(
}

var principalKeyValue = foreignKey.GetDependentKeyValueFactory()
.CreatePrincipalValueIndex(entry, fromOriginalValues: true);
.CreatePrincipalEquatableKey(entry, fromOriginalValues: true);
Check.DebugAssert(principalKeyValue != null, "null principalKeyValue");
AddMatchingPredecessorEdge(
originalPredecessorsMap, principalKeyValue, commandGraph, command, foreignKey);
Expand Down Expand Up @@ -949,8 +948,8 @@ private static bool IsModified(IReadOnlyList<IColumn> columns, IReadOnlyModifica
var columnMapping = column.FindColumnMapping(entry.EntityType);
var property = columnMapping?.Property;
if (property != null
&& ((property.GetAfterSaveBehavior() == PropertySaveBehavior.Save)
|| (!property.IsPrimaryKey() && entry.EntityState != EntityState.Modified)))
&& (property.GetAfterSaveBehavior() == PropertySaveBehavior.Save
|| !property.IsPrimaryKey() && entry.EntityState != EntityState.Modified))
{
switch (entry.EntityState)
{
Expand Down Expand Up @@ -1114,7 +1113,7 @@ private void AddUniqueValueEdges(Multigraph<IReadOnlyModificationCommand, IAnnot
}

var indexValue = ((TableIndex)index).GetRowIndexValueFactory()
.CreateValueIndex(command, fromOriginalValues: true);
.CreateEquatableIndexValue(command, fromOriginalValues: true);
if (indexValue != null)
{
indexPredecessorsMap ??= new();
Expand All @@ -1139,7 +1138,7 @@ private void AddUniqueValueEdges(Multigraph<IReadOnlyModificationCommand, IAnnot
foreach (var key in command.Table.UniqueConstraints)
{
var keyValue = ((UniqueConstraint)key).GetRowKeyValueFactory()
.CreateValueIndex(command, fromOriginalValues: true);
.CreateEquatableKeyValue(command, fromOriginalValues: true);
Check.DebugAssert(keyValue != null, "null keyValue");
if (!keyPredecessorsMap.TryGetValue((key, keyValue), out var predecessorCommands))
{
Expand All @@ -1163,7 +1162,7 @@ private void AddUniqueValueEdges(Multigraph<IReadOnlyModificationCommand, IAnnot
}

var keyValue = key.GetPrincipalKeyValueFactory()
.CreateValueIndex(entry, fromOriginalValues: true);
.CreateEquatableKey(entry, fromOriginalValues: true);
Check.DebugAssert(keyValue != null, "null keyValue");
if (!keyPredecessorsMap.TryGetValue((key, keyValue), out var predecessorCommands))
{
Expand Down Expand Up @@ -1196,7 +1195,7 @@ private void AddUniqueValueEdges(Multigraph<IReadOnlyModificationCommand, IAnnot
}

var indexValue = ((TableIndex)index).GetRowIndexValueFactory()
.CreateValueIndex(command);
.CreateEquatableIndexValue(command);
if (indexValue != null)
{
AddMatchingPredecessorEdge(
Expand All @@ -1220,7 +1219,7 @@ private void AddUniqueValueEdges(Multigraph<IReadOnlyModificationCommand, IAnnot
foreach (var key in command.Table.UniqueConstraints)
{
var keyValue = ((UniqueConstraint)key).GetRowKeyValueFactory()
.CreateValueIndex(command, fromOriginalValues: true);
.CreateEquatableKeyValue(command, fromOriginalValues: true);
Check.DebugAssert(keyValue != null, "null keyValue");

AddMatchingPredecessorEdge(
Expand All @@ -1240,7 +1239,7 @@ private void AddUniqueValueEdges(Multigraph<IReadOnlyModificationCommand, IAnnot
}

var keyValue = key.GetPrincipalKeyValueFactory()
.CreateValueIndex(entry, fromOriginalValues: true);
.CreateEquatableKey(entry, fromOriginalValues: true);
Check.DebugAssert(keyValue != null, "null keyValue");

AddMatchingPredecessorEdge(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,22 @@ public CompositeRowForeignKeyValueFactory(IForeignKeyConstraint foreignKey)
/// 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 object CreatePrincipalValueIndex(IReadOnlyModificationCommand command, bool fromOriginalValues = false)
#pragma warning disable EF1001 // Internal EF Core API usage.
=> new ValueIndex<object?[]>(
public virtual object CreatePrincipalEquatableKeyValue(IReadOnlyModificationCommand command, bool fromOriginalValues = false)
=> new EquatableKeyValue<object?[]>(
_foreignKey,
_principalKeyValueFactory.CreateKeyValue(command, fromOriginalValues),
EqualityComparer);
#pragma warning restore EF1001 // Internal EF Core API usage.

/// <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 object? CreateDependentValueIndex(IReadOnlyModificationCommand command, bool fromOriginalValues = false)
#pragma warning disable EF1001 // Internal EF Core API usage.
public virtual object? CreateDependentEquatableKeyValue(IReadOnlyModificationCommand command, bool fromOriginalValues = false)
=> TryCreateDependentKeyValue(command, fromOriginalValues, out var keyValue)
? new ValueIndex<object?[]>(_foreignKey, keyValue, EqualityComparer)
? new EquatableKeyValue<object?[]>(_foreignKey, keyValue, EqualityComparer)
: null;
#pragma warning restore EF1001 // Internal EF Core API usage.

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,18 @@ public virtual bool TryCreateIndexValue(IReadOnlyModificationCommand command, bo
/// 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 object? CreateValueIndex(IReadOnlyModificationCommand command, bool fromOriginalValues = false)
#pragma warning disable EF1001 // Internal EF Core API usage.
public virtual object? CreateEquatableIndexValue(IReadOnlyModificationCommand command, bool fromOriginalValues = false)
=> TryCreateDependentKeyValue(command, fromOriginalValues, out var keyValue)
? new ValueIndex<object?[]>(_index, keyValue, EqualityComparer)
? new EquatableKeyValue<object?[]>(_index, keyValue, EqualityComparer)
: null;
#pragma warning restore EF1001 // Internal EF Core API usage.

/// <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 object[]? CreateValue(IReadOnlyModificationCommand command, bool fromOriginalValues = false)
public virtual object[]? CreateIndexValue(IReadOnlyModificationCommand command, bool fromOriginalValues = false)
=> TryCreateIndexValue(command, fromOriginalValues, out var keyValue)
? (object[])keyValue
: null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,11 @@ private IColumn FindNullColumnInKeyValues(object?[]? keyValues)
/// 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 object CreateValueIndex(IReadOnlyModificationCommand command, bool fromOriginalValues = false)
#pragma warning disable EF1001 // Internal EF Core API usage.
=> new ValueIndex<object?[]>(
public virtual object CreateEquatableKeyValue(IReadOnlyModificationCommand command, bool fromOriginalValues = false)
=> new EquatableKeyValue<object?[]>(
_constraint,
CreateKeyValue(command, fromOriginalValues),
EqualityComparer);
#pragma warning restore EF1001 // Internal EF Core API usage.

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public virtual bool TryCreateDependentKeyValue(IReadOnlyModificationCommand comm
value = fromOriginalValues ? entry.GetOriginalProviderValue(property) : entry.GetCurrentProviderValue(property);
if (!fromOriginalValues
&& (entry.EntityState == EntityState.Added
|| (entry.EntityState == EntityState.Modified && entry.IsModified(property))))
|| entry.EntityState == EntityState.Modified && entry.IsModified(property)))
{
break;
}
Expand Down Expand Up @@ -203,9 +203,9 @@ public int GetHashCode(object?[] obj)
// ReSharper disable once LoopCanBeConvertedToQuery
for (var i = 0; i < obj.Length; i++)
{
hashCode *= 397;
var value = obj[i];
var hash = value == null ? 0 : _hashCodes[i](value);
hashCode = (hashCode * 397) ^ hash;
hashCode ^= value == null ? 0 : _hashCodes[i](value);
}

return hashCode;
Expand Down
Loading

0 comments on commit 3d2757c

Please sign in to comment.