Skip to content

Commit 2f67dc6

Browse files
committed
IndexInfo ctor: Use IEnumerable<IndexInfo> instead of instance + array
Prevents usage of temp collections when Union or Join indexes are built
1 parent 7e351e8 commit 2f67dc6

4 files changed

Lines changed: 47 additions & 21 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private void BuildClassTableIndexes(TypeInfo type)
120120
var filteredIndex = BuildFilterIndex(type, ancestorIndex, filterByTypes);
121121
baseIndexes.Push(filteredIndex);
122122
}
123-
var virtualPrimaryIndex = baseIndexes.Count == 1
123+
var virtualPrimaryIndex = baseIndexes.Count == 1
124124
? baseIndexes.Pop()
125125
: BuildJoinIndex(type, baseIndexes);
126126
type.Indexes.Add(virtualPrimaryIndex);

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,9 @@ private void BuildConcreteTableIndexes(TypeInfo type)
9292

9393
// Build primary virtual union index
9494
if (descendants.Count > 0) {
95-
var indexesToUnion = new List<IndexInfo>(descendants.Count + 1) { type.Indexes.PrimaryIndex };
96-
foreach (var index in descendants.Select(static t => t.Indexes.PrimaryIndex)) {
97-
var indexView = BuildViewIndex(type, index);
98-
indexesToUnion.Add(indexView);
99-
}
95+
var indexesToUnion = descendants.Select(t => BuildViewIndex(type, t.Indexes.PrimaryIndex))
96+
.Prepend(type.Indexes.PrimaryIndex);
97+
10098
var virtualPrimaryIndex = BuildUnionIndex(type, indexesToUnion);
10199
type.Indexes.Add(virtualPrimaryIndex);
102100

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,10 @@ private void BuildInterfaceIndexes()
165165
var filterIndex = BuildFilterIndex(implementor,
166166
typedIndex ?? typeIndexes.Dequeue(),
167167
NonAbstractTypeWithDescendants(implementor, hierarchyImplementors));
168-
var indexesToJoin = new List<IndexInfo>(1 + typeIndexes.Count);
169-
indexesToJoin.Add(filterIndex);
170-
indexesToJoin.AddRange(typeIndexes);
171168

172-
var indexToApplyView = indexesToJoin.Count > 1
173-
? BuildJoinIndex(implementor, indexesToJoin)
174-
: indexesToJoin[0];
169+
var indexToApplyView = typeIndexes.Count > 0
170+
? BuildJoinIndex(implementor, typeIndexes.Prepend(filterIndex))
171+
: filterIndex;
175172
var indexView = BuildViewIndex(@interface, indexToApplyView);
176173
underlyingIndex.UnderlyingIndexes.Add(indexView);
177174
}
@@ -496,7 +493,7 @@ private IndexInfo BuildTypedIndex(TypeInfo reflectedType, IndexInfo realIndex)
496493
var attributes = realIndex.Attributes
497494
& (IndexAttributes.Primary | IndexAttributes.Secondary | IndexAttributes.Unique | IndexAttributes.Abstract)
498495
| IndexAttributes.Typed | IndexAttributes.Virtual;
499-
var result = new IndexInfo(reflectedType, attributes, realIndex, Array.Empty<IndexInfo>());
496+
var result = new IndexInfo(reflectedType, attributes, realIndex, addAncestorToUnderlyings: true);
500497

501498
// Adding key columns
502499
foreach (KeyValuePair<ColumnInfo, Direction> pair in realIndex.KeyColumns) {
@@ -531,7 +528,7 @@ private IndexInfo BuildFilterIndex(TypeInfo reflectedType, IndexInfo indexToFilt
531528
var attributes = indexToFilter.Attributes
532529
& (IndexAttributes.Primary | IndexAttributes.Secondary | IndexAttributes.Unique | IndexAttributes.Abstract)
533530
| IndexAttributes.Filtered | IndexAttributes.Virtual;
534-
var result = new IndexInfo(reflectedType, attributes, indexToFilter, Array.Empty<IndexInfo>()) {
531+
var result = new IndexInfo(reflectedType, attributes, indexToFilter, addAncestorToUnderlyings: true) {
535532
FilterByTypes = filterByTypes
536533
};
537534

@@ -560,11 +557,10 @@ private IndexInfo BuildJoinIndex(TypeInfo reflectedType, IEnumerable<IndexInfo>
560557
{
561558
var nameBuilder = context.NameBuilder;
562559
var firstIndex = indexesToJoin.First();
563-
var otherIndexes = indexesToJoin.Skip(1).ToArray();
564560
var attributes = firstIndex.Attributes
565561
& (IndexAttributes.Primary | IndexAttributes.Secondary | IndexAttributes.Unique)
566562
| IndexAttributes.Join | IndexAttributes.Virtual;
567-
var result = new IndexInfo(reflectedType, attributes, firstIndex, otherIndexes);
563+
var result = new IndexInfo(reflectedType, attributes, indexesToJoin);
568564

569565
// Adding key columns
570566
foreach (KeyValuePair<ColumnInfo, Direction> pair in firstIndex.KeyColumns) {
@@ -645,11 +641,10 @@ private IndexInfo BuildUnionIndex(TypeInfo reflectedType, IEnumerable<IndexInfo>
645641
{
646642
var nameBuilder = context.NameBuilder;
647643
var firstIndex = indexesToUnion.First();
648-
var otherIndexes = indexesToUnion.Skip(1).ToArray();
649644
var attributes = firstIndex.Attributes
650645
& (IndexAttributes.Primary | IndexAttributes.Secondary | IndexAttributes.Unique)
651646
| IndexAttributes.Union | IndexAttributes.Virtual;
652-
var result = new IndexInfo(reflectedType, attributes, firstIndex, otherIndexes);
647+
var result = new IndexInfo(reflectedType, attributes, indexesToUnion);
653648

654649
// Adding key columns
655650
foreach (KeyValuePair<ColumnInfo, Direction> pair in firstIndex.KeyColumns) {
@@ -682,7 +677,7 @@ private IndexInfo BuildViewIndex(TypeInfo reflectedType, IndexInfo indexToApplyV
682677
var attributes = indexToApplyView.Attributes
683678
& (IndexAttributes.Primary | IndexAttributes.Secondary | IndexAttributes.Unique | IndexAttributes.Abstract)
684679
| IndexAttributes.View | IndexAttributes.Virtual;
685-
var result = new IndexInfo(reflectedType, attributes, indexToApplyView, Array.Empty<IndexInfo>());
680+
var result = new IndexInfo(reflectedType, attributes, indexToApplyView, addAncestorToUnderlyings: true);
686681

687682
// Adding key columns
688683
foreach (KeyValuePair<ColumnInfo, Direction> pair in indexToApplyView.KeyColumns) {

Orm/Xtensive.Orm/Orm/Model/IndexInfo.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,10 @@ public IndexInfo(TypeInfo declaringType, IndexAttributes indexAttributes)
423423
/// Initializes a new instance of this class.
424424
/// </summary>
425425
/// <param name="reflectedType">Reflected type.</param>
426-
/// <param name="ancestorIndex">The ancestors index.</param>
427426
/// <param name="indexAttributes"><see cref="IndexAttributes"/> attributes for this instance.</param>
428-
public IndexInfo(TypeInfo reflectedType, IndexAttributes indexAttributes, IndexInfo ancestorIndex)
427+
/// <param name="ancestorIndex">The ancestors index.</param>
428+
/// <param name="addAncestorToUnderlyings"><see langword="true"/> if <paramref name="ancestorIndex"/> should also be treated as underlying index.</param>
429+
public IndexInfo(TypeInfo reflectedType, IndexAttributes indexAttributes, IndexInfo ancestorIndex, bool addAncestorToUnderlyings = false)
429430
: this()
430431
{
431432
declaringType = ancestorIndex.DeclaringType;
@@ -436,6 +437,10 @@ public IndexInfo(TypeInfo reflectedType, IndexAttributes indexAttributes, IndexI
436437
filterExpression = ancestorIndex.FilterExpression;
437438
declaringIndex = ancestorIndex.DeclaringIndex;
438439
shortName = ancestorIndex.ShortName;
440+
441+
if (addAncestorToUnderlyings) {
442+
UnderlyingIndexes.Add(ancestorIndex);
443+
}
439444
}
440445

441446
/// <summary>
@@ -445,6 +450,7 @@ public IndexInfo(TypeInfo reflectedType, IndexAttributes indexAttributes, IndexI
445450
/// <param name="indexAttributes">The index attributes.</param>
446451
/// <param name="baseIndex">Base index.</param>
447452
/// <param name="baseIndexes">The base indexes.</param>
453+
[Obsolete("Use either IndexInfo(reflectedType, indexAttributes, ancestorIndex, true) in case of one base index or varaint with sequence of indexes")]
448454
public IndexInfo(TypeInfo reflectedType, IndexAttributes indexAttributes, IndexInfo baseIndex, params IndexInfo[] baseIndexes)
449455
: this()
450456
{
@@ -462,5 +468,32 @@ public IndexInfo(TypeInfo reflectedType, IndexAttributes indexAttributes, IndexI
462468
foreach (IndexInfo info in baseIndexes)
463469
UnderlyingIndexes.Add(info);
464470
}
471+
472+
/// <summary>
473+
/// Initializes a new instance of this class.
474+
/// </summary>
475+
/// <param name="reflectedType">Reflected type.</param>
476+
/// <param name="indexAttributes">The index attributes.</param>
477+
/// <param name="baseIndexes">The base indexes, first of which will be used as source index properties.</param>
478+
public IndexInfo(TypeInfo reflectedType, IndexAttributes indexAttributes, IEnumerable<IndexInfo> baseIndexes)
479+
: this()
480+
{
481+
this.reflectedType = reflectedType;
482+
attributes = indexAttributes;
483+
484+
foreach (var info in baseIndexes) {
485+
if (declaringType is null) {
486+
declaringType = info.DeclaringType;
487+
fillFactor = info.FillFactor;
488+
filterExpression = info.FilterExpression;
489+
declaringIndex = info.DeclaringIndex;
490+
shortName = info.ShortName;
491+
}
492+
UnderlyingIndexes.Add(info);
493+
}
494+
if (UnderlyingIndexes.Count == 0) {
495+
throw new ArgumentException(Strings.ExSequenceContainsNoElements, nameof(baseIndexes));
496+
}
497+
}
465498
}
466499
}

0 commit comments

Comments
 (0)