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

io.tarantool.driver.core.TarantoolClientConfiguratorImpl 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.core;

import io.tarantool.driver.api.TarantoolClient;
import io.tarantool.driver.api.TarantoolClientConfigurator;
import io.tarantool.driver.api.TarantoolResult;
import io.tarantool.driver.api.proxy.ProxyOperationsMappingConfig;
import io.tarantool.driver.api.retry.RequestRetryPolicyFactory;
import io.tarantool.driver.api.retry.TarantoolRequestRetryPolicies;
import io.tarantool.driver.api.tuple.TarantoolTuple;
import io.tarantool.driver.utils.Assert;

import java.util.function.Predicate;
import java.util.function.UnaryOperator;

import static io.tarantool.driver.api.retry.TarantoolRequestRetryPolicies.retryNetworkErrors;

/**
 * Tarantool client configurator implementation.
 *
 * @author Oleg Kuznetsov
 */
public class TarantoolClientConfiguratorImpl>
    implements TarantoolClientConfigurator {

    private final TarantoolClient> client;

    protected RequestRetryPolicyFactory retryPolicyFactory;
    protected ProxyOperationsMappingConfig mappingConfig;

    public TarantoolClientConfiguratorImpl(TarantoolClient> client) {
        this.client = client;
    }

    protected TarantoolClientConfiguratorImpl() {
        this.client = null;
    }

    @Override
    public SELF withProxyMethodMapping() {
        return withProxyMethodMapping(UnaryOperator.identity());
    }

    @Override
    public SELF withProxyMethodMapping(
        UnaryOperator builder) {
        this.mappingConfig = builder.apply(ProxyOperationsMappingConfig.builder()).build();
        return getSelf();
    }

    @Override
    public SELF withRetryingByNumberOfAttempts(int numberOfAttempts) {
        return withRetryingByNumberOfAttempts(numberOfAttempts, UnaryOperator.identity());
    }

    @Override
    public SELF withRetryingByNumberOfAttempts(
        int numberOfAttempts, UnaryOperator>> policy) {
        return withRetryingByNumberOfAttempts(numberOfAttempts, retryNetworkErrors(), policy);
    }

    @Override
    public > SELF withRetryingByNumberOfAttempts(
        int numberOfAttempts, T exceptionsCheck,
        UnaryOperator> policy) {
        return withRetrying(policy.apply(TarantoolRequestRetryPolicies.AttemptsBoundRetryPolicyFactory
            .builder(numberOfAttempts, exceptionsCheck)).build());
    }

    @Override
    public SELF withRetryingIndefinitely(
        UnaryOperator>> policy) {
        return withRetryingIndefinitely(retryNetworkErrors(), policy);
    }

    @Override
    public > SELF withRetryingIndefinitely(
        T callback, UnaryOperator> policy) {
        return withRetrying(policy.apply(TarantoolRequestRetryPolicies.InfiniteRetryPolicyFactory.builder(callback))
            .build());
    }

    @Override
    public SELF withRetrying(RequestRetryPolicyFactory factory) {
        this.retryPolicyFactory = factory;
        return getSelf();
    }

    @Override
    public TarantoolClient> build() {
        return decorate(this.client);
    }

    /**
     * Decorates provided client by user specified parameters.
     *
     * @param client Tarantool client for decorating
     * @return decorated client or the same client
     * if parameters for decorating in {@link TarantoolClientConfigurator} have not been provided
     */
    protected TarantoolClient>
    decorate(TarantoolClient> client) {
        Assert.notNull(client, "Tarantool client must not be null!");

        if (this.mappingConfig != null) {
            client = new ProxyTarantoolTupleClient(client, this.mappingConfig);
        }
        if (this.retryPolicyFactory != null) {
            client = new RetryingTarantoolTupleClient(client, this.retryPolicyFactory);
        }

        return client;
    }

    @SuppressWarnings("unchecked")
    private SELF getSelf() {
        return (SELF) this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy