io.micronaut.data.runtime.operations.ExecutorAsyncOperations 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-2020 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.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.propagation.PropagatedContext;
import io.micronaut.core.util.ArgumentUtils;
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.RepositoryOperations;
import io.micronaut.data.operations.async.AsyncRepositoryOperations;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
/**
* An implementation of {@link AsyncRepositoryOperations} that delegates to a blocking operations and specified {@link Executor}.
* This can be used in absence of true asynchronous support at the driver level.
*
* If a backing implementation provides a async API then the backing implementation should not use this class and instead directly implement the {@link AsyncRepositoryOperations} interface.
*
* @author graemerocher
* @since 1.0.0
*/
public class ExecutorAsyncOperations implements AsyncRepositoryOperations {
private final RepositoryOperations datastore;
private final Executor executor;
/**
* Default constructor.
*
* @param operations The target operations
* @param executor The executor to use.
*/
public ExecutorAsyncOperations(@NonNull RepositoryOperations operations, @NonNull Executor executor) {
ArgumentUtils.requireNonNull("operations", operations);
ArgumentUtils.requireNonNull("executor", executor);
this.datastore = operations;
this.executor = executor;
}
@Internal
final CompletableFuture supplyAsync(Supplier supplier) {
CompletableFuture cf = new CompletableFuture<>();
PropagatedContext propagatedContext = PropagatedContext.getOrEmpty();
CompletableFuture.supplyAsync(PropagatedContext.wrapCurrent(supplier), executor).whenComplete((value, throwable) -> {
try (PropagatedContext.Scope ignore = propagatedContext.propagate()) {
if (throwable != null) {
cf.completeExceptionally(throwable);
} else {
cf.complete(value);
}
}
});
return cf;
}
@Override
public Executor getExecutor() {
return executor;
}
@NonNull
@Override
public CompletableFuture findOne(@NonNull Class type, @NonNull Object id) {
return supplyAsync(() -> datastore.findOne(type, id)
);
}
@Override
public CompletableFuture exists(@NonNull PreparedQuery preparedQuery) {
return supplyAsync(() -> datastore.exists(preparedQuery));
}
@NonNull
@Override
public CompletableFuture findOne(@NonNull PreparedQuery preparedQuery) {
return supplyAsync(() -> datastore.findOne(preparedQuery));
}
@NonNull
@Override
public CompletableFuture findOptional(@NonNull Class type, @NonNull Object id) {
return supplyAsync(() -> datastore.findOne(type, id));
}
@NonNull
@Override
public CompletableFuture findOptional(@NonNull PreparedQuery preparedQuery) {
return supplyAsync(() -> datastore.findOne(preparedQuery));
}
@NonNull
@Override
public CompletableFuture> findAll(@NonNull PagedQuery pagedQuery) {
return supplyAsync(() -> datastore.findAll(pagedQuery));
}
@Override
public CompletableFuture count(@NonNull PagedQuery pagedQuery) {
return supplyAsync(() -> datastore.count(pagedQuery));
}
@NonNull
@Override
public CompletableFuture> findAll(@NonNull PreparedQuery preparedQuery) {
return supplyAsync(() -> datastore.findAll(preparedQuery));
}
@NonNull
@Override
public CompletableFuture persist(@NonNull InsertOperation entity) {
return supplyAsync(() -> datastore.persist(entity));
}
@NonNull
@Override
public CompletableFuture update(@NonNull UpdateOperation operation) {
return supplyAsync(() -> datastore.update(operation));
}
@NonNull
@Override
public CompletableFuture> updateAll(@NonNull UpdateBatchOperation operation) {
return supplyAsync(() -> datastore.updateAll(operation));
}
@NonNull
@Override
public CompletableFuture delete(@NonNull DeleteOperation operation) {
return supplyAsync(() -> datastore.delete(operation));
}
@NonNull
@Override
public CompletableFuture> persistAll(@NonNull InsertBatchOperation operation) {
return supplyAsync(() -> datastore.persistAll(operation));
}
@NonNull
@Override
public CompletableFuture executeUpdate(@NonNull PreparedQuery, Number> preparedQuery) {
return supplyAsync(() -> datastore.executeUpdate(preparedQuery).orElse(0));
}
@Override
public CompletionStage executeDelete(PreparedQuery, Number> preparedQuery) {
return supplyAsync(() -> datastore.executeDelete(preparedQuery).orElse(0));
}
@NonNull
@Override
public CompletableFuture deleteAll(@NonNull DeleteBatchOperation operation) {
return supplyAsync(() -> datastore.deleteAll(operation).orElse(0));
}
@Override
public CompletableFuture> findPage(@NonNull PagedQuery pagedQuery) {
return supplyAsync(() -> datastore.findPage(pagedQuery));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy