Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e0b6718

Browse files
committedApr 2, 2024·
Consolidate primitive collections across relational and Cosmos
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
1 parent 183fe47 commit e0b6718

File tree

43 files changed

+2151
-2005
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2151
-2005
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,92 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;
5-
6-
/// <summary>
7-
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
8-
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
9-
/// any release. You should only use it directly in your code with extreme caution and knowing that
10-
/// doing so can result in application failures when updating to a new Entity Framework Core release.
11-
/// </summary>
12-
public sealed class ListComparer<TElement, TCollection> : ValueComparer<TCollection>
13-
where TCollection : class, IEnumerable<TElement>
14-
{
15-
/// <summary>
16-
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
17-
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
18-
/// any release. You should only use it directly in your code with extreme caution and knowing that
19-
/// doing so can result in application failures when updating to a new Entity Framework Core release.
20-
/// </summary>
21-
public ListComparer(ValueComparer elementComparer, bool readOnly)
22-
: base(
23-
(a, b) => Compare(a, b, (ValueComparer<TElement>)elementComparer),
24-
o => GetHashCode(o, (ValueComparer<TElement>)elementComparer),
25-
source => Snapshot(source, (ValueComparer<TElement>)elementComparer, readOnly))
26-
{
27-
}
28-
29-
/// <summary>
30-
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
31-
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
32-
/// any release. You should only use it directly in your code with extreme caution and knowing that
33-
/// doing so can result in application failures when updating to a new Entity Framework Core release.
34-
/// </summary>
35-
public override Type Type
36-
=> typeof(TCollection);
37-
38-
private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TElement> elementComparer)
39-
{
40-
if (a is not IReadOnlyList<TElement> aList)
41-
{
42-
return b is not IReadOnlyList<TElement>;
43-
}
44-
45-
if (b is not IReadOnlyList<TElement> bList || aList.Count != bList.Count)
46-
{
47-
return false;
48-
}
49-
50-
if (ReferenceEquals(aList, bList))
51-
{
52-
return true;
53-
}
54-
55-
for (var i = 0; i < aList.Count; i++)
56-
{
57-
if (!elementComparer.Equals(aList[i], bList[i]))
58-
{
59-
return false;
60-
}
61-
}
62-
63-
return true;
64-
}
65-
66-
private static int GetHashCode(TCollection source, ValueComparer<TElement> elementComparer)
67-
{
68-
var hash = new HashCode();
69-
foreach (var el in source)
70-
{
71-
hash.Add(el, elementComparer);
72-
}
73-
74-
return hash.ToHashCode();
75-
}
76-
77-
private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
78-
{
79-
if (readOnly)
80-
{
81-
return source;
82-
}
83-
84-
var snapshot = new List<TElement>(((IReadOnlyList<TElement>)source).Count);
85-
foreach (var e in source)
86-
{
87-
snapshot.Add(e is null ? default! : elementComparer.Snapshot(e));
88-
}
89-
90-
return (TCollection)(object)snapshot;
91-
}
92-
}
4+
// namespace Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;
5+
//
6+
// /// <summary>
7+
// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
8+
// /// the same compatibility standards as public APIs. It may be changed or removed without notice in
9+
// /// any release. You should only use it directly in your code with extreme caution and knowing that
10+
// /// doing so can result in application failures when updating to a new Entity Framework Core release.
11+
// /// </summary>
12+
// public sealed class ListComparer<TElement, TCollection> : ValueComparer<TCollection>
13+
// where TCollection : class, IEnumerable<TElement>
14+
// {
15+
// /// <summary>
16+
// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
17+
// /// the same compatibility standards as public APIs. It may be changed or removed without notice in
18+
// /// any release. You should only use it directly in your code with extreme caution and knowing that
19+
// /// doing so can result in application failures when updating to a new Entity Framework Core release.
20+
// /// </summary>
21+
// public ListComparer(ValueComparer elementComparer, bool readOnly)
22+
// : base(
23+
// (a, b) => Compare(a, b, (ValueComparer<TElement>)elementComparer),
24+
// o => GetHashCode(o, (ValueComparer<TElement>)elementComparer),
25+
// source => Snapshot(source, (ValueComparer<TElement>)elementComparer, readOnly))
26+
// {
27+
// }
28+
//
29+
// /// <summary>
30+
// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
31+
// /// the same compatibility standards as public APIs. It may be changed or removed without notice in
32+
// /// any release. You should only use it directly in your code with extreme caution and knowing that
33+
// /// doing so can result in application failures when updating to a new Entity Framework Core release.
34+
// /// </summary>
35+
// public override Type Type
36+
// => typeof(TCollection);
37+
//
38+
// private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TElement> elementComparer)
39+
// {
40+
// if (a is not IReadOnlyList<TElement> aList)
41+
// {
42+
// return b is not IReadOnlyList<TElement>;
43+
// }
44+
//
45+
// if (b is not IReadOnlyList<TElement> bList || aList.Count != bList.Count)
46+
// {
47+
// return false;
48+
// }
49+
//
50+
// if (ReferenceEquals(aList, bList))
51+
// {
52+
// return true;
53+
// }
54+
//
55+
// for (var i = 0; i < aList.Count; i++)
56+
// {
57+
// if (!elementComparer.Equals(aList[i], bList[i]))
58+
// {
59+
// return false;
60+
// }
61+
// }
62+
//
63+
// return true;
64+
// }
65+
//
66+
// private static int GetHashCode(TCollection source, ValueComparer<TElement> elementComparer)
67+
// {
68+
// var hash = new HashCode();
69+
// foreach (var el in source)
70+
// {
71+
// hash.Add(el, elementComparer);
72+
// }
73+
//
74+
// return hash.ToHashCode();
75+
// }
76+
//
77+
// private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
78+
// {
79+
// if (readOnly)
80+
// {
81+
// return source;
82+
// }
83+
//
84+
// var snapshot = new List<TElement>(((IReadOnlyList<TElement>)source).Count);
85+
// foreach (var e in source)
86+
// {
87+
// snapshot.Add(e is null ? default! : elementComparer.Snapshot(e));
88+
// }
89+
//
90+
// return (TCollection)(object)snapshot;
91+
// }
92+
// }
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,105 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;
5-
6-
/// <summary>
7-
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
8-
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
9-
/// any release. You should only use it directly in your code with extreme caution and knowing that
10-
/// doing so can result in application failures when updating to a new Entity Framework Core release.
11-
/// </summary>
12-
public sealed class NullableListComparer<TElement, TCollection> : ValueComparer<TCollection>
13-
where TCollection : class, IEnumerable<TElement?>
14-
where TElement : struct
15-
{
16-
/// <summary>
17-
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
18-
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
19-
/// any release. You should only use it directly in your code with extreme caution and knowing that
20-
/// doing so can result in application failures when updating to a new Entity Framework Core release.
21-
/// </summary>
22-
public NullableListComparer(ValueComparer elementComparer, bool readOnly)
23-
: base(
24-
(a, b) => Compare(a, b, (ValueComparer<TElement>)elementComparer),
25-
o => GetHashCode(o, (ValueComparer<TElement>)elementComparer),
26-
source => Snapshot(source, (ValueComparer<TElement>)elementComparer, readOnly))
27-
{
28-
}
29-
30-
/// <summary>
31-
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
32-
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
33-
/// any release. You should only use it directly in your code with extreme caution and knowing that
34-
/// doing so can result in application failures when updating to a new Entity Framework Core release.
35-
/// </summary>
36-
public override Type Type
37-
=> typeof(TCollection);
38-
39-
private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TElement> elementComparer)
40-
{
41-
if (a is not IReadOnlyList<TElement?> aList)
42-
{
43-
return b is not IReadOnlyList<TElement?>;
44-
}
45-
46-
if (b is not IReadOnlyList<TElement?> bList || aList.Count != bList.Count)
47-
{
48-
return false;
49-
}
50-
51-
if (ReferenceEquals(aList, bList))
52-
{
53-
return true;
54-
}
55-
56-
for (var i = 0; i < aList.Count; i++)
57-
{
58-
var (aElement, bElement) = (aList[i], bList[i]);
59-
if (aElement is null)
60-
{
61-
if (bElement is null)
62-
{
63-
continue;
64-
}
65-
66-
return false;
67-
}
68-
69-
if (bElement is null || !elementComparer.Equals(aElement, bElement))
70-
{
71-
return false;
72-
}
73-
}
74-
75-
return true;
76-
}
77-
78-
private static int GetHashCode(TCollection source, ValueComparer<TElement> elementComparer)
79-
{
80-
var nullableEqualityComparer = new NullableEqualityComparer<TElement>(elementComparer);
81-
var hash = new HashCode();
82-
foreach (var el in source)
83-
{
84-
hash.Add(el, nullableEqualityComparer);
85-
}
86-
87-
return hash.ToHashCode();
88-
}
89-
90-
private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
91-
{
92-
if (readOnly)
93-
{
94-
return source;
95-
}
96-
97-
var snapshot = new List<TElement?>(((IReadOnlyList<TElement?>)source).Count);
98-
foreach (var e in source)
99-
{
100-
snapshot.Add(e is null ? null : elementComparer.Snapshot(e.Value));
101-
}
102-
103-
return (TCollection)(object)snapshot;
104-
}
105-
}
4+
// namespace Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;
5+
//
6+
// /// <summary>
7+
// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
8+
// /// the same compatibility standards as public APIs. It may be changed or removed without notice in
9+
// /// any release. You should only use it directly in your code with extreme caution and knowing that
10+
// /// doing so can result in application failures when updating to a new Entity Framework Core release.
11+
// /// </summary>
12+
// public sealed class NullableListComparer<TElement, TCollection> : ValueComparer<TCollection>
13+
// where TCollection : class, IEnumerable<TElement?>
14+
// where TElement : struct
15+
// {
16+
// /// <summary>
17+
// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
18+
// /// the same compatibility standards as public APIs. It may be changed or removed without notice in
19+
// /// any release. You should only use it directly in your code with extreme caution and knowing that
20+
// /// doing so can result in application failures when updating to a new Entity Framework Core release.
21+
// /// </summary>
22+
// public NullableListComparer(ValueComparer elementComparer, bool readOnly)
23+
// : base(
24+
// (a, b) => Compare(a, b, (ValueComparer<TElement>)elementComparer),
25+
// o => GetHashCode(o, (ValueComparer<TElement>)elementComparer),
26+
// source => Snapshot(source, (ValueComparer<TElement>)elementComparer, readOnly))
27+
// {
28+
// }
29+
//
30+
// /// <summary>
31+
// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
32+
// /// the same compatibility standards as public APIs. It may be changed or removed without notice in
33+
// /// any release. You should only use it directly in your code with extreme caution and knowing that
34+
// /// doing so can result in application failures when updating to a new Entity Framework Core release.
35+
// /// </summary>
36+
// public override Type Type
37+
// => typeof(TCollection);
38+
//
39+
// private static bool Compare(TCollection? a, TCollection? b, ValueComparer<TElement> elementComparer)
40+
// {
41+
// if (a is not IReadOnlyList<TElement?> aList)
42+
// {
43+
// return b is not IReadOnlyList<TElement?>;
44+
// }
45+
//
46+
// if (b is not IReadOnlyList<TElement?> bList || aList.Count != bList.Count)
47+
// {
48+
// return false;
49+
// }
50+
//
51+
// if (ReferenceEquals(aList, bList))
52+
// {
53+
// return true;
54+
// }
55+
//
56+
// for (var i = 0; i < aList.Count; i++)
57+
// {
58+
// var (aElement, bElement) = (aList[i], bList[i]);
59+
// if (aElement is null)
60+
// {
61+
// if (bElement is null)
62+
// {
63+
// continue;
64+
// }
65+
//
66+
// return false;
67+
// }
68+
//
69+
// if (bElement is null || !elementComparer.Equals(aElement, bElement))
70+
// {
71+
// return false;
72+
// }
73+
// }
74+
//
75+
// return true;
76+
// }
77+
//
78+
// private static int GetHashCode(TCollection source, ValueComparer<TElement> elementComparer)
79+
// {
80+
// var nullableEqualityComparer = new NullableEqualityComparer<TElement>(elementComparer);
81+
// var hash = new HashCode();
82+
// foreach (var el in source)
83+
// {
84+
// hash.Add(el, nullableEqualityComparer);
85+
// }
86+
//
87+
// return hash.ToHashCode();
88+
// }
89+
//
90+
// private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
91+
// {
92+
// if (readOnly)
93+
// {
94+
// return source;
95+
// }
96+
//
97+
// var snapshot = new List<TElement?>(((IReadOnlyList<TElement?>)source).Count);
98+
// foreach (var e in source)
99+
// {
100+
// snapshot.Add(e is null ? null : elementComparer.Snapshot(e.Value));
101+
// }
102+
//
103+
// return (TCollection)(object)snapshot;
104+
// }
105+
// }

0 commit comments

Comments
 (0)
Please sign in to comment.