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

Allow property values to be explicitly flagged as temporary #24620

Merged
merged 1 commit into from
Apr 19, 2021
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
61 changes: 56 additions & 5 deletions src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,26 +552,60 @@ public virtual bool IsConceptualNull(IProperty property)
public virtual bool HasTemporaryValue(IProperty property)
=> GetValueType(property) == CurrentValueType.Temporary;

/// <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 void PropagateValue(
InternalEntityEntry principalEntry,
IProperty principalProperty,
IProperty dependentProperty,
bool isMaterialization = false,
bool setModified = true)
{
var principalValue = principalEntry[principalProperty];
if (principalEntry.HasTemporaryValue(principalProperty))
{
if (principalEntry._stateData.IsPropertyFlagged(principalProperty.GetIndex(), PropertyFlag.IsTemporary))
{
SetProperty(dependentProperty, principalValue, isMaterialization, setModified);
_stateData.FlagProperty(dependentProperty.GetIndex(), PropertyFlag.IsTemporary, true);
}
else
{
SetTemporaryValue(dependentProperty, principalValue);
}
}
else
{
SetProperty(dependentProperty, principalValue, isMaterialization, setModified);
_stateData.FlagProperty(dependentProperty.GetIndex(), PropertyFlag.IsTemporary, false);
}
}

private CurrentValueType GetValueType(
IProperty property,
Func<object?, object?, bool>? equals = null)
{
var tempIndex = property.GetStoreGeneratedIndex();
if (tempIndex == -1)
if (_stateData.IsPropertyFlagged(property.GetIndex(), PropertyFlag.IsTemporary))
{
return CurrentValueType.Normal;
return CurrentValueType.Temporary;
}

if (equals == null)
var tempIndex = property.GetStoreGeneratedIndex();
if (tempIndex == -1)
{
equals = ValuesEqualFunc(property);
return CurrentValueType.Normal;
}

if (!PropertyHasDefaultValue(property))
{
return CurrentValueType.Normal;
}

@equals ??= ValuesEqualFunc(property);
var defaultValue = property.ClrType.GetDefaultValue();
var value = ReadPropertyValue(property);
if (!equals(value, defaultValue))
Expand Down Expand Up @@ -611,6 +645,15 @@ public virtual void SetTemporaryValue(IProperty property, object? value, bool se
SetProperty(property, value, isMaterialization: false, setModified, isCascadeDelete: false, CurrentValueType.Temporary);
}

/// <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 void MarkAsTemporary(IProperty property, bool temporary)
=> _stateData.FlagProperty(property.GetIndex(), PropertyFlag.IsTemporary, temporary);

/// <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
Expand Down Expand Up @@ -1208,6 +1251,12 @@ private void SetProperty(

if (valueType == CurrentValueType.StoreGenerated)
{
var defaultValue = asProperty!.ClrType.GetDefaultValue();
if (!equals(currentValue, defaultValue))
{
WritePropertyValue(asProperty, defaultValue, isMaterialization);
}

EnsureStoreGeneratedValues();
_storeGeneratedValues.SetValue(asProperty!, value, storeGeneratedIndex);
}
Expand Down Expand Up @@ -1285,6 +1334,8 @@ public virtual void AcceptChanges()
_storeGeneratedValues = new SidecarValues();
_temporaryValues = new SidecarValues();
}

_stateData.FlagAllProperties(EntityType.PropertyCount(), PropertyFlag.IsTemporary, false);

var currentState = EntityState;
if ((currentState == EntityState.Unchanged)
Expand Down
9 changes: 1 addition & 8 deletions src/EFCore/ChangeTracking/Internal/KeyPropagator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,7 @@ public KeyPropagator(
if (generationProperty == null
|| !principalProperty.ClrType.IsDefaultValue(principalValue))
{
if (principalEntry.HasTemporaryValue(principalProperty))
{
entry.SetTemporaryValue(property, principalValue);
}
else
{
entry[property] = principalValue;
}
entry.PropagateValue(principalEntry, principalProperty, property);

return principalEntry;
}
Expand Down
9 changes: 1 addition & 8 deletions src/EFCore/ChangeTracking/Internal/NavigationFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,14 +1213,7 @@ private static void SetForeignKeyProperties(
|| (dependentEntry.IsConceptualNull(dependentProperty)
&& principalValue != null))
{
if (principalEntry.HasTemporaryValue(principalProperty))
{
dependentEntry.SetTemporaryValue(dependentProperty, principalValue, setModified);
}
else
{
dependentEntry.SetProperty(dependentProperty, principalValue, fromQuery, setModified);
}
dependentEntry.PropagateValue(principalEntry, principalProperty, dependentProperty, fromQuery, setModified);

dependentEntry.StateManager.UpdateDependentMap(dependentEntry, foreignKey);
dependentEntry.SetRelationshipSnapshotValue(dependentProperty, principalValue);
Expand Down
11 changes: 6 additions & 5 deletions src/EFCore/ChangeTracking/Internal/StateData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ internal enum PropertyFlag
Modified = 0,
Null = 1,
Unknown = 2,
IsLoaded = 3
IsLoaded = 3,
IsTemporary = 4
}

internal readonly struct StateData
{
private const int BitsPerInt = 32;
private const int BitsForEntityState = 3;
private const int BitsForEntityFlags = 1;
private const int BitsForPropertyFlags = 4;
private const int BitsForEntityFlags = 5;
private const int BitsForPropertyFlags = 8;
private const int BitsForAdditionalState = BitsForEntityState + BitsForEntityFlags;
private const int EntityStateMask = 0x07;
private const int UnusedStateMask = 0x08; // So entity state uses even number of bits
private const int UnusedStateMask = 0xF8; // So entity state uses even number of bits
private const int AdditionalStateMask = EntityStateMask | UnusedStateMask;
private const int PropertyFlagMask = 0x11111111;
private const int PropertyFlagMask = 0x01010101;

private readonly int[] _bits;

Expand Down
10 changes: 2 additions & 8 deletions src/EFCore/ChangeTracking/PropertyEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,8 @@ public virtual bool IsTemporary
get => InternalEntry.HasTemporaryValue(Metadata);
set
{
if (value)
{
InternalEntry.SetTemporaryValue(Metadata, CurrentValue);
}
else
{
InternalEntry[Metadata] = CurrentValue;
}
InternalEntry[Metadata] = CurrentValue;
InternalEntry.MarkAsTemporary(Metadata, value);
}
}

Expand Down
38 changes: 34 additions & 4 deletions test/EFCore.Tests/ChangeTracking/Internal/StateDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public void Can_read_and_manipulate_modification_flags()
InternalEntityEntry.PropertyFlag.Modified,
InternalEntityEntry.PropertyFlag.Null,
InternalEntityEntry.PropertyFlag.Unknown,
InternalEntityEntry.PropertyFlag.IsLoaded);
InternalEntityEntry.PropertyFlag.IsLoaded,
InternalEntityEntry.PropertyFlag.IsTemporary);
}
}

Expand All @@ -31,7 +32,8 @@ public void Can_read_and_manipulate_null_flags()
InternalEntityEntry.PropertyFlag.Null,
InternalEntityEntry.PropertyFlag.Modified,
InternalEntityEntry.PropertyFlag.Unknown,
InternalEntityEntry.PropertyFlag.IsLoaded);
InternalEntityEntry.PropertyFlag.IsLoaded,
InternalEntityEntry.PropertyFlag.IsTemporary);
}
}

Expand All @@ -45,7 +47,8 @@ public void Can_read_and_manipulate_not_set_flags()
InternalEntityEntry.PropertyFlag.Unknown,
InternalEntityEntry.PropertyFlag.Modified,
InternalEntityEntry.PropertyFlag.Null,
InternalEntityEntry.PropertyFlag.IsLoaded);
InternalEntityEntry.PropertyFlag.IsLoaded,
InternalEntityEntry.PropertyFlag.IsTemporary);
}
}

Expand All @@ -59,6 +62,22 @@ public void Can_read_and_manipulate_is_loaded_flags()
InternalEntityEntry.PropertyFlag.IsLoaded,
InternalEntityEntry.PropertyFlag.Modified,
InternalEntityEntry.PropertyFlag.Null,
InternalEntityEntry.PropertyFlag.Unknown,
InternalEntityEntry.PropertyFlag.IsTemporary);
}
}

[ConditionalFact]
public void Can_read_and_manipulate_temporary_flags()
{
for (var i = 0; i < 70; i++)
{
PropertyManipulation(
i,
InternalEntityEntry.PropertyFlag.IsTemporary,
InternalEntityEntry.PropertyFlag.IsLoaded,
InternalEntityEntry.PropertyFlag.Modified,
InternalEntityEntry.PropertyFlag.Null,
InternalEntityEntry.PropertyFlag.Unknown);
}
}
Expand All @@ -68,14 +87,16 @@ private void PropertyManipulation(
InternalEntityEntry.PropertyFlag propertyFlag,
InternalEntityEntry.PropertyFlag unusedFlag1,
InternalEntityEntry.PropertyFlag unusedFlag2,
InternalEntityEntry.PropertyFlag unusedFlag3)
InternalEntityEntry.PropertyFlag unusedFlag3,
InternalEntityEntry.PropertyFlag unusedFlag4)
{
var data = new InternalEntityEntry.StateData(propertyCount, propertyCount);

Assert.False(data.AnyPropertiesFlagged(propertyFlag));
Assert.False(data.AnyPropertiesFlagged(unusedFlag1));
Assert.False(data.AnyPropertiesFlagged(unusedFlag2));
Assert.False(data.AnyPropertiesFlagged(unusedFlag3));
Assert.False(data.AnyPropertiesFlagged(unusedFlag4));

for (var i = 0; i < propertyCount; i++)
{
Expand All @@ -87,12 +108,14 @@ private void PropertyManipulation(
Assert.False(data.IsPropertyFlagged(j, unusedFlag1));
Assert.False(data.IsPropertyFlagged(j, unusedFlag2));
Assert.False(data.IsPropertyFlagged(j, unusedFlag3));
Assert.False(data.IsPropertyFlagged(j, unusedFlag4));
}

Assert.True(data.AnyPropertiesFlagged(propertyFlag));
Assert.False(data.AnyPropertiesFlagged(unusedFlag1));
Assert.False(data.AnyPropertiesFlagged(unusedFlag2));
Assert.False(data.AnyPropertiesFlagged(unusedFlag3));
Assert.False(data.AnyPropertiesFlagged(unusedFlag4));
}

for (var i = 0; i < propertyCount; i++)
Expand All @@ -105,12 +128,14 @@ private void PropertyManipulation(
Assert.False(data.IsPropertyFlagged(j, unusedFlag1));
Assert.False(data.IsPropertyFlagged(j, unusedFlag2));
Assert.False(data.IsPropertyFlagged(j, unusedFlag3));
Assert.False(data.IsPropertyFlagged(j, unusedFlag4));
}

Assert.Equal(i < propertyCount - 1, data.AnyPropertiesFlagged(propertyFlag));
Assert.False(data.AnyPropertiesFlagged(unusedFlag1));
Assert.False(data.AnyPropertiesFlagged(unusedFlag2));
Assert.False(data.AnyPropertiesFlagged(unusedFlag3));
Assert.False(data.AnyPropertiesFlagged(unusedFlag4));
}

for (var i = 0; i < propertyCount; i++)
Expand All @@ -119,6 +144,7 @@ private void PropertyManipulation(
Assert.False(data.IsPropertyFlagged(i, unusedFlag1));
Assert.False(data.IsPropertyFlagged(i, unusedFlag2));
Assert.False(data.IsPropertyFlagged(i, unusedFlag3));
Assert.False(data.IsPropertyFlagged(i, unusedFlag4));
}

data.FlagAllProperties(propertyCount, propertyFlag, flagged: true);
Expand All @@ -127,13 +153,15 @@ private void PropertyManipulation(
Assert.False(data.AnyPropertiesFlagged(unusedFlag1));
Assert.False(data.AnyPropertiesFlagged(unusedFlag2));
Assert.False(data.AnyPropertiesFlagged(unusedFlag3));
Assert.False(data.AnyPropertiesFlagged(unusedFlag4));

for (var i = 0; i < propertyCount; i++)
{
Assert.True(data.IsPropertyFlagged(i, propertyFlag));
Assert.False(data.IsPropertyFlagged(i, unusedFlag1));
Assert.False(data.IsPropertyFlagged(i, unusedFlag2));
Assert.False(data.IsPropertyFlagged(i, unusedFlag3));
Assert.False(data.IsPropertyFlagged(i, unusedFlag4));
}

data.FlagAllProperties(propertyCount, propertyFlag, flagged: false);
Expand All @@ -142,13 +170,15 @@ private void PropertyManipulation(
Assert.False(data.AnyPropertiesFlagged(unusedFlag1));
Assert.False(data.AnyPropertiesFlagged(unusedFlag2));
Assert.False(data.AnyPropertiesFlagged(unusedFlag3));
Assert.False(data.AnyPropertiesFlagged(unusedFlag4));

for (var i = 0; i < propertyCount; i++)
{
Assert.False(data.IsPropertyFlagged(i, propertyFlag));
Assert.False(data.IsPropertyFlagged(i, unusedFlag1));
Assert.False(data.IsPropertyFlagged(i, unusedFlag2));
Assert.False(data.IsPropertyFlagged(i, unusedFlag3));
Assert.False(data.IsPropertyFlagged(i, unusedFlag4));
}
}

Expand Down
Loading