@Nullable private IndexInfo fetchIndexInformation(@Nullable IndexDefinitionHolder indexDefinition) { if (indexDefinition == null) { return null; } try { IndexOperations indexOperations = indexOperationsProvider.indexOps(indexDefinition.getCollection()); Object indexNameToLookUp = indexDefinition.getIndexOptions().get("name"); List<IndexInfo> existingIndexes = indexOperations.getIndexInfo(); return existingIndexes.stream().// filter(indexInfo -> ObjectUtils.nullSafeEquals(indexNameToLookUp, indexInfo.getName())).// findFirst().// orElse(null); } catch (Exception e) { LOGGER.debug( String.format("Failed to load index information for collection '%s'.", indexDefinition.getCollection()), e); } return null; } }
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)); }
@Nullable private IndexInfo fetchIndexInformation(@Nullable IndexDefinitionHolder indexDefinition) { if (indexDefinition == null) { return null; } try { IndexOperations indexOperations = indexOperationsProvider.indexOps(indexDefinition.getCollection()); Object indexNameToLookUp = indexDefinition.getIndexOptions().get("name"); List<IndexInfo> existingIndexes = indexOperations.getIndexInfo(); return existingIndexes.stream().// filter(indexInfo -> ObjectUtils.nullSafeEquals(indexNameToLookUp, indexInfo.getName())).// findFirst().// orElse(null); } catch (Exception e) { LOGGER.debug( String.format("Failed to load index information for collection '%s'.", indexDefinition.getCollection()), e); } return null; } }
void createIndex(IndexDefinitionHolder indexDefinition) { try { IndexOperations indexOperations = indexOperationsProvider.indexOps(indexDefinition.getCollection()); indexOperations.ensureIndex(indexDefinition); } catch (UncategorizedMongoDbException ex) { if (ex.getCause() instanceof MongoException && MongoDbErrorCodes.isDataIntegrityViolationCode(((MongoException) ex.getCause()).getCode())) { IndexInfo existingIndex = fetchIndexInformation(indexDefinition); String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'."; if (existingIndex != null) { message += " Index already defined as '%s'."; } throw new DataIntegrityViolationException( String.format(message, indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions(), existingIndex), ex.getCause()); } throw ex; } }
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)); }
private Mono<IndexInfo> fetchIndexInformation(IndexDefinitionHolder indexDefinition) { Object indexNameToLookUp = indexDefinition.getIndexOptions().get("name"); Flux<IndexInfo> existingIndexes = operationsProvider.indexOps(indexDefinition.getCollection()).getIndexInfo(); return existingIndexes // .filter(indexInfo -> ObjectUtils.nullSafeEquals(indexNameToLookUp, indexInfo.getName())) // .next() // .doOnError(e -> { LOGGER.debug( String.format("Failed to load index information for collection '%s'.", indexDefinition.getCollection()), e); }); }
void createIndex(IndexDefinitionHolder indexDefinition) { try { IndexOperations indexOperations = indexOperationsProvider.indexOps(indexDefinition.getCollection()); indexOperations.ensureIndex(indexDefinition); } catch (UncategorizedMongoDbException ex) { if (ex.getCause() instanceof MongoException && MongoDbErrorCodes.isDataIntegrityViolationCode(((MongoException) ex.getCause()).getCode())) { IndexInfo existingIndex = fetchIndexInformation(indexDefinition); String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'."; if (existingIndex != null) { message += " Index already defined as '%s'."; } throw new DataIntegrityViolationException( String.format(message, indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions(), existingIndex), ex.getCause()); } throw ex; } }
private Mono<IndexInfo> fetchIndexInformation(IndexDefinitionHolder indexDefinition) { Object indexNameToLookUp = indexDefinition.getIndexOptions().get("name"); Flux<IndexInfo> existingIndexes = operationsProvider.indexOps(indexDefinition.getCollection()).getIndexInfo(); return existingIndexes // .filter(indexInfo -> ObjectUtils.nullSafeEquals(indexNameToLookUp, indexInfo.getName())) // .next() // .doOnError(e -> { LOGGER.debug( String.format("Failed to load index information for collection '%s'.", indexDefinition.getCollection()), e); }); }