Skip to content

Commit 02e34a6

Browse files
authored
Merge pull request #271 from servicetitan/upstream/TypeDependency
Refactor & optimize Type-dependency operations (#79)
2 parents 1c70d62 + ed81e6b commit 02e34a6

28 files changed

+507
-529
lines changed

Orm/Xtensive.Orm.Tests.Framework/DomainModelExtensions.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static void Dump(this TypeInfoCollection target)
8181

8282
public static void DumpAncestor(this TypeInfo target, int indent)
8383
{
84-
TypeInfo ancestor = target.GetAncestor();
84+
TypeInfo ancestor = target.Ancestor;
8585
if (ancestor!=null)
8686
WriteLine(indent + 1, "Ancestor: " + ancestor.Name);
8787
else {
@@ -92,8 +92,8 @@ public static void DumpAncestor(this TypeInfo target, int indent)
9292
public static void DumpDescendants(this TypeInfo target, int indent)
9393
{
9494
WriteLine(indent, "Descendants:");
95-
HashSet<TypeInfo> direct = new HashSet<TypeInfo>(target.GetDescendants());
96-
foreach (TypeInfo descendant in target.GetDescendants(true)) {
95+
HashSet<TypeInfo> direct = new HashSet<TypeInfo>(target.DirectDescendants);
96+
foreach (TypeInfo descendant in target.AllDescendants) {
9797
if (direct.Contains(descendant))
9898
WriteLine(indent + 1, descendant.Name + " (direct)");
9999
else
@@ -104,8 +104,8 @@ public static void DumpDescendants(this TypeInfo target, int indent)
104104
public static void DumpInterfaces(this TypeInfo target, int indent)
105105
{
106106
WriteLine(indent, "Interfaces:");
107-
HashSet<TypeInfo> direct = new HashSet<TypeInfo>(target.GetInterfaces());
108-
foreach (TypeInfo @interface in target.GetInterfaces(true)) {
107+
var direct = target.DirectInterfaces;
108+
foreach (TypeInfo @interface in target.AllInterfaces) {
109109
if (direct.Contains(@interface))
110110
WriteLine(indent + 1, @interface.Name + " (direct)");
111111
else
@@ -116,8 +116,8 @@ public static void DumpInterfaces(this TypeInfo target, int indent)
116116
public static void DumpImplementors(this TypeInfo target, int indent)
117117
{
118118
WriteLine(indent, "Implementors:");
119-
HashSet<TypeInfo> direct = new HashSet<TypeInfo>(target.GetImplementors());
120-
foreach (TypeInfo implementor in target.GetImplementors(true)) {
119+
HashSet<TypeInfo> direct = new HashSet<TypeInfo>(target.DirectImplementors);
120+
foreach (TypeInfo implementor in target.AllImplementors) {
121121
if (direct.Contains(implementor))
122122
WriteLine(indent + 1, implementor.Name + " (direct)");
123123
else
@@ -163,10 +163,10 @@ public static void Dump(this TypeInfo target, int indent)
163163
if (target.IsEntity) {
164164
WriteLine(indent, "Hierarchy: " + target.Hierarchy.Root.Name);
165165
if (target.Hierarchy.Root!=target)
166-
WriteLine(indent, "Ancestor: " + target.GetAncestor().Name);
166+
WriteLine(indent, "Ancestor: " + target.Ancestor.Name);
167167
}
168168
else if (target.IsInterface) {
169-
WriteLine(indent, "Implementors: " + target.GetImplementors().Select(t => t.Name).ToCommaDelimitedString());
169+
WriteLine(indent, "Implementors: " + target.DirectImplementors.Select(t => t.Name).ToCommaDelimitedString());
170170
}
171171

172172
target.DumpMappingName(indent);

Orm/Xtensive.Orm.Tests/Issues/IssueJira0117_FKStructureTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected override DomainConfiguration BuildConfiguration()
9696
public void AssociationThroughStructureTest()
9797
{
9898
var type = Domain.Model.Types[typeof (Owner1)];
99-
Assert.AreEqual(2, type.GetOwnerAssociations().Count);
99+
Assert.AreEqual(2, type.GetOwnerAssociations().Count());
100100
Assert.AreEqual(8, Domain.Model.Associations.Count);
101101
}
102102

Orm/Xtensive.Orm.Tests/Model/LibraryTest.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -393,34 +393,34 @@ protected override DomainConfiguration BuildConfiguration()
393393
private static void VerifyModel(Domain domain)
394394
{
395395
TypeInfoCollection types = domain.Model.Types;
396-
Assert.AreEqual(types.FindAncestor(types[typeof (Person)]), null);
397-
Assert.AreEqual(types.FindAncestor(types[typeof (Book)]), null);
398-
Assert.AreEqual(types.FindAncestor(types[typeof (BookReview)]), null);
399-
Assert.AreEqual(types.FindAncestor(types[typeof (Author)]), types[typeof (Person)]);
400-
401-
Assert.AreEqual(types[typeof (Person)].GetAncestor(), null);
402-
Assert.AreEqual(types[typeof (Book)].GetAncestor(), null);
403-
Assert.AreEqual(types[typeof (BookReview)].GetAncestor(), null);
404-
Assert.AreEqual(types[typeof (Author)].GetAncestor(), types[typeof (Person)]);
405-
406-
ICollection<TypeInfo> collection = types.Structures;
407-
Assert.IsTrue(collection.Count > 0);
396+
Assert.AreEqual(types[typeof (Person)].Ancestor, null);
397+
Assert.AreEqual(types[typeof (Book)].Ancestor, null);
398+
Assert.AreEqual(types[typeof (BookReview)].Ancestor, null);
399+
Assert.AreEqual(types[typeof (Author)].Ancestor, types[typeof (Person)]);
400+
401+
Assert.AreEqual(types[typeof (Person)].Ancestor, null);
402+
Assert.AreEqual(types[typeof (Book)].Ancestor, null);
403+
Assert.AreEqual(types[typeof (BookReview)].Ancestor, null);
404+
Assert.AreEqual(types[typeof (Author)].Ancestor, types[typeof (Person)]);
405+
406+
var collection = types.Structures;
407+
Assert.IsTrue(collection.Any());
408408
foreach (TypeInfo item in collection) {
409409
Assert.IsTrue(item.IsStructure);
410410
Assert.IsFalse(item.IsInterface);
411411
Assert.IsFalse(item.IsEntity);
412412
}
413413

414414
collection = types.Interfaces;
415-
Assert.IsFalse(collection.Count > 0);
415+
Assert.IsFalse(collection.Any());
416416
foreach (TypeInfo item in collection) {
417417
Assert.IsTrue(item.IsInterface);
418418
Assert.IsFalse(item.IsStructure);
419419
Assert.IsFalse(item.IsEntity);
420420
}
421421

422422
collection = types.Entities;
423-
Assert.IsTrue(collection.Count > 0);
423+
Assert.IsTrue(collection.Any());
424424
foreach (TypeInfo item in collection) {
425425
Assert.IsTrue(item.IsEntity);
426426
Assert.IsFalse(item.IsInterface);
@@ -555,8 +555,8 @@ private static void VerifyModel(Domain domain)
555555
Assert.AreEqual(true, typeInfo.Fields["Books"].IsNullable);
556556
Assert.AreEqual("Books", typeInfo.Fields["Books"].Name);
557557

558-
Assert.AreEqual(2, typeInfo.Fields.Find(FieldAttributes.Declared).Count);
559-
Assert.AreEqual(9, typeInfo.Fields.Find(FieldAttributes.Inherited).Count);
558+
Assert.AreEqual(2, typeInfo.Fields.Find(FieldAttributes.Declared).Count());
559+
Assert.AreEqual(9, typeInfo.Fields.Find(FieldAttributes.Inherited).Count());
560560

561561
// KeyColumns
562562
Assert.IsNotNull(typeInfo.Columns["PassportNumber"]);

Orm/Xtensive.Orm.Tests/Storage/Prefetch/PrefetchTestHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public static void AssertOnlySpecifiedColumnsAreLoaded(Key key, TypeInfo type, S
4242
var state = session.EntityStateCache[key, true];
4343
var realType = state.Key.TypeInfo;
4444
Assert.IsTrue(realType.Equals(type)
45-
|| realType.GetAncestors().Contains(type)
46-
|| (type.IsInterface && realType.GetInterfaces(true).Contains(type)));
45+
|| realType.Ancestors.Contains(type)
46+
|| (type.IsInterface && realType.AllInterfaces.Contains(type)));
4747
var tuple = state.Tuple;
4848
Assert.IsNotNull(tuple);
4949
foreach (var field in type.Fields) {

Orm/Xtensive.Orm.Tests/Storage/VersionBehaviorTest.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created: 2010.08.05
66

77
using System;
8+
using System.Linq;
89
using System.Diagnostics;
910
using NUnit.Framework;
1011
using Xtensive.Orm.Tests;
@@ -186,8 +187,8 @@ public void DefaultTest()
186187
var domain = Domain.Build(config);
187188
var defaultTypeInfo = domain.Model.Types[typeof(Default)];
188189
var defaultInheritorTypeInfo = domain.Model.Types[typeof(DefaultInheritor)];
189-
Assert.AreEqual(3, defaultTypeInfo.GetVersionColumns().Count);
190-
Assert.AreEqual(4, defaultInheritorTypeInfo.GetVersionColumns().Count);
190+
Assert.AreEqual(3, defaultTypeInfo.GetVersionColumns().Count());
191+
Assert.AreEqual(4, defaultInheritorTypeInfo.GetVersionColumns().Count());
191192
using (var session = domain.OpenSession()) {
192193
var versions = new VersionSet();
193194
var updatedVersions = new VersionSet();
@@ -256,10 +257,10 @@ public void ManualTest()
256257
var anotherManualTypeInfo = domain.Model.Types[typeof(AnotherManual)];
257258
var manualInheritorTypeInfo = domain.Model.Types[typeof(ManualInheritor)];
258259
var anotherManualInheritorTypeInfo = domain.Model.Types[typeof(AnotherManualInheritor)];
259-
Assert.AreEqual(1, manualTypeInfo.GetVersionColumns().Count);
260-
Assert.AreEqual(2, anotherManualTypeInfo.GetVersionColumns().Count);
261-
Assert.AreEqual(1, manualInheritorTypeInfo.GetVersionColumns().Count);
262-
Assert.AreEqual(2, anotherManualInheritorTypeInfo.GetVersionColumns().Count);
260+
Assert.AreEqual(1, manualTypeInfo.GetVersionColumns().Count());
261+
Assert.AreEqual(2, anotherManualTypeInfo.GetVersionColumns().Count());
262+
Assert.AreEqual(1, manualInheritorTypeInfo.GetVersionColumns().Count());
263+
Assert.AreEqual(2, anotherManualInheritorTypeInfo.GetVersionColumns().Count());
263264
using (var session = domain.OpenSession()) {
264265
var versions = new VersionSet();
265266
var updatedVersions = new VersionSet();
@@ -322,8 +323,8 @@ public void AutoTest()
322323
var domain = Domain.Build(config);
323324
var autoTypeInfo = domain.Model.Types[typeof(Auto)];
324325
var autoInheritorTypeInfo = domain.Model.Types[typeof(AutoInheritor)];
325-
Assert.AreEqual(1, autoTypeInfo.GetVersionColumns().Count);
326-
Assert.AreEqual(2, autoInheritorTypeInfo.GetVersionColumns().Count);
326+
Assert.AreEqual(1, autoTypeInfo.GetVersionColumns().Count());
327+
Assert.AreEqual(2, autoInheritorTypeInfo.GetVersionColumns().Count());
327328
using (var session = domain.OpenSession()) {
328329
var versions = new VersionSet();
329330
var updatedVersions = new VersionSet();
@@ -400,9 +401,9 @@ public void SkipTest()
400401
var skipTypeInfo = domain.Model.Types[typeof(Skip)];
401402
var hasVersionTypeInfo = domain.Model.Types[typeof(HasVersion)];
402403
var hasSkipVersionTypeInfo = domain.Model.Types[typeof(HasSkipVersion)];
403-
Assert.AreEqual(2, skipTypeInfo.GetVersionColumns().Count);
404-
Assert.AreEqual(2, hasVersionTypeInfo.GetVersionColumns().Count);
405-
Assert.AreEqual(2, hasSkipVersionTypeInfo.GetVersionColumns().Count);
404+
Assert.AreEqual(2, skipTypeInfo.GetVersionColumns().Count());
405+
Assert.AreEqual(2, hasVersionTypeInfo.GetVersionColumns().Count());
406+
Assert.AreEqual(2, hasSkipVersionTypeInfo.GetVersionColumns().Count());
406407
using (var session = domain.OpenSession()) {
407408
var versions = new VersionSet();
408409
var updatedVersions = new VersionSet();

Orm/Xtensive.Orm/Collections/Interfaces/IFilterable.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public interface IFilterable<TFilter, TItem>
2020
/// Finds the items from initial collection according to specified filter <paramref name="criteria"/>.
2121
/// </summary>
2222
/// <param name="criteria">The object to filter initial collection with.</param>
23-
/// <returns><see cref="ICollection{TItem}"/> object.</returns>
24-
ICollection<TItem> Find(TFilter criteria);
23+
/// <returns><see cref="IEnumerable{TItem}"/> object.</returns>
24+
IEnumerable<TItem> Find(TFilter criteria);
2525

2626
/// <summary>
2727
/// Finds the items from initial collection according to specified filter <paramref name="criteria"/>.
2828
/// </summary>
2929
/// <param name="criteria">The object to filter initial collection with.</param>
3030
/// <param name="matchType">Type of the match.</param>
31-
/// <returns><see cref="ICollection{TItem}"/> object.</returns>
32-
ICollection<TItem> Find(TFilter criteria, MatchType matchType);
31+
/// <returns><see cref="IEnumerable{TItem}"/> object.</returns>
32+
IEnumerable<TItem> Find(TFilter criteria, MatchType matchType);
3333
}
3434
}

Orm/Xtensive.Orm/Orm/Building/Builders/IndexBuilder.ClassTable.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ private void BuildClassTableIndexes(TypeInfo type)
2424

2525
var root = type.Hierarchy.Root;
2626
var typeDef = context.ModelDef.Types[type.UnderlyingType];
27-
var ancestors = type.GetAncestors().ToList();
28-
var interfaces = type.GetInterfaces();
27+
var ancestors = type.Ancestors;
28+
var interfaces = type.DirectInterfaces;
2929

3030
// Building declared indexes both secondary and primary (for root of the hierarchy only)
3131
foreach (var indexDescriptor in typeDef.Indexes) {
@@ -42,7 +42,7 @@ private void BuildClassTableIndexes(TypeInfo type)
4242
}
4343

4444
// Building primary index for non root entities
45-
var parent = type.GetAncestor();
45+
var parent = type.Ancestor;
4646
if (parent != null) {
4747
var parentPrimaryIndex = parent.Indexes.FindFirst(IndexAttributes.Primary | IndexAttributes.Real);
4848
var inheritedIndex = BuildInheritedIndex(type, parentPrimaryIndex, false);
@@ -80,14 +80,14 @@ private void BuildClassTableIndexes(TypeInfo type)
8080
}
8181

8282
// Build indexes for descendants
83-
foreach (var descendant in type.GetDescendants())
83+
foreach (var descendant in type.DirectDescendants)
8484
BuildClassTableIndexes(descendant);
8585

8686
// Import inherited indexes
8787
var primaryIndex = type.Indexes.FindFirst(IndexAttributes.Primary | IndexAttributes.Real);
8888
if (untypedIndexes.Contains(primaryIndex) && primaryIndex.ReflectedType == root)
8989
primaryIndex = type.Indexes.Single(i => i.DeclaringIndex == primaryIndex.DeclaringIndex && i.IsTyped);
90-
var filterByTypes = type.GetDescendants(true).Append(type).ToList();
90+
var filterByTypes = type.AllDescendants.Append(type).ToList(type.AllDescendants.Count + 1);
9191

9292
// Build virtual primary index
9393
if (ancestors.Count > 0) {

Orm/Xtensive.Orm/Orm/Building/Builders/IndexBuilder.ClusteredIndexes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private void ChooseClusteredIndexes()
2323
var clusteredIndexesMap = new Dictionary<TypeInfo, IndexInfo>();
2424
queue.Enqueue(hierarchy.Root);
2525
while (queue.TryDequeue(out var type)) {
26-
foreach (var decendant in type.GetDescendants())
26+
foreach (var decendant in type.DirectDescendants)
2727
queue.Enqueue(decendant);
2828
var clusteredIndexes = type.Indexes
2929
.Where(index => index.IsClustered && index.IsSecondary)
@@ -108,7 +108,7 @@ private static IndexInfo ChooseClusteredIndexConcreteTable(TypeInfo type, List<I
108108

109109
// We need to choose index that is clustered in parent type.
110110

111-
var parentClusteredIndex = clusteredIndexesMap[type.GetAncestor()];
111+
var parentClusteredIndex = clusteredIndexesMap[type.Ancestor];
112112
if (parentClusteredIndex == null)
113113
throw Exceptions.InternalError("inheritedIndexes is not empty, but parentClusteredIndex is not specified", BuildLog.Instance);
114114

Orm/Xtensive.Orm/Orm/Building/Builders/IndexBuilder.ConcreteTable.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private void BuildConcreteTableIndexes(TypeInfo type)
4141
}
4242

4343
// Building primary index for non root entities
44-
var parent = type.GetAncestor();
44+
var parent = type.Ancestor;
4545
if (parent != null) {
4646
var parentPrimaryIndex = parent.Indexes.FindFirst(IndexAttributes.Primary | IndexAttributes.Real);
4747
var inheritedIndex = BuildInheritedIndex(type, parentPrimaryIndex, type.IsAbstract);
@@ -53,7 +53,7 @@ private void BuildConcreteTableIndexes(TypeInfo type)
5353
}
5454

5555
// Building inherited from interfaces indexes
56-
foreach (var @interface in type.GetInterfaces(true)) {
56+
foreach (var @interface in type.AllInterfaces) {
5757
foreach (var parentIndex in @interface.Indexes.Find(IndexAttributes.Primary, MatchType.None)) {
5858
if (parentIndex.DeclaringIndex != parentIndex)
5959
continue;
@@ -75,11 +75,11 @@ private void BuildConcreteTableIndexes(TypeInfo type)
7575
}
7676

7777
// Build indexes for descendants
78-
foreach (var descendant in type.GetDescendants())
78+
foreach (var descendant in type.DirectDescendants)
7979
BuildConcreteTableIndexes(descendant);
8080

81-
var ancestors = type.GetAncestors().ToList();
82-
var descendants = type.GetDescendants(true).ToList();
81+
var ancestors = type.Ancestors;
82+
var descendants = type.AllDescendants;
8383

8484
// Build primary virtual union index
8585
if (descendants.Count > 0) {

Orm/Xtensive.Orm/Orm/Building/Builders/IndexBuilder.FullText.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private void BuildFullTextIndexesClassTable(TypeInfo root, IEnumerable<FullTextI
4444
var indexesToDefine = hierarchyIndexes.ToList();
4545
if (indexesToDefine.Any(fti => fti.Type.UnderlyingType != root.UnderlyingType) || indexesToDefine.Count > 1)
4646
throw new DomainBuilderException(string.Format(Strings.ExUnableToBuildFulltextIndexesForHierarchyWithInheritanceSchemaClassTable, root.Name));
47-
var descendants = root.GetDescendants(true).Append(root);
47+
var descendants = root.AllDescendants.Append(root);
4848
var indexDef = indexesToDefine[0];
4949
var primaryIndex = root.Indexes.Single(i => i.IsPrimary && !i.IsVirtual);
5050
var name = context.NameBuilder.BuildFullTextIndexName(root);
@@ -68,7 +68,7 @@ private void BuildFullTextIndexesSingleTable(TypeInfo root, IEnumerable<FullText
6868
foreach (var fullTextIndexDef in hierarchyIndexes) {
6969
var type = model.Types[fullTextIndexDef.Type.UnderlyingType];
7070
types.Add(type);
71-
foreach (var descendant in type.GetDescendants(true))
71+
foreach (var descendant in type.AllDescendants)
7272
types.Add(descendant);
7373
foreach (var fullTextFieldDef in fullTextIndexDef.Fields) {
7474
var fullTextColumn = GetFullTextColumn(type, fullTextFieldDef);
@@ -120,7 +120,7 @@ private Dictionary<TypeInfo, List<FullTextIndexDef>> GatherFullTextIndexDefinito
120120
{
121121
var model = context.Model;
122122
var processQueue = new Queue<TypeInfo>();
123-
foreach (var type in root.GetDescendants())
123+
foreach (var type in root.DirectDescendants)
124124
processQueue.Enqueue(type);
125125

126126
var indexDefs = hierarchyIndexes.ToDictionary(
@@ -131,7 +131,7 @@ private Dictionary<TypeInfo, List<FullTextIndexDef>> GatherFullTextIndexDefinito
131131
List<FullTextIndexDef> indexes;
132132
List<FullTextIndexDef> parentIndexes;
133133
var typeHasIndexDef = indexDefs.TryGetValue(type, out indexes);
134-
if (indexDefs.TryGetValue(type.GetAncestor(), out parentIndexes)) {
134+
if (indexDefs.TryGetValue(type.Ancestor, out parentIndexes)) {
135135
if (typeHasIndexDef)
136136
indexes.AddRange(parentIndexes);
137137
else {
@@ -141,7 +141,7 @@ private Dictionary<TypeInfo, List<FullTextIndexDef>> GatherFullTextIndexDefinito
141141
}
142142
}
143143
if (typeHasIndexDef)
144-
foreach (var descendant in type.GetDescendants())
144+
foreach (var descendant in type.DirectDescendants)
145145
processQueue.Enqueue(descendant);
146146
}
147147
return indexDefs;

0 commit comments

Comments
 (0)