Search in sources :

Example 1 with IndexDefinitionHolder

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();
}
Also used : IndexDefinitionHolder(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder) Mono(reactor.core.publisher.Mono) ArrayList(java.util.ArrayList)

Example 2 with IndexDefinitionHolder

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);
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) IndexDefinitionHolder(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder) MongoException(com.mongodb.MongoException) ObjectUtils(org.springframework.util.ObjectUtils) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MappingContext(org.springframework.data.mapping.context.MappingContext) Mono(reactor.core.publisher.Mono) ArrayList(java.util.ArrayList) Document(org.springframework.data.mongodb.core.mapping.Document) Flux(reactor.core.publisher.Flux) List(java.util.List) MongoDbErrorCodes(org.springframework.data.mongodb.util.MongoDbErrorCodes) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Map(java.util.Map) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) UncategorizedMongoDbException(org.springframework.data.mongodb.UncategorizedMongoDbException) Assert(org.springframework.util.Assert) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 3 with IndexDefinitionHolder

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);
        }
    }
}
Also used : IndexDefinitionHolder(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder)

Example 4 with IndexDefinitionHolder

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"));
    }
}
Also used : MongoClient(com.mongodb.client.MongoClient) MongoCommandException(com.mongodb.MongoCommandException) IndexDefinitionHolder(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Test(org.junit.Test)

Aggregations

IndexDefinitionHolder (org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder)4 ArrayList (java.util.ArrayList)2 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)2 Mono (reactor.core.publisher.Mono)2 MongoCommandException (com.mongodb.MongoCommandException)1 MongoException (com.mongodb.MongoException)1 MongoClient (com.mongodb.client.MongoClient)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 Test (org.junit.Test)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1 MappingContext (org.springframework.data.mapping.context.MappingContext)1 UncategorizedMongoDbException (org.springframework.data.mongodb.UncategorizedMongoDbException)1 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)1 Document (org.springframework.data.mongodb.core.mapping.Document)1 MongoPersistentEntity (org.springframework.data.mongodb.core.mapping.MongoPersistentEntity)1 MongoDbErrorCodes (org.springframework.data.mongodb.util.MongoDbErrorCodes)1