private List<IndexDefinitionHolder> potentiallyCreateCompoundIndexDefinitions(String dotPath, String collection, MongoPersistentEntity<?> entity) { if (entity.findAnnotation(CompoundIndexes.class) == null && entity.findAnnotation(CompoundIndex.class) == null) { return Collections.emptyList(); } return createCompoundIndexDefinitions(dotPath, collection, entity); }
/** * Create {@link IndexDefinition} wrapped in {@link IndexDefinitionHolder} for {@link CompoundIndexes} of given type. * * @param dotPath The properties {@literal "dot"} path representation from its document root. * @param fallbackCollection * @param type * @return */ protected List<IndexDefinitionHolder> createCompoundIndexDefinitions(String dotPath, String fallbackCollection, MongoPersistentEntity<?> entity) { List<IndexDefinitionHolder> indexDefinitions = new ArrayList<>(); CompoundIndexes indexes = entity.findAnnotation(CompoundIndexes.class); if (indexes != null) { indexDefinitions = Arrays.stream(indexes.value()) .map(index -> createCompoundIndexDefinition(dotPath, fallbackCollection, index, entity)) .collect(Collectors.toList()); } CompoundIndex index = entity.findAnnotation(CompoundIndex.class); if (index != null) { indexDefinitions.add(createCompoundIndexDefinition(dotPath, fallbackCollection, index, entity)); } return indexDefinitions; }
/** * Resolve the {@link IndexDefinition}s for given {@literal root} entity by traversing {@link MongoPersistentProperty} * scanning for index annotations {@link Indexed}, {@link CompoundIndex} and {@link GeospatialIndex}. The given * {@literal root} has therefore to be annotated with {@link Document}. * * @param root must not be null. * @return List of {@link IndexDefinitionHolder}. Will never be {@code null}. * @throws IllegalArgumentException in case of missing {@link Document} annotation marking root entities. */ public List<IndexDefinitionHolder> resolveIndexForEntity(final MongoPersistentEntity<?> root) { Assert.notNull(root, "Index cannot be resolved for given 'null' entity."); Document document = root.findAnnotation(Document.class); Assert.notNull(document, "Given entity is not collection root."); final List<IndexDefinitionHolder> indexInformation = new ArrayList<>(); indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", root.getCollection(), root)); indexInformation.addAll(potentiallyCreateTextIndexDefinition(root)); root.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> this .potentiallyAddIndexForProperty(root, property, indexInformation, new CycleGuard())); indexInformation.addAll(resolveIndexesForDbrefs("", root.getCollection(), root)); return indexInformation; }
private List<IndexDefinitionHolder> potentiallyCreateCompoundIndexDefinitions(String dotPath, String collection, MongoPersistentEntity<?> entity) { if (entity.findAnnotation(CompoundIndexes.class) == null && entity.findAnnotation(CompoundIndex.class) == null) { return Collections.emptyList(); } return createCompoundIndexDefinitions(dotPath, collection, entity); }
/** * Create {@link IndexDefinition} wrapped in {@link IndexDefinitionHolder} for {@link CompoundIndexes} of given type. * * @param dotPath The properties {@literal "dot"} path representation from its document root. * @param fallbackCollection * @param type * @return */ protected List<IndexDefinitionHolder> createCompoundIndexDefinitions(String dotPath, String fallbackCollection, MongoPersistentEntity<?> entity) { List<IndexDefinitionHolder> indexDefinitions = new ArrayList<>(); CompoundIndexes indexes = entity.findAnnotation(CompoundIndexes.class); if (indexes != null) { indexDefinitions = Arrays.stream(indexes.value()) .map(index -> createCompoundIndexDefinition(dotPath, fallbackCollection, index, entity)) .collect(Collectors.toList()); } CompoundIndex index = entity.findAnnotation(CompoundIndex.class); if (index != null) { indexDefinitions.add(createCompoundIndexDefinition(dotPath, fallbackCollection, index, entity)); } return indexDefinitions; }
/** * Resolve the {@link IndexDefinition}s for given {@literal root} entity by traversing {@link MongoPersistentProperty} * scanning for index annotations {@link Indexed}, {@link CompoundIndex} and {@link GeospatialIndex}. The given * {@literal root} has therefore to be annotated with {@link Document}. * * @param root must not be null. * @return List of {@link IndexDefinitionHolder}. Will never be {@code null}. * @throws IllegalArgumentException in case of missing {@link Document} annotation marking root entities. */ public List<IndexDefinitionHolder> resolveIndexForEntity(final MongoPersistentEntity<?> root) { Assert.notNull(root, "Index cannot be resolved for given 'null' entity."); Document document = root.findAnnotation(Document.class); Assert.notNull(document, "Given entity is not collection root."); final List<IndexDefinitionHolder> indexInformation = new ArrayList<>(); indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", root.getCollection(), root)); indexInformation.addAll(potentiallyCreateTextIndexDefinition(root)); root.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> this .potentiallyAddIndexForProperty(root, property, indexInformation, new CycleGuard())); indexInformation.addAll(resolveIndexesForDbrefs("", root.getCollection(), root)); return indexInformation; }
public <A extends Annotation> A findAnnotation(Class<A> annotationType) { return delegate.findAnnotation(annotationType); }
private void checkForAndCreateIndexes(final MongoPersistentEntityIndexResolver indexResolver, final MongoPersistentEntity<?> entity) { // make sure its a root document if (entity.findAnnotation(Document.class) != null) { for (IndexDefinitionHolder indexDefinitionHolder : indexResolver.resolveIndexForClass(entity.getType())) { // work because of javas reentered lock feature this.mongoTemplate.indexOps(entity.getType()).ensureIndex(indexDefinitionHolder); } } } }