All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.data.operations.reactive.BlockingReactorRepositoryOperations Maven / Gradle / Ivy

There is a newer version: 4.10.3
Show 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.operations.reactive;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
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.RepositoryOperations;
import reactor.util.context.Context;
import reactor.util.context.ContextView;

import java.util.Optional;
import java.util.stream.Stream;

/**
 * Implementation of {@link RepositoryOperations} that blocks every call from {@link ReactorReactiveRepositoryOperations}.
 *
 * @author Denis Stepanov
 * @since 3.3.0
 */
public interface BlockingReactorRepositoryOperations extends RepositoryOperations, ReactorReactiveCapableRepository {

    @NonNull
    private ContextView getContextView() {
        return ReactorPropagation.addPropagatedContext(Context.empty(), PropagatedContext.getOrEmpty());
    }

    @Nullable
    default  T findOne(@NonNull Class type, @NonNull Object id) {
        return reactive().findOne(type, id)
            .contextWrite(getContextView())
            .block();
    }

    @Nullable
    @Override
    default  R findOne(@NonNull PreparedQuery preparedQuery) {
        return reactive().findOne(preparedQuery)
            .contextWrite(getContextView())
            .block();
    }

    @NonNull
    @Override
    default  Iterable findAll(@NonNull PreparedQuery preparedQuery) {
        return reactive().findAll(preparedQuery)
            .contextWrite(getContextView())
            .collectList()
            .block();
    }

    @NonNull
    @Override
    default  Stream findStream(@NonNull PreparedQuery preparedQuery) {
        return reactive().findAll(preparedQuery)
            .contextWrite(getContextView())
            .toStream();
    }

    @NonNull
    @Override
    default  T persist(@NonNull InsertOperation operation) {
        return reactive().persist(operation)
            .contextWrite(getContextView())
            .blockOptional()
            .orElseGet(operation::getEntity);
    }

    @NonNull
    @Override
    default  T update(@NonNull UpdateOperation operation) {
        return reactive().update(operation)
            .contextWrite(getContextView())
            .blockOptional()
            .orElseGet(operation::getEntity);
    }

    @Override
    default  Iterable updateAll(UpdateBatchOperation operation) {
        return reactive().updateAll(operation)
            .contextWrite(getContextView())
            .collectList()
            .>map(it -> it)
            .blockOptional()
            .orElse(operation);
    }

    @NonNull
    @Override
    default  Iterable persistAll(@NonNull InsertBatchOperation operation) {
        return reactive().persistAll(operation)
            .contextWrite(getContextView())
            .collectList()
            .>map(it -> it)
            .blockOptional()
            .orElse(operation);
    }

    @NonNull
    @Override
    default Optional executeUpdate(@NonNull PreparedQuery preparedQuery) {
        return reactive().executeUpdate(preparedQuery)
            .contextWrite(getContextView())
            .blockOptional();
    }

    @Override
    default Optional executeDelete(@NonNull PreparedQuery preparedQuery) {
        return reactive().executeDelete(preparedQuery)
            .contextWrite(getContextView())
            .blockOptional();
    }

    @Override
    default  int delete(@NonNull DeleteOperation operation) {
        return reactive().delete(operation)
            .contextWrite(getContextView())
            .blockOptional().orElse(0).intValue();
    }

    @Override
    default  Optional deleteAll(@NonNull DeleteBatchOperation operation) {
        return reactive().deleteAll(operation)
            .contextWrite(getContextView())
            .blockOptional();
    }

    @Override
    default  boolean exists(@NonNull PreparedQuery preparedQuery) {
        return reactive().exists(preparedQuery)
            .contextWrite(getContextView())
            .blockOptional().orElse(false);
    }

    @Override
    default  Page findPage(@NonNull PagedQuery query) {
        return reactive().findPage(query)
            .contextWrite(getContextView())
            .block();
    }

    @NonNull
    @Override
    default  Iterable findAll(@NonNull PagedQuery query) {
        return reactive().findAll(query)
            .contextWrite(getContextView())
            .collectList()
            .block();
    }

    @Override
    default  long count(PagedQuery pagedQuery) {
        return reactive().count(pagedQuery)
            .contextWrite(getContextView())
            .blockOptional().orElse(0L);
    }

    @NonNull
    @Override
    default  Stream findStream(@NonNull PagedQuery query) {
        return reactive().findAll(query)
            .contextWrite(getContextView())
            .toStream();
    }

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy