Skip to content

Commit

Permalink
Unify interfaces for ProxyOperations classes
Browse files Browse the repository at this point in the history
- Add OperationWith<option-name>Options contract interfaces.
- Add ProxyOperationArgument enum class for parts of arguments list.
- Change <operation-name>ProxyOperation classes for using with contract interfaces.
- Move ProxyOperation interface from `io.tarantool.driver.core.proxy` package to `io.tarantool.driver.core.proxy.interfaces` package.

Closes #417.
  • Loading branch information
nickkkccc committed Sep 18, 2023
1 parent f16d9ca commit e712961
Show file tree
Hide file tree
Showing 23 changed files with 230 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import io.tarantool.driver.api.SingleValueCallResult;
import io.tarantool.driver.api.TarantoolCallOperations;
import io.tarantool.driver.api.space.options.interfaces.Options;
import io.tarantool.driver.api.space.options.interfaces.Self;
import io.tarantool.driver.core.proxy.enums.ProxyOperationArgument;
import io.tarantool.driver.core.proxy.interfaces.BuilderOptions;
import io.tarantool.driver.core.proxy.interfaces.ProxyOperation;
import io.tarantool.driver.mappers.CallResultMapper;
import io.tarantool.driver.mappers.MessagePackObjectMapper;

import java.util.EnumMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

Expand All @@ -21,8 +26,8 @@ abstract class AbstractProxyOperation<T> implements ProxyOperation<T> {
protected final TarantoolCallOperations client;
protected final String functionName;
protected final List<?> arguments;
private final MessagePackObjectMapper argumentsMapper;
protected final CallResultMapper<T, SingleValueCallResult<T>> resultMapper;
private final MessagePackObjectMapper argumentsMapper;

AbstractProxyOperation(
TarantoolCallOperations client,
Expand Down Expand Up @@ -59,18 +64,21 @@ public CompletableFuture<T> execute() {
}

abstract static
class GenericOperationsBuilder<T, O extends Options, B extends GenericOperationsBuilder<T, O, B>> {
class GenericOperationsBuilder<T, O extends Options, B extends GenericOperationsBuilder<T, O, B>>
implements BuilderOptions, Self<B> {
protected TarantoolCallOperations client;
protected String spaceName;
protected String functionName;
protected MessagePackObjectMapper argumentsMapper;
protected CallResultMapper<T, SingleValueCallResult<T>> resultMapper;
protected O options;
protected EnumMap<ProxyOperationArgument, Object> arguments;

GenericOperationsBuilder() {
this.arguments = new EnumMap<>(ProxyOperationArgument.class);
}

abstract B self();
public void addArgument(ProxyOperationArgument optionName, Object option) {
this.arguments.put(optionName, option);
}

/**
* Specify a client for sending and receiving requests from Tarantool server
Expand All @@ -90,7 +98,7 @@ public B withClient(TarantoolCallOperations client) {
* @return builder
*/
public B withSpaceName(String spaceName) {
this.spaceName = spaceName;
addArgument(ProxyOperationArgument.SPACE_NAME, spaceName);
return self();
}

Expand Down Expand Up @@ -134,7 +142,7 @@ public B withResultMapper(CallResultMapper<T, SingleValueCallResult<T>> resultMa
* @return builder
*/
public B withOptions(O options) {
this.options = options;
addArgument(ProxyOperationArgument.OPTIONS, options.asMap());
return self();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import io.tarantool.driver.api.SingleValueCallResult;
import io.tarantool.driver.api.TarantoolCallOperations;
import io.tarantool.driver.api.space.options.interfaces.DeleteOptions;
import io.tarantool.driver.core.proxy.contracts.OperationWithIndexQueryBuilderOptions;
import io.tarantool.driver.mappers.CallResultMapper;
import io.tarantool.driver.mappers.MessagePackObjectMapper;
import io.tarantool.driver.protocol.TarantoolIndexQuery;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -32,27 +32,22 @@ private DeleteProxyOperation(
* The builder for this class.
*/
public static final class Builder<T>
extends GenericOperationsBuilder<T, DeleteOptions<?>, Builder<T>> {
private TarantoolIndexQuery indexQuery;
extends GenericOperationsBuilder<T, DeleteOptions<?>, Builder<T>> implements
OperationWithIndexQueryBuilderOptions<Builder<T>> {

public Builder() {
}

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

public Builder<T> withIndexQuery(TarantoolIndexQuery indexQuery) {
this.indexQuery = indexQuery;
public Builder<T> self() {
return this;
}

public DeleteProxyOperation<T> build() {
List<?> arguments = Arrays.asList(spaceName, indexQuery.getKeyValues(), options.asMap());

return new DeleteProxyOperation<>(
this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper);
this.client, this.functionName, new ArrayList<>(arguments.values()), this.argumentsMapper,
this.resultMapper);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import io.tarantool.driver.api.SingleValueCallResult;
import io.tarantool.driver.api.TarantoolCallOperations;
import io.tarantool.driver.api.space.options.interfaces.InsertManyOptions;
import io.tarantool.driver.core.proxy.contracts.OperationWithTuplesBuilderOptions;
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.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
* Proxy operation for inserting many records at once
Expand All @@ -35,31 +35,21 @@ public final class InsertManyProxyOperation<T extends Packable, R extends Collec
* The builder for this class.
*/
public static final class Builder<T extends Packable, R extends Collection<T>>
extends GenericOperationsBuilder<R, InsertManyOptions<?>, Builder<T, R>> {
private Collection<T> tuples;
extends GenericOperationsBuilder<R, InsertManyOptions<?>, Builder<T, R>> implements
OperationWithTuplesBuilderOptions<Builder<T, R>, T> {

public Builder() {
}

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

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

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

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

return new InsertManyProxyOperation<>(
this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper);
this.client, this.functionName, new ArrayList<>(arguments.values()), this.argumentsMapper,
this.resultMapper);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import io.tarantool.driver.api.SingleValueCallResult;
import io.tarantool.driver.api.TarantoolCallOperations;
import io.tarantool.driver.api.space.options.interfaces.InsertOptions;
import io.tarantool.driver.core.proxy.contracts.OperationWithTupleBuilderOptions;
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.ArrayList;
import java.util.Collection;
import java.util.List;

Expand All @@ -34,28 +35,22 @@ private InsertProxyOperation(
* The builder for this class.
*/
public static final class Builder<T extends Packable, R extends Collection<T>>
extends GenericOperationsBuilder<R, InsertOptions<?>, Builder<T, R>> {
private T tuple;
extends GenericOperationsBuilder<R, InsertOptions<?>, Builder<T, R>> implements
OperationWithTupleBuilderOptions<Builder<T, R>, T> {

public Builder() {
}

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

public Builder<T, R> withTuple(T tuple) {
this.tuple = tuple;
public Builder<T, R> self() {
return this;
}

public InsertProxyOperation<T, R> build() {

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

return new InsertProxyOperation<>(
this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper);
this.client, this.functionName, new ArrayList<>(arguments.values()), this.argumentsMapper,
this.resultMapper);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import io.tarantool.driver.api.SingleValueCallResult;
import io.tarantool.driver.api.TarantoolCallOperations;
import io.tarantool.driver.api.space.options.interfaces.ReplaceManyOptions;
import io.tarantool.driver.core.proxy.contracts.OperationWithTuplesBuilderOptions;
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.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
* Proxy operation for replacing many records at once
Expand All @@ -35,31 +35,22 @@ public final class ReplaceManyProxyOperation<T extends Packable, R extends Colle
* The builder for this class.
*/
public static final class Builder<T extends Packable, R extends Collection<T>>
extends GenericOperationsBuilder<R, ReplaceManyOptions<?>, Builder<T, R>> {
private Collection<T> tuples;
extends GenericOperationsBuilder<R, ReplaceManyOptions<?>, Builder<T, R>> implements
OperationWithTuplesBuilderOptions<Builder<T, R>, T> {

public Builder() {
}

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

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

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

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

return new ReplaceManyProxyOperation<>(
this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper);
this.client, this.functionName, new ArrayList<>(arguments.values()), this.argumentsMapper,
this.resultMapper);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import io.tarantool.driver.api.SingleValueCallResult;
import io.tarantool.driver.api.TarantoolCallOperations;
import io.tarantool.driver.api.space.options.interfaces.ReplaceOptions;
import io.tarantool.driver.core.proxy.contracts.OperationWithTupleBuilderOptions;
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.ArrayList;
import java.util.Collection;
import java.util.List;

Expand Down Expand Up @@ -35,28 +36,22 @@ public final class ReplaceProxyOperation<T extends Packable, R extends Collectio
* The builder for this class.
*/
public static final class Builder<T extends Packable, R extends Collection<T>>
extends GenericOperationsBuilder<R, ReplaceOptions<?>, Builder<T, R>> {
private T tuple;
extends GenericOperationsBuilder<R, ReplaceOptions<?>, Builder<T, R>>
implements OperationWithTupleBuilderOptions<Builder<T, R>, T> {

public Builder() {
}

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

public Builder<T, R> withTuple(T tuple) {
this.tuple = tuple;
public Builder<T, R> self() {
return this;
}

public ReplaceProxyOperation<T, R> build() {

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

return new ReplaceProxyOperation<>(
this.client, this.functionName, arguments, this.argumentsMapper, this.resultMapper);
this.client, this.functionName, new ArrayList<>(arguments.values()), this.argumentsMapper,
this.resultMapper);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
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.core.proxy.enums.ProxyOperationArgument;
import io.tarantool.driver.mappers.CallResultMapper;
import io.tarantool.driver.mappers.MessagePackObjectMapper;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* Proxy operation for select
Expand Down Expand Up @@ -46,7 +49,7 @@ public Builder(TarantoolMetadataOperations operations, TarantoolSpaceMetadata me
}

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

Expand All @@ -57,17 +60,19 @@ public Builder<T> withConditions(Conditions conditions) {

public SelectProxyOperation<T> build() {

options.addOption(ProxyOption.FIRST, conditions.getLimit());
options.addOption(ProxyOption.AFTER, conditions.getStartTuple());
addArgument(ProxyOperationArgument.PROXY_QUERY,
this.conditions.toProxyQuery(this.operations, this.metadata));

List<?> arguments = Arrays.asList(
spaceName,
conditions.toProxyQuery(operations, metadata),
options.asMap()
);
Map<String, Object> options = (Map<String, Object>) arguments.get(ProxyOperationArgument.OPTIONS);

options.put(ProxyOption.FIRST.toString(), conditions.getLimit());

Optional.ofNullable(conditions.getStartTuple())
.ifPresent(after -> options.put(ProxyOption.AFTER.toString(), after));

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

0 comments on commit e712961

Please sign in to comment.