Skip to content

Commit

Permalink
Consolidate primitive collections across relational and Cosmos
Browse files Browse the repository at this point in the history
This change starts using the EF8 primitive collection infrastructure for primitive collections in Cosmos. This involves adding support for nested collections and read-only collections.

The main challenge here is supporting all the different collection types when, for example, `List<List<string>>` cannot be cast to `IEnumerable<IEnumerable<string>>`. To support this, we use exact collection types in snapshots, and we use non-generic methods in the nested comparers and serializers.

General dictionary mapping still not supported.
No extensive query testing done yet.

Model building and change tracking for #30713
Fixes #25364
Fixes #25343
Part of #4179
Fixes #31722
  • Loading branch information
ajcvickers committed Apr 3, 2024
1 parent 48ac5fa commit bffed80
Show file tree
Hide file tree
Showing 43 changed files with 2,151 additions and 2,005 deletions.
178 changes: 89 additions & 89 deletions src/EFCore.Cosmos/ChangeTracking/Internal/ListComparer.cs
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;

/// <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 sealed class ListComparer<TElement, TCollection> : ValueComparer<TCollection>
where TCollection : class, IEnumerable<TElement>
{
/// <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 ListComparer(ValueComparer elementComparer, bool readOnly)
: base(
(a, b) => Compare(a, b, (ValueComparer<TElement>)elementComparer),
o => GetHashCode(o, (ValueComparer<TElement>)elementComparer),
source => Snapshot(source, (ValueComparer<TElement>)elementComparer, readOnly))
{
}

/// <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 override Type Type
=> typeof(TCollection);

private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TElement> elementComparer)
{
if (a is not IReadOnlyList<TElement> aList)
{
return b is not IReadOnlyList<TElement>;
}

if (b is not IReadOnlyList<TElement> bList || aList.Count != bList.Count)
{
return false;
}

if (ReferenceEquals(aList, bList))
{
return true;
}

for (var i = 0; i < aList.Count; i++)
{
if (!elementComparer.Equals(aList[i], bList[i]))
{
return false;
}
}

return true;
}

private static int GetHashCode(TCollection source, ValueComparer<TElement> elementComparer)
{
var hash = new HashCode();
foreach (var el in source)
{
hash.Add(el, elementComparer);
}

return hash.ToHashCode();
}

private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
{
if (readOnly)
{
return source;
}

var snapshot = new List<TElement>(((IReadOnlyList<TElement>)source).Count);
foreach (var e in source)
{
snapshot.Add(e is null ? default! : elementComparer.Snapshot(e));
}

return (TCollection)(object)snapshot;
}
}
// namespace Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;
//
// /// <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 sealed class ListComparer<TElement, TCollection> : ValueComparer<TCollection>
// where TCollection : class, IEnumerable<TElement>
// {
// /// <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 ListComparer(ValueComparer elementComparer, bool readOnly)
// : base(
// (a, b) => Compare(a, b, (ValueComparer<TElement>)elementComparer),
// o => GetHashCode(o, (ValueComparer<TElement>)elementComparer),
// source => Snapshot(source, (ValueComparer<TElement>)elementComparer, readOnly))
// {
// }
//
// /// <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 override Type Type
// => typeof(TCollection);
//
// private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TElement> elementComparer)
// {
// if (a is not IReadOnlyList<TElement> aList)
// {
// return b is not IReadOnlyList<TElement>;
// }
//
// if (b is not IReadOnlyList<TElement> bList || aList.Count != bList.Count)
// {
// return false;
// }
//
// if (ReferenceEquals(aList, bList))
// {
// return true;
// }
//
// for (var i = 0; i < aList.Count; i++)
// {
// if (!elementComparer.Equals(aList[i], bList[i]))
// {
// return false;
// }
// }
//
// return true;
// }
//
// private static int GetHashCode(TCollection source, ValueComparer<TElement> elementComparer)
// {
// var hash = new HashCode();
// foreach (var el in source)
// {
// hash.Add(el, elementComparer);
// }
//
// return hash.ToHashCode();
// }
//
// private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
// {
// if (readOnly)
// {
// return source;
// }
//
// var snapshot = new List<TElement>(((IReadOnlyList<TElement>)source).Count);
// foreach (var e in source)
// {
// snapshot.Add(e is null ? default! : elementComparer.Snapshot(e));
// }
//
// return (TCollection)(object)snapshot;
// }
// }
204 changes: 102 additions & 102 deletions src/EFCore.Cosmos/ChangeTracking/Internal/NullableListComparer.cs
Original file line number Diff line number Diff line change
@@ -1,105 +1,105 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;

/// <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 sealed class NullableListComparer<TElement, TCollection> : ValueComparer<TCollection>
where TCollection : class, IEnumerable<TElement?>
where TElement : struct
{
/// <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 NullableListComparer(ValueComparer elementComparer, bool readOnly)
: base(
(a, b) => Compare(a, b, (ValueComparer<TElement>)elementComparer),
o => GetHashCode(o, (ValueComparer<TElement>)elementComparer),
source => Snapshot(source, (ValueComparer<TElement>)elementComparer, readOnly))
{
}

/// <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 override Type Type
=> typeof(TCollection);

private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TElement> elementComparer)
{
if (a is not IReadOnlyList<TElement?> aList)
{
return b is not IReadOnlyList<TElement?>;
}

if (b is not IReadOnlyList<TElement?> bList || aList.Count != bList.Count)
{
return false;
}

if (ReferenceEquals(aList, bList))
{
return true;
}

for (var i = 0; i < aList.Count; i++)
{
var (aElement, bElement) = (aList[i], bList[i]);
if (aElement is null)
{
if (bElement is null)
{
continue;
}

return false;
}

if (bElement is null || !elementComparer.Equals(aElement, bElement))
{
return false;
}
}

return true;
}

private static int GetHashCode(TCollection source, ValueComparer<TElement> elementComparer)
{
var nullableEqualityComparer = new NullableEqualityComparer<TElement>(elementComparer);
var hash = new HashCode();
foreach (var el in source)
{
hash.Add(el, nullableEqualityComparer);
}

return hash.ToHashCode();
}

private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
{
if (readOnly)
{
return source;
}

var snapshot = new List<TElement?>(((IReadOnlyList<TElement?>)source).Count);
foreach (var e in source)
{
snapshot.Add(e is null ? null : elementComparer.Snapshot(e.Value));
}

return (TCollection)(object)snapshot;
}
}
// namespace Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;
//
// /// <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 sealed class NullableListComparer<TElement, TCollection> : ValueComparer<TCollection>
// where TCollection : class, IEnumerable<TElement?>
// where TElement : struct
// {
// /// <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 NullableListComparer(ValueComparer elementComparer, bool readOnly)
// : base(
// (a, b) => Compare(a, b, (ValueComparer<TElement>)elementComparer),
// o => GetHashCode(o, (ValueComparer<TElement>)elementComparer),
// source => Snapshot(source, (ValueComparer<TElement>)elementComparer, readOnly))
// {
// }
//
// /// <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 override Type Type
// => typeof(TCollection);
//
// private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TElement> elementComparer)
// {
// if (a is not IReadOnlyList<TElement?> aList)
// {
// return b is not IReadOnlyList<TElement?>;
// }
//
// if (b is not IReadOnlyList<TElement?> bList || aList.Count != bList.Count)
// {
// return false;
// }
//
// if (ReferenceEquals(aList, bList))
// {
// return true;
// }
//
// for (var i = 0; i < aList.Count; i++)
// {
// var (aElement, bElement) = (aList[i], bList[i]);
// if (aElement is null)
// {
// if (bElement is null)
// {
// continue;
// }
//
// return false;
// }
//
// if (bElement is null || !elementComparer.Equals(aElement, bElement))
// {
// return false;
// }
// }
//
// return true;
// }
//
// private static int GetHashCode(TCollection source, ValueComparer<TElement> elementComparer)
// {
// var nullableEqualityComparer = new NullableEqualityComparer<TElement>(elementComparer);
// var hash = new HashCode();
// foreach (var el in source)
// {
// hash.Add(el, nullableEqualityComparer);
// }
//
// return hash.ToHashCode();
// }
//
// private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
// {
// if (readOnly)
// {
// return source;
// }
//
// var snapshot = new List<TElement?>(((IReadOnlyList<TElement?>)source).Count);
// foreach (var e in source)
// {
// snapshot.Add(e is null ? null : elementComparer.Snapshot(e.Value));
// }
//
// return (TCollection)(object)snapshot;
// }
// }
Loading

0 comments on commit bffed80

Please sign in to comment.