79
79
* @author Mark Paluch
80
80
* @author Dave Perryman
81
81
* @author Stefan Tirea
82
+ * @author Julia Lee
82
83
* @since 1.5
83
84
*/
84
85
public class MongoPersistentEntityIndexResolver implements IndexResolver {
@@ -129,6 +130,7 @@ public List<IndexDefinitionHolder> resolveIndexForEntity(MongoPersistentEntity<?
129
130
indexInformation .addAll (potentiallyCreateCompoundIndexDefinitions ("" , collection , root ));
130
131
indexInformation .addAll (potentiallyCreateWildcardIndexDefinitions ("" , collection , root ));
131
132
indexInformation .addAll (potentiallyCreateTextIndexDefinition (root , collection ));
133
+ indexInformation .addAll (potentiallyCreateCompoundWildcardDefinition (root , collection ));
132
134
133
135
root .doWithProperties ((PropertyHandler <MongoPersistentProperty >) property -> this
134
136
.potentiallyAddIndexForProperty (root , property , indexInformation , new CycleGuard ()));
@@ -154,6 +156,22 @@ private void verifyWildcardIndexedProjection(MongoPersistentEntity<?> entity) {
154
156
}
155
157
}
156
158
});
159
+
160
+ if (entity .isAnnotationPresent (CompoundWildcardIndexed .class )) {
161
+ CompoundWildcardIndexed indexed = entity .getRequiredAnnotation (CompoundWildcardIndexed .class );
162
+
163
+ if (!ObjectUtils .isEmpty (indexed .wildcardFieldName ()) && !ObjectUtils .isEmpty (indexed .wildcardProjection ())) {
164
+
165
+ throw new MappingException (
166
+ String .format ("CompoundWildcardIndex.wildcardProjection is only allowed on \" $**\" ; Offending property: %s" ,
167
+ indexed .wildcardFieldName ()));
168
+ }
169
+
170
+ if (ObjectUtils .isEmpty (indexed .wildcardFieldName ()) && ObjectUtils .isEmpty (indexed .wildcardProjection ())) {
171
+
172
+ throw new MappingException (String .format ("CompoundWildcardIndex.wildcardProjection is required on \" $**\" " ));
173
+ }
174
+ }
157
175
}
158
176
159
177
private void potentiallyAddIndexForProperty (MongoPersistentEntity <?> root , MongoPersistentProperty persistentProperty ,
@@ -280,7 +298,8 @@ private List<IndexDefinitionHolder> createIndexDefinitionHolderForProperty(Strin
280
298
private List <IndexDefinitionHolder > potentiallyCreateCompoundIndexDefinitions (String dotPath , String collection ,
281
299
MongoPersistentEntity <?> entity ) {
282
300
283
- if (entity .findAnnotation (CompoundIndexes .class ) == null && entity .findAnnotation (CompoundIndex .class ) == null ) {
301
+ if ((!entity .isAnnotationPresent (CompoundIndexes .class ) && !entity .isAnnotationPresent (CompoundIndex .class ))
302
+ || entity .isAnnotationPresent (CompoundWildcardIndexed .class )) {
284
303
return Collections .emptyList ();
285
304
}
286
305
@@ -290,7 +309,8 @@ private List<IndexDefinitionHolder> potentiallyCreateCompoundIndexDefinitions(St
290
309
private List <IndexDefinitionHolder > potentiallyCreateWildcardIndexDefinitions (String dotPath , String collection ,
291
310
MongoPersistentEntity <?> entity ) {
292
311
293
- if (!entity .isAnnotationPresent (WildcardIndexed .class )) {
312
+ if (!entity .isAnnotationPresent (WildcardIndexed .class )
313
+ || entity .isAnnotationPresent (CompoundWildcardIndexed .class )) {
294
314
return Collections .emptyList ();
295
315
}
296
316
@@ -345,6 +365,19 @@ private Collection<? extends IndexDefinitionHolder> potentiallyCreateTextIndexDe
345
365
346
366
}
347
367
368
+ private Collection <? extends IndexDefinitionHolder > potentiallyCreateCompoundWildcardDefinition (
369
+ MongoPersistentEntity <?> entity , String collection ) {
370
+
371
+ if (!entity .isAnnotationPresent (CompoundWildcardIndexed .class )) {
372
+ return Collections .emptyList ();
373
+ }
374
+
375
+ CompoundWildcardIndexed compoundWildcardIndex = entity .getRequiredAnnotation (CompoundWildcardIndexed .class );
376
+ IndexDefinitionHolder compoundWildcardIndexDefinition = createCompoundWildcardIndexDefinition (collection ,
377
+ compoundWildcardIndex , entity );
378
+ return Collections .singletonList (compoundWildcardIndexDefinition );
379
+ }
380
+
348
381
private void appendTextIndexInformation (DotPath dotPath , Path path , TextIndexDefinitionBuilder indexDefinitionBuilder ,
349
382
MongoPersistentEntity <?> entity , TextIndexIncludeOptions includeOptions , CycleGuard guard ) {
350
383
@@ -483,6 +516,30 @@ protected IndexDefinitionHolder createWildcardIndexDefinition(String dotPath, St
483
516
return new IndexDefinitionHolder (dotPath , indexDefinition , collection );
484
517
}
485
518
519
+ protected IndexDefinitionHolder createCompoundWildcardIndexDefinition (String collection , CompoundWildcardIndexed index ,
520
+ @ Nullable MongoPersistentEntity <?> entity ) {
521
+
522
+ String wildcardField = index .wildcardFieldName ();
523
+ org .bson .Document indexKeys = resolveCompoundIndexKeyFromStringDefinition ("" , index .fields (), entity );
524
+
525
+ CompoundWildcardIndexDefinition indexDefinition = new CompoundWildcardIndexDefinition (wildcardField , indexKeys );
526
+
527
+ if (StringUtils .hasText (index .wildcardProjection ()) && ObjectUtils .isEmpty (wildcardField )) {
528
+ indexDefinition .wildcardProjection (evaluateWildcardProjection (index .wildcardProjection (), entity ));
529
+ }
530
+
531
+ if (StringUtils .hasText (index .partialFilter ())) {
532
+ indexDefinition .partial (evaluatePartialFilter (index .partialFilter (), entity ));
533
+ }
534
+
535
+ if (!index .useGeneratedName ()) {
536
+ indexDefinition .named (pathAwareIndexName (index .name (), "" , entity , null ));
537
+ }
538
+
539
+ indexDefinition .collation (resolveCollation (index , entity ));
540
+ return new IndexDefinitionHolder ("" , indexDefinition , collection );
541
+ }
542
+
486
543
private org .bson .Document resolveCompoundIndexKeyFromStringDefinition (String dotPath , String keyDefinitionString ,
487
544
PersistentEntity <?, ?> entity ) {
488
545
0 commit comments