From 4526101634868d2a766c44e36a9da04d01234ed0 Mon Sep 17 00:00:00 2001 From: Alexey Kuzin Date: Wed, 23 Aug 2023 17:05:31 -0400 Subject: [PATCH] Use suppliers instead of mappers in eval and space operations This change is breaking and it is required for making on-demand mapper instantiation only in cases if the corresponding mapper is not found in the mapper cache (will be done later). --- .../driver/api/TarantoolEvalOperations.java | 14 ++-- .../driver/core/AbstractTarantoolClient.java | 14 ++-- .../driver/core/ProxyTarantoolClient.java | 13 ++-- .../driver/core/RetryingTarantoolClient.java | 12 ++-- .../core/space/ProxyTarantoolSpace.java | 69 ++++++++++--------- .../driver/core/space/TarantoolSpace.java | 49 ++++++------- 6 files changed, 90 insertions(+), 81 deletions(-) diff --git a/src/main/java/io/tarantool/driver/api/TarantoolEvalOperations.java b/src/main/java/io/tarantool/driver/api/TarantoolEvalOperations.java index a69a144bf..cfe845319 100644 --- a/src/main/java/io/tarantool/driver/api/TarantoolEvalOperations.java +++ b/src/main/java/io/tarantool/driver/api/TarantoolEvalOperations.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.function.Supplier; /** * Aggregates all value operation variants @@ -42,11 +43,11 @@ public interface TarantoolEvalOperations { * keyword return. * * @param expression lua expression, must not be null or empty - * @param resultMapper mapper for result value MessagePack entity-to-object conversion + * @param resultMapperSupplier mapper supplier for result value MessagePack entity-to-object conversion * @return some result * @throws TarantoolClientException if the client is not connected */ - CompletableFuture> eval(String expression, MessagePackValueMapper resultMapper) + CompletableFuture> eval(String expression, Supplier resultMapperSupplier) throws TarantoolClientException; /** @@ -56,11 +57,12 @@ CompletableFuture> eval(String expression, MessagePackValueMapper result * @param expression lua expression, must not be null or empty * @param arguments the list of function arguments. The object mapper specified in the client configuration * will be used for arguments conversion to MessagePack entities - * @param resultMapper mapper for result value MessagePack entity-to-object conversion + * @param resultMapperSupplier mapper supplier for result value MessagePack entity-to-object conversion * @return some result * @throws TarantoolClientException if the client is not connected */ - CompletableFuture> eval(String expression, List arguments, MessagePackValueMapper resultMapper) + CompletableFuture> eval( + String expression, List arguments, Supplier resultMapperSupplier) throws TarantoolClientException; /** @@ -70,7 +72,7 @@ CompletableFuture> eval(String expression, List arguments, MessagePac * @param expression lua expression, must not be null or empty * @param arguments the list of function arguments * @param argumentsMapper mapper for arguments object-to-MessagePack entity conversion - * @param resultMapper mapper for result value MessagePack entity-to-object conversion + * @param resultMapperSupplier mapper supplier for result value MessagePack entity-to-object conversion * @return some result * @throws TarantoolClientException if the client is not connected */ @@ -78,5 +80,5 @@ CompletableFuture> eval( String expression, List arguments, MessagePackObjectMapper argumentsMapper, - MessagePackValueMapper resultMapper) throws TarantoolClientException; + Supplier resultMapperSupplier) throws TarantoolClientException; } diff --git a/src/main/java/io/tarantool/driver/core/AbstractTarantoolClient.java b/src/main/java/io/tarantool/driver/core/AbstractTarantoolClient.java index 1ffc39a0c..399b6db47 100644 --- a/src/main/java/io/tarantool/driver/core/AbstractTarantoolClient.java +++ b/src/main/java/io/tarantool/driver/core/AbstractTarantoolClient.java @@ -517,19 +517,20 @@ public CompletableFuture> eval(String expression) throws TarantoolClient @Override public CompletableFuture> eval(String expression, List arguments) throws TarantoolClientException { - return eval(expression, arguments, config.getMessagePackMapper()); + return eval(expression, arguments, config::getMessagePackMapper); } @Override - public CompletableFuture> eval(String expression, MessagePackValueMapper resultMapper) + public CompletableFuture> eval(String expression, Supplier resultMapperSupplier) throws TarantoolClientException { - return eval(expression, Collections.emptyList(), resultMapper); + return eval(expression, Collections.emptyList(), resultMapperSupplier); } @Override - public CompletableFuture> eval(String expression, List arguments, MessagePackValueMapper resultMapper) + public CompletableFuture> eval( + String expression, List arguments, Supplier resultMapperSupplier) throws TarantoolClientException { - return eval(expression, arguments, config.getMessagePackMapper(), resultMapper); + return eval(expression, arguments, config.getMessagePackMapper(), resultMapperSupplier); } @Override @@ -537,12 +538,13 @@ public CompletableFuture> eval( String expression, List arguments, MessagePackObjectMapper argumentsMapper, - MessagePackValueMapper resultMapper) throws TarantoolClientException { + Supplier resultMapperSupplier) throws TarantoolClientException { try { TarantoolEvalRequest request = new TarantoolEvalRequest.Builder() .withExpression(expression) .withArguments(arguments) .build(argumentsMapper); + MessagePackValueMapper resultMapper = resultMapperSupplier.get(); return connectionManager().getConnection() .thenCompose(c -> c.sendRequest(request).getFuture()) .thenApply(resultMapper::fromValue); diff --git a/src/main/java/io/tarantool/driver/core/ProxyTarantoolClient.java b/src/main/java/io/tarantool/driver/core/ProxyTarantoolClient.java index ee4b935c6..e4dd0aed4 100644 --- a/src/main/java/io/tarantool/driver/core/ProxyTarantoolClient.java +++ b/src/main/java/io/tarantool/driver/core/ProxyTarantoolClient.java @@ -391,15 +391,16 @@ public CompletableFuture> eval(String expression, List arguments) thr } @Override - public CompletableFuture> eval(String expression, MessagePackValueMapper resultMapper) + public CompletableFuture> eval(String expression, Supplier resultMapperSupplier) throws TarantoolClientException { - return client.eval(expression, resultMapper); + return client.eval(expression, resultMapperSupplier); } @Override - public CompletableFuture> eval(String expression, List arguments, MessagePackValueMapper resultMapper) + public CompletableFuture> eval( + String expression, List arguments, Supplier resultMapperSupplier) throws TarantoolClientException { - return client.eval(expression, arguments, resultMapper); + return client.eval(expression, arguments, resultMapperSupplier); } @Override @@ -407,8 +408,8 @@ public CompletableFuture> eval( String expression, List arguments, MessagePackObjectMapper argumentsMapper, - MessagePackValueMapper resultMapper) throws TarantoolClientException { - return client.eval(expression, arguments, argumentsMapper, resultMapper); + Supplier resultMapperSupplier) throws TarantoolClientException { + return client.eval(expression, arguments, argumentsMapper, resultMapperSupplier); } @Override diff --git a/src/main/java/io/tarantool/driver/core/RetryingTarantoolClient.java b/src/main/java/io/tarantool/driver/core/RetryingTarantoolClient.java index 4ee0a6c99..b351f6946 100644 --- a/src/main/java/io/tarantool/driver/core/RetryingTarantoolClient.java +++ b/src/main/java/io/tarantool/driver/core/RetryingTarantoolClient.java @@ -369,24 +369,24 @@ public CompletableFuture> eval(String expression, List arguments) thr @Override public CompletableFuture> eval( String expression, - MessagePackValueMapper resultMapper) throws TarantoolClientException { - return wrapOperation(() -> client.eval(expression, resultMapper)); + Supplier resultMapperSupplier) throws TarantoolClientException { + return wrapOperation(() -> client.eval(expression, resultMapperSupplier)); } @Override public CompletableFuture> eval( String expression, List arguments, - MessagePackValueMapper resultMapper) throws TarantoolClientException { - return wrapOperation(() -> client.eval(expression, arguments, resultMapper)); + Supplier resultMapperSupplier) throws TarantoolClientException { + return wrapOperation(() -> client.eval(expression, arguments, resultMapperSupplier)); } @Override public CompletableFuture> eval( String expression, List arguments, MessagePackObjectMapper argumentsMapper, - MessagePackValueMapper resultMapper) throws TarantoolClientException { - return wrapOperation(() -> client.eval(expression, arguments, argumentsMapper, resultMapper)); + Supplier resultMapperSupplier) throws TarantoolClientException { + return wrapOperation(() -> client.eval(expression, arguments, argumentsMapper, resultMapperSupplier)); } @Override diff --git a/src/main/java/io/tarantool/driver/core/space/ProxyTarantoolSpace.java b/src/main/java/io/tarantool/driver/core/space/ProxyTarantoolSpace.java index 098b42508..414a0a3a0 100644 --- a/src/main/java/io/tarantool/driver/core/space/ProxyTarantoolSpace.java +++ b/src/main/java/io/tarantool/driver/core/space/ProxyTarantoolSpace.java @@ -44,6 +44,7 @@ import java.util.Collection; import java.util.concurrent.CompletableFuture; +import java.util.function.Supplier; /** * Basic proxy {@link TarantoolSpaceOperations} implementation, which uses calls to API functions defined in @@ -78,7 +79,7 @@ public ProxyTarantoolSpace( @Override public CompletableFuture delete(Conditions conditions) throws TarantoolClientException { - return delete(conditions, rowsMetadataTupleResultMapper(), ProxyDeleteOptions.create()); + return delete(conditions, this::rowsMetadataTupleResultMapper, ProxyDeleteOptions.create()); } @Override @@ -86,12 +87,12 @@ public CompletableFuture delete(Conditions conditions, DeleteOptions options) if (options == null) { throw new IllegalArgumentException("Options should not be null"); } - return delete(conditions, rowsMetadataTupleResultMapper(), options); + return delete(conditions, this::rowsMetadataTupleResultMapper, options); } private CompletableFuture delete( Conditions conditions, - CallResultMapper> resultMapper, + Supplier>> resultMapperSupplier, DeleteOptions options) throws TarantoolClientException { TarantoolIndexQuery indexQuery = conditions.toIndexQuery(metadataOperations, spaceMetadata); @@ -102,7 +103,7 @@ private CompletableFuture delete( .withFunctionName(operationsMapping.getDeleteFunctionName()) .withIndexQuery(indexQuery) .withArgumentsMapper(config.getMessagePackMapper()) - .withResultMapperSupplier(() -> resultMapper) + .withResultMapperSupplier(resultMapperSupplier) .withOptions(options) .build(); @@ -111,7 +112,7 @@ private CompletableFuture delete( @Override public CompletableFuture insert(T tuple) throws TarantoolClientException { - return insert(tuple, rowsMetadataTupleResultMapper(), ProxyInsertOptions.create()); + return insert(tuple, this::rowsMetadataTupleResultMapper, ProxyInsertOptions.create()); } @Override @@ -119,12 +120,12 @@ public CompletableFuture insert(T tuple, InsertOptions options) throws Tarant if (options == null) { throw new IllegalArgumentException("Options should not be null"); } - return insert(tuple, rowsMetadataTupleResultMapper(), options); + return insert(tuple, this::rowsMetadataTupleResultMapper, options); } private CompletableFuture insert( T tuple, - CallResultMapper> resultMapper, + Supplier>> resultMapperSupplier, InsertOptions options) throws TarantoolClientException { InsertProxyOperation operation = new InsertProxyOperation.Builder() @@ -133,7 +134,7 @@ private CompletableFuture insert( .withFunctionName(operationsMapping.getInsertFunctionName()) .withTuple(tuple) .withArgumentsMapper(config.getMessagePackMapper()) - .withResultMapperSupplier(() -> resultMapper) + .withResultMapperSupplier(resultMapperSupplier) .withOptions(options) .build(); @@ -142,7 +143,7 @@ private CompletableFuture insert( @Override public CompletableFuture insertMany(Collection tuples) { - return insertMany(tuples, rowsMetadataTupleResultMapper(), ProxyInsertManyOptions.create() + return insertMany(tuples, this::rowsMetadataTupleResultMapper, ProxyInsertManyOptions.create() .withStopOnError(true) .withRollbackOnError(true) ); @@ -154,12 +155,12 @@ public CompletableFuture insertMany(Collection tuples, InsertManyOptions o if (options == null) { throw new IllegalArgumentException("Options should not be null"); } - return insertMany(tuples, rowsMetadataTupleResultMapper(), options); + return insertMany(tuples, this::rowsMetadataTupleResultMapper, options); } private CompletableFuture insertMany( Collection tuples, - CallResultMapper> resultMapper, + Supplier>> resultMapperSupplier, InsertManyOptions options) throws TarantoolClientException { InsertManyProxyOperation operation = new InsertManyProxyOperation.Builder() @@ -168,7 +169,7 @@ private CompletableFuture insertMany( .withFunctionName(operationsMapping.getInsertManyFunctionName()) .withTuples(tuples) .withArgumentsMapper(config.getMessagePackMapper()) - .withResultMapperSupplier(() -> resultMapper) + .withResultMapperSupplier(resultMapperSupplier) .withOptions(options) .build(); @@ -177,7 +178,7 @@ private CompletableFuture insertMany( @Override public CompletableFuture replace(T tuple) throws TarantoolClientException { - return replace(tuple, rowsMetadataTupleResultMapper(), ProxyReplaceOptions.create()); + return replace(tuple, this::rowsMetadataTupleResultMapper, ProxyReplaceOptions.create()); } @Override @@ -185,12 +186,12 @@ public CompletableFuture replace(T tuple, ReplaceOptions options) throws Tara if (options == null) { throw new IllegalArgumentException("Options should not be null"); } - return replace(tuple, rowsMetadataTupleResultMapper(), options); + return replace(tuple, this::rowsMetadataTupleResultMapper, options); } private CompletableFuture replace( T tuple, - CallResultMapper> resultMapper, + Supplier>> resultMapperSupplier, ReplaceOptions options) throws TarantoolClientException { ReplaceProxyOperation operation = new ReplaceProxyOperation.Builder() @@ -199,7 +200,7 @@ private CompletableFuture replace( .withFunctionName(operationsMapping.getReplaceFunctionName()) .withTuple(tuple) .withArgumentsMapper(config.getMessagePackMapper()) - .withResultMapperSupplier(() -> resultMapper) + .withResultMapperSupplier(resultMapperSupplier) .withOptions(options) .build(); @@ -208,7 +209,7 @@ private CompletableFuture replace( @Override public CompletableFuture replaceMany(Collection tuples) throws TarantoolClientException { - return replaceMany(tuples, rowsMetadataTupleResultMapper(), ProxyReplaceManyOptions.create() + return replaceMany(tuples, this::rowsMetadataTupleResultMapper, ProxyReplaceManyOptions.create() .withStopOnError(true) .withRollbackOnError(true) ); @@ -219,12 +220,12 @@ public CompletableFuture replaceMany(Collection tuples, ReplaceManyOptions if (options == null) { throw new IllegalArgumentException("Options should not be null"); } - return replaceMany(tuples, rowsMetadataTupleResultMapper(), options); + return replaceMany(tuples, this::rowsMetadataTupleResultMapper, options); } private CompletableFuture replaceMany( Collection tuples, - CallResultMapper> resultMapper, + Supplier>> resultMapperSupplier, ReplaceManyOptions options) throws TarantoolClientException { ReplaceManyProxyOperation operation = new ReplaceManyProxyOperation.Builder() @@ -233,7 +234,7 @@ private CompletableFuture replaceMany( .withFunctionName(operationsMapping.getReplaceManyFunctionName()) .withTuples(tuples) .withArgumentsMapper(config.getMessagePackMapper()) - .withResultMapperSupplier(() -> resultMapper) + .withResultMapperSupplier(resultMapperSupplier) .withOptions(options) .build(); @@ -242,7 +243,7 @@ private CompletableFuture replaceMany( @Override public CompletableFuture select(Conditions conditions) throws TarantoolClientException { - return select(conditions, rowsMetadataTupleResultMapper(), ProxySelectOptions.create()); + return select(conditions, this::rowsMetadataTupleResultMapper, ProxySelectOptions.create()); } @Override @@ -252,12 +253,12 @@ public CompletableFuture select( if (options == null) { throw new IllegalArgumentException("Options should not be null"); } - return select(conditions, rowsMetadataTupleResultMapper(), options); + return select(conditions, this::rowsMetadataTupleResultMapper, options); } private CompletableFuture select( Conditions conditions, - CallResultMapper> resultMapper, + Supplier>> resultMapperSupplier, SelectOptions options) throws TarantoolClientException { SelectProxyOperation operation = new SelectProxyOperation.Builder(metadataOperations, spaceMetadata) @@ -266,7 +267,7 @@ private CompletableFuture select( .withFunctionName(operationsMapping.getSelectFunctionName()) .withConditions(conditions) .withArgumentsMapper(config.getMessagePackMapper()) - .withResultMapperSupplier(() -> resultMapper) + .withResultMapperSupplier(resultMapperSupplier) .withOptions(options) .build(); @@ -275,7 +276,7 @@ private CompletableFuture select( @Override public CompletableFuture update(Conditions conditions, T tuple) { - return update(conditions, makeOperationsFromTuple(tuple), rowsMetadataTupleResultMapper(), + return update(conditions, makeOperationsFromTuple(tuple), this::rowsMetadataTupleResultMapper, ProxyUpdateOptions.create() ); } @@ -285,7 +286,7 @@ public CompletableFuture update(Conditions conditions, T tuple, UpdateOptions if (options == null) { throw new IllegalArgumentException("Options should not be null"); } - return update(conditions, makeOperationsFromTuple(tuple), rowsMetadataTupleResultMapper(), options); + return update(conditions, makeOperationsFromTuple(tuple), this::rowsMetadataTupleResultMapper, options); } /** @@ -298,7 +299,7 @@ public CompletableFuture update(Conditions conditions, T tuple, UpdateOptions @Override public CompletableFuture update(Conditions conditions, TupleOperations operations) { - return update(conditions, operations, rowsMetadataTupleResultMapper(), ProxyUpdateOptions.create()); + return update(conditions, operations, this::rowsMetadataTupleResultMapper, ProxyUpdateOptions.create()); } @Override @@ -306,13 +307,13 @@ public CompletableFuture update(Conditions conditions, TupleOperations operat if (options == null) { throw new IllegalArgumentException("Options should not be null"); } - return update(conditions, operations, rowsMetadataTupleResultMapper(), options); + return update(conditions, operations, this::rowsMetadataTupleResultMapper, options); } private CompletableFuture update( Conditions conditions, TupleOperations operations, - CallResultMapper> resultMapper, + Supplier>> resultMapperSupplier, UpdateOptions options) { TarantoolIndexQuery indexQuery = conditions.toIndexQuery(metadataOperations, spaceMetadata); @@ -323,7 +324,7 @@ private CompletableFuture update( .withIndexQuery(indexQuery) .withTupleOperation(operations) .withArgumentsMapper(config.getMessagePackMapper()) - .withResultMapperSupplier(() -> resultMapper) + .withResultMapperSupplier(resultMapperSupplier) .withOptions(options) .build(); @@ -332,7 +333,7 @@ private CompletableFuture update( @Override public CompletableFuture upsert(Conditions conditions, T tuple, TupleOperations operations) { - return upsert(conditions, tuple, operations, rowsMetadataTupleResultMapper(), ProxyUpsertOptions.create()); + return upsert(conditions, tuple, operations, this::rowsMetadataTupleResultMapper, ProxyUpsertOptions.create()); } @Override @@ -342,14 +343,14 @@ public CompletableFuture upsert( if (options == null) { throw new IllegalArgumentException("Options should not be null"); } - return upsert(conditions, tuple, operations, rowsMetadataTupleResultMapper(), options); + return upsert(conditions, tuple, operations, this::rowsMetadataTupleResultMapper, options); } private CompletableFuture upsert( Conditions conditions, T tuple, TupleOperations operations, - CallResultMapper> resultMapper, + Supplier>> resultMapperSupplier, UpsertOptions options) { UpsertProxyOperation operation = new UpsertProxyOperation.Builder() @@ -359,7 +360,7 @@ private CompletableFuture upsert( .withTuple(tuple) .withTupleOperation(operations) .withArgumentsMapper(config.getMessagePackMapper()) - .withResultMapperSupplier(() -> resultMapper) + .withResultMapperSupplier(resultMapperSupplier) .withOptions(options) .build(); diff --git a/src/main/java/io/tarantool/driver/core/space/TarantoolSpace.java b/src/main/java/io/tarantool/driver/core/space/TarantoolSpace.java index 56ad7cb38..c69896b5c 100644 --- a/src/main/java/io/tarantool/driver/core/space/TarantoolSpace.java +++ b/src/main/java/io/tarantool/driver/core/space/TarantoolSpace.java @@ -31,6 +31,7 @@ import java.util.List; import java.util.Optional; import java.util.concurrent.CompletableFuture; +import java.util.function.Supplier; import java.util.stream.Collectors; /** @@ -61,10 +62,10 @@ public TarantoolSpace( @Override public CompletableFuture delete(Conditions conditions) throws TarantoolClientException { - return delete(conditions, arrayTupleResultMapper()); + return delete(conditions, this::arrayTupleResultMapper); } - private CompletableFuture delete(Conditions conditions, MessagePackValueMapper resultMapper) + private CompletableFuture delete(Conditions conditions, Supplier resultMapperSupplier) throws TarantoolClientException { try { TarantoolIndexQuery indexQuery = conditions.toIndexQuery(metadataOperations, spaceMetadata); @@ -75,7 +76,7 @@ private CompletableFuture delete(Conditions conditions, MessagePackValueMappe .withKeyValues(indexQuery.getKeyValues()) .build(config.getMessagePackMapper()); - return sendRequest(request, resultMapper); + return sendRequest(request, resultMapperSupplier); } catch (TarantoolProtocolException e) { throw new TarantoolClientException(e); } @@ -83,7 +84,7 @@ private CompletableFuture delete(Conditions conditions, MessagePackValueMappe @Override public CompletableFuture insert(T tuple) throws TarantoolClientException { - return insert(tuple, arrayTupleResultMapper()); + return insert(tuple, this::arrayTupleResultMapper); } @Override @@ -94,7 +95,7 @@ public CompletableFuture insertMany(Collection tuples) throws TarantoolCli "Standalone node API does not support inserting several tuples at once yet"); } - private CompletableFuture insert(T tuple, MessagePackValueMapper resultMapper) + private CompletableFuture insert(T tuple, Supplier resultMapperSupplier) throws TarantoolClientException { try { TarantoolInsertRequest request = new TarantoolInsertRequest.Builder() @@ -102,7 +103,7 @@ private CompletableFuture insert(T tuple, MessagePackValueMapper resultMapper .withTuple(tuple) .build(config.getMessagePackMapper()); - return sendRequest(request, resultMapper); + return sendRequest(request, resultMapperSupplier); } catch (TarantoolProtocolException e) { throw new TarantoolClientException(e); } @@ -110,7 +111,7 @@ private CompletableFuture insert(T tuple, MessagePackValueMapper resultMapper @Override public CompletableFuture replace(T tuple) throws TarantoolClientException { - return replace(tuple, arrayTupleResultMapper()); + return replace(tuple, this::arrayTupleResultMapper); } @Override @@ -121,7 +122,7 @@ public CompletableFuture replaceMany(Collection tuples) throws TarantoolCl "Standalone node API does not support replacing several tuples at once yet"); } - private CompletableFuture replace(T tuple, MessagePackValueMapper resultMapper) + private CompletableFuture replace(T tuple, Supplier resultMapperSupplier) throws TarantoolClientException { try { TarantoolReplaceRequest request = new TarantoolReplaceRequest.Builder() @@ -129,7 +130,7 @@ private CompletableFuture replace(T tuple, MessagePackValueMapper resultMappe .withTuple(tuple) .build(config.getMessagePackMapper()); - return sendRequest(request, resultMapper); + return sendRequest(request, resultMapperSupplier); } catch (TarantoolProtocolException e) { throw new TarantoolClientException(e); } @@ -137,10 +138,10 @@ private CompletableFuture replace(T tuple, MessagePackValueMapper resultMappe @Override public CompletableFuture select(Conditions conditions) throws TarantoolClientException { - return select(conditions, arrayTupleResultMapper()); + return select(conditions, this::arrayTupleResultMapper); } - private CompletableFuture select(Conditions conditions, MessagePackValueMapper resultMapper) + private CompletableFuture select(Conditions conditions, Supplier resultMapperSupplier) throws TarantoolClientException { try { TarantoolIndexQuery indexQuery = conditions.toIndexQuery(metadataOperations, spaceMetadata); @@ -153,7 +154,7 @@ private CompletableFuture select(Conditions conditions, MessagePackValueMappe .withOffset(conditions.getOffset()) .build(config.getMessagePackMapper()); - return sendRequest(request, resultMapper); + return sendRequest(request, resultMapperSupplier); } catch (TarantoolProtocolException e) { throw new TarantoolClientException(e); } @@ -161,7 +162,7 @@ private CompletableFuture select(Conditions conditions, MessagePackValueMappe @Override public CompletableFuture update(Conditions conditions, T tuple) { - return update(conditions, makeOperationsFromTuple(tuple), arrayTupleResultMapper()); + return update(conditions, makeOperationsFromTuple(tuple), this::arrayTupleResultMapper); } /** @@ -174,13 +175,13 @@ public CompletableFuture update(Conditions conditions, T tuple) { @Override public CompletableFuture update(Conditions conditions, TupleOperations operations) { - return update(conditions, operations, arrayTupleResultMapper()); + return update(conditions, operations, this::arrayTupleResultMapper); } private CompletableFuture update( Conditions conditions, TupleOperations operations, - MessagePackValueMapper resultMapper) + Supplier resultMapperSupplier) throws TarantoolClientException { try { TarantoolIndexQuery indexQuery = conditions.toIndexQuery(metadataOperations, spaceMetadata); @@ -199,7 +200,7 @@ private CompletableFuture update( .withTupleOperations(fillFieldIndexFromMetadata(operations)) .build(config.getMessagePackMapper()); - return sendRequest(request, resultMapper); + return sendRequest(request, resultMapperSupplier); } catch (TarantoolProtocolException e) { throw new TarantoolClientException(e); } @@ -207,14 +208,14 @@ private CompletableFuture update( @Override public CompletableFuture upsert(Conditions conditions, T tuple, TupleOperations operations) { - return upsert(conditions, tuple, operations, arrayTupleResultMapper()); + return upsert(conditions, tuple, operations, this::arrayTupleResultMapper); } private CompletableFuture upsert( Conditions conditions, T tuple, TupleOperations operations, - MessagePackValueMapper resultMapper) + Supplier resultMapperSupplier) throws TarantoolClientException { try { TarantoolIndexQuery indexQuery = conditions.toIndexQuery(metadataOperations, spaceMetadata); @@ -226,7 +227,7 @@ private CompletableFuture upsert( .withTupleOperations(fillFieldIndexFromMetadata(operations)) .build(config.getMessagePackMapper()); - return sendRequest(request, resultMapper); + return sendRequest(request, resultMapperSupplier); } catch (TarantoolProtocolException e) { throw new TarantoolClientException(e); } @@ -234,17 +235,17 @@ private CompletableFuture upsert( @Override public CompletableFuture truncate() throws TarantoolClientException { - return truncate(arrayTupleResultMapper()); + return truncate(this::arrayTupleResultMapper); } - private CompletableFuture truncate(MessagePackValueMapper resultMapper) + private CompletableFuture truncate(Supplier resultMapperSupplier) throws TarantoolClientException { try { String spaceName = spaceMetadata.getSpaceName(); TarantoolCallRequest request = new TarantoolCallRequest.Builder() .withFunctionName("box.space." + spaceName + ":truncate") .build(config.getMessagePackMapper()); - return sendRequest(request, resultMapper) + return sendRequest(request, resultMapperSupplier) .thenApply(v -> TarantoolVoidResult.INSTANCE.value()); } catch (TarantoolProtocolException e) { throw new TarantoolClientException(e); @@ -259,7 +260,9 @@ private CompletableFuture truncate(MessagePackValueMapper resultMapper) */ protected abstract MessagePackValueMapper arrayTupleResultMapper(); - private CompletableFuture sendRequest(TarantoolRequest request, MessagePackValueMapper resultMapper) { + private CompletableFuture sendRequest( + TarantoolRequest request, Supplier resultMapperSupplier) { + MessagePackValueMapper resultMapper = resultMapperSupplier.get(); return connectionManager.getConnection() .thenCompose(c -> c.sendRequest(request).getFuture()) .thenApply(resultMapper::fromValue);