Skip to content

Commit

Permalink
Added "mode" option for crud select operation
Browse files Browse the repository at this point in the history
Needed for #107
  • Loading branch information
nickkkccc committed Sep 6, 2023
1 parent 770b59f commit 4063fc7
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 38 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

### API changes
- Add "mode" option for crud select operation ([#107](https://github.com/tarantool/cartridge-java/issues/107))

### Bugfixes
- Fix Instant converter to parse 8 bytes datetime ([#408](https://github.com/tarantool/cartridge-java/issues/408))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.tarantool.driver.api.space.options;

import java.util.Optional;

/**
* Base interface for all operation options that may have a configurable mode.
*
* @author Belonogov Nikolay
*/
public interface OperationWithModeOptions<T extends OperationWithModeOptions<T>> extends Options, Self<T> {
String MODE = "mode";

/**
* Specifies the mode for operations (select, count, get) on a specific node type (mode == "write" - master, mode
* == "read" - replica). By default, mode is "read".
*
* @param mode mode for operations (select, get, count).
* @return this options instance.
*/
default T withMode(String mode) {
if (!mode.equals("read") && !mode.equals("write")) {
throw new IllegalArgumentException("Mode should be \"read\" or \"write\"");
}

addOption(MODE, mode);
return self();
}

/**
* Return operation mode.
*
* @return mode.
*/
default Optional<String> getMode() {
return getOption(MODE, String.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* @author Alexey Kuzin
*/
public interface SelectOptions<T extends SelectOptions<T>>
extends OperationWithBucketIdOptions<T>, OperationWithTimeoutOptions<T>, OperationWithFieldsOptions<T> {
extends OperationWithBucketIdOptions<T>, OperationWithTimeoutOptions<T>, OperationWithFieldsOptions<T>,
OperationWithModeOptions<T> {
/**
* Return the internal size of batch for transferring data between
* storage and router nodes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class CRUDSelectOptions extends CRUDBucketIdOptions {
public static final String SELECT_AFTER = "after";
public static final String SELECT_BATCH_SIZE = "batch_size";
public static final String FIELDS = "fields";
public static final String MODE = "mode";

private <B extends AbstractBuilder<B>> CRUDSelectOptions(AbstractBuilder<B> builder) {
super(builder);
Expand All @@ -28,6 +29,7 @@ private <B extends AbstractBuilder<B>> CRUDSelectOptions(AbstractBuilder<B> buil
addOption(SELECT_AFTER, builder.after);
addOption(SELECT_BATCH_SIZE, builder.selectBatchSize);
addOption(FIELDS, builder.fields);
addOption(MODE, builder.mode);
}

/**
Expand All @@ -41,6 +43,7 @@ protected abstract static class AbstractBuilder<B extends AbstractBuilder<B>>
private Optional<Packable> after = Optional.empty();
private Optional<Integer> selectBatchSize = Optional.empty();
private Optional<List> fields = Optional.empty();
private Optional<String> mode = Optional.empty();

public B withSelectLimit(Optional<Long> selectLimit) {
this.selectLimit = selectLimit;
Expand All @@ -61,6 +64,11 @@ public B withFields(Optional<List> fields) {
this.fields = fields;
return self();
}

public B withMode(Optional<String> mode) {
this.mode = mode;
return self();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public SelectProxyOperation<T> build() {
.withSelectLimit(Optional.of(conditions.getLimit()))
.withSelectAfter(Optional.ofNullable(conditions.getStartTuple()))
.withBucketId(options.getBucketId())
.withFields(options.getFields());
.withFields(options.getFields())
.withMode(options.getMode());

List<?> arguments = Arrays.asList(
spaceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import io.tarantool.driver.mappers.TarantoolTupleResultMapperFactory;
import io.tarantool.driver.mappers.TarantoolTupleResultMapperFactoryImpl;
import io.tarantool.driver.mappers.factories.DefaultMessagePackMapperFactory;
import io.tarantool.driver.mappers.factories.ResultMapperFactoryFactoryImpl;
import io.tarantool.driver.protocol.TarantoolIndexQuery;
import org.junit.jupiter.api.Test;

Expand All @@ -42,13 +41,13 @@ public class ProxyOperationBuildersTest {
private static final ClusterTarantoolTupleClient client = new ClusterTarantoolTupleClient();
private final MessagePackMapper defaultMapper =
DefaultMessagePackMapperFactory.getInstance().defaultComplexTypesMapper();
private final TarantoolTupleFactory factory = new DefaultTarantoolTupleFactory(defaultMapper);
TarantoolTupleResultMapperFactory tarantoolTupleResultMapperFactory =
TarantoolTupleResultMapperFactoryImpl.getInstance();
private final
CallResultMapper<TarantoolResult<TarantoolTuple>, SingleValueCallResult<TarantoolResult<TarantoolTuple>>>
defaultResultMapper = tarantoolTupleResultMapperFactory
.withSingleValueArrayToTarantoolTupleResultMapper(defaultMapper, null);
private final TarantoolTupleFactory factory = new DefaultTarantoolTupleFactory(defaultMapper);

@Test
public void deleteOperationBuilderTest() {
Expand Down Expand Up @@ -221,13 +220,15 @@ public void selectOperationBuilderTest() {
.withOptions(ProxySelectOptions.create()
.withTimeout(client.getConfig().getRequestTimeout())
.withBatchSize(123456)
.withMode("write")
)
.build();

Map<String, Object> options = new HashMap<>();
options.put(CRUDBaseOptions.TIMEOUT, client.getConfig().getRequestTimeout());
options.put(CRUDSelectOptions.SELECT_BATCH_SIZE, 123456);
options.put(CRUDSelectOptions.SELECT_LIMIT, 100L);
options.put(CRUDSelectOptions.MODE, "write");

assertEquals(client, op.getClient());
assertEquals("function1", op.getFunctionName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import io.tarantool.driver.api.TarantoolResult;
import io.tarantool.driver.api.conditions.Conditions;
import io.tarantool.driver.api.space.TarantoolSpaceOperations;
import io.tarantool.driver.api.space.options.proxy.ProxySelectOptions;
import io.tarantool.driver.api.space.options.SelectOptions;
import io.tarantool.driver.api.space.options.proxy.ProxySelectOptions;
import io.tarantool.driver.api.tuple.DefaultTarantoolTupleFactory;
import io.tarantool.driver.api.tuple.TarantoolTuple;
import io.tarantool.driver.api.tuple.TarantoolTupleFactory;
Expand All @@ -24,25 +24,21 @@
import java.util.List;
import java.util.concurrent.ExecutionException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.*;

/**
* @author Artyom Dubinin
*/
public class ProxySpaceSelectOptionsIT 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 @@ -53,26 +49,24 @@ public static void setUp() throws Exception {
}

private static void initClient() {
TarantoolClientConfig config = TarantoolClientConfig.builder()
.withCredentials(new SimpleTarantoolCredentials(USER_NAME, PASSWORD))
.withConnectTimeout(1000)
.withReadTimeout(1000)
.build();

ClusterTarantoolTupleClient clusterClient = new ClusterTarantoolTupleClient(
config, container.getRouterHost(), container.getRouterPort());
TarantoolClientConfig config =
TarantoolClientConfig.builder().withCredentials(new SimpleTarantoolCredentials(USER_NAME, PASSWORD))
.withConnectTimeout(1000).withReadTimeout(1000).build();

ClusterTarantoolTupleClient clusterClient =
new ClusterTarantoolTupleClient(config, container.getRouterHost(), container.getRouterPort());
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 withBatchSizeTest() throws ExecutionException, InterruptedException {
TarantoolSpaceOperations<TarantoolTuple, TarantoolResult<TarantoolTuple>> profileSpace =
Expand All @@ -94,10 +88,7 @@ public void withBatchSizeTest() throws ExecutionException, InterruptedException
assertNull(((HashMap) crudSelectOpts.get(0)).get("batch_size"));

// with batchSize
selectResult = profileSpace.select(
conditions,
ProxySelectOptions.create().withBatchSize(5)
).get();
selectResult = profileSpace.select(conditions, ProxySelectOptions.create().withBatchSize(5)).get();
assertEquals(10, selectResult.size());
crudSelectOpts = client.eval("return crud_select_opts").get();
assertEquals(5, ((HashMap) crudSelectOpts.get(0)).get("batch_size"));
Expand All @@ -116,26 +107,20 @@ public void withTimeout() throws ExecutionException, InterruptedException {
List<?> crudSelectOpts = client.eval("return crud_select_opts").get();
assertNull(((HashMap) crudSelectOpts.get(0)).get("timeout"));

profileSpace.select(
Conditions.any(),
ProxySelectOptions.create()
).get();
profileSpace.select(Conditions.any(), ProxySelectOptions.create()).get();
crudSelectOpts = client.eval("return crud_select_opts").get();
assertNull(((HashMap) crudSelectOpts.get(0)).get("timeout"));

// with option timeout
profileSpace.select(
Conditions.any(),
ProxySelectOptions.create().withTimeout(customRequestTimeout)
).get();
profileSpace.select(Conditions.any(), ProxySelectOptions.create().withTimeout(customRequestTimeout)).get();
crudSelectOpts = client.eval("return crud_select_opts").get();
assertEquals(customRequestTimeout, ((HashMap) crudSelectOpts.get(0)).get("timeout"));
}

@Test
public void withFieldsTest() throws ExecutionException, InterruptedException {
TarantoolSpaceOperations<TarantoolTuple, TarantoolResult<TarantoolTuple>> profileSpace =
client.space(TEST_SPACE_NAME);
client.space(TEST_SPACE_NAME);

TarantoolTuple tarantoolTuple;

Expand Down Expand Up @@ -167,4 +152,23 @@ public void withFieldsTest() throws ExecutionException, InterruptedException {
assertEquals(1, tuple.getInteger("profile_id"));
assertEquals(50, tuple.getInteger("age"));
}

@Test
public void withMode() throws ExecutionException, InterruptedException {
TarantoolSpaceOperations<TarantoolTuple, TarantoolResult<TarantoolTuple>> operations =
client.space(TEST_SPACE_NAME);

operations.select(Conditions.any()).get();
List<?> crudSelectOpts = client.eval("return crud_select_opts").get();
assertNull(((HashMap<?, ?>) crudSelectOpts.get(0)).get("mode"));

int customTimeout = 2000;
operations.select(Conditions.any(), ProxySelectOptions.create().withTimeout(customTimeout)).get();
crudSelectOpts = client.eval("return crud_select_opts").get();
assertNull(((HashMap<?, ?>) crudSelectOpts.get(0)).get("mode"));

operations.select(Conditions.any(), ProxySelectOptions.create().withMode("write")).get();
crudSelectOpts = client.eval("return crud_select_opts").get();
assertEquals("write", ((HashMap<?, ?>) crudSelectOpts.get(0)).get("mode"));
}
}

0 comments on commit 4063fc7

Please sign in to comment.