Skip to content

Commit

Permalink
Support insert_many and replace_many CRUD operations
Browse files Browse the repository at this point in the history
  • Loading branch information
akudiyar committed Sep 18, 2022
1 parent 4f35b61 commit 489521d
Show file tree
Hide file tree
Showing 12 changed files with 474 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

## [Unreleased]

### Features
- Added support for insert_many and replace_many CRUD operations ([#259](https://github.com/tarantool/cartridge-java/issues/259))

## [0.8.2] - 2022-09-16

### Features
- Removed code duplication in *ProxyOperations builders ([#256](https://github.com/tarantool/cartridge-java/issues/256))
- Added client EventLoopThreadsNumber property for control netty work threads ([#253](https://github.com/tarantool/cartridge-java/pull/253))

### Misc
- Removed code duplication in *ProxyOperations builders ([#256](https://github.com/tarantool/cartridge-java/issues/256))
- Refactor CRUDOperationOptions to a hierarchy of classes ([#258](https://github.com/tarantool/cartridge-java/issues/258))

## [0.8.1] - 2022-08-18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public final class ProxyOperationsMappingConfig {
public static final String SCHEMA_FUNCTION = "ddl.get_schema";
public static final String DELETE_FUNCTION = CRUD_PREFIX + "delete";
public static final String INSERT_FUNCTION = CRUD_PREFIX + "insert";
public static final String INSERT_MANY_FUNCTION = CRUD_PREFIX + "insert_many";
public static final String REPLACE_FUNCTION = CRUD_PREFIX + "replace";
public static final String REPLACE_MANY_FUNCTION = CRUD_PREFIX + "replace_many";
public static final String SELECT_FUNCTION = CRUD_PREFIX + "select";
public static final String UPDATE_FUNCTION = CRUD_PREFIX + "update";
public static final String UPSERT_FUNCTION = CRUD_PREFIX + "upsert";
Expand All @@ -21,7 +23,9 @@ public final class ProxyOperationsMappingConfig {
private final String schemaFunctionName;
private final String deleteFunctionName;
private final String insertFunctionName;
private final String insertManyFunctionName;
private final String replaceFunctionName;
private final String replaceManyFunctionName;
private final String updateFunctionName;
private final String upsertFunctionName;
private final String selectFunctionName;
Expand Down Expand Up @@ -58,6 +62,16 @@ public String getInsertFunctionName() {
return insertFunctionName;
}

/**
* Get API function name for performing the insert_many operation.
* The default value is <code>crud.insert_many</code>.
*
* @return a callable API function name
*/
public String getInsertManyFunctionName() {
return insertManyFunctionName;
}

/**
* Get API function name for performing the replace operation. The default value is <code>crud.replace</code>.
*
Expand All @@ -67,6 +81,16 @@ public String getReplaceFunctionName() {
return replaceFunctionName;
}

/**
* Get API function name for performing the replace_many operation.
* The default value is <code>crud.replace_many</code>.
*
* @return a callable API function name
*/
public String getReplaceManyFunctionName() {
return replaceManyFunctionName;
}

/**
* Get API function name for performing the update operation. The default value is <code>crud.update</code>.
*
Expand Down Expand Up @@ -104,13 +128,16 @@ public String getTruncateFunctionName() {
}

private ProxyOperationsMappingConfig(String schemaFunctionName, String deleteFunctionName,
String insertFunctionName, String replaceFunctionName,
String insertFunctionName, String insertManyFunctionName,
String replaceFunctionName, String replaceManyFunctionName,
String updateFunctionName, String upsertFunctionName,
String selectFunctionName, String truncateFunctionName) {
this.schemaFunctionName = schemaFunctionName;
this.deleteFunctionName = deleteFunctionName;
this.insertFunctionName = insertFunctionName;
this.insertManyFunctionName = insertManyFunctionName;
this.replaceFunctionName = replaceFunctionName;
this.replaceManyFunctionName = replaceManyFunctionName;
this.updateFunctionName = updateFunctionName;
this.upsertFunctionName = upsertFunctionName;
this.selectFunctionName = selectFunctionName;
Expand All @@ -134,7 +161,9 @@ public static final class Builder {
private String schemaFunctionName = SCHEMA_FUNCTION;
private String deleteFunctionName = DELETE_FUNCTION;
private String insertFunctionName = INSERT_FUNCTION;
private String insertManyFunctionName = INSERT_MANY_FUNCTION;
private String replaceFunctionName = REPLACE_FUNCTION;
private String replaceManyFunctionName = REPLACE_MANY_FUNCTION;
private String updateFunctionName = UPDATE_FUNCTION;
private String upsertFunctionName = UPSERT_FUNCTION;
private String selectFunctionName = SELECT_FUNCTION;
Expand Down Expand Up @@ -176,6 +205,17 @@ public Builder withInsertFunctionName(String insertFunctionName) {
return this;
}

/**
* Get API function name for performing the insert_many operation
*
* @param insertManyFunctionName name for stored function performing insert_many operation
* @return a callable API function name
*/
public Builder withInsertManyFunctionName(String insertManyFunctionName) {
this.insertManyFunctionName = insertManyFunctionName;
return this;
}

/**
* Get API function name for performing the replace operation
*
Expand All @@ -187,6 +227,17 @@ public Builder withReplaceFunctionName(String replaceFunctionName) {
return this;
}

/**
* Get API function name for performing the replace_many operation
*
* @param replaceManyFunctionName name for stored function performing replace_many operation
* @return a callable API function name
*/
public Builder withReplaceManyFunctionName(String replaceManyFunctionName) {
this.replaceManyFunctionName = replaceManyFunctionName;
return this;
}

/**
* Get API function name for performing the update operation
*
Expand Down Expand Up @@ -238,8 +289,8 @@ public Builder withTruncateFunctionName(String truncateFunctionName) {
*/
public ProxyOperationsMappingConfig build() {
return new ProxyOperationsMappingConfig(schemaFunctionName, deleteFunctionName, insertFunctionName,
replaceFunctionName, updateFunctionName, upsertFunctionName, selectFunctionName,
truncateFunctionName);
insertManyFunctionName, replaceFunctionName, replaceManyFunctionName, updateFunctionName,
upsertFunctionName, selectFunctionName, truncateFunctionName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public interface TarantoolSpaceOperations<T extends Packable, R extends Collecti
*/
CompletableFuture<R> insert(T tuple) throws TarantoolClientException;

/**
* Inserts several tuples into the space at once. If writing of any tuple fails,
* all tuples will not be saved.
*
* @param tuples new data
* @return a future that will contain all corresponding tuples once completed
* @throws TarantoolClientException in case if request failed
*/
CompletableFuture<R> insertMany(Collection<T> tuples) throws TarantoolClientException;

/**
* Insert a tuple into the space or replace an existing one.
*
Expand All @@ -46,6 +56,16 @@ public interface TarantoolSpaceOperations<T extends Packable, R extends Collecti
*/
CompletableFuture<R> replace(T tuple) throws TarantoolClientException;

/**
* Insert or replace several tuples into the space at once. If writing of any tuple fails,
* all tuples will not be saved.
*
* @param tuples new data
* @return a future that will contain all corresponding tuples once completed
* @throws TarantoolClientException in case if request failed
*/
CompletableFuture<R> replaceMany(Collection<T> tuples) throws TarantoolClientException;

/**
* Select tuples matching the specified query with options.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.tarantool.driver.core.proxy;

import io.tarantool.driver.api.SingleValueCallResult;
import io.tarantool.driver.api.TarantoolCallOperations;
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.Collection;
import java.util.List;

/**
* Proxy operation for inserting many records at once
*
* @param <T> result type
* @param <R> result collection type
* @author Alexey Kuzin
*/
public final class InsertManyProxyOperation<T extends Packable, R extends Collection<T>>
extends AbstractProxyOperation<R> {

InsertManyProxyOperation(TarantoolCallOperations client,
String functionName,
List<?> arguments,
MessagePackObjectMapper argumentsMapper,
CallResultMapper<R, SingleValueCallResult<R>> resultMapper) {
super(client, functionName, arguments, argumentsMapper, resultMapper);
}

/**
* The builder for this class.
*/
public static final class Builder<T extends Packable, R extends Collection<T>>
extends GenericOperationsBuilder<R, Builder<T, R>> {
private Collection<T> tuples;
private Boolean stopOnError;
private Boolean rollbackOnError;

public Builder() {
}

@Override
Builder<T, R> self() {
return this;
}

public Builder<T, R> withTuples(Collection<T> tuples) {
this.tuples = tuples;
return this;
}

public Builder<T, R> withStopOnError(boolean stopOnError) {
this.stopOnError = stopOnError;
return this;
}

public Builder<T, R> withRollbackOnError(boolean rollbackOnError) {
this.rollbackOnError = rollbackOnError;
return this;
}

public InsertManyProxyOperation<T, R> build() {
if (tuples == null) {
throw new IllegalArgumentException("Tuples must be specified for batch insert operation");
}

CRUDBatchOptions options = new CRUDBatchOptions.Builder()
.withTimeout(requestTimeout)
.withStopOnError(stopOnError)
.withRollbackOnError(rollbackOnError)
.build();

List<?> arguments = Arrays.asList(spaceName, tuples, options.asMap());

return new InsertManyProxyOperation<>(
this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.tarantool.driver.core.proxy;

import io.tarantool.driver.api.SingleValueCallResult;
import io.tarantool.driver.api.TarantoolCallOperations;
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.Collection;
import java.util.List;

/**
* Proxy operation for replacing many records at once
*
* @param <T> result type
* @param <R> result collection type
* @author Alexey Kuzin
*/
public final class ReplaceManyProxyOperation<T extends Packable, R extends Collection<T>>
extends AbstractProxyOperation<R> {

ReplaceManyProxyOperation(TarantoolCallOperations client,
String functionName,
List<?> arguments,
MessagePackObjectMapper argumentsMapper,
CallResultMapper<R, SingleValueCallResult<R>> resultMapper) {
super(client, functionName, arguments, argumentsMapper, resultMapper);
}

/**
* The builder for this class.
*/
public static final class Builder<T extends Packable, R extends Collection<T>>
extends GenericOperationsBuilder<R, Builder<T, R>> {
private Collection<T> tuples;
private Boolean stopOnError;
private Boolean rollbackOnError;

public Builder() {
}

@Override
Builder<T, R> self() {
return this;
}

public Builder<T, R> withTuples(Collection<T> tuples) {
this.tuples = tuples;
return this;
}

public Builder<T, R> withStopOnError(boolean stopOnError) {
this.stopOnError = stopOnError;
return this;
}

public Builder<T, R> withRollbackOnError(boolean rollbackOnError) {
this.rollbackOnError = rollbackOnError;
return this;
}

public ReplaceManyProxyOperation<T, R> build() {
if (tuples == null) {
throw new IllegalArgumentException("Tuples must be specified for batch replace operation");
}

CRUDBatchOptions options = new CRUDBatchOptions.Builder()
.withTimeout(requestTimeout)
.withStopOnError(stopOnError)
.withRollbackOnError(rollbackOnError)
.build();

List<?> arguments = Arrays.asList(spaceName, tuples, options.asMap());

return new ReplaceManyProxyOperation<>(
this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper);
}
}
}
Loading

0 comments on commit 489521d

Please sign in to comment.