/** * Inspect entities for index creation. * * @return a {@link Mono} that completes without value after indexes were created. */ public Mono<Void> checkForIndexes(MongoPersistentEntity<?> entity) { Class<?> type = entity.getType(); if (!classesSeen.containsKey(type)) { if (this.classesSeen.put(type, Boolean.TRUE) == null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Analyzing class " + type + " for index information."); } return checkForAndCreateIndexes(entity); } } return Mono.empty(); }
private void onCheckForIndexes(MongoPersistentEntity<?> entity, Consumer<Throwable> subscriptionExceptionHandler) { if (indexCreator != null) { indexCreator.checkForIndexes(entity).subscribe(v -> {}, subscriptionExceptionHandler); } }
private Mono<Void> checkForAndCreateIndexes(MongoPersistentEntity<?> entity) { List<Mono<?>> publishers = new ArrayList<>(); if (entity.isAnnotationPresent(Document.class)) { for (IndexDefinition indexDefinition : indexResolver.resolveIndexFor(entity.getTypeInformation())) { IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder ? (IndexDefinitionHolder) indexDefinition : new IndexDefinitionHolder("", indexDefinition, entity.getCollection()); publishers.add(createIndex(indexToCreate)); } } return publishers.isEmpty() ? Mono.empty() : Flux.merge(publishers).then(); }
private Mono<? extends String> translateException(Throwable e, IndexDefinitionHolder indexDefinition) { Mono<IndexInfo> existingIndex = fetchIndexInformation(indexDefinition); Mono<String> defaultError = Mono.error(new DataIntegrityViolationException( String.format("Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'.", indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions()), e.getCause())); return existingIndex.flatMap(it -> { return Mono.<String> error(new DataIntegrityViolationException( String.format("Index already defined as '%s'.", indexDefinition.getPath()), e.getCause())); }).switchIfEmpty(defaultError); }
Mono<String> createIndex(IndexDefinitionHolder indexDefinition) { JustOnceLogger.logWarnIndexCreationConfigurationChange(this.getClass().getName()); return operationsProvider.indexOps(indexDefinition.getCollection()).ensureIndex(indexDefinition) // .onErrorResume(ReactiveMongoPersistentEntityIndexCreator::isDataIntegrityViolation, e -> translateException(e, indexDefinition)); }
/** * Inspects the given {@link ApplicationContext} for {@link ReactiveMongoPersistentEntityIndexCreator} and those in * turn if they were registered for the current {@link MappingContext}. If no creator for the current * {@link MappingContext} can be found we manually add the internally created one as {@link ApplicationListener} to * make sure indexes get created appropriately for entity types persisted through this {@link ReactiveMongoTemplate} * instance. * * @param context must not be {@literal null}. */ private void prepareIndexCreator(ApplicationContext context) { String[] indexCreators = context.getBeanNamesForType(ReactiveMongoPersistentEntityIndexCreator.class); for (String creator : indexCreators) { ReactiveMongoPersistentEntityIndexCreator creatorBean = context.getBean(creator, ReactiveMongoPersistentEntityIndexCreator.class); if (creatorBean.isIndexCreatorFor(mappingContext)) { return; } } if (context instanceof ConfigurableApplicationContext) { ((ConfigurableApplicationContext) context).addApplicationListener(indexCreatorListener); } }
this.indexCreator = new ReactiveMongoPersistentEntityIndexCreator(mongoMappingContext, this::indexOps); this.eventPublisher = new MongoMappingEventPublisher(this.indexCreatorListener);
private Mono<? extends String> translateException(Throwable e, IndexDefinitionHolder indexDefinition) { Mono<IndexInfo> existingIndex = fetchIndexInformation(indexDefinition); Mono<String> defaultError = Mono.error(new DataIntegrityViolationException( String.format("Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'.", indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions()), e.getCause())); return existingIndex.flatMap(it -> { return Mono.<String> error(new DataIntegrityViolationException( String.format("Index already defined as '%s'.", indexDefinition.getPath()), e.getCause())); }).switchIfEmpty(defaultError); }
Mono<String> createIndex(IndexDefinitionHolder indexDefinition) { return operationsProvider.indexOps(indexDefinition.getCollection()).ensureIndex(indexDefinition) // .onErrorResume(ReactiveMongoPersistentEntityIndexCreator::isDataIntegrityViolation, e -> translateException(e, indexDefinition)); }
/** * Inspects the given {@link ApplicationContext} for {@link ReactiveMongoPersistentEntityIndexCreator} and those in * turn if they were registered for the current {@link MappingContext}. If no creator for the current * {@link MappingContext} can be found we manually add the internally created one as {@link ApplicationListener} to * make sure indexes get created appropriately for entity types persisted through this {@link ReactiveMongoTemplate} * instance. * * @param context must not be {@literal null}. */ private void prepareIndexCreator(ApplicationContext context) { String[] indexCreators = context.getBeanNamesForType(ReactiveMongoPersistentEntityIndexCreator.class); for (String creator : indexCreators) { ReactiveMongoPersistentEntityIndexCreator creatorBean = context.getBean(creator, ReactiveMongoPersistentEntityIndexCreator.class); if (creatorBean.isIndexCreatorFor(mappingContext)) { return; } } if (context instanceof ConfigurableApplicationContext) { ((ConfigurableApplicationContext) context).addApplicationListener(indexCreatorListener); } }
this.indexCreator = new ReactiveMongoPersistentEntityIndexCreator(mongoMappingContext, this::indexOps); this.eventPublisher = new MongoMappingEventPublisher(this.indexCreatorListener);
private void onCheckForIndexes(MongoPersistentEntity<?> entity, Consumer<Throwable> subscriptionExceptionHandler) { if (indexCreator != null) { indexCreator.checkForIndexes(entity).subscribe(v -> {}, subscriptionExceptionHandler); } }
private Mono<Void> checkForAndCreateIndexes(MongoPersistentEntity<?> entity) { List<Mono<?>> publishers = new ArrayList<>(); if (entity.isAnnotationPresent(Document.class)) { for (IndexDefinitionHolder indexToCreate : indexResolver.resolveIndexFor(entity.getTypeInformation())) { publishers.add(createIndex(indexToCreate)); } } return publishers.isEmpty() ? Mono.empty() : Flux.merge(publishers).then(); }
/** * Inspect entities for index creation. * * @return a {@link Mono} that completes without value after indexes were created. */ public Mono<Void> checkForIndexes(MongoPersistentEntity<?> entity) { Class<?> type = entity.getType(); if (!classesSeen.containsKey(type)) { if (this.classesSeen.put(type, Boolean.TRUE) == null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Analyzing class " + type + " for index information."); } return checkForAndCreateIndexes(entity); } } return Mono.empty(); }