diff --git a/src/main/java/io/tarantool/driver/api/space/options/SelectOptions.java b/src/main/java/io/tarantool/driver/api/space/options/SelectOptions.java index 81a69dc6..6bb1b210 100644 --- a/src/main/java/io/tarantool/driver/api/space/options/SelectOptions.java +++ b/src/main/java/io/tarantool/driver/api/space/options/SelectOptions.java @@ -6,6 +6,7 @@ import io.tarantool.driver.api.space.options.crud.OperationWithFullScanOptions; import io.tarantool.driver.api.space.options.crud.OperationWithForceMapCallOptions; import io.tarantool.driver.api.space.options.crud.OperationWithModeOptions; +import io.tarantool.driver.api.space.options.crud.OperationWithPreferReplicaOptions; import io.tarantool.driver.api.space.options.crud.OperationWithTimeoutOptions; import io.tarantool.driver.api.space.options.crud.OperationWithYieldEveryOptions; @@ -20,5 +21,5 @@ public interface SelectOptions> extends OperationWithBucketIdOptions, OperationWithTimeoutOptions, OperationWithFieldsOptions, OperationWithModeOptions, OperationWithBatchSizeOptions, OperationWithYieldEveryOptions, - OperationWithForceMapCallOptions, OperationWithFullScanOptions { + OperationWithForceMapCallOptions, OperationWithFullScanOptions, OperationWithPreferReplicaOptions { } diff --git a/src/main/java/io/tarantool/driver/api/space/options/crud/OperationWithPreferReplicaOptions.java b/src/main/java/io/tarantool/driver/api/space/options/crud/OperationWithPreferReplicaOptions.java new file mode 100644 index 00000000..317cd050 --- /dev/null +++ b/src/main/java/io/tarantool/driver/api/space/options/crud/OperationWithPreferReplicaOptions.java @@ -0,0 +1,34 @@ +package io.tarantool.driver.api.space.options.crud; + +import java.util.Optional; + +import io.tarantool.driver.api.space.options.Options; +import io.tarantool.driver.api.space.options.Self; +import io.tarantool.driver.api.space.options.crud.enums.ProxyOption; + +/** + * Base interface for all operation options that may have a configurable "prefer_replica". + * + * @author Belonogov Nikolay + */ +public interface OperationWithPreferReplicaOptions> + extends Options, Self { + + /** + * Sets "prefer_replica" option value to true. If true then the preferred target is one of the replicas. + * Default value is false. + * + * @return this options instance. + */ + default T preferReplica() { + addOption(ProxyOption.PREFER_REPLICA, true); + return self(); + } + + /** + * @return "prefer_replica" option value. + */ + default Optional getPreferReplica() { + return getOption(ProxyOption.PREFER_REPLICA, Boolean.class); + } +} diff --git a/src/main/java/io/tarantool/driver/api/space/options/crud/enums/ProxyOption.java b/src/main/java/io/tarantool/driver/api/space/options/crud/enums/ProxyOption.java index 21cb5db7..467b6ce3 100644 --- a/src/main/java/io/tarantool/driver/api/space/options/crud/enums/ProxyOption.java +++ b/src/main/java/io/tarantool/driver/api/space/options/crud/enums/ProxyOption.java @@ -29,7 +29,9 @@ public enum ProxyOption { FORCE_MAP_CALL("force_map_call"), - FULL_SCAN("fullscan"); + FULL_SCAN("fullscan"), + + PREFER_REPLICA("prefer_replica"); private final String name; diff --git a/src/test/java/io/tarantool/driver/integration/proxy/options/ProxySpaceSelectOptionsIT.java b/src/test/java/io/tarantool/driver/integration/proxy/options/ProxySpaceSelectOptionsIT.java index a41ed7c6..c6631932 100644 --- a/src/test/java/io/tarantool/driver/integration/proxy/options/ProxySpaceSelectOptionsIT.java +++ b/src/test/java/io/tarantool/driver/integration/proxy/options/ProxySpaceSelectOptionsIT.java @@ -286,4 +286,20 @@ public void withFullScanTest() { List crudSelectOpts = client.eval("return crud_select_opts").join(); assertEquals(true, ((HashMap) crudSelectOpts.get(0)).get(ProxyOption.FULL_SCAN.toString())); } + + @Test + public void withPreferReplicaTest() { + TarantoolSpaceOperations> operations = + client.space(TEST_SPACE_NAME); + + SelectOptions options = ProxySelectOptions.create().preferReplica(); + + assertTrue(options.getPreferReplica().isPresent()); + assertTrue(options.getPreferReplica().get()); + + operations.select(Conditions.any(), options).join(); + List crudSelectOpts = client.eval("return crud_select_opts").join(); + + assertEquals(true, ((HashMap) crudSelectOpts.get(0)).get(ProxyOption.PREFER_REPLICA.toString())); + } }