Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CRUDOperationOptions to a hierarchy of classes #258

Merged
merged 5 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ tmp/
.DS_Store
*.swp
*.swo
.bloop/
.metals/
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.tarantool.driver.core.proxy;

import java.util.HashMap;
import java.util.Map;

/**
* This class is not part of the public API.
*
* An abstract class necessary for implementing CRT (curiously recurring template)
* pattern for the cluster proxy operation options builders.
*
* @author Alexey Kuzin
*/
abstract class CRUDAbstractOperationOptions {
akudiyar marked this conversation as resolved.
Show resolved Hide resolved

private final Map<String, Object> resultMap = new HashMap<>();

/**
* Inheritable Builder for select cluster proxy operation options.
akudiyar marked this conversation as resolved.
Show resolved Hide resolved
* <p>
* This abstract class is necessary for implementing fluent builder inheritance.
* The solution with {@code self()} method allows to avoid weird java
* compiler errors when you cannot call the inherited methods from derived
* concrete {@code Builder} classes because their type erasure doesn't
* correspond to the {@code AbstractBuilder} type.
* <p>
* The {@code self()} method must be implemented only in the derived concrete
* classes. These concrete classes are used to work with the operation
* options in the calling code. They doesn't require to specify the generic
* types and therefore are more convenient. Also they ensure that the right
* combination of generic types will be used to avoid potential inheritance
* flaws.
*/
protected abstract static
class AbstractBuilder<O extends CRUDAbstractOperationOptions, B extends AbstractBuilder<O, B>> {
abstract B self();

public abstract O build();
}

protected void addOption(String option, Object value) {
resultMap.put(option, value);
}

/**
* Return serializable options representation.
*
* @return a map
*/
public Map<String, Object> asMap() {
return resultMap;
}
}
54 changes: 54 additions & 0 deletions src/main/java/io/tarantool/driver/core/proxy/CRUDBaseOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.tarantool.driver.core.proxy;

/**
* This class is not part of the public API.
*
* Represent basic options for all cluster operations
*
* @author Alexey Kuzin
*/
public class CRUDBaseOptions extends CRUDAbstractOperationOptions {

public static final String TIMEOUT = "timeout";

protected
<O extends CRUDBaseOptions, T extends AbstractBuilder<O, T>>
CRUDBaseOptions(AbstractBuilder<O, T> builder) {
if (builder.timeout != null) {
addOption(TIMEOUT, builder.timeout);
}
}

/**
* Inheritable Builder for basic cluster proxy operation options.
*
* @see CRUDAbstractOperationOptions.AbstractBuilder
*/
protected abstract static
class AbstractBuilder<O extends CRUDBaseOptions, T extends AbstractBuilder<O, T>>
extends CRUDAbstractOperationOptions.AbstractBuilder<O, T> {
protected Integer timeout;

public T withTimeout(int timeout) {
this.timeout = timeout;
return self();
}
}

/**
* Concrete Builder implementation for basic cluster proxy operation options.
*/
protected static final class Builder
extends AbstractBuilder<CRUDBaseOptions, Builder> {

@Override
Builder self() {
return this;
}

@Override
public CRUDBaseOptions build() {
return new CRUDBaseOptions(this);
}
}
}
103 changes: 0 additions & 103 deletions src/main/java/io/tarantool/driver/core/proxy/CRUDOperationOptions.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.tarantool.driver.core.proxy;

import io.tarantool.driver.protocol.Packable;

/**
* This class is not part of the public API.
*
* Represent options for select cluster proxy operations
*
* @author Sergey Volgin
* @author Alexey Kuzin
*/
public final class CRUDSelectOptions extends CRUDBaseOptions {

public static final String SELECT_LIMIT = "first";
public static final String SELECT_AFTER = "after";
public static final String SELECT_BATCH_SIZE = "batch_size";

protected
<O extends CRUDSelectOptions, T extends AbstractBuilder<O, T>>
CRUDSelectOptions(AbstractBuilder<O, T> builder) {
super(builder);

if (builder.selectLimit != null) {
addOption(SELECT_LIMIT, builder.selectLimit);
}

if (builder.after != null) {
addOption(SELECT_AFTER, builder.after);
}

if (builder.selectBatchSize != null) {
addOption(SELECT_BATCH_SIZE, builder.selectBatchSize);
}
}

/**
* Inheritable Builder for select cluster proxy operation options.
*
* @see CRUDAbstractOperationOptions.AbstractBuilder
*/
protected abstract static
class AbstractBuilder<O extends CRUDSelectOptions, T extends AbstractBuilder<O, T>>
extends CRUDBaseOptions.AbstractBuilder<O, T> {
private Long selectLimit;
private Packable after;
private Long selectBatchSize;

public T withSelectLimit(long selectLimit) {
this.selectLimit = selectLimit;
return self();
}

public T withSelectBatchSize(long selectBatchSize) {
this.selectBatchSize = selectBatchSize;
return self();
}

public T withSelectAfter(Packable startTuple) {
this.after = startTuple;
return self();
}
}

/**
* Concrete Builder implementation for select cluster proxy operation options.
*/
protected static final class Builder
extends AbstractBuilder<CRUDSelectOptions, Builder> {

@Override
Builder self() {
return this;
}

@Override
public CRUDSelectOptions build() {
return new CRUDSelectOptions(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Builder<T> withIndexQuery(TarantoolIndexQuery indexQuery) {
}

public DeleteProxyOperation<T> build() {
CRUDOperationOptions options = CRUDOperationOptions.builder()
CRUDBaseOptions options = new CRUDBaseOptions.Builder()
.withTimeout(requestTimeout)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Builder<T, R> withTuple(T tuple) {
}

public InsertProxyOperation<T, R> build() {
CRUDOperationOptions options = CRUDOperationOptions.builder()
CRUDBaseOptions options = new CRUDBaseOptions.Builder()
.withTimeout(requestTimeout)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Builder<T, R> withTuple(T tuple) {
}

public ReplaceProxyOperation<T, R> build() {
CRUDOperationOptions options = CRUDOperationOptions.builder()
CRUDBaseOptions options = new CRUDBaseOptions.Builder()
.withTimeout(requestTimeout)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Builder<T> withConditions(Conditions conditions) {
}

public SelectProxyOperation<T> build() {
CRUDOperationOptions.Builder requestOptions = CRUDOperationOptions.builder()
CRUDSelectOptions.Builder requestOptions = new CRUDSelectOptions.Builder()
.withTimeout(requestTimeout)
.withSelectBatchSize(conditions.getLimit())
.withSelectLimit(conditions.getLimit())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Builder self() {
* @return TruncateProxyOperation instance
*/
public TruncateProxyOperation build() {
CRUDOperationOptions options = CRUDOperationOptions.builder()
CRUDBaseOptions options = new CRUDBaseOptions.Builder()
.withTimeout(requestTimeout)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Builder<T> withTupleOperation(TupleOperations operations) {
}

public UpdateProxyOperation<T> build() {
CRUDOperationOptions options = CRUDOperationOptions.builder()
CRUDBaseOptions options = new CRUDBaseOptions.Builder()
.withTimeout(requestTimeout)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Builder<T, R> withTupleOperation(TupleOperations operations) {
}

public UpsertProxyOperation<T, R> build() {
CRUDOperationOptions options = CRUDOperationOptions.builder()
CRUDBaseOptions options = new CRUDBaseOptions.Builder()
.withTimeout(requestTimeout)
.build();

Expand Down
Loading