Skip to content

Commit 4284628

Browse files
committed
IndexBuilder: Static funcs and actions in Linq
1 parent e81a718 commit 4284628

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private void BuildClassTableIndexes(TypeInfo type)
3131
// Skip indef building for inherited fields
3232
var hasInheritedFields = indexDescriptor.KeyFields
3333
.Select(kvp => type.Fields[kvp.Key])
34-
.Any(f => f.IsInherited);
34+
.Any(static f => f.IsInherited);
3535
if (hasInheritedFields) {
3636
continue;
3737
}
@@ -102,8 +102,8 @@ private void BuildClassTableIndexes(TypeInfo type)
102102
// Build virtual primary index
103103
if (ancestors.Count > 0) {
104104
var baseIndexes = new Stack<IndexInfo>();
105-
foreach (var ancestor in ancestors.Where(t => t.Fields.Any(f => !f.IsPrimaryKey && !f.IsTypeId && f.IsDeclared))) {
106-
var ancestorIndex = ancestor.Indexes.Single(i => i.IsPrimary && !i.IsVirtual);
105+
foreach (var ancestor in ancestors.Where(t => t.Fields.Any(static f => !f.IsPrimaryKey && !f.IsTypeId && f.IsDeclared))) {
106+
var ancestorIndex = ancestor.Indexes.Single(static i => i.IsPrimary && !i.IsVirtual);
107107
if (untypedIndexes.Contains(ancestorIndex) && ancestorIndex.ReflectedType == root) {
108108
ancestorIndex = ancestor.Indexes.Single(i => i.DeclaringIndex == ancestorIndex.DeclaringIndex && i.IsTyped);
109109
}
@@ -112,7 +112,7 @@ private void BuildClassTableIndexes(TypeInfo type)
112112
}
113113
}
114114
if (baseIndexes.Count > 0) {
115-
if (primaryIndex.ValueColumns.Count > 0 && type.Fields.Any(f => !f.IsPrimaryKey && !f.IsTypeId && f.IsDeclared)) {
115+
if (primaryIndex.ValueColumns.Count > 0 && type.Fields.Any(static f => !f.IsPrimaryKey && !f.IsTypeId && f.IsDeclared)) {
116116
baseIndexes.Push(primaryIndex);
117117
}
118118
else {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private void BuildConcreteTableIndexes(TypeInfo type)
3030
// and if they have some indexes then IndexDef.IsInherited of them will be true and it's truth actually,
3131
// but fields inherited from removed entities will have FieldInfo.IsInherited = false.
3232
// So, if we check only IndexDef.IsInherited then some indexes will be ignored.
33-
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(static f => f.IsInherited)) {
3434
continue;
3535
}
3636

@@ -92,8 +92,8 @@ private void BuildConcreteTableIndexes(TypeInfo type)
9292

9393
// Build primary virtual union index
9494
if (descendants.Count > 0) {
95-
var indexesToUnion = new List<IndexInfo>() { type.Indexes.PrimaryIndex };
96-
foreach (var index in descendants.Select(t => t.Indexes.PrimaryIndex)) {
95+
var indexesToUnion = new List<IndexInfo>(descendants.Count + 1) { type.Indexes.PrimaryIndex };
96+
foreach (var index in descendants.Select(static t => t.Indexes.PrimaryIndex)) {
9797
var indexView = BuildViewIndex(type, index);
9898
indexesToUnion.Add(indexView);
9999
}
@@ -128,7 +128,7 @@ private void BuildConcreteTableIndexes(TypeInfo type)
128128

129129
// Build virtual secondary indexes
130130
if (descendants.Count > 0) {
131-
foreach (var index in type.Indexes.Where(i => !i.IsPrimary && !i.IsVirtual).ToList()) {
131+
foreach (var index in type.Indexes.Where(static i => !i.IsPrimary && !i.IsVirtual).ToChainedBuffer()) {
132132
var isUntyped = untypedIndexes.Contains(index);
133133
var indexToUnion = isUntyped
134134
? type.Indexes.Single(i => i.DeclaringIndex == index.DeclaringIndex && i.IsTyped)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private void BuildSingleTableIndexes(TypeInfo type)
3232
// and if they have some indexes then IndexDef.IsInherited of them will be true and it's truth actually,
3333
// but fields inherited from removed entities will have FieldInfo.IsInherited = false.
3434
// So, if we check only IndexDef.IsInherited then some indexes will be ignored.
35-
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(static f => f.IsInherited)) {
3636
continue;
3737
}
3838

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void CreateInterfaceIndexes(TypeInfo @interface, ICollection<TypeInfo> p
7373
var interfaceDef = context.ModelDef.Types[@interface.UnderlyingType];
7474

7575
// Build virtual declared interface index
76-
foreach (var indexDescriptor in interfaceDef.Indexes.Where(i => !i.IsPrimary)) {
76+
foreach (var indexDescriptor in interfaceDef.Indexes.Where(static i => !i.IsPrimary)) {
7777
var index = BuildIndex(@interface, indexDescriptor, false);
7878

7979
@interface.Indexes.Add(index);
@@ -118,9 +118,9 @@ private void BuildInterfaceIndexes()
118118
}
119119
else {
120120
var interfaceDef = context.ModelDef.Types[@interface.UnderlyingType];
121-
var indexDef = interfaceDef.Indexes.Single(i => i.IsPrimary);
121+
var indexDef = interfaceDef.Indexes.Single(static i => i.IsPrimary);
122122
var index = BuildIndex(@interface, indexDef, false);
123-
var lookup = implementors.ToLookup(t => t.Hierarchy);
123+
var lookup = implementors.ToLookup(static t => t.Hierarchy);
124124
var underlyingIndexes = new List<IndexInfo>();
125125
foreach (var hierarchy in lookup) {
126126
var underlyingIndex = BuildIndex(@interface, indexDef, false);
@@ -146,12 +146,12 @@ private void BuildInterfaceIndexes()
146146
if (untypedIndexes.Contains(typeIndex)) {
147147
if (type == hierarchy.Key.Root) {
148148
typeIndex = null;
149-
typedIndex = type.Indexes.Single(i => i.IsPrimary && i.IsTyped);
149+
typedIndex = type.Indexes.Single(static i => i.IsPrimary && i.IsTyped);
150150
}
151151
else {
152-
typeIndex = type.Indexes.Single(i => i.IsPrimary && !i.IsVirtual);
152+
typeIndex = type.Indexes.Single(static i => i.IsPrimary && !i.IsVirtual);
153153
if (typedIndex == null)
154-
typedIndex = hierarchy.Key.Root.Indexes.Single(i => i.IsPrimary && i.IsTyped);
154+
typedIndex = hierarchy.Key.Root.Indexes.Single(static i => i.IsPrimary && i.IsTyped);
155155
}
156156
}
157157
if (typeIndex != null)
@@ -194,7 +194,7 @@ private void BuildInterfaceIndexes()
194194
.Where(t => t.Hierarchy == grouping.Key && !t.IsAbstract)
195195
.ToList();
196196
var primaryIndexes = allImplementors
197-
.Select(t => (Index: t.Indexes.Single(i => i.IsPrimary && !i.IsVirtual), Type: t))
197+
.Select(t => (Index: t.Indexes.Single(static i => i.IsPrimary && !i.IsVirtual), Type: t))
198198
.Select(p => untypedIndexes.Contains(p.Index)
199199
? p.Type.Indexes.Single(i => i.IsPrimary && i.IsTyped)
200200
: p.Index)
@@ -216,9 +216,9 @@ private void BuildInterfaceIndexes()
216216
}
217217

218218
// Building secondary virtual indexes
219-
foreach (var interfaceIndex in @interface.Indexes.Where(i => i.IsVirtual && !i.IsPrimary)) {
219+
foreach (var interfaceIndex in @interface.Indexes.Where(static i => i.IsVirtual && !i.IsPrimary)) {
220220
var localIndex = interfaceIndex;
221-
var lookup = implementors.ToLookup(t => t.Hierarchy);
221+
var lookup = implementors.ToLookup(static t => t.Hierarchy);
222222
var underlyingIndexes = new List<IndexInfo>();
223223
foreach (var hierarchy in lookup) {
224224
var grouping = hierarchy;
@@ -243,7 +243,8 @@ private void BuildInterfaceIndexes()
243243
break;
244244
}
245245
case InheritanceSchema.SingleTable: {
246-
var rootIndexes = hierarchy.Key.Root.Indexes.Where(i => i.DeclaringIndex == localIndex.DeclaringIndex && implementors.Contains(i.ReflectedType) && !i.IsVirtual);
246+
var rootIndexes = hierarchy.Key.Root.Indexes
247+
.Where(i => i.DeclaringIndex == localIndex.DeclaringIndex && implementors.Contains(i.ReflectedType) && !i.IsVirtual);
247248
foreach (var rootIndex in rootIndexes) {
248249
var index = untypedIndexes.Contains(rootIndex)
249250
? hierarchy.Key.Root.Indexes.Single(i => i.DeclaringIndex == localIndex.DeclaringIndex && i.ReflectedType == rootIndex.ReflectedType && i.IsTyped)
@@ -310,7 +311,7 @@ private IndexInfo BuildIndex(TypeInfo typeInfo, IndexDef indexDef, bool buildAbs
310311
else if (typeInfo.Hierarchy.TypeDiscriminatorMap != null)
311312
skipTypeId = true;
312313
}
313-
if (typeInfo.Fields.Any(f => f.IsTypeId && f.IsPrimaryKey))
314+
if (typeInfo.Fields.Any(static f => f.IsTypeId && f.IsPrimaryKey))
314315
skipTypeId = false;
315316

316317
// Adding key columns
@@ -375,7 +376,7 @@ private IndexInfo BuildIndex(TypeInfo typeInfo, IndexDef indexDef, bool buildAbs
375376
}
376377
}
377378
else {
378-
foreach (var column in typeInfo.Columns.Where(c => c.IsPrimaryKey)) {
379+
foreach (var column in typeInfo.Columns.Where(static c => c.IsPrimaryKey)) {
379380
if (!result.KeyColumns.ContainsKey(column))
380381
result.ValueColumns.Add(column);
381382
}
@@ -414,7 +415,7 @@ private IndexInfo BuildInheritedIndex(TypeInfo reflectedType, IndexInfo ancestor
414415
else if (reflectedType.Hierarchy.TypeDiscriminatorMap != null)
415416
skipTypeId = true;
416417
}
417-
if (reflectedType.Fields.Any(f => f.IsTypeId && f.IsPrimaryKey))
418+
if (reflectedType.Fields.Any(static f => f.IsTypeId && f.IsPrimaryKey))
418419
skipTypeId = false;
419420

420421

@@ -512,12 +513,12 @@ private IndexInfo BuildTypedIndex(TypeInfo reflectedType, IndexInfo realIndex)
512513

513514
// Adding TypeId column
514515
if (realIndex.IsPrimary)
515-
result.ValueColumns.Add(reflectedType.Columns.Single(c => c.IsSystem && c.Field.IsTypeId));
516+
result.ValueColumns.Add(reflectedType.Columns.Single(static c => c.IsSystem && c.Field.IsTypeId));
516517
// Adding value columns
517518
result.ValueColumns.AddRange(realIndex.ValueColumns);
518519
// Adding TypeId column
519520
if (!realIndex.IsPrimary)
520-
result.ValueColumns.Add(reflectedType.Columns.Single(c => c.IsSystem && c.Field.IsTypeId));
521+
result.ValueColumns.Add(reflectedType.Columns.Single(static c => c.IsSystem && c.Field.IsTypeId));
521522

522523
result.Name = nameBuilder.BuildIndexName(reflectedType, result);
523524
result.Group = BuildColumnGroup(result);
@@ -581,8 +582,8 @@ private IndexInfo BuildJoinIndex(TypeInfo reflectedType, IEnumerable<IndexInfo>
581582
// Adding value columns
582583
var typeOrder = reflectedType.Ancestors
583584
.Append(reflectedType)
584-
.Select((t, i) => (Type: t, Index: i))
585-
.ToDictionary(a => a.Type, a => a.Index);
585+
.Select(static (t, i) => (Type: t, Index: i))
586+
.ToDictionary(static a => a.Type, static a => a.Index);
586587
var types = reflectedType.Ancestors.ToHashSet();
587588
types.Add(reflectedType);
588589

@@ -747,7 +748,7 @@ private IndexInfo BuildViewIndex(TypeInfo reflectedType, IndexInfo indexToApplyV
747748
columnMap.Add(keyLength + i);
748749
}
749750
var actualColumnMapping = valueColumns
750-
.Zip(columnMap, (column, sourceIndex) => (column, sourceIndex))
751+
.Zip(columnMap, static (column, sourceIndex) => (column, sourceIndex))
751752
.OrderBy(p => reflectedType.Columns.IndexOf(p.column))
752753
.ToList();
753754
valueColumns.Clear();
@@ -809,11 +810,11 @@ private ColumnGroup BuildColumnGroup(IndexInfo index)
809810
var keyColumns = index.IsPrimary
810811
? Enumerable.Range(0, index.KeyColumns.Count).ToList(index.KeyColumns.Count)
811812
: index.KeyColumns
812-
.Select(pair => pair.Key)
813+
.Select(static pair => pair.Key)
813814
.Concat(index.ValueColumns)
814-
.Select((c, i) => (c, i))
815-
.Where(arg => arg.c.IsPrimaryKey)
816-
.Select(arg => arg.i)
815+
.Select(static (c, i) => (c, i))
816+
.Where(static arg => arg.c.IsPrimaryKey)
817+
.Select(static arg => arg.i)
817818
.ToList();
818819
var columns = Enumerable.Range(0, index.KeyColumns.Count + index.ValueColumns.Count).ToList(index.KeyColumns.Count + index.ValueColumns.Count);
819820
return new ColumnGroup(reflectedType, keyColumns, columns);
@@ -822,9 +823,9 @@ private ColumnGroup BuildColumnGroup(IndexInfo index)
822823
private void CleanupTypedIndexes()
823824
{
824825

825-
foreach (var typeInfo in context.Model.Types.Where(t => t.IsEntity)) {
826-
var indexes = typeInfo.Indexes.Where(i => i.IsVirtual).ToList();
827-
var typedIndexes = indexes.Where(i => i.IsTyped);
826+
foreach (var typeInfo in context.Model.Types.Where(static t => t.IsEntity)) {
827+
var indexes = typeInfo.Indexes.Where(static i => i.IsVirtual).ToList();
828+
var typedIndexes = indexes.Where(static i => i.IsTyped);
828829
foreach (var typedIndex in typedIndexes) {
829830
bool remove = false;
830831
foreach (var index in indexes)
@@ -895,7 +896,7 @@ private void ExtractAffectedIndexes(
895896
private static void BuildAffectedIndexesForMaterializedInterface(TypeInfo typeInfo)
896897
{
897898
var primaryIndex = typeInfo.Indexes.PrimaryIndex;
898-
foreach (var descendant in typeInfo.AllDescendants.Where(t => t.IsEntity).Distinct()) {
899+
foreach (var descendant in typeInfo.AllDescendants.Where(static t => t.IsEntity).Distinct()) {
899900
descendant.AffectedIndexes.Add(primaryIndex);
900901
foreach (var indexInfo in typeInfo.Indexes.Find(IndexAttributes.Primary, MatchType.None).ToChainedBuffer()) {
901902
var descendantIndex = descendant.Indexes.Where(i => i.DeclaringIndex == indexInfo.DeclaringIndex).FirstOrDefault();
@@ -932,7 +933,7 @@ private static void ProcessAncestors(TypeInfo typeInfo, Action<TypeInfo> ancesto
932933

933934
private void BuildFiltersForPartialIndexes()
934935
{
935-
foreach (var index in context.Model.RealIndexes.Where(index => index.FilterExpression != null)) {
936+
foreach (var index in context.Model.RealIndexes.Where(static index => index.FilterExpression != null)) {
936937
PartialIndexFilterBuilder.BuildFilter(index);
937938
}
938939
}

0 commit comments

Comments
 (0)