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

com.eventstore.dbclient.GetProjectionState Maven / Gradle / Ivy

package com.eventstore.dbclient;

import com.eventstore.dbclient.proto.projections.Projectionmanagement;
import com.eventstore.dbclient.proto.projections.ProjectionsGrpc;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.google.protobuf.util.JsonFormat;
import io.grpc.Metadata;
import io.grpc.stub.MetadataUtils;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

class GetProjectionState {

    private final GrpcClient client;
    private final String projectionName;
    private final GetProjectionStateOptions options;

    private ThrowingBiFunction deserializationStrategy;

    GetProjectionState(final GrpcClient client,
                       final String projectionName,
                       GetProjectionStateOptions options,
                       Class resultType) {

        this(client, projectionName, options, (jsonMapper, json) -> jsonMapper.readValue(json, resultType));
    }

    GetProjectionState(final GrpcClient client,
                       final String projectionName,
                       GetProjectionStateOptions options,
                       Function javaTypeFunction) {

        this(client, projectionName, options, (jsonMapper, json) -> jsonMapper.readValue(json, javaTypeFunction.apply(jsonMapper.getTypeFactory())));
    }

    public GetProjectionState(final GrpcClient client,
                              final String projectionName,
                              GetProjectionStateOptions options,
                              ThrowingBiFunction deserializationStrategy) {

        this.client = client;
        this.projectionName = projectionName;
        this.options = options;
        this.deserializationStrategy = deserializationStrategy;
    }

    public CompletableFuture execute() {

        return this.client.run(channel -> {

            Projectionmanagement.StateReq.Options.Builder optionsBuilder =
                    Projectionmanagement.StateReq.Options.newBuilder()
                            .setName(projectionName);

            if(!options.getPartition().isEmpty()) {
                optionsBuilder.setPartition(options.getPartition());
            }

            Projectionmanagement.StateReq request = Projectionmanagement.StateReq.newBuilder()
                    .setOptions(optionsBuilder)
                    .build();

            ProjectionsGrpc.ProjectionsStub client = GrpcUtils.configureStub(ProjectionsGrpc.newStub(channel), this.client.getSettings(), this.options);

            CompletableFuture result = new CompletableFuture<>();

            ThrowingFunction converter = source -> {

                String json = JsonFormat.printer().print(source.getState());
                return deserializationStrategy.apply(new JsonMapper(), json);
            };

            client.state(request, GrpcUtils.convertSingleResponse(result, converter));

            return result;
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy