io.micronaut.data.runtime.operations.ReactorToAsyncOperationsAdaptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-data-runtime Show documentation
Show all versions of micronaut-data-runtime Show documentation
Data Repository Support for Micronaut
The newest version!
/*
* Copyright 2017-2021 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.runtime.operations;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.async.propagation.ReactorPropagation;
import io.micronaut.core.propagation.PropagatedContext;
import io.micronaut.data.model.Page;
import io.micronaut.data.model.runtime.DeleteBatchOperation;
import io.micronaut.data.model.runtime.DeleteOperation;
import io.micronaut.data.model.runtime.InsertBatchOperation;
import io.micronaut.data.model.runtime.InsertOperation;
import io.micronaut.data.model.runtime.PagedQuery;
import io.micronaut.data.model.runtime.PreparedQuery;
import io.micronaut.data.model.runtime.UpdateBatchOperation;
import io.micronaut.data.model.runtime.UpdateOperation;
import io.micronaut.data.operations.async.AsyncRepositoryOperations;
import io.micronaut.data.operations.reactive.ReactorReactiveRepositoryOperations;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
/**
* An implementation of {@link AsyncRepositoryOperations} that delegates to a reactive operation.
*
* @author Denis Stepanov
* @since 3.5.0
*/
@Experimental
public final class ReactorToAsyncOperationsAdaptor implements AsyncRepositoryOperations {
private final ReactorReactiveRepositoryOperations reactiveOperations;
private final Executor executor;
public ReactorToAsyncOperationsAdaptor(ReactorReactiveRepositoryOperations reactiveOperations,
Executor executor) {
this.reactiveOperations = reactiveOperations;
this.executor = executor;
}
@NonNull
@Override
public Executor getExecutor() {
return executor;
}
@NonNull
@Override
public CompletionStage findOne(@NonNull Class type, @NonNull Object id) {
return toCompletionStage(reactiveOperations.findOne(type, id));
}
@Override
public CompletionStage exists(@NonNull PreparedQuery preparedQuery) {
return toCompletionStage(reactiveOperations.exists(preparedQuery));
}
@NonNull
@Override
public CompletionStage findOne(@NonNull PreparedQuery preparedQuery) {
return toCompletionStage(reactiveOperations.findOne(preparedQuery));
}
@NonNull
@Override
public CompletionStage findOptional(@NonNull Class type, @NonNull Object id) {
return toCompletionStage(reactiveOperations.findOptional(type, id));
}
@NonNull
@Override
public CompletionStage findOptional(@NonNull PreparedQuery preparedQuery) {
return toCompletionStage(reactiveOperations.findOptional(preparedQuery));
}
@NonNull
@Override
public CompletionStage> findAll(PagedQuery pagedQuery) {
return toCompletionStage(reactiveOperations.findAll(pagedQuery));
}
@NonNull
@Override
public CompletionStage count(PagedQuery pagedQuery) {
return toCompletionStage(reactiveOperations.count(pagedQuery));
}
@NonNull
@Override
public CompletionStage> findAll(@NonNull PreparedQuery preparedQuery) {
return toCompletionStage(reactiveOperations.findAll(preparedQuery));
}
@NonNull
@Override
public CompletionStage persist(@NonNull InsertOperation operation) {
return toCompletionStage(reactiveOperations.persist(operation));
}
@NonNull
@Override
public CompletionStage update(@NonNull UpdateOperation operation) {
return toCompletionStage(reactiveOperations.update(operation));
}
@NonNull
@Override
public CompletionStage delete(@NonNull DeleteOperation operation) {
return toCompletionStage(reactiveOperations.delete(operation));
}
@NonNull
@Override
public CompletionStage> persistAll(@NonNull InsertBatchOperation operation) {
return toCompletionStage(reactiveOperations.persistAll(operation));
}
@NonNull
@Override
public CompletionStage executeUpdate(@NonNull PreparedQuery, Number> preparedQuery) {
return toCompletionStage(reactiveOperations.executeUpdate(preparedQuery));
}
@Override
public CompletionStage executeDelete(PreparedQuery, Number> preparedQuery) {
return toCompletionStage(reactiveOperations.executeDelete(preparedQuery));
}
@Override
public CompletionStage> execute(PreparedQuery, R> preparedQuery) {
return toCompletionStage(reactiveOperations.execute(preparedQuery).collectList());
}
@NonNull
@Override
public CompletionStage deleteAll(@NonNull DeleteBatchOperation operation) {
return toCompletionStage(reactiveOperations.deleteAll(operation));
}
@NonNull
@Override
public CompletionStage> findPage(@NonNull PagedQuery pagedQuery) {
return toCompletionStage(reactiveOperations.findPage(pagedQuery));
}
@NonNull
@Override
public CompletionStage> updateAll(@NonNull UpdateBatchOperation operation) {
return toCompletionStage(reactiveOperations.updateAll(operation));
}
private CompletionStage> toCompletionStage(Flux flux) {
return flux
.contextWrite(ctx -> ReactorPropagation.addPropagatedContext(ctx, PropagatedContext.getOrEmpty()))
.collectList()
.>map(list -> list)
.toFuture();
}
private CompletionStage toCompletionStage(Mono mono) {
return mono
.contextWrite(ctx -> ReactorPropagation.addPropagatedContext(ctx, PropagatedContext.getOrEmpty()))
.toFuture();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy