use of org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder in project spring-data-mongodb by spring-projects.
the class ReactiveMongoPersistentEntityIndexCreator method checkForAndCreateIndexes.
private Mono<Void> checkForAndCreateIndexes(MongoPersistentEntity<?> entity) {
List<Mono<?>> publishers = new ArrayList<>();
if (entity.isAnnotationPresent(Document.class)) {
String collection = entity.getCollection();
for (IndexDefinition indexDefinition : indexResolver.resolveIndexFor(entity.getTypeInformation())) {
IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder ? (IndexDefinitionHolder) indexDefinition : new IndexDefinitionHolder("", indexDefinition, collection);
publishers.add(createIndex(indexToCreate));
}
}
return publishers.isEmpty() ? Mono.empty() : Flux.merge(publishers).then();
}
use of org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder in project spring-data-mongodb by spring-projects.
the class ReactiveMongoPersistentEntityIndexCreator method translateException.
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);
}
use of org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder in project spring-data-mongodb by spring-projects.
the class MongoPersistentEntityIndexCreator method checkForAndCreateIndexes.
private void checkForAndCreateIndexes(MongoPersistentEntity<?> entity) {
if (entity.isAnnotationPresent(Document.class)) {
String collection = entity.getCollection();
for (IndexDefinition indexDefinition : indexResolver.resolveIndexFor(entity.getTypeInformation())) {
IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder ? (IndexDefinitionHolder) indexDefinition : new IndexDefinitionHolder("", indexDefinition, collection);
createIndex(indexToCreate);
}
}
}
use of org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder in project spring-data-mongodb by spring-projects.
the class MongoPersistentEntityIndexCreatorIntegrationTests method createIndexShouldThrowMeaningfulExceptionWhenIndexCreationFails.
// DATAMONGO-1125
@Test
public void createIndexShouldThrowMeaningfulExceptionWhenIndexCreationFails() {
expectedException.expect(DataIntegrityViolationException.class);
expectedException.expectMessage("collection 'datamongo-1125'");
expectedException.expectMessage("dalinar.kohlin");
expectedException.expectMessage("lastname");
expectedException.expectCause(IsInstanceOf.<Throwable>instanceOf(MongoCommandException.class));
try (MongoClient client = MongoTestUtils.client()) {
MongoTemplate mongoTemplate = new MongoTemplate(client, "issue");
MongoPersistentEntityIndexCreator indexCreator = new MongoPersistentEntityIndexCreator(new MongoMappingContext(), mongoTemplate);
indexCreator.createIndex(new IndexDefinitionHolder("dalinar.kohlin", new Index().named("stormlight").on("lastname", Direction.ASC).unique(), "datamongo-1125"));
indexCreator.createIndex(new IndexDefinitionHolder("dalinar.kohlin", new Index().named("stormlight").on("lastname", Direction.ASC).sparse(), "datamongo-1125"));
}
}
Aggregations