1111
1212namespace 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}
0 commit comments