io.tarantool.driver.core.space.RetryingTarantoolSpace Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cartridge-driver Show documentation
Show all versions of cartridge-driver Show documentation
Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework
package io.tarantool.driver.core.space;
import io.tarantool.driver.api.conditions.Conditions;
import io.tarantool.driver.api.cursor.TarantoolCursor;
import io.tarantool.driver.api.metadata.TarantoolSpaceMetadata;
import io.tarantool.driver.api.retry.RequestRetryPolicy;
import io.tarantool.driver.api.retry.RequestRetryPolicyFactory;
import io.tarantool.driver.api.space.TarantoolSpaceOperations;
import io.tarantool.driver.api.space.options.DeleteOptions;
import io.tarantool.driver.api.space.options.InsertManyOptions;
import io.tarantool.driver.api.space.options.InsertOptions;
import io.tarantool.driver.api.space.options.ReplaceManyOptions;
import io.tarantool.driver.api.space.options.ReplaceOptions;
import io.tarantool.driver.api.space.options.SelectOptions;
import io.tarantool.driver.api.space.options.UpdateOptions;
import io.tarantool.driver.api.space.options.UpsertOptions;
import io.tarantool.driver.api.tuple.operations.TupleOperations;
import io.tarantool.driver.exceptions.TarantoolClientException;
import io.tarantool.driver.protocol.Packable;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
/**
* Wrapper for {@link TarantoolSpaceOperations} instances which adds request retry policy to each operation
*
* @param target tuple type
* @param target tuple collection type
* @author Alexey Kuzin
*/
public class RetryingTarantoolSpace>
implements TarantoolSpaceOperations {
private final TarantoolSpaceOperations spaceOperations;
private final RequestRetryPolicyFactory retryPolicyFactory;
private final Executor executor;
/**
* Basic constructor
*
* @param spaceOperations {@link TarantoolSpaceOperations} instance which operations will be wrapped
* @param retryPolicyFactory request retrying policy factory
* @param executor executor service for retry callbacks
*/
public RetryingTarantoolSpace(
TarantoolSpaceOperations spaceOperations,
RequestRetryPolicyFactory retryPolicyFactory,
Executor executor) {
this.spaceOperations = spaceOperations;
this.retryPolicyFactory = retryPolicyFactory;
this.executor = executor;
}
@Override
public CompletableFuture delete(Conditions conditions)
throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.delete(conditions));
}
@Override
public CompletableFuture delete(Conditions conditions, DeleteOptions options)
throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.delete(conditions, options));
}
@Override
public CompletableFuture insert(T tuple) throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.insert(tuple));
}
@Override
public CompletableFuture insert(T tuple, InsertOptions options) throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.insert(tuple, options));
}
@Override
public CompletableFuture insertMany(Collection tuples) throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.insertMany(tuples));
}
@Override
public CompletableFuture insertMany(Collection tuples, InsertManyOptions options)
throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.insertMany(tuples, options));
}
@Override
public CompletableFuture replace(T tuple)
throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.replace(tuple));
}
@Override
public CompletableFuture replace(T tuple, ReplaceOptions options)
throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.replace(tuple, options));
}
@Override
public CompletableFuture replaceMany(Collection tuples)
throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.replaceMany(tuples));
}
@Override
public CompletableFuture replaceMany(Collection tuples, ReplaceManyOptions options)
throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.replaceMany(tuples, options));
}
@Override
public CompletableFuture select(Conditions conditions)
throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.select(conditions));
}
@Override
public CompletableFuture select(Conditions conditions, SelectOptions options)
throws TarantoolClientException {
return wrapOperation(() -> spaceOperations.select(conditions, options));
}
@Override
public CompletableFuture update(Conditions conditions, T tuple) {
return wrapOperation(() -> spaceOperations.update(conditions, tuple));
}
@Override
public CompletableFuture update(Conditions conditions, T tuple, UpdateOptions options) {
return wrapOperation(() -> spaceOperations.update(conditions, tuple, options));
}
@Override
public CompletableFuture update(Conditions conditions, TupleOperations operations) {
return wrapOperation(() -> spaceOperations.update(conditions, operations));
}
@Override
public CompletableFuture update(Conditions conditions, TupleOperations operations, UpdateOptions options) {
return wrapOperation(() -> spaceOperations.update(conditions, operations, options));
}
@Override
public CompletableFuture upsert(Conditions conditions, T tuple, TupleOperations operations) {
return wrapOperation(() -> spaceOperations.upsert(conditions, tuple, operations));
}
@Override
public CompletableFuture upsert(
Conditions conditions, T tuple, TupleOperations operations,
UpsertOptions options) {
return wrapOperation(() -> spaceOperations.upsert(conditions, tuple, operations, options));
}
@Override
public CompletableFuture truncate() throws TarantoolClientException {
return wrapVoidOperation(spaceOperations::truncate);
}
@Override
public TarantoolSpaceMetadata getMetadata() {
return spaceOperations.getMetadata();
}
@Override
public TarantoolCursor cursor(Conditions conditions, int batchSize) {
return spaceOperations.cursor(conditions, batchSize);
}
@Override
public TarantoolCursor cursor(Conditions conditions) {
return spaceOperations.cursor(conditions);
}
private CompletableFuture wrapOperation(Supplier> operation) {
RequestRetryPolicy retryPolicy = retryPolicyFactory.create();
return retryPolicy.wrapOperation(operation, executor);
}
private CompletableFuture wrapVoidOperation(Supplier> operation) {
RequestRetryPolicy retryPolicy = retryPolicyFactory.create();
return retryPolicy.wrapOperation(operation, executor);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy