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

io.tarantool.driver.api.space.ProxyTarantoolSpace Maven / Gradle / Ivy

Go to download

Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework

There is a newer version: 0.14.0
Show newest version
package io.tarantool.driver.api.space;

import io.tarantool.driver.ProxyTarantoolClient;
import io.tarantool.driver.protocol.TarantoolIndexQuery;
import io.tarantool.driver.api.TarantoolResult;
import io.tarantool.driver.api.conditions.Conditions;
import io.tarantool.driver.api.tuple.TarantoolTuple;
import io.tarantool.driver.exceptions.TarantoolClientException;
import io.tarantool.driver.mappers.TarantoolCallResultMapper;
import io.tarantool.driver.mappers.TarantoolCallResultMapperFactory;
import io.tarantool.driver.mappers.ValueConverter;
import io.tarantool.driver.metadata.TarantoolMetadataOperations;
import io.tarantool.driver.metadata.TarantoolSpaceMetadata;
import io.tarantool.driver.api.tuple.operations.TupleOperations;
import io.tarantool.driver.proxy.DeleteProxyOperation;
import io.tarantool.driver.proxy.InsertProxyOperation;
import io.tarantool.driver.proxy.ProxyOperation;
import io.tarantool.driver.proxy.ReplaceProxyOperation;
import io.tarantool.driver.proxy.SelectProxyOperation;
import io.tarantool.driver.proxy.UpdateProxyOperation;
import io.tarantool.driver.proxy.UpsertProxyOperation;
import org.msgpack.value.ArrayValue;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
 * Represents a proxy {@link TarantoolSpaceOperations} implementation, which uses calls to API functions defined in
 * Tarantool instance for performing CRUD operations on a space
 *
 * @author Sergey Volgin
 */
public class ProxyTarantoolSpace implements TarantoolSpaceOperations {

    private final String spaceName;
    private final ProxyTarantoolClient client;
    private final TarantoolMetadataOperations metadataOperations;
    private final TarantoolSpaceMetadata spaceMetadata;

    private final TarantoolCallResultMapperFactory tarantoolResultMapperFactory;

    public ProxyTarantoolSpace(ProxyTarantoolClient client,
                               TarantoolSpaceMetadata spaceMetadata) {
        this.client = client;
        this.spaceMetadata = spaceMetadata;
        this.spaceName = spaceMetadata.getSpaceName();
        this.metadataOperations = client.metadata();
        this.tarantoolResultMapperFactory =
                new TarantoolCallResultMapperFactory(client.getConfig().getMessagePackMapper());
    }

    @Override
    public CompletableFuture> delete(Conditions conditions)
            throws TarantoolClientException {
        return delete(conditions, defaultTupleResultMapper());
    }

    @Override
    public  CompletableFuture> delete(Conditions conditions,
                                                            ValueConverter tupleMapper)
            throws TarantoolClientException {

        return delete(conditions, tarantoolResultMapperFactory.withConverter(tupleMapper));
    }

    private  CompletableFuture> delete(Conditions conditions,
                                                            TarantoolCallResultMapper resultMapper)
            throws TarantoolClientException {
        TarantoolIndexQuery indexQuery = conditions.toIndexQuery(metadataOperations, spaceMetadata);

        DeleteProxyOperation operation = new DeleteProxyOperation.Builder()
                .withClient(client)
                .withSpaceName(spaceName)
                .withFunctionName(client.getDeleteFunctionName())
                .withIndexQuery(indexQuery)
                .withResultMapper(resultMapper)
                .build();

        return executeOperation(operation);
    }

    @Override
    public CompletableFuture> insert(TarantoolTuple tuple)
            throws TarantoolClientException {
        return insert(tuple, defaultTupleResultMapper());
    }

    @Override
    public  CompletableFuture> insert(TarantoolTuple tuple,
                                                            ValueConverter tupleMapper)
            throws TarantoolClientException {
        return insert(tuple, tarantoolResultMapperFactory.withConverter(tupleMapper));
    }

    private  CompletableFuture> insert(TarantoolTuple tuple,
                                                            TarantoolCallResultMapper resultMapper)
            throws TarantoolClientException {
        InsertProxyOperation operation = new InsertProxyOperation.Builder()
                .withClient(client)
                .withSpaceName(spaceName)
                .withFunctionName(client.getInsertFunctionName())
                .withTuple(tuple)
                .withResultMapper(resultMapper)
                .build();

        return executeOperation(operation);
    }

    @Override
    public CompletableFuture> replace(TarantoolTuple tuple)
            throws TarantoolClientException {
        return replace(tuple, defaultTupleResultMapper());
    }

    @Override
    public  CompletableFuture> replace(TarantoolTuple tuple,
                                                             ValueConverter tupleMapper)
            throws TarantoolClientException {
        return replace(tuple, tarantoolResultMapperFactory.withConverter(tupleMapper));
    }

    private  CompletableFuture> replace(TarantoolTuple tuple,
                                                             TarantoolCallResultMapper resultMapper)
            throws TarantoolClientException {
        ReplaceProxyOperation operation = new ReplaceProxyOperation.Builder()
                .withClient(client)
                .withSpaceName(spaceName)
                .withFunctionName(client.getReplaceFunctionName())
                .withTuple(tuple)
                .withResultMapper(resultMapper)
                .build();

        return executeOperation(operation);
    }

    @Override
    public CompletableFuture> select(Conditions conditions)
            throws TarantoolClientException {
        return select(conditions, defaultTupleResultMapper());
    }

    @Override
    public  CompletableFuture> select(Conditions conditions,
                                                            Class tupleClass) throws TarantoolClientException {
        TarantoolCallResultMapper mapper;
        if (TarantoolTuple.class.isAssignableFrom(tupleClass)) {
            mapper = (TarantoolCallResultMapper) defaultTupleResultMapper();
        } else {
            ValueConverter converter = getConverter(tupleClass);
            mapper = tarantoolResultMapperFactory.withConverter(tupleClass, converter);
        }
        return select(conditions, mapper);
    }

    @Override
    public  CompletableFuture> select(Conditions conditions,
                                                            ValueConverter tupleMapper)
            throws TarantoolClientException {
        return select(conditions, tarantoolResultMapperFactory.withConverter(tupleMapper));
    }

    private  CompletableFuture> select(Conditions conditions,
                                                             TarantoolCallResultMapper resultMapper)
            throws TarantoolClientException {

        SelectProxyOperation operation = new SelectProxyOperation.Builder(metadataOperations, spaceMetadata)
                .withClient(client)
                .withSpaceName(spaceName)
                .withFunctionName(client.getSelectFunctionName())
                .withConditions(conditions)
                .withResultMapper(resultMapper)
                .build();

        return executeOperation(operation);
    }

    @Override
    public CompletableFuture> update(Conditions conditions,
                                                                     TarantoolTuple tuple) {
        return update(conditions, TupleOperations.fromTarantoolTuple(tuple), defaultTupleResultMapper());
    }

    @Override
    public CompletableFuture> update(Conditions conditions,
                                                                     TupleOperations operations) {
        return update(conditions, operations, defaultTupleResultMapper());
    }

    @Override
    public  CompletableFuture> update(Conditions conditions,
                                                            TupleOperations operations,
                                                            ValueConverter tupleMapper) {
        return update(conditions, operations, tarantoolResultMapperFactory.withConverter(tupleMapper));
    }

    private  CompletableFuture> update(Conditions conditions,
                                                             TupleOperations operations,
                                                             TarantoolCallResultMapper resultMapper) {
        TarantoolIndexQuery indexQuery = conditions.toIndexQuery(metadataOperations, spaceMetadata);

        UpdateProxyOperation operation = new UpdateProxyOperation.Builder()
                .withClient(client)
                .withSpaceName(spaceName)
                .withFunctionName(client.getUpdateFunctionName())
                .withIndexQuery(indexQuery)
                .withTupleOperation(operations)
                .withResultMapper(resultMapper)
                .build();

        return executeOperation(operation);
    }

    @Override
    public CompletableFuture> upsert(Conditions conditions,
                                                                     TarantoolTuple tuple,
                                                                     TupleOperations operations) {
        return upsert(conditions, tuple, operations, defaultTupleResultMapper());
    }

    @Override
    public  CompletableFuture> upsert(Conditions conditions,
                                                            TarantoolTuple tuple,
                                                            TupleOperations operations,
                                                            ValueConverter tupleMapper) {
        return upsert(conditions, tuple, operations, tarantoolResultMapperFactory.withConverter(tupleMapper));
    }

    private  CompletableFuture> upsert(Conditions conditions,
                                                             TarantoolTuple tuple,
                                                             TupleOperations operations,
                                                             TarantoolCallResultMapper resultMapper) {
        UpsertProxyOperation operation = new UpsertProxyOperation.Builder()
                .withClient(client)
                .withSpaceName(spaceName)
                .withFunctionName(client.getUpsertFunctionName())
                .withTuple(tuple)
                .withTupleOperation(operations)
                .withResultMapper(resultMapper)
                .build();

        return executeOperation(operation);
    }

    private TarantoolCallResultMapper defaultTupleResultMapper() {
        return tarantoolResultMapperFactory.withDefaultTupleValueConverter(spaceMetadata);
    }

    private  ValueConverter getConverter(Class tupleClass) {
        Optional> converter =
                client.getConfig().getMessagePackMapper().getValueConverter(ArrayValue.class, tupleClass);
        if (!converter.isPresent()) {
            throw new TarantoolClientException("No ArrayValue converter for type " + tupleClass + " is present");
        }
        return converter.get();
    }

    private  CompletableFuture> executeOperation(ProxyOperation operation) {
        return operation.execute();
    }

    @Override
    public TarantoolSpaceMetadata getMetadata() {
        return spaceMetadata;
    }

    @Override
    public String toString() {
        return String.format("ProxyTarantoolSpace [%s]", spaceName);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy