Skip to content

Commit 4c6da72

Browse files
committed
After-merge changes
1 parent b7673bb commit 4c6da72

File tree

6 files changed

+151
-95
lines changed

6 files changed

+151
-95
lines changed

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

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313

1414
namespace Xtensive.Orm.Building.Builders
1515
{
16-
partial class IndexBuilder
16+
internal partial class IndexBuilder
1717
{
1818
private void BuildClassTableIndexes(TypeInfo type)
1919
{
20-
if (type.Indexes.Count > 0)
21-
return;
22-
if (type.IsStructure)
20+
if (type.Indexes.Count > 0 || type.IsStructure) {
2321
return;
22+
}
2423

2524
var root = type.Hierarchy.Root;
2625
var typeDef = context.ModelDef.Types[type.UnderlyingType];
@@ -33,8 +32,10 @@ private void BuildClassTableIndexes(TypeInfo type)
3332
var hasInheritedFields = indexDescriptor.KeyFields
3433
.Select(kvp => type.Fields[kvp.Key])
3534
.Any(f => f.IsInherited);
36-
if (hasInheritedFields)
35+
if (hasInheritedFields) {
3736
continue;
37+
}
38+
3839
var declaredIndex = BuildIndex(type, indexDescriptor, false);
3940

4041
type.Indexes.Add(declaredIndex);
@@ -53,16 +54,21 @@ private void BuildClassTableIndexes(TypeInfo type)
5354

5455
// Building inherited from interfaces indexes
5556
foreach (var @interface in interfaces) {
56-
foreach (var interfaceIndex in @interface.Indexes.Find(IndexAttributes.Primary, MatchType.None)) {
57+
foreach (var interfaceIndex in @interface.Indexes.Find(IndexAttributes.Primary, MatchType.None).ToChainedBuffer()) {
5758
if (interfaceIndex.DeclaringIndex != interfaceIndex &&
5859
parent != null &&
59-
parent.Indexes.Any(i => i.DeclaringIndex == interfaceIndex))
60+
parent.Indexes.Any(i => i.DeclaringIndex == interfaceIndex)) {
6061
continue;
61-
if (type.Indexes.Any(i => i.DeclaringIndex == interfaceIndex))
62+
}
63+
64+
if (type.Indexes.Any(i => i.DeclaringIndex == interfaceIndex)) {
6265
continue;
66+
}
67+
6368
var index = BuildInheritedIndex(type, interfaceIndex, false);
64-
if (IndexBuiltOverInheritedFields(index))
69+
if (IndexBuiltOverInheritedFields(index)) {
6570
BuildLog.Warning(string.Format(Strings.ExUnableToBuildIndexXBecauseItWasBuiltOverInheritedFields, index.Name));
71+
}
6672
else {
6773
type.Indexes.Add(index);
6874
context.Model.RealIndexes.Add(index);
@@ -71,37 +77,44 @@ private void BuildClassTableIndexes(TypeInfo type)
7177
}
7278

7379
// Build typed indexes
74-
if (type == root)
75-
foreach (var realIndex in type.Indexes.Find(IndexAttributes.Real)) {
76-
if (!untypedIndexes.Contains(realIndex))
80+
if (type == root) {
81+
foreach (var realIndex in type.Indexes.Find(IndexAttributes.Real).ToChainedBuffer()) {
82+
if (!untypedIndexes.Contains(realIndex)) {
7783
continue;
84+
}
7885
var typedIndex = BuildTypedIndex(type, realIndex);
7986
type.Indexes.Add(typedIndex);
8087
}
88+
}
8189

8290
// Build indexes for descendants
83-
foreach (var descendant in type.DirectDescendants)
91+
foreach (var descendant in type.DirectDescendants) {
8492
BuildClassTableIndexes(descendant);
93+
}
8594

8695
// Import inherited indexes
8796
var primaryIndex = type.Indexes.FindFirst(IndexAttributes.Primary | IndexAttributes.Real);
88-
if (untypedIndexes.Contains(primaryIndex) && primaryIndex.ReflectedType == root)
97+
if (untypedIndexes.Contains(primaryIndex) && primaryIndex.ReflectedType == root) {
8998
primaryIndex = type.Indexes.Single(i => i.DeclaringIndex == primaryIndex.DeclaringIndex && i.IsTyped);
99+
}
90100
var filterByTypes = type.AllDescendants.Append(type).ToList(type.AllDescendants.Count + 1);
91101

92102
// Build virtual primary index
93103
if (ancestors.Count > 0) {
94104
var baseIndexes = new Stack<IndexInfo>();
95105
foreach (var ancestor in ancestors.Where(t => t.Fields.Any(f => !f.IsPrimaryKey && !f.IsTypeId && f.IsDeclared))) {
96106
var ancestorIndex = ancestor.Indexes.Single(i => i.IsPrimary && !i.IsVirtual);
97-
if (untypedIndexes.Contains(ancestorIndex) && ancestorIndex.ReflectedType == root)
107+
if (untypedIndexes.Contains(ancestorIndex) && ancestorIndex.ReflectedType == root) {
98108
ancestorIndex = ancestor.Indexes.Single(i => i.DeclaringIndex == ancestorIndex.DeclaringIndex && i.IsTyped);
99-
if (ancestorIndex.ValueColumns.Count > 0)
109+
}
110+
if (ancestorIndex.ValueColumns.Count > 0) {
100111
baseIndexes.Push(ancestorIndex);
112+
}
101113
}
102114
if (baseIndexes.Count > 0) {
103-
if (primaryIndex.ValueColumns.Count > 0 && type.Fields.Any(f => !f.IsPrimaryKey && !f.IsTypeId && f.IsDeclared))
115+
if (primaryIndex.ValueColumns.Count > 0 && type.Fields.Any(f => !f.IsPrimaryKey && !f.IsTypeId && f.IsDeclared)) {
104116
baseIndexes.Push(primaryIndex);
117+
}
105118
else {
106119
var ancestorIndex = baseIndexes.Pop();
107120
var filteredIndex = BuildFilterIndex(type, ancestorIndex, filterByTypes);
@@ -115,9 +128,15 @@ private void BuildClassTableIndexes(TypeInfo type)
115128
}
116129

117130
// Build virtual secondary index
118-
foreach (var ancestorIndex in ancestors.SelectMany(ancestor => ancestor.Indexes.Find(IndexAttributes.Primary | IndexAttributes.Virtual, MatchType.None))) {
119-
if (ancestorIndex.DeclaringIndex != ancestorIndex)
131+
var primaryOrVirtualIndexes = ancestors
132+
.SelectMany(
133+
ancestor => ancestor.Indexes.Find(IndexAttributes.Primary | IndexAttributes.Virtual, MatchType.None).ToChainedBuffer());
134+
135+
foreach (var ancestorIndex in primaryOrVirtualIndexes) {
136+
if (ancestorIndex.DeclaringIndex != ancestorIndex) {
120137
continue;
138+
}
139+
121140
var ancestorType = ancestorIndex.ReflectedType;
122141
var indexToFilter = untypedIndexes.Contains(ancestorIndex) && ancestorIndex.ReflectedType == root
123142
? ancestorType.Indexes.Single(i => i.DeclaringIndex == ancestorIndex.DeclaringIndex && i.IsTyped)
@@ -127,7 +146,7 @@ private void BuildClassTableIndexes(TypeInfo type)
127146
}
128147
}
129148

130-
static bool IndexBuiltOverInheritedFields(IndexInfo index)
149+
private static bool IndexBuiltOverInheritedFields(IndexInfo index)
131150
{
132151
if (index.IsVirtual)
133152
return false;

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

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111

1212
namespace Xtensive.Orm.Building.Builders
1313
{
14-
partial class IndexBuilder
14+
internal partial class IndexBuilder
1515
{
1616
private void BuildConcreteTableIndexes(TypeInfo type)
1717
{
18-
if (type.Indexes.Count > 0)
19-
return;
20-
if (type.IsStructure)
18+
if (type.Indexes.Count > 0 || type.IsStructure) {
2119
return;
20+
}
2221

2322
var typeDef = context.ModelDef.Types[type.UnderlyingType];
2423
var root = type.Hierarchy.Root;
@@ -31,13 +30,15 @@ private void BuildConcreteTableIndexes(TypeInfo type)
3130
// and if they have some indexes then IndexDef.IsInherited of them will be true and it's truth actually,
3231
// but fields inherited from removed entities will have FieldInfo.IsInherited = false.
3332
// So, if we check only IndexDef.IsInherited then some indexes will be ignored.
34-
if (indexDescriptor.IsInherited && indexDescriptor.KeyFields.Select(kf=> type.Fields[kf.Key]).Any(f=>f.IsInherited))
33+
if (indexDescriptor.IsInherited && indexDescriptor.KeyFields.Select(kf=> type.Fields[kf.Key]).Any(f=>f.IsInherited)) {
3534
continue;
36-
var declaredIndex = BuildIndex(type, indexDescriptor, type.IsAbstract);
35+
}
3736

37+
var declaredIndex = BuildIndex(type, indexDescriptor, type.IsAbstract);
3838
type.Indexes.Add(declaredIndex);
39-
if (!declaredIndex.IsAbstract)
39+
if (!declaredIndex.IsAbstract) {
4040
context.Model.RealIndexes.Add(declaredIndex);
41+
}
4142
}
4243

4344
// Building primary index for non root entities
@@ -48,28 +49,35 @@ private void BuildConcreteTableIndexes(TypeInfo type)
4849

4950
// Registering built primary index
5051
type.Indexes.Add(inheritedIndex);
51-
if (!inheritedIndex.IsAbstract)
52+
if (!inheritedIndex.IsAbstract) {
5253
context.Model.RealIndexes.Add(inheritedIndex);
54+
}
5355
}
5456

5557
// Building inherited from interfaces indexes
5658
foreach (var @interface in type.AllInterfaces) {
57-
foreach (var parentIndex in @interface.Indexes.Find(IndexAttributes.Primary, MatchType.None)) {
58-
if (parentIndex.DeclaringIndex != parentIndex)
59+
foreach (var parentIndex in @interface.Indexes.Find(IndexAttributes.Primary, MatchType.None).ToChainedBuffer()) {
60+
if (parentIndex.DeclaringIndex != parentIndex) {
5961
continue;
62+
}
63+
6064
var index = BuildInheritedIndex(type, parentIndex, type.IsAbstract);
61-
if ((parent != null && parent.Indexes.Contains(index.Name)) || type.Indexes.Contains(index.Name))
65+
if ((parent != null && parent.Indexes.Contains(index.Name)) || type.Indexes.Contains(index.Name)) {
6266
continue;
67+
}
68+
6369
type.Indexes.Add(index);
64-
if (!index.IsAbstract)
70+
if (!index.IsAbstract) {
6571
context.Model.RealIndexes.Add(index);
72+
}
6673
}
6774
}
6875

6976
// Build typed indexes
70-
foreach (var realIndex in type.Indexes.Find(IndexAttributes.Real)) {
71-
if (!untypedIndexes.Contains(realIndex))
77+
foreach (var realIndex in type.Indexes.Find(IndexAttributes.Real).ToChainedBuffer()) {
78+
if (!untypedIndexes.Contains(realIndex)) {
7279
continue;
80+
}
7381
var typedIndex = BuildTypedIndex(type, realIndex);
7482
type.Indexes.Add(typedIndex);
7583
}
@@ -84,7 +92,7 @@ private void BuildConcreteTableIndexes(TypeInfo type)
8492

8593
// Build primary virtual union index
8694
if (descendants.Count > 0) {
87-
var indexesToUnion = new List<IndexInfo>(){type.Indexes.PrimaryIndex};
95+
var indexesToUnion = new List<IndexInfo>() { type.Indexes.PrimaryIndex };
8896
foreach (var index in descendants.Select(t => t.Indexes.PrimaryIndex)) {
8997
var indexView = BuildViewIndex(type, index);
9098
indexesToUnion.Add(indexView);
@@ -95,34 +103,45 @@ private void BuildConcreteTableIndexes(TypeInfo type)
95103
}
96104

97105
// Build inherited secondary indexes
98-
foreach (var ancestorIndex in ancestors.SelectMany(ancestor => ancestor.Indexes.Find(IndexAttributes.Primary | IndexAttributes.Virtual, MatchType.None))) {
99-
if (ancestorIndex.DeclaringIndex != ancestorIndex)
106+
var primaryOrVirtualIndexes = ancestors
107+
.SelectMany(
108+
ancestor => ancestor.Indexes.Find(IndexAttributes.Primary | IndexAttributes.Virtual, MatchType.None).ToChainedBuffer());
109+
110+
foreach (var ancestorIndex in primaryOrVirtualIndexes) {
111+
if (ancestorIndex.DeclaringIndex != ancestorIndex) {
100112
continue;
113+
}
114+
101115
var secondaryIndex = BuildInheritedIndex(type, ancestorIndex, type.IsAbstract);
102116
type.Indexes.Add(secondaryIndex);
103-
if (!secondaryIndex.IsAbstract)
117+
if (!secondaryIndex.IsAbstract) {
104118
context.Model.RealIndexes.Add(secondaryIndex);
119+
}
105120
// Build typed index for secondary one
106-
if (!untypedIndexes.Contains(secondaryIndex))
121+
if (!untypedIndexes.Contains(secondaryIndex)) {
107122
continue;
123+
}
124+
108125
var typedIndex = BuildTypedIndex(type, secondaryIndex);
109126
type.Indexes.Add(typedIndex);
110127
}
111128

112129
// Build virtual secondary indexes
113-
if (descendants.Count > 0)
130+
if (descendants.Count > 0) {
114131
foreach (var index in type.Indexes.Where(i => !i.IsPrimary && !i.IsVirtual).ToList()) {
115132
var isUntyped = untypedIndexes.Contains(index);
116133
var indexToUnion = isUntyped
117134
? type.Indexes.Single(i => i.DeclaringIndex == index.DeclaringIndex && i.IsTyped)
118135
: index;
119136

120-
var indexesToUnion = new List<IndexInfo>() {indexToUnion};
121-
indexesToUnion.AddRange(descendants
122-
.Select(t => t.Indexes.Single(i => i.DeclaringIndex == index.DeclaringIndex && (isUntyped ? i.IsTyped : !i.IsVirtual))));
137+
var indexesToUnion = descendants
138+
.Select(t => t.Indexes.Single(i => i.DeclaringIndex == index.DeclaringIndex && (isUntyped ? i.IsTyped : !i.IsVirtual)))
139+
.Prepend(indexToUnion);
140+
123141
var virtualSecondaryIndex = BuildUnionIndex(type, indexesToUnion);
124142
type.Indexes.Add(virtualSecondaryIndex);
125143
}
144+
}
126145
}
127146
}
128147
}

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

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212

1313
namespace Xtensive.Orm.Building.Builders
1414
{
15-
partial class IndexBuilder
15+
internal partial class IndexBuilder
1616
{
1717
private void BuildSingleTableIndexes(TypeInfo type)
1818
{
19-
if (type.Indexes.Count > 0)
20-
return;
21-
if (type.IsStructure)
19+
if (type.Indexes.Count > 0 || type.IsStructure) {
2220
return;
21+
}
2322

2423
var typeDef = context.ModelDef.Types[type.UnderlyingType];
2524
var root = type.Hierarchy.Root;
@@ -33,8 +32,10 @@ private void BuildSingleTableIndexes(TypeInfo type)
3332
// and if they have some indexes then IndexDef.IsInherited of them will be true and it's truth actually,
3433
// but fields inherited from removed entities will have FieldInfo.IsInherited = false.
3534
// So, if we check only IndexDef.IsInherited then some indexes will be ignored.
36-
if (indexDescriptor.IsInherited && indexDescriptor.KeyFields.Select(kf => type.Fields[kf.Key]).Any(f => f.IsInherited))
35+
if (indexDescriptor.IsInherited && indexDescriptor.KeyFields.Select(kf => type.Fields[kf.Key]).Any(f => f.IsInherited)) {
3736
continue;
37+
}
38+
3839
var declaredIndex = BuildIndex(type, indexDescriptor, false);
3940
root.Indexes.Add(declaredIndex);
4041
context.Model.RealIndexes.Add(declaredIndex);
@@ -43,26 +44,31 @@ private void BuildSingleTableIndexes(TypeInfo type)
4344
var parent = type.Ancestor;
4445
// Building inherited from interfaces indexes
4546
foreach (var @interface in type.DirectInterfaces) {
46-
foreach (var interfaceIndex in @interface.Indexes.Find(IndexAttributes.Primary, MatchType.None)) {
47-
if (root.Indexes.Any(i => i.DeclaringIndex == interfaceIndex.DeclaringIndex && i.ReflectedType == type))
47+
foreach (var interfaceIndex in @interface.Indexes.Find(IndexAttributes.Primary, MatchType.None).ToChainedBuffer()) {
48+
if (root.Indexes.Any(i => i.DeclaringIndex == interfaceIndex.DeclaringIndex && i.ReflectedType == type)) {
4849
continue;
50+
}
51+
4952
var index = BuildInheritedIndex(type, interfaceIndex, false);
5053
root.Indexes.Add(index);
5154
context.Model.RealIndexes.Add(index);
5255
}
5356
}
5457

5558
var types = type.Ancestors.ToHashSet();
56-
types.Add(type);
59+
_ = types.Add(type);
5760

5861
// Build typed indexes
59-
foreach (var realIndex in root.Indexes.Find(IndexAttributes.Real)) {
60-
if (!types.Contains(realIndex.ReflectedType))
62+
foreach (var realIndex in root.Indexes.Find(IndexAttributes.Real).ToChainedBuffer()) {
63+
if (!types.Contains(realIndex.ReflectedType)) {
6164
continue;
62-
if (!untypedIndexes.Contains(realIndex))
65+
}
66+
if (!untypedIndexes.Contains(realIndex)) {
6367
continue;
64-
if (root.Indexes.Any(i => i.DeclaringIndex == realIndex.DeclaringIndex && i.ReflectedType == type && i.IsTyped))
68+
}
69+
if (root.Indexes.Any(i => i.DeclaringIndex == realIndex.DeclaringIndex && i.ReflectedType == type && i.IsTyped)) {
6570
continue;
71+
}
6672
var typedIndex = BuildTypedIndex(type, realIndex);
6773
root.Indexes.Add(typedIndex);
6874
}
@@ -72,34 +78,39 @@ private void BuildSingleTableIndexes(TypeInfo type)
7278
BuildSingleTableIndexes(descendant);
7379
}
7480

75-
if (type == root) return;
81+
if (type == root) {
82+
return;
83+
}
7684
var descendants = type.AllDescendants;
7785

78-
var primaryIndexFilterTypes = new List<TypeInfo>();
86+
var primaryIndexFilterTypes = new List<TypeInfo>(type.IsAbstract ? descendants.Count : descendants.Count + 1);
7987
if (!type.IsAbstract)
8088
primaryIndexFilterTypes.Add(type);
8189
primaryIndexFilterTypes.AddRange(descendants);
82-
90+
8391
// Import inherited indexes
8492
var ancestorIndexes = root.Indexes
8593
.Where(i => types.Contains(i.ReflectedType) && !i.IsTyped)
8694
.Reverse()
8795
.Select(i => untypedIndexes.Contains(i)
8896
? root.Indexes.Single(index => index.DeclaringIndex == i.DeclaringIndex && index.ReflectedType == type && index.IsTyped)
8997
: i)
90-
.ToList();
98+
.ToChainedBuffer();
9199
foreach (var ancestorIndex in ancestorIndexes) {
92100
if (type.Indexes.Any(i =>
93101
i.DeclaringIndex == ancestorIndex.DeclaringIndex &&
94102
i.ReflectedType == type &&
95-
i.IsVirtual))
103+
i.IsVirtual)) {
96104
continue;
105+
}
106+
97107
if (ancestorIndex.DeclaringType.IsInterface) {
98108
var filteredDescendants = descendants
99109
.Where(t => !t.IsAbstract && !t.DirectInterfaces.Contains(ancestorIndex.DeclaringType));
100-
var filterByTypes = new List<TypeInfo>();
101-
if (!type.IsAbstract)
110+
var filterByTypes = new List<TypeInfo>(2);
111+
if (!type.IsAbstract) {
102112
filterByTypes.Add(type);
113+
}
103114
filterByTypes.AddRange(filterByTypes);
104115
var filterIndex = BuildFilterIndex(type, ancestorIndex, filterByTypes);
105116
var indexView = BuildViewIndex(type, filterIndex);

0 commit comments

Comments
 (0)