From c8bfc3f5027ac5e4385b45264b651408e6aa4f5c Mon Sep 17 00:00:00 2001 From: Belonogov Nikolay Date: Thu, 14 Sep 2023 02:19:53 +0300 Subject: [PATCH] Fix duplicates in CRUD options - Remove CRUDOptions classes like a duplicates. - Add options directly to the argument list without CRUDOptions class builders. - Change Map:toString() test in the ProxyOperationBuildersTest class with EnumMap specific. - Add test in ProxySpaceSelectOptionsIT class with after and first options. Closes #417. --- CHANGELOG.md | 8 +- .../options/interfaces/SelectOptions.java | 5 +- .../proxy/CRUDAbstractOperationOptions.java | 58 ------------- .../driver/core/proxy/CRUDBaseOptions.java | 53 ------------ .../driver/core/proxy/CRUDBatchOptions.java | 50 ----------- .../core/proxy/CRUDBucketIdOptions.java | 47 ----------- .../driver/core/proxy/CRUDDeleteOptions.java | 47 ----------- .../driver/core/proxy/CRUDInsertOptions.java | 47 ----------- .../driver/core/proxy/CRUDReplaceOptions.java | 47 ----------- .../driver/core/proxy/CRUDReturnOptions.java | 48 ----------- .../driver/core/proxy/CRUDSelectOptions.java | 83 ------------------- .../driver/core/proxy/CRUDUpdateOptions.java | 47 ----------- .../driver/core/proxy/CRUDUpsertOptions.java | 47 ----------- .../core/proxy/DeleteProxyOperation.java | 10 +-- .../core/proxy/InsertManyProxyOperation.java | 14 +--- .../core/proxy/InsertProxyOperation.java | 9 +- .../core/proxy/ReplaceManyProxyOperation.java | 14 +--- .../core/proxy/ReplaceProxyOperation.java | 9 +- .../core/proxy/SelectProxyOperation.java | 27 +++--- .../core/proxy/TruncateProxyOperation.java | 7 +- .../core/proxy/UpdateProxyOperation.java | 9 +- .../core/proxy/UpsertProxyOperation.java | 9 +- .../proxy/ProxyOperationBuildersTest.java | 21 +++-- .../options/ProxySpaceSelectOptionsIT.java | 69 ++++++++++++++- 24 files changed, 127 insertions(+), 658 deletions(-) delete mode 100644 src/main/java/io/tarantool/driver/core/proxy/CRUDAbstractOperationOptions.java delete mode 100644 src/main/java/io/tarantool/driver/core/proxy/CRUDBaseOptions.java delete mode 100644 src/main/java/io/tarantool/driver/core/proxy/CRUDBatchOptions.java delete mode 100644 src/main/java/io/tarantool/driver/core/proxy/CRUDBucketIdOptions.java delete mode 100644 src/main/java/io/tarantool/driver/core/proxy/CRUDDeleteOptions.java delete mode 100644 src/main/java/io/tarantool/driver/core/proxy/CRUDInsertOptions.java delete mode 100644 src/main/java/io/tarantool/driver/core/proxy/CRUDReplaceOptions.java delete mode 100644 src/main/java/io/tarantool/driver/core/proxy/CRUDReturnOptions.java delete mode 100644 src/main/java/io/tarantool/driver/core/proxy/CRUDSelectOptions.java delete mode 100644 src/main/java/io/tarantool/driver/core/proxy/CRUDUpdateOptions.java delete mode 100644 src/main/java/io/tarantool/driver/core/proxy/CRUDUpsertOptions.java diff --git a/CHANGELOG.md b/CHANGELOG.md index a5e19831..53eca11a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,12 @@ ### API changes -- Add "mode" option for crud select operation ([#107](https://github.com/tarantool/cartridge-java/issues/107)) -- Change using of proxy client parameters (mode, rollback_on_error, stop_on_error) with enum classes ([#419](https://github.com/tarantool/cartridge-java/issues/419)) +- Add `"mode"` option for crud select operation ([#107](https://github.com/tarantool/cartridge-java/issues/107)) +- Change using of proxy client parameters (`mode`, `rollback_on_error`, `stop_on_error`) with enum classes ([#419](https://github.com/tarantool/cartridge-java/issues/419)) +- Add `"after"` and `"first"` option for select operation like API in SelectOptions ([#417](https://github.com/tarantool/cartridge-java/issues/417)) +#### Note: +- When using `Conditions:limit()` and `SelectOptions:first()` at the same time, the `smallest` one is selected. +- When using `Conditions:after()` and `SelectOptions:after()` at the same time, the `Conditions:after()` is selected. ### Bugfixes diff --git a/src/main/java/io/tarantool/driver/api/space/options/interfaces/SelectOptions.java b/src/main/java/io/tarantool/driver/api/space/options/interfaces/SelectOptions.java index d22a3372..5dd47722 100644 --- a/src/main/java/io/tarantool/driver/api/space/options/interfaces/SelectOptions.java +++ b/src/main/java/io/tarantool/driver/api/space/options/interfaces/SelectOptions.java @@ -1,8 +1,10 @@ package io.tarantool.driver.api.space.options.interfaces; import io.tarantool.driver.api.space.options.contracts.OperationWIthBatchSizeOptions; +import io.tarantool.driver.api.space.options.contracts.OperationWithAfterOptions; import io.tarantool.driver.api.space.options.contracts.OperationWithBucketIdOptions; import io.tarantool.driver.api.space.options.contracts.OperationWithFieldsOptions; +import io.tarantool.driver.api.space.options.contracts.OperationWithFirstOptions; import io.tarantool.driver.api.space.options.contracts.OperationWithModeOptions; import io.tarantool.driver.api.space.options.contracts.OperationWithTimeoutOptions; @@ -14,5 +16,6 @@ */ public interface SelectOptions> extends OperationWithBucketIdOptions, OperationWithTimeoutOptions, OperationWithFieldsOptions, - OperationWithModeOptions, OperationWIthBatchSizeOptions { + OperationWithModeOptions, OperationWIthBatchSizeOptions, OperationWithAfterOptions, + OperationWithFirstOptions { } diff --git a/src/main/java/io/tarantool/driver/core/proxy/CRUDAbstractOperationOptions.java b/src/main/java/io/tarantool/driver/core/proxy/CRUDAbstractOperationOptions.java deleted file mode 100644 index edada5da..00000000 --- a/src/main/java/io/tarantool/driver/core/proxy/CRUDAbstractOperationOptions.java +++ /dev/null @@ -1,58 +0,0 @@ -package io.tarantool.driver.core.proxy; - -import io.tarantool.driver.api.space.options.enums.ProxyOption; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -/** - * This class is not part of the public API. - *

- * An abstract class necessary for implementing CRT (curiously recurring template) - * pattern for the cluster proxy operation options builders. - * - * @author Alexey Kuzin - */ -abstract class CRUDAbstractOperationOptions { - - private final Map resultMap = new HashMap<>(); - - protected void addOption(ProxyOption option, Optional value) { - if (value.isPresent()) { - resultMap.put(option.toString(), value.get()); - } - } - - /** - * Return serializable options representation. - * - * @return a map - */ - public Map asMap() { - return resultMap; - } - - /** - * Inheritable Builder for cluster proxy operation options. - *

- * This abstract class is necessary for implementing fluent builder inheritance. - * The solution with {@code self()} method allows to avoid weird java - * compiler errors when you cannot call the inherited methods from derived - * concrete {@code Builder} classes because their type erasure doesn't - * correspond to the {@code AbstractBuilder} type. - *

- * The {@code self()} method must be implemented only in the derived concrete - * classes. These concrete classes are used to work with the operation - * options in the calling code. They doesn't require to specify the generic - * types and therefore are more convenient. Also they ensure that the right - * combination of generic types will be used to avoid potential inheritance - * flaws. - */ - protected abstract static - class AbstractBuilder> { - abstract B self(); - - public abstract O build(); - } -} diff --git a/src/main/java/io/tarantool/driver/core/proxy/CRUDBaseOptions.java b/src/main/java/io/tarantool/driver/core/proxy/CRUDBaseOptions.java deleted file mode 100644 index 30c2bf02..00000000 --- a/src/main/java/io/tarantool/driver/core/proxy/CRUDBaseOptions.java +++ /dev/null @@ -1,53 +0,0 @@ -package io.tarantool.driver.core.proxy; - -import io.tarantool.driver.api.space.options.enums.ProxyOption; - -import java.util.Optional; - -/** - * This class is not part of the public API. - *

- * Represent basic options for all cluster operations - * - * @author Alexey Kuzin - * @author Artyom Dubinin - */ -class CRUDBaseOptions extends CRUDAbstractOperationOptions { - - protected > - CRUDBaseOptions(AbstractBuilder builder) { - addOption(ProxyOption.TIMEOUT, builder.timeout); - } - - /** - * Inheritable Builder for basic cluster proxy operation options. - * - * @see CRUDAbstractOperationOptions.AbstractBuilder - */ - protected abstract static class AbstractBuilder> - extends CRUDAbstractOperationOptions.AbstractBuilder { - protected Optional timeout = Optional.empty(); - - public B withTimeout(Optional timeout) { - this.timeout = timeout; - return self(); - } - } - - /** - * Concrete Builder implementation for basic cluster proxy operation options. - */ - protected static final class Builder - extends AbstractBuilder { - - @Override - Builder self() { - return this; - } - - @Override - public CRUDBaseOptions build() { - return new CRUDBaseOptions(this); - } - } -} diff --git a/src/main/java/io/tarantool/driver/core/proxy/CRUDBatchOptions.java b/src/main/java/io/tarantool/driver/core/proxy/CRUDBatchOptions.java deleted file mode 100644 index dbce46e1..00000000 --- a/src/main/java/io/tarantool/driver/core/proxy/CRUDBatchOptions.java +++ /dev/null @@ -1,50 +0,0 @@ -package io.tarantool.driver.core.proxy; - -import io.tarantool.driver.api.space.options.enums.ProxyOption; - -import java.util.Optional; - -/** - * This class is not part of the public API. - *

- * Represent options for proxy cluster batch operations - * - * @author Alexey Kuzin - */ -final class CRUDBatchOptions extends CRUDReturnOptions { - - private > - CRUDBatchOptions(AbstractBuilder builder) { - super(builder); - addOption(ProxyOption.STOP_ON_ERROR, builder.stopOnError); - addOption(ProxyOption.ROLLBACK_ON_ERROR, builder.rollbackOnError); - } - - protected abstract static class AbstractBuilder> - extends CRUDReturnOptions.AbstractBuilder { - private Optional stopOnError = Optional.empty(); - private Optional rollbackOnError = Optional.empty(); - - public B withStopOnError(Optional stopOnError) { - this.stopOnError = stopOnError; - return self(); - } - - public B withRollbackOnError(Optional rollbackOnError) { - this.rollbackOnError = rollbackOnError; - return self(); - } - } - - protected static final class Builder extends AbstractBuilder { - @Override - Builder self() { - return this; - } - - @Override - public CRUDBatchOptions build() { - return new CRUDBatchOptions(this); - } - } -} diff --git a/src/main/java/io/tarantool/driver/core/proxy/CRUDBucketIdOptions.java b/src/main/java/io/tarantool/driver/core/proxy/CRUDBucketIdOptions.java deleted file mode 100644 index 821d14a9..00000000 --- a/src/main/java/io/tarantool/driver/core/proxy/CRUDBucketIdOptions.java +++ /dev/null @@ -1,47 +0,0 @@ -package io.tarantool.driver.core.proxy; - -import io.tarantool.driver.api.space.options.enums.ProxyOption; - -import java.util.Optional; - -/** - * This class is not part of the public API. - *

- * Represent bucket id options for cluster proxy operations. - * Options for functions that uses bucket id to find storage location. - * - * @author Alexey Kuzin - * @author Artyom Dubinin - */ -class CRUDBucketIdOptions extends CRUDBaseOptions { - - protected > - CRUDBucketIdOptions(CRUDBucketIdOptions.AbstractBuilder builder) { - super(builder); - addOption(ProxyOption.BUCKET_ID, builder.bucketId); - } - - protected abstract static - class AbstractBuilder> - extends CRUDBaseOptions.AbstractBuilder { - private Optional bucketId = Optional.empty(); - - public B withBucketId(Optional bucketId) { - this.bucketId = bucketId; - return self(); - } - } - - protected static final class Builder extends AbstractBuilder { - - @Override - CRUDBucketIdOptions.Builder self() { - return this; - } - - @Override - public CRUDBucketIdOptions build() { - return new CRUDBucketIdOptions(this); - } - } -} diff --git a/src/main/java/io/tarantool/driver/core/proxy/CRUDDeleteOptions.java b/src/main/java/io/tarantool/driver/core/proxy/CRUDDeleteOptions.java deleted file mode 100644 index 445526c1..00000000 --- a/src/main/java/io/tarantool/driver/core/proxy/CRUDDeleteOptions.java +++ /dev/null @@ -1,47 +0,0 @@ -package io.tarantool.driver.core.proxy; - -import io.tarantool.driver.api.space.options.enums.ProxyOption; - -import java.util.List; -import java.util.Optional; - -/** - * This class is not part of the public API. - *

- * Represent options for cluster delete operation. - * - * @author Alexey Kuzin - * @author Artyom Dubinin - */ -class CRUDDeleteOptions extends CRUDBucketIdOptions { - - protected > - CRUDDeleteOptions(CRUDDeleteOptions.AbstractBuilder builder) { - super(builder); - addOption(ProxyOption.FIELDS, builder.fields); - } - - protected abstract static - class AbstractBuilder> - extends CRUDBucketIdOptions.AbstractBuilder { - private Optional fields = Optional.empty(); - - public B withFields(Optional fields) { - this.fields = fields; - return self(); - } - } - - protected static final class Builder extends AbstractBuilder { - - @Override - CRUDDeleteOptions.Builder self() { - return this; - } - - @Override - public CRUDDeleteOptions build() { - return new CRUDDeleteOptions(this); - } - } -} diff --git a/src/main/java/io/tarantool/driver/core/proxy/CRUDInsertOptions.java b/src/main/java/io/tarantool/driver/core/proxy/CRUDInsertOptions.java deleted file mode 100644 index 8f4abc4d..00000000 --- a/src/main/java/io/tarantool/driver/core/proxy/CRUDInsertOptions.java +++ /dev/null @@ -1,47 +0,0 @@ -package io.tarantool.driver.core.proxy; - -import io.tarantool.driver.api.space.options.enums.ProxyOption; - -import java.util.List; -import java.util.Optional; - -/** - * This class is not part of the public API. - *

- * Represent options for cluster insert operation. - * - * @author Alexey Kuzin - * @author Artyom Dubinin - */ -class CRUDInsertOptions extends CRUDBucketIdOptions { - - protected > - CRUDInsertOptions(CRUDInsertOptions.AbstractBuilder builder) { - super(builder); - addOption(ProxyOption.FIELDS, builder.fields); - } - - protected abstract static - class AbstractBuilder> - extends CRUDBucketIdOptions.AbstractBuilder { - private Optional fields = Optional.empty(); - - public B withFields(Optional fields) { - this.fields = fields; - return self(); - } - } - - protected static final class Builder extends AbstractBuilder { - - @Override - CRUDInsertOptions.Builder self() { - return this; - } - - @Override - public CRUDInsertOptions build() { - return new CRUDInsertOptions(this); - } - } -} diff --git a/src/main/java/io/tarantool/driver/core/proxy/CRUDReplaceOptions.java b/src/main/java/io/tarantool/driver/core/proxy/CRUDReplaceOptions.java deleted file mode 100644 index 78ce1312..00000000 --- a/src/main/java/io/tarantool/driver/core/proxy/CRUDReplaceOptions.java +++ /dev/null @@ -1,47 +0,0 @@ -package io.tarantool.driver.core.proxy; - -import io.tarantool.driver.api.space.options.enums.ProxyOption; - -import java.util.List; -import java.util.Optional; - -/** - * This class is not part of the public API. - *

- * Represent options for cluster replace operation. - * - * @author Alexey Kuzin - * @author Artyom Dubinin - */ -class CRUDReplaceOptions extends CRUDBucketIdOptions { - - protected > - CRUDReplaceOptions(CRUDReplaceOptions.AbstractBuilder builder) { - super(builder); - addOption(ProxyOption.FIELDS, builder.fields); - } - - protected abstract static - class AbstractBuilder> - extends CRUDBucketIdOptions.AbstractBuilder { - private Optional fields = Optional.empty(); - - public B withFields(Optional fields) { - this.fields = fields; - return self(); - } - } - - protected static final class Builder extends AbstractBuilder { - - @Override - CRUDReplaceOptions.Builder self() { - return this; - } - - @Override - public CRUDReplaceOptions build() { - return new CRUDReplaceOptions(this); - } - } -} diff --git a/src/main/java/io/tarantool/driver/core/proxy/CRUDReturnOptions.java b/src/main/java/io/tarantool/driver/core/proxy/CRUDReturnOptions.java deleted file mode 100644 index d7d74b94..00000000 --- a/src/main/java/io/tarantool/driver/core/proxy/CRUDReturnOptions.java +++ /dev/null @@ -1,48 +0,0 @@ -package io.tarantool.driver.core.proxy; - -import io.tarantool.driver.api.space.options.enums.ProxyOption; - -import java.util.List; -import java.util.Optional; - -/** - * This class is not part of the public API. - *

- * Represent returned options for cluster proxy operations. - * The return set of fields of the tuples can be specified. - * - * @author Alexey Kuzin - * @author Artyom Dubinin - */ -class CRUDReturnOptions extends CRUDBaseOptions { - - protected > - CRUDReturnOptions(CRUDReturnOptions.AbstractBuilder builder) { - super(builder); - addOption(ProxyOption.FIELDS, builder.fields); - } - - protected abstract static - class AbstractBuilder> - extends CRUDBaseOptions.AbstractBuilder { - private Optional fields = Optional.empty(); - - public B withFields(Optional fields) { - this.fields = fields; - return self(); - } - } - - protected static final class Builder extends AbstractBuilder { - - @Override - CRUDReturnOptions.Builder self() { - return this; - } - - @Override - public CRUDReturnOptions build() { - return new CRUDReturnOptions(this); - } - } -} diff --git a/src/main/java/io/tarantool/driver/core/proxy/CRUDSelectOptions.java b/src/main/java/io/tarantool/driver/core/proxy/CRUDSelectOptions.java deleted file mode 100644 index ace205c4..00000000 --- a/src/main/java/io/tarantool/driver/core/proxy/CRUDSelectOptions.java +++ /dev/null @@ -1,83 +0,0 @@ -package io.tarantool.driver.core.proxy; - -import io.tarantool.driver.api.space.options.enums.ProxyOption; -import io.tarantool.driver.protocol.Packable; - -import java.util.List; -import java.util.Optional; - -/** - * This class is not part of the public API. - *

- * Represent options for select cluster proxy operation - * - * @author Sergey Volgin - * @author Alexey Kuzin - * @author Artyom Dubinin - */ -final class CRUDSelectOptions extends CRUDBucketIdOptions { - - private > CRUDSelectOptions(AbstractBuilder builder) { - super(builder); - - addOption(ProxyOption.FIRST, builder.first); - addOption(ProxyOption.AFTER, builder.after); - addOption(ProxyOption.BATCH_SIZE, builder.batchSize); - addOption(ProxyOption.FIELDS, builder.fields); - addOption(ProxyOption.MODE, builder.mode); - } - - /** - * Inheritable Builder for select cluster proxy operation options. - * - * @see CRUDAbstractOperationOptions.AbstractBuilder - */ - protected abstract static class AbstractBuilder> - extends CRUDBucketIdOptions.AbstractBuilder { - private Optional first = Optional.empty(); - private Optional after = Optional.empty(); - private Optional batchSize = Optional.empty(); - private Optional fields = Optional.empty(); - private Optional mode = Optional.empty(); - - public B withSelectLimit(Optional first) { - this.first = first; - return self(); - } - - public B withSelectBatchSize(Optional batchSize) { - this.batchSize = batchSize; - return self(); - } - - public B withSelectAfter(Optional after) { - this.after = after; - return self(); - } - - public B withFields(Optional fields) { - this.fields = fields; - return self(); - } - - public B withMode(Optional mode) { - this.mode = mode; - return self(); - } - } - - /** - * Concrete Builder implementation for select cluster proxy operation options. - */ - protected static final class Builder extends AbstractBuilder { - @Override - Builder self() { - return this; - } - - @Override - public CRUDSelectOptions build() { - return new CRUDSelectOptions(this); - } - } -} diff --git a/src/main/java/io/tarantool/driver/core/proxy/CRUDUpdateOptions.java b/src/main/java/io/tarantool/driver/core/proxy/CRUDUpdateOptions.java deleted file mode 100644 index b6fece9f..00000000 --- a/src/main/java/io/tarantool/driver/core/proxy/CRUDUpdateOptions.java +++ /dev/null @@ -1,47 +0,0 @@ -package io.tarantool.driver.core.proxy; - -import io.tarantool.driver.api.space.options.enums.ProxyOption; - -import java.util.List; -import java.util.Optional; - -/** - * This class is not part of the public API. - *

- * Represent options for cluster update operation. - * - * @author Alexey Kuzin - * @author Artyom Dubinin - */ -class CRUDUpdateOptions extends CRUDBucketIdOptions { - - protected > - CRUDUpdateOptions(CRUDUpdateOptions.AbstractBuilder builder) { - super(builder); - addOption(ProxyOption.FIELDS, builder.fields); - } - - protected abstract static - class AbstractBuilder> - extends CRUDBucketIdOptions.AbstractBuilder { - private Optional fields = Optional.empty(); - - public B withFields(Optional fields) { - this.fields = fields; - return self(); - } - } - - protected static final class Builder extends AbstractBuilder { - - @Override - CRUDUpdateOptions.Builder self() { - return this; - } - - @Override - public CRUDUpdateOptions build() { - return new CRUDUpdateOptions(this); - } - } -} diff --git a/src/main/java/io/tarantool/driver/core/proxy/CRUDUpsertOptions.java b/src/main/java/io/tarantool/driver/core/proxy/CRUDUpsertOptions.java deleted file mode 100644 index a24f48b4..00000000 --- a/src/main/java/io/tarantool/driver/core/proxy/CRUDUpsertOptions.java +++ /dev/null @@ -1,47 +0,0 @@ -package io.tarantool.driver.core.proxy; - -import io.tarantool.driver.api.space.options.enums.ProxyOption; - -import java.util.List; -import java.util.Optional; - -/** - * This class is not part of the public API. - *

- * Represent options for cluster upsert operation. - * - * @author Alexey Kuzin - * @author Artyom Dubinin - */ -class CRUDUpsertOptions extends CRUDBucketIdOptions { - - protected > - CRUDUpsertOptions(CRUDUpsertOptions.AbstractBuilder builder) { - super(builder); - addOption(ProxyOption.FIELDS, builder.fields); - } - - protected abstract static - class AbstractBuilder> - extends CRUDBucketIdOptions.AbstractBuilder { - private Optional fields = Optional.empty(); - - public B withFields(Optional fields) { - this.fields = fields; - return self(); - } - } - - protected static final class Builder extends AbstractBuilder { - - @Override - CRUDUpsertOptions.Builder self() { - return this; - } - - @Override - public CRUDUpsertOptions build() { - return new CRUDUpsertOptions(this); - } - } -} diff --git a/src/main/java/io/tarantool/driver/core/proxy/DeleteProxyOperation.java b/src/main/java/io/tarantool/driver/core/proxy/DeleteProxyOperation.java index 92a6be97..12f9085d 100644 --- a/src/main/java/io/tarantool/driver/core/proxy/DeleteProxyOperation.java +++ b/src/main/java/io/tarantool/driver/core/proxy/DeleteProxyOperation.java @@ -32,7 +32,7 @@ private DeleteProxyOperation( * The builder for this class. */ public static final class Builder - extends GenericOperationsBuilder> { + extends GenericOperationsBuilder, Builder> { private TarantoolIndexQuery indexQuery; public Builder() { @@ -49,13 +49,7 @@ public Builder withIndexQuery(TarantoolIndexQuery indexQuery) { } public DeleteProxyOperation build() { - CRUDBucketIdOptions requestOptions = new CRUDDeleteOptions.Builder() - .withTimeout(options.getTimeout()) - .withBucketId(options.getBucketId()) - .withFields(options.getFields()) - .build(); - - List arguments = Arrays.asList(spaceName, indexQuery.getKeyValues(), requestOptions.asMap()); + List arguments = Arrays.asList(spaceName, indexQuery.getKeyValues(), options.asMap()); return new DeleteProxyOperation<>( this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper); diff --git a/src/main/java/io/tarantool/driver/core/proxy/InsertManyProxyOperation.java b/src/main/java/io/tarantool/driver/core/proxy/InsertManyProxyOperation.java index 7adf3cfa..0aade92d 100644 --- a/src/main/java/io/tarantool/driver/core/proxy/InsertManyProxyOperation.java +++ b/src/main/java/io/tarantool/driver/core/proxy/InsertManyProxyOperation.java @@ -10,6 +10,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.Objects; /** * Proxy operation for inserting many records at once @@ -34,7 +35,7 @@ public final class InsertManyProxyOperation> - extends GenericOperationsBuilder> { + extends GenericOperationsBuilder, Builder> { private Collection tuples; public Builder() { @@ -51,18 +52,11 @@ public Builder withTuples(Collection tuples) { } public InsertManyProxyOperation build() { - if (tuples == null) { + if (Objects.isNull(tuples)) { throw new IllegalArgumentException("Tuples must be specified for batch insert operation"); } - CRUDBatchOptions requestOptions = new CRUDBatchOptions.Builder() - .withTimeout(options.getTimeout()) - .withStopOnError(options.getStopOnError()) - .withRollbackOnError(options.getRollbackOnError()) - .withFields(options.getFields()) - .build(); - - List arguments = Arrays.asList(spaceName, tuples, requestOptions.asMap()); + List arguments = Arrays.asList(spaceName, tuples, options.asMap()); return new InsertManyProxyOperation<>( this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper); diff --git a/src/main/java/io/tarantool/driver/core/proxy/InsertProxyOperation.java b/src/main/java/io/tarantool/driver/core/proxy/InsertProxyOperation.java index 2782e36d..4680cf81 100644 --- a/src/main/java/io/tarantool/driver/core/proxy/InsertProxyOperation.java +++ b/src/main/java/io/tarantool/driver/core/proxy/InsertProxyOperation.java @@ -34,7 +34,7 @@ private InsertProxyOperation( * The builder for this class. */ public static final class Builder> - extends GenericOperationsBuilder> { + extends GenericOperationsBuilder, Builder> { private T tuple; public Builder() { @@ -51,13 +51,8 @@ public Builder withTuple(T tuple) { } public InsertProxyOperation build() { - CRUDBucketIdOptions requestOptions = new CRUDInsertOptions.Builder() - .withTimeout(options.getTimeout()) - .withBucketId(options.getBucketId()) - .withFields(options.getFields()) - .build(); - List arguments = Arrays.asList(spaceName, tuple, requestOptions.asMap()); + List arguments = Arrays.asList(spaceName, tuple, options.asMap()); return new InsertProxyOperation<>( this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper); diff --git a/src/main/java/io/tarantool/driver/core/proxy/ReplaceManyProxyOperation.java b/src/main/java/io/tarantool/driver/core/proxy/ReplaceManyProxyOperation.java index e7ac3e1d..b65639c0 100644 --- a/src/main/java/io/tarantool/driver/core/proxy/ReplaceManyProxyOperation.java +++ b/src/main/java/io/tarantool/driver/core/proxy/ReplaceManyProxyOperation.java @@ -10,6 +10,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.Objects; /** * Proxy operation for replacing many records at once @@ -34,7 +35,7 @@ public final class ReplaceManyProxyOperation> - extends GenericOperationsBuilder> { + extends GenericOperationsBuilder, Builder> { private Collection tuples; public Builder() { @@ -51,18 +52,11 @@ public Builder withTuples(Collection tuples) { } public ReplaceManyProxyOperation build() { - if (tuples == null) { + if (Objects.isNull(tuples)) { throw new IllegalArgumentException("Tuples must be specified for batch replace operation"); } - CRUDBatchOptions requestOptions = new CRUDBatchOptions.Builder() - .withTimeout(options.getTimeout()) - .withStopOnError(options.getStopOnError()) - .withRollbackOnError(options.getRollbackOnError()) - .withFields(options.getFields()) - .build(); - - List arguments = Arrays.asList(spaceName, tuples, requestOptions.asMap()); + List arguments = Arrays.asList(spaceName, tuples, options.asMap()); return new ReplaceManyProxyOperation<>( this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper); diff --git a/src/main/java/io/tarantool/driver/core/proxy/ReplaceProxyOperation.java b/src/main/java/io/tarantool/driver/core/proxy/ReplaceProxyOperation.java index 7030e635..d7ae16a2 100644 --- a/src/main/java/io/tarantool/driver/core/proxy/ReplaceProxyOperation.java +++ b/src/main/java/io/tarantool/driver/core/proxy/ReplaceProxyOperation.java @@ -35,7 +35,7 @@ public final class ReplaceProxyOperation> - extends GenericOperationsBuilder> { + extends GenericOperationsBuilder, Builder> { private T tuple; public Builder() { @@ -52,13 +52,8 @@ public Builder withTuple(T tuple) { } public ReplaceProxyOperation build() { - CRUDBucketIdOptions requestOptions = new CRUDReplaceOptions.Builder() - .withTimeout(options.getTimeout()) - .withBucketId(options.getBucketId()) - .withFields(options.getFields()) - .build(); - List arguments = Arrays.asList(spaceName, tuple, requestOptions.asMap()); + List arguments = Arrays.asList(spaceName, tuple, options.asMap()); return new ReplaceProxyOperation<>( this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper); diff --git a/src/main/java/io/tarantool/driver/core/proxy/SelectProxyOperation.java b/src/main/java/io/tarantool/driver/core/proxy/SelectProxyOperation.java index 792169ea..8aa25f02 100644 --- a/src/main/java/io/tarantool/driver/core/proxy/SelectProxyOperation.java +++ b/src/main/java/io/tarantool/driver/core/proxy/SelectProxyOperation.java @@ -5,12 +5,15 @@ import io.tarantool.driver.api.conditions.Conditions; import io.tarantool.driver.api.metadata.TarantoolMetadataOperations; import io.tarantool.driver.api.metadata.TarantoolSpaceMetadata; +import io.tarantool.driver.api.space.options.enums.ProxyOption; import io.tarantool.driver.api.space.options.interfaces.SelectOptions; import io.tarantool.driver.mappers.CallResultMapper; import io.tarantool.driver.mappers.MessagePackObjectMapper; +import io.tarantool.driver.protocol.Packable; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.Optional; /** @@ -35,7 +38,7 @@ private SelectProxyOperation( * The builder for this class. */ public static final class Builder - extends GenericOperationsBuilder> { + extends GenericOperationsBuilder, Builder> { private final TarantoolMetadataOperations operations; private final TarantoolSpaceMetadata metadata; private Conditions conditions; @@ -56,19 +59,23 @@ public Builder withConditions(Conditions conditions) { } public SelectProxyOperation build() { - CRUDSelectOptions.Builder requestOptions = new CRUDSelectOptions.Builder() - .withTimeout(options.getTimeout()) - .withSelectBatchSize(options.getBatchSize()) - .withSelectLimit(Optional.of(conditions.getLimit())) - .withSelectAfter(Optional.ofNullable(conditions.getStartTuple())) - .withBucketId(options.getBucketId()) - .withFields(options.getFields()) - .withMode(options.getMode()); + + Optional first = options.getFirst(); + if (first.isPresent()) { + options.addOption(ProxyOption.FIRST, Math.min(conditions.getLimit(), first.get())); + } else { + options.addOption(ProxyOption.FIRST, conditions.getLimit()); + } + + Packable tuple = conditions.getStartTuple(); + if (Objects.nonNull(tuple)) { + options.addOption(ProxyOption.AFTER, conditions.getStartTuple()); + } List arguments = Arrays.asList( spaceName, conditions.toProxyQuery(operations, metadata), - requestOptions.build().asMap() + options.asMap() ); return new SelectProxyOperation<>( diff --git a/src/main/java/io/tarantool/driver/core/proxy/TruncateProxyOperation.java b/src/main/java/io/tarantool/driver/core/proxy/TruncateProxyOperation.java index 5d21b7ee..46df2fb6 100644 --- a/src/main/java/io/tarantool/driver/core/proxy/TruncateProxyOperation.java +++ b/src/main/java/io/tarantool/driver/core/proxy/TruncateProxyOperation.java @@ -57,7 +57,7 @@ public static Builder builder() { } public static final class Builder - extends AbstractProxyOperation.GenericOperationsBuilder { + extends AbstractProxyOperation.GenericOperationsBuilder, Builder> { public Builder() { } @@ -73,11 +73,8 @@ Builder self() { * @return TruncateProxyOperation instance */ public TruncateProxyOperation build() { - CRUDBaseOptions requestOptions = new CRUDBaseOptions.Builder() - .withTimeout(options.getTimeout()) - .build(); - List arguments = Arrays.asList(spaceName, requestOptions.asMap()); + List arguments = Arrays.asList(spaceName, options.asMap()); return new TruncateProxyOperation(this.client, this.functionName, arguments); } diff --git a/src/main/java/io/tarantool/driver/core/proxy/UpdateProxyOperation.java b/src/main/java/io/tarantool/driver/core/proxy/UpdateProxyOperation.java index e30ee734..ad8e1489 100644 --- a/src/main/java/io/tarantool/driver/core/proxy/UpdateProxyOperation.java +++ b/src/main/java/io/tarantool/driver/core/proxy/UpdateProxyOperation.java @@ -33,7 +33,7 @@ public final class UpdateProxyOperation extends AbstractProxyOperation { * The builder for this class. */ public static final class Builder - extends GenericOperationsBuilder> { + extends GenericOperationsBuilder, Builder> { private TarantoolIndexQuery indexQuery; private TupleOperations operations; @@ -56,16 +56,11 @@ public Builder withTupleOperation(TupleOperations operations) { } public UpdateProxyOperation build() { - CRUDBucketIdOptions requestOptions = new CRUDUpdateOptions.Builder() - .withTimeout(options.getTimeout()) - .withBucketId(options.getBucketId()) - .withFields(options.getFields()) - .build(); List arguments = Arrays.asList(spaceName, indexQuery.getKeyValues(), operations.asProxyOperationList(), - requestOptions.asMap()); + options.asMap()); return new UpdateProxyOperation<>( this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper); diff --git a/src/main/java/io/tarantool/driver/core/proxy/UpsertProxyOperation.java b/src/main/java/io/tarantool/driver/core/proxy/UpsertProxyOperation.java index e61cb200..7a955898 100644 --- a/src/main/java/io/tarantool/driver/core/proxy/UpsertProxyOperation.java +++ b/src/main/java/io/tarantool/driver/core/proxy/UpsertProxyOperation.java @@ -35,7 +35,7 @@ public final class UpsertProxyOperation> - extends GenericOperationsBuilder> { + extends GenericOperationsBuilder, Builder> { private T tuple; private TupleOperations operations; @@ -58,17 +58,12 @@ public Builder withTupleOperation(TupleOperations operations) { } public UpsertProxyOperation build() { - CRUDBucketIdOptions requestOptions = new CRUDUpsertOptions.Builder() - .withTimeout(options.getTimeout()) - .withBucketId(options.getBucketId()) - .withFields(options.getFields()) - .build(); List arguments = Arrays.asList( spaceName, tuple, operations.asProxyOperationList(), - requestOptions.asMap() + options.asMap() ); return new UpsertProxyOperation<>( diff --git a/src/test/java/io/tarantool/driver/core/proxy/ProxyOperationBuildersTest.java b/src/test/java/io/tarantool/driver/core/proxy/ProxyOperationBuildersTest.java index a63de4ce..378af8de 100644 --- a/src/test/java/io/tarantool/driver/core/proxy/ProxyOperationBuildersTest.java +++ b/src/test/java/io/tarantool/driver/core/proxy/ProxyOperationBuildersTest.java @@ -33,9 +33,11 @@ import java.util.Arrays; import java.util.Collections; +import java.util.EnumMap; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -227,19 +229,24 @@ public void selectOperationBuilderTest() { ) .build(); - Map options = new HashMap<>(); - options.put(ProxyOption.TIMEOUT.toString(), client.getConfig().getRequestTimeout()); - options.put(ProxyOption.BATCH_SIZE.toString(), 123456); - options.put(ProxyOption.FIRST.toString(), 100L); - options.put(ProxyOption.MODE.toString(), Mode.WRITE.value()); - + EnumMap options = new EnumMap<>(ProxyOption.class); + options.put(ProxyOption.TIMEOUT, client.getConfig().getRequestTimeout()); + options.put(ProxyOption.BATCH_SIZE, 123456); + options.put(ProxyOption.FIRST, 100L); + options.put(ProxyOption.MODE, Mode.WRITE.value()); + + Map opt = options.entrySet().stream() + .collect(Collectors.toMap( + option -> option.getKey().toString(), + Map.Entry::getValue + )); assertEquals(client, op.getClient()); assertEquals("function1", op.getFunctionName()); assertEquals(3, op.getArguments().size()); assertEquals("space1", op.getArguments().get(0)); assertEquals(selectArguments, op.getArguments().get(1)); Map actualOptions = (Map) op.getArguments().get(2); - assertEquals(options.toString(), actualOptions.toString()); + assertEquals(opt.toString(), actualOptions.toString()); assertEquals(defaultResultMapper, op.getResultMapper()); } 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 e6243698..ed6b754c 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 @@ -4,9 +4,9 @@ import io.tarantool.driver.api.TarantoolClientConfig; import io.tarantool.driver.api.TarantoolResult; import io.tarantool.driver.api.conditions.Conditions; -import io.tarantool.driver.api.space.options.interfaces.TarantoolSpaceOperations; -import io.tarantool.driver.api.space.options.interfaces.SelectOptions; import io.tarantool.driver.api.space.options.enums.Mode; +import io.tarantool.driver.api.space.options.interfaces.SelectOptions; +import io.tarantool.driver.api.space.options.interfaces.TarantoolSpaceOperations; import io.tarantool.driver.api.space.options.proxy.ProxySelectOptions; import io.tarantool.driver.api.tuple.DefaultTarantoolTupleFactory; import io.tarantool.driver.api.tuple.TarantoolTuple; @@ -25,7 +25,9 @@ import java.util.List; import java.util.concurrent.ExecutionException; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Artyom Dubinin @@ -172,4 +174,65 @@ public void withModeTest() throws ExecutionException, InterruptedException { crudSelectOpts = client.eval("return crud_select_opts").get(); assertEquals(Mode.WRITE.value(), ((HashMap) crudSelectOpts.get(0)).get("mode")); } + + @Test + public void withFirstTest() throws ExecutionException, InterruptedException { + TarantoolSpaceOperations> operations = + client.space(TEST_SPACE_NAME); + + //default + operations.select(Conditions.any()).get(); + List crudSelectOpts = client.eval("return crud_select_opts").get(); + assertEquals(4294967295L, ((HashMap) crudSelectOpts.get(0)).get("first")); + + operations.select(Conditions.limit(0)).get(); + crudSelectOpts = client.eval("return crud_select_opts").get(); + assertEquals(0, ((HashMap) crudSelectOpts.get(0)).get("first")); + + operations.select(Conditions.any(), ProxySelectOptions.create().withFirst(2)).get(); + crudSelectOpts = client.eval("return crud_select_opts").get(); + assertEquals(2, ((HashMap) crudSelectOpts.get(0)).get("first")); + + operations.select(Conditions.any(), ProxySelectOptions.create().withFirst(0)).get(); + crudSelectOpts = client.eval("return crud_select_opts").get(); + assertEquals(0, ((HashMap) crudSelectOpts.get(0)).get("first")); + + operations.select(Conditions.any(), ProxySelectOptions.create().withFirst(1)).get(); + crudSelectOpts = client.eval("return crud_select_opts").get(); + assertEquals(1, ((HashMap) crudSelectOpts.get(0)).get("first")); + + operations.select(Conditions.limit(3), ProxySelectOptions.create().withFirst(1)).get(); + crudSelectOpts = client.eval("return crud_select_opts").get(); + assertEquals(1, ((HashMap) crudSelectOpts.get(0)).get("first")); + + operations.select(Conditions.limit(1), ProxySelectOptions.create().withFirst(2)).get(); + crudSelectOpts = client.eval("return crud_select_opts").get(); + assertEquals(1, ((HashMap) crudSelectOpts.get(0)).get("first")); + } + + @Test + public void withAfterTest() throws ExecutionException, InterruptedException { + TarantoolSpaceOperations> operations = + client.space(TEST_SPACE_NAME); + TarantoolTuple firstTuple = new DefaultTarantoolTupleFactory(mapperFactory.defaultComplexTypesMapper()).create( + Arrays.asList(1, 1, "kolya", 500)); + TarantoolTuple secondTuple = new DefaultTarantoolTupleFactory(mapperFactory.defaultComplexTypesMapper()).create( + Arrays.asList(2, 1, "roma", 600)); + + operations.select(Conditions.any()).get(); + List crudSelectOpts = client.eval("return crud_select_opts").get(); + assertEquals(null, ((HashMap) crudSelectOpts.get(0)).get("after")); + + operations.select(Conditions.after(firstTuple)).get(); + crudSelectOpts = client.eval("return crud_select_opts").get(); + assertEquals(Arrays.asList(1, 1, "kolya", 500), ((HashMap) crudSelectOpts.get(0)).get("after")); + + operations.select(Conditions.any(), ProxySelectOptions.create().withAfter(firstTuple)).get(); + crudSelectOpts = client.eval("return crud_select_opts").get(); + assertEquals(Arrays.asList(1, 1, "kolya", 500), ((HashMap) crudSelectOpts.get(0)).get("after")); + + operations.select(Conditions.after(secondTuple), ProxySelectOptions.create().withAfter(firstTuple)).get(); + crudSelectOpts = client.eval("return crud_select_opts").get(); + assertEquals(Arrays.asList(2, 1, "roma", 600), ((HashMap) crudSelectOpts.get(0)).get("after")); + } }