io.micronaut.data.runtime.operations.AsyncFromReactiveAsyncRepositoryOperation 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
/*
* 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.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.ReactiveRepositoryOperations;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
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 2.4.0
*/
public class AsyncFromReactiveAsyncRepositoryOperation implements AsyncRepositoryOperations {
private final ReactiveRepositoryOperations reactiveOperations;
private final Executor executor;
public AsyncFromReactiveAsyncRepositoryOperation(ReactiveRepositoryOperations 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 toIterableCompletionStage(reactiveOperations.findAll(pagedQuery));
}
@NonNull
@Override
public CompletionStage count(PagedQuery pagedQuery) {
return toCompletionStage(reactiveOperations.count(pagedQuery));
}
@NonNull
@Override
public CompletionStage> findAll(@NonNull PreparedQuery preparedQuery) {
return toIterableCompletionStage(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 toIterableCompletionStage(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));
}
@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 toIterableCompletionStage(reactiveOperations.updateAll(operation));
}
private CompletionStage> toIterableCompletionStage(Publisher publisher) {
return Flux.from(publisher)
.contextWrite(ctx -> ReactorPropagation.addPropagatedContext(ctx, PropagatedContext.getOrEmpty()))
.collectList()
.map(v -> (Iterable) v)
.toFuture();
}
private CompletionStage toCompletionStage(Publisher publisher) {
return Mono.from(publisher)
.contextWrite(ctx -> ReactorPropagation.addPropagatedContext(ctx, PropagatedContext.getOrEmpty()))
.toFuture();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy