Skip to content

Commit

Permalink
Add RollbackOnError enum and refactor public proxy API with it
Browse files Browse the repository at this point in the history
  • Loading branch information
nickkkccc authored and ArtDu committed Sep 12, 2023
1 parent 91f4b74 commit 684e8a5
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@
* @author Alexey Kuzin
*/
public interface InsertManyOptions<T extends InsertManyOptions<T>>
extends OperationWithTimeoutOptions<T>, OperationWithFieldsOptions<T> {
/**
* Return whether all changes should not be saved if any tuple insertion
* was unsuccesful.
*
* @return true, if the operation should rollback on error
*/
Optional<Boolean> getRollbackOnError();
extends OperationWithTimeoutOptions<T>, OperationWithFieldsOptions<T>, OperationWithRollbackOnErrorOptions<T> {

/**
* Return whether the operation should be interrupted if any tuple insertion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.tarantool.driver.api.space.options;


import io.tarantool.driver.api.space.options.enums.crud.RollbackOnError;

import java.util.Optional;

/**
* Base interface for all operation options that may have a configurable rollback_on_error.
*
* @author Belonogov Nikolay
*/
public interface OperationWithRollbackOnErrorOptions<T extends OperationWithRollbackOnErrorOptions<T>>
extends Options, Self<T> {
/**
* Specifies whether to not save any changes in the space if any tuple replace operation
* is unsuccesful. Default value is <code>true</code>.
*
* @param rollbackOnError should rollback batch on error
* @return this options instance
*/
default T withRollbackOnError(RollbackOnError rollbackOnError) {
addOption(RollbackOnError.NAME, rollbackOnError.value());
return self();
}

/**
* Return whether all changes should not be saved if any tuple replace
* was unsuccesful.
*
* @return true, if the operation should rollback on error
*/
default Optional<Boolean> getRollbackOnError() {
return getOption(RollbackOnError.NAME, Boolean.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@
* @author Alexey Kuzin
*/
public interface ReplaceManyOptions<T extends ReplaceManyOptions<T>>
extends OperationWithTimeoutOptions<T>, OperationWithFieldsOptions<T> {
/**
* Return whether all changes should not be saved if any tuple replace
* was unsuccesful.
*
* @return true, if the operation should rollback on error
*/
Optional<Boolean> getRollbackOnError();
extends OperationWithTimeoutOptions<T>, OperationWithFieldsOptions<T>, OperationWithRollbackOnErrorOptions<T> {

/**
* Return whether the operation should be interrupted if any tuple replace
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.tarantool.driver.api.space.options.enums.crud;

/**
* Enum represents the CRUD predefined rollback_on_error option values.
*
* @author Belonogov Nikolay.
* @see <a href="https://github.com/tarantool/crud">tarantool/crud</a>.
*/
public enum RollbackOnError {

TRUE(true),

FALSE(false);

public static final String NAME = "rollback_on_error";
private final boolean value;

RollbackOnError(boolean value) {
this.value = value;
}

public boolean value() {
return this.value;
}

@Override
public String toString() {
return Boolean.toString(this.value);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.tarantool.driver.api.space.options.proxy;

import io.tarantool.driver.api.space.options.InsertManyOptions;
import io.tarantool.driver.api.space.options.BaseOptions;
import io.tarantool.driver.api.space.options.InsertManyOptions;

import java.util.Optional;

Expand All @@ -13,7 +13,6 @@
public final class ProxyInsertManyOptions extends BaseOptions
implements InsertManyOptions<ProxyInsertManyOptions> {

public static final String ROLLBACK_ON_ERROR = "rollback_on_error";
public static final String STOP_ON_ERROR = "stop_on_error";

private ProxyInsertManyOptions() {
Expand All @@ -28,18 +27,6 @@ public static ProxyInsertManyOptions create() {
return new ProxyInsertManyOptions();
}

/**
* Specifies whether to not save any changes in the space if any tuple insert operation
* is unsuccesful. Default value is <code>true</code>.
*
* @param rollbackOnError should rollback batch on error
* @return this options instance
*/
public ProxyInsertManyOptions withRollbackOnError(boolean rollbackOnError) {
addOption(ROLLBACK_ON_ERROR, rollbackOnError);
return self();
}

/**
* Specifies whether to not try to insert more tuples into the space if any tuple insert
* operation is unsuccesful. Default value is <code>true</code>.
Expand All @@ -57,11 +44,6 @@ public ProxyInsertManyOptions self() {
return this;
}

@Override
public Optional<Boolean> getRollbackOnError() {
return getOption(ROLLBACK_ON_ERROR, Boolean.class);
}

@Override
public Optional<Boolean> getStopOnError() {
return getOption(STOP_ON_ERROR, Boolean.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
public final class ProxyReplaceManyOptions extends BaseOptions
implements ReplaceManyOptions<ProxyReplaceManyOptions> {

public static final String ROLLBACK_ON_ERROR = "rollback_on_error";
public static final String STOP_ON_ERROR = "stop_on_error";

private ProxyReplaceManyOptions() {
Expand All @@ -28,18 +27,6 @@ public static ProxyReplaceManyOptions create() {
return new ProxyReplaceManyOptions();
}

/**
* Specifies whether to not save any changes in the space if any tuple replace operation
* is unsuccesful. Default value is <code>true</code>.
*
* @param rollbackOnError should rollback batch on error
* @return this options instance
*/
public ProxyReplaceManyOptions withRollbackOnError(boolean rollbackOnError) {
addOption(ROLLBACK_ON_ERROR, rollbackOnError);
return self();
}

/**
* Specifies whether to not try to replace more tuples into the space if any tuple replace
* operation is unsuccesful. Default value is <code>true</code>.
Expand All @@ -57,11 +44,6 @@ public ProxyReplaceManyOptions self() {
return this;
}

@Override
public Optional<Boolean> getRollbackOnError() {
return getOption(ROLLBACK_ON_ERROR, Boolean.class);
}

@Override
public Optional<Boolean> getStopOnError() {
return getOption(STOP_ON_ERROR, Boolean.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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.space.options.enums.crud.RollbackOnError;
import io.tarantool.driver.api.space.options.proxy.ProxyDeleteOptions;
import io.tarantool.driver.api.space.options.proxy.ProxyInsertManyOptions;
import io.tarantool.driver.api.space.options.proxy.ProxyInsertOptions;
Expand Down Expand Up @@ -144,7 +145,7 @@ private CompletableFuture<R> insert(
public CompletableFuture<R> insertMany(Collection<T> tuples) {
return insertMany(tuples, rowsMetadataTupleResultMapper(), ProxyInsertManyOptions.create()
.withStopOnError(true)
.withRollbackOnError(true)
.withRollbackOnError(RollbackOnError.TRUE)
);
}

Expand Down Expand Up @@ -210,7 +211,7 @@ private CompletableFuture<R> replace(
public CompletableFuture<R> replaceMany(Collection<T> tuples) throws TarantoolClientException {
return replaceMany(tuples, rowsMetadataTupleResultMapper(), ProxyReplaceManyOptions.create()
.withStopOnError(true)
.withRollbackOnError(true)
.withRollbackOnError(RollbackOnError.TRUE)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.tarantool.driver.api.TarantoolResult;
import io.tarantool.driver.api.conditions.Conditions;
import io.tarantool.driver.api.space.options.enums.crud.Mode;
import io.tarantool.driver.api.space.options.enums.crud.RollbackOnError;
import io.tarantool.driver.api.space.options.proxy.ProxyDeleteOptions;
import io.tarantool.driver.api.space.options.proxy.ProxyInsertManyOptions;
import io.tarantool.driver.api.space.options.proxy.ProxyInsertOptions;
Expand Down Expand Up @@ -122,7 +123,7 @@ public void insertManyOperationBuilderTest() {
.withArgumentsMapper(defaultMapper)
.withOptions(ProxyInsertManyOptions.create()
.withTimeout(client.getConfig().getRequestTimeout())
.withRollbackOnError(true)
.withRollbackOnError(RollbackOnError.TRUE)
.withStopOnError(false)
)
.build();
Expand Down Expand Up @@ -182,7 +183,7 @@ public void replaceManyOperationBuilderTest() {
.withArgumentsMapper(defaultMapper)
.withOptions(ProxyReplaceManyOptions.create()
.withTimeout(client.getConfig().getRequestTimeout())
.withRollbackOnError(true)
.withRollbackOnError(RollbackOnError.TRUE)
.withStopOnError(false)
)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import io.tarantool.driver.api.conditions.Conditions;
import io.tarantool.driver.api.space.TarantoolSpaceOperations;
import io.tarantool.driver.api.space.options.InsertManyOptions;
import io.tarantool.driver.api.space.options.enums.crud.RollbackOnError;
import io.tarantool.driver.api.space.options.proxy.ProxyInsertManyOptions;
import io.tarantool.driver.api.space.options.proxy.ProxyReplaceManyOptions;
import io.tarantool.driver.api.tuple.DefaultTarantoolTupleFactory;
import io.tarantool.driver.api.tuple.TarantoolTuple;
import io.tarantool.driver.api.tuple.TarantoolTupleFactory;
Expand Down Expand Up @@ -101,7 +101,7 @@ public void withStopOnError_withRollbackOnError() throws ExecutionException, Int
profileSpace.insertMany(
tarantoolTuples,
ProxyInsertManyOptions.create()
.withRollbackOnError(false)
.withRollbackOnError(RollbackOnError.FALSE)
.withStopOnError(false)
).get();
crudInsertManyOpts = client.eval("return crud_insert_many_opts").get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.tarantool.driver.api.TarantoolResult;
import io.tarantool.driver.api.space.TarantoolSpaceOperations;
import io.tarantool.driver.api.space.options.ReplaceManyOptions;
import io.tarantool.driver.api.space.options.enums.crud.RollbackOnError;
import io.tarantool.driver.api.space.options.proxy.ProxyReplaceManyOptions;
import io.tarantool.driver.api.tuple.DefaultTarantoolTupleFactory;
import io.tarantool.driver.api.tuple.TarantoolTuple;
Expand Down Expand Up @@ -92,7 +93,7 @@ public void withStopOnError_withRollbackOnError() throws ExecutionException, Int
profileSpace.replaceMany(
tarantoolTuples,
ProxyReplaceManyOptions.create()
.withRollbackOnError(false)
.withRollbackOnError(RollbackOnError.FALSE)
.withStopOnError(false)
).get();
crudReplaceManyOpts = client.eval("return crud_replace_many_opts").get();
Expand Down

0 comments on commit 684e8a5

Please sign in to comment.