Skip to content

Commit

Permalink
Add StopOnError enum and refactor public proxy API with it
Browse files Browse the repository at this point in the history
Closes #419
  • Loading branch information
nickkkccc committed Sep 11, 2023
1 parent 4a17bd4 commit 313d943
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 128 deletions.
151 changes: 102 additions & 49 deletions CHANGELOG.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package io.tarantool.driver.api.space.options;

import java.util.Optional;

/**
* Marker interface for space insert_many operation options
*
* @author Alexey Kuzin
*/
public interface InsertManyOptions<T extends InsertManyOptions<T>>
extends OperationWithTimeoutOptions<T>, OperationWithFieldsOptions<T>, OperationWithRollbackOnErrorOptions<T> {

/**
* Return whether the operation should be interrupted if any tuple insertion
* was unsuccesful.
*
* @return true, if the operation should stop on error
*/
Optional<Boolean> getStopOnError();
extends OperationWithTimeoutOptions<T>, OperationWithFieldsOptions<T>, OperationWithRollbackOnErrorOptions<T>,
OperationWithStopOnErrorOptions<T> {
}
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.StopOnError;

import java.util.Optional;

/**
* Base interface for all operation options that may have a configurable stop_on_error.
*
* @author Belonogov Nikolay
*/
public interface OperationWithStopOnErrorOptions<T extends OperationWithStopOnErrorOptions<T>>
extends Options, Self<T> {

/**
* 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>.
*
* @param stopOnError should stop batch on error
* @return this options instance
*/
default T withStopOnError(StopOnError stopOnError) {
addOption(StopOnError.NAME, stopOnError.value());
return self();
}

/**
* Return whether the operation should be interrupted if any tuple replace
* was unsuccesful.
*
* @return true, if the operation should stop on error
*/
default Optional<Boolean> getStopOnError() {
return getOption(StopOnError.NAME, Boolean.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package io.tarantool.driver.api.space.options;

import java.util.Optional;

/**
* Marker interface for space replace_many operation options
*
* @author Alexey Kuzin
*/
public interface ReplaceManyOptions<T extends ReplaceManyOptions<T>>
extends OperationWithTimeoutOptions<T>, OperationWithFieldsOptions<T>, OperationWithRollbackOnErrorOptions<T> {

/**
* Return whether the operation should be interrupted if any tuple replace
* was unsuccesful.
*
* @return true, if the operation should stop on error
*/
Optional<Boolean> getStopOnError();
extends OperationWithTimeoutOptions<T>, OperationWithFieldsOptions<T>, OperationWithRollbackOnErrorOptions<T>,
OperationWithStopOnErrorOptions<T> {
}
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 stop_on_error option values.
*
* @author Belonogov Nikolay.
* @see <a href="https://github.com/tarantool/crud">tarantool/crud</a>.
*/
public enum StopOnError {

TRUE(true),

FALSE(false);

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

StopOnError(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
Expand Up @@ -3,8 +3,6 @@
import io.tarantool.driver.api.space.options.BaseOptions;
import io.tarantool.driver.api.space.options.InsertManyOptions;

import java.util.Optional;

/**
* Represent options for insert_many cluster proxy operation
*
Expand All @@ -13,8 +11,6 @@
public final class ProxyInsertManyOptions extends BaseOptions
implements InsertManyOptions<ProxyInsertManyOptions> {

public static final String STOP_ON_ERROR = "stop_on_error";

private ProxyInsertManyOptions() {
}

Expand All @@ -27,25 +23,8 @@ public static ProxyInsertManyOptions create() {
return new ProxyInsertManyOptions();
}

/**
* 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>.
*
* @param stopOnError should stop batch on error
* @return this options instance
*/
public ProxyInsertManyOptions withStopOnError(boolean stopOnError) {
addOption(STOP_ON_ERROR, stopOnError);
return self();
}

@Override
public ProxyInsertManyOptions self() {
return this;
}

@Override
public Optional<Boolean> getStopOnError() {
return getOption(STOP_ON_ERROR, Boolean.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import io.tarantool.driver.api.space.options.BaseOptions;
import io.tarantool.driver.api.space.options.ReplaceManyOptions;

import java.util.Optional;

/**
* Represent options for replace_many cluster proxy operation
*
Expand All @@ -13,8 +11,6 @@
public final class ProxyReplaceManyOptions extends BaseOptions
implements ReplaceManyOptions<ProxyReplaceManyOptions> {

public static final String STOP_ON_ERROR = "stop_on_error";

private ProxyReplaceManyOptions() {
}

Expand All @@ -27,25 +23,8 @@ public static ProxyReplaceManyOptions create() {
return new ProxyReplaceManyOptions();
}

/**
* 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>.
*
* @param stopOnError should stop batch on error
* @return this options instance
*/
public ProxyReplaceManyOptions withStopOnError(boolean stopOnError) {
addOption(STOP_ON_ERROR, stopOnError);
return self();
}

@Override
public ProxyReplaceManyOptions self() {
return this;
}

@Override
public Optional<Boolean> getStopOnError() {
return getOption(STOP_ON_ERROR, Boolean.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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.enums.crud.StopOnError;
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(
@Override
public CompletableFuture<R> insertMany(Collection<T> tuples) {
return insertMany(tuples, rowsMetadataTupleResultMapper(), ProxyInsertManyOptions.create()
.withStopOnError(true)
.withStopOnError(StopOnError.TRUE)
.withRollbackOnError(RollbackOnError.TRUE)
);
}
Expand Down Expand Up @@ -210,7 +211,7 @@ private CompletableFuture<R> replace(
@Override
public CompletableFuture<R> replaceMany(Collection<T> tuples) throws TarantoolClientException {
return replaceMany(tuples, rowsMetadataTupleResultMapper(), ProxyReplaceManyOptions.create()
.withStopOnError(true)
.withStopOnError(StopOnError.TRUE)
.withRollbackOnError(RollbackOnError.TRUE)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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.enums.crud.StopOnError;
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 @@ -124,7 +125,7 @@ public void insertManyOperationBuilderTest() {
.withOptions(ProxyInsertManyOptions.create()
.withTimeout(client.getConfig().getRequestTimeout())
.withRollbackOnError(RollbackOnError.TRUE)
.withStopOnError(false)
.withStopOnError(StopOnError.FALSE)
)
.build();

Expand Down Expand Up @@ -184,7 +185,7 @@ public void replaceManyOperationBuilderTest() {
.withOptions(ProxyReplaceManyOptions.create()
.withTimeout(client.getConfig().getRequestTimeout())
.withRollbackOnError(RollbackOnError.TRUE)
.withStopOnError(false)
.withStopOnError(StopOnError.FALSE)
)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.enums.crud.StopOnError;
import io.tarantool.driver.api.space.options.proxy.ProxyInsertManyOptions;
import io.tarantool.driver.api.tuple.DefaultTarantoolTupleFactory;
import io.tarantool.driver.api.tuple.TarantoolTuple;
Expand Down Expand Up @@ -36,16 +37,14 @@
*/
public class ProxySpaceInsertManyOptionsIT extends SharedCartridgeContainer {

private static TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> client;
private static final DefaultMessagePackMapperFactory mapperFactory = DefaultMessagePackMapperFactory.getInstance();
private static final TarantoolTupleFactory tupleFactory =
new DefaultTarantoolTupleFactory(mapperFactory.defaultComplexTypesMapper());

public static String USER_NAME;
public static String PASSWORD;

private static final String TEST_SPACE_NAME = "test__profile";
private static final String PK_FIELD_NAME = "profile_id";
public static String USER_NAME;
public static String PASSWORD;
private static TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> client;

@BeforeAll
public static void setUp() throws Exception {
Expand All @@ -67,15 +66,15 @@ private static void initClient() {
client = new ProxyTarantoolTupleClient(clusterClient);
}

private static void truncateSpace(String spaceName) {
client.space(spaceName).truncate().join();
}

@BeforeEach
public void truncateSpace() {
truncateSpace(TEST_SPACE_NAME);
}

private static void truncateSpace(String spaceName) {
client.space(spaceName).truncate().join();
}

@Test
public void withStopOnError_withRollbackOnError() throws ExecutionException, InterruptedException {
TarantoolSpaceOperations<TarantoolTuple, TarantoolResult<TarantoolTuple>> profileSpace =
Expand All @@ -102,7 +101,7 @@ public void withStopOnError_withRollbackOnError() throws ExecutionException, Int
tarantoolTuples,
ProxyInsertManyOptions.create()
.withRollbackOnError(RollbackOnError.FALSE)
.withStopOnError(false)
.withStopOnError(StopOnError.FALSE)
).get();
crudInsertManyOpts = client.eval("return crud_insert_many_opts").get();
assertEquals(false, ((HashMap) crudInsertManyOpts.get(0)).get("rollback_on_error"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.enums.crud.StopOnError;
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 @@ -94,7 +95,7 @@ public void withStopOnError_withRollbackOnError() throws ExecutionException, Int
tarantoolTuples,
ProxyReplaceManyOptions.create()
.withRollbackOnError(RollbackOnError.FALSE)
.withStopOnError(false)
.withStopOnError(StopOnError.FALSE)
).get();
crudReplaceManyOpts = client.eval("return crud_replace_many_opts").get();
assertEquals(false, ((HashMap) crudReplaceManyOpts.get(0)).get("rollback_on_error"));
Expand Down

0 comments on commit 313d943

Please sign in to comment.