forked from dork/tarantool-java
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now client keeps actual schema metadata and sends schemaId header to be checked against current Tarantool schema version. If client version mismatches DB version client does schema reloading in the background. Client operation interface was reworked in scope of support not only number identifiers for spaces and indexes but also their string names. This also includes set of request builders that can be used as a public API to construct requests. The main idea here is to provide more natural DSL-like approach to build operations instead of current abstract types like List<?> or List<Object>. Closes: #7, #137 Affects: #212
- Loading branch information
1 parent
08e37a2
commit 39b7dfb
Showing
58 changed files
with
3,396 additions
and
424 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,158 @@ | ||
package org.tarantool; | ||
|
||
import static org.tarantool.dsl.Requests.callRequest; | ||
import static org.tarantool.dsl.Requests.deleteRequest; | ||
import static org.tarantool.dsl.Requests.evalRequest; | ||
import static org.tarantool.dsl.Requests.insertRequest; | ||
import static org.tarantool.dsl.Requests.pingRequest; | ||
import static org.tarantool.dsl.Requests.replaceRequest; | ||
import static org.tarantool.dsl.Requests.selectRequest; | ||
import static org.tarantool.dsl.Requests.updateRequest; | ||
import static org.tarantool.dsl.Requests.upsertRequest; | ||
|
||
public abstract class AbstractTarantoolOps<Space, Tuple, Operation, Result> | ||
implements TarantoolClientOps<Space, Tuple, Operation, Result> { | ||
import org.tarantool.dsl.Operation; | ||
import org.tarantool.dsl.TarantoolRequestConvertible; | ||
import org.tarantool.schema.TarantoolSchemaMeta; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public abstract class AbstractTarantoolOps<Result> | ||
implements TarantoolClientOps<List<?>, Object, Result> { | ||
|
||
private Code callCode = Code.CALL; | ||
|
||
protected abstract Result exec(Code code, Object... args); | ||
protected abstract Result exec(TarantoolRequest request); | ||
|
||
protected abstract TarantoolSchemaMeta getSchemaMeta(); | ||
|
||
public Result select(Integer space, Integer index, List<?> key, int offset, int limit, Iterator iterator) { | ||
return execute( | ||
selectRequest(space, index) | ||
.key(key) | ||
.offset(offset).limit(limit) | ||
.iterator(iterator) | ||
); | ||
} | ||
|
||
@Override | ||
public Result select(String space, String index, List<?> key, int offset, int limit, Iterator iterator) { | ||
return execute( | ||
selectRequest(space, index) | ||
.key(key) | ||
.offset(offset).limit(limit) | ||
.iterator(iterator) | ||
); | ||
} | ||
|
||
public Result select(Space space, Space index, Tuple key, int offset, int limit, Iterator iterator) { | ||
return select(space, index, key, offset, limit, iterator.getValue()); | ||
@Override | ||
public Result select(Integer space, Integer index, List<?> key, int offset, int limit, int iterator) { | ||
return execute( | ||
selectRequest(space, index) | ||
.key(key) | ||
.offset(offset).limit(limit) | ||
.iterator(iterator) | ||
); | ||
} | ||
|
||
public Result select(Space space, Space index, Tuple key, int offset, int limit, int iterator) { | ||
return exec( | ||
Code.SELECT, | ||
Key.SPACE, space, | ||
Key.INDEX, index, | ||
Key.KEY, key, | ||
Key.ITERATOR, iterator, | ||
Key.LIMIT, limit, | ||
Key.OFFSET, offset | ||
@Override | ||
public Result select(String space, String index, List<?> key, int offset, int limit, int iterator) { | ||
return execute( | ||
selectRequest(space, index) | ||
.key(key) | ||
.offset(offset).limit(limit) | ||
.iterator(iterator) | ||
); | ||
} | ||
|
||
public Result insert(Space space, Tuple tuple) { | ||
return exec(Code.INSERT, Key.SPACE, space, Key.TUPLE, tuple); | ||
@Override | ||
public Result insert(Integer space, List<?> tuple) { | ||
return execute(insertRequest(space, tuple)); | ||
} | ||
|
||
@Override | ||
public Result insert(String space, List<?> tuple) { | ||
return execute(insertRequest(space, tuple)); | ||
} | ||
|
||
public Result replace(Space space, Tuple tuple) { | ||
return exec(Code.REPLACE, Key.SPACE, space, Key.TUPLE, tuple); | ||
@Override | ||
public Result replace(Integer space, List<?> tuple) { | ||
return execute(replaceRequest(space, tuple)); | ||
} | ||
|
||
public Result update(Space space, Tuple key, Operation... args) { | ||
return exec(Code.UPDATE, Key.SPACE, space, Key.KEY, key, Key.TUPLE, args); | ||
@Override | ||
public Result replace(String space, List<?> tuple) { | ||
return execute(replaceRequest(space, tuple)); | ||
} | ||
|
||
public Result upsert(Space space, Tuple key, Tuple def, Operation... args) { | ||
return exec(Code.UPSERT, Key.SPACE, space, Key.KEY, key, Key.TUPLE, def, Key.UPSERT_OPS, args); | ||
@Override | ||
public Result update(Integer space, List<?> key, Object... operations) { | ||
Operation[] ops = Arrays.stream(operations) | ||
.map(Operation::fromArray) | ||
.toArray(org.tarantool.dsl.Operation[]::new); | ||
return execute(updateRequest(space, key, ops)); | ||
} | ||
|
||
public Result delete(Space space, Tuple key) { | ||
return exec(Code.DELETE, Key.SPACE, space, Key.KEY, key); | ||
@Override | ||
public Result update(String space, List<?> key, Object... operations) { | ||
Operation[] ops = Arrays.stream(operations) | ||
.map(Operation::fromArray) | ||
.toArray(org.tarantool.dsl.Operation[]::new); | ||
return execute(updateRequest(space, key, ops)); | ||
} | ||
|
||
@Override | ||
public Result upsert(Integer space, List<?> key, List<?> defTuple, Object... operations) { | ||
Operation[] ops = Arrays.stream(operations) | ||
.map(Operation::fromArray) | ||
.toArray(Operation[]::new); | ||
return execute(upsertRequest(space, key, defTuple, ops)); | ||
} | ||
|
||
@Override | ||
public Result upsert(String space, List<?> key, List<?> defTuple, Object... operations) { | ||
Operation[] ops = Arrays.stream(operations) | ||
.map(Operation::fromArray) | ||
.toArray(Operation[]::new); | ||
return execute(upsertRequest(space, key, defTuple, ops)); | ||
} | ||
|
||
@Override | ||
public Result delete(Integer space, List<?> key) { | ||
return execute(deleteRequest(space, key)); | ||
} | ||
|
||
@Override | ||
public Result delete(String space, List<?> key) { | ||
return execute(deleteRequest(space, key)); | ||
} | ||
|
||
@Override | ||
public Result call(String function, Object... args) { | ||
return exec(callCode, Key.FUNCTION, function, Key.TUPLE, args); | ||
return execute( | ||
callRequest(function) | ||
.arguments(args) | ||
.useCall16(callCode == Code.OLD_CALL) | ||
); | ||
} | ||
|
||
@Override | ||
public Result eval(String expression, Object... args) { | ||
return exec(Code.EVAL, Key.EXPRESSION, expression, Key.TUPLE, args); | ||
return execute(evalRequest(expression).arguments(args)); | ||
} | ||
|
||
@Override | ||
public void ping() { | ||
exec(Code.PING); | ||
execute(pingRequest()); | ||
} | ||
|
||
@Override | ||
public Result execute(TarantoolRequestConvertible requestSpec) { | ||
return exec(requestSpec.toTarantoolRequest(getSchemaMeta())); | ||
} | ||
|
||
public void setCallCode(Code callCode) { | ||
this.callCode = callCode; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.tarantool; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Request argument factory. | ||
* | ||
* @see TarantoolRequestArgument | ||
*/ | ||
public class RequestArguments { | ||
|
||
private RequestArguments() { | ||
} | ||
|
||
public static TarantoolRequestArgument value(Object value) { | ||
return new SimpleArgument(value); | ||
} | ||
|
||
public static TarantoolRequestArgument cacheLookupValue(Supplier<Object> supplier) { | ||
return new LookupArgument(supplier); | ||
} | ||
|
||
/** | ||
* Simple wrapper that holds the original value. | ||
*/ | ||
private static class SimpleArgument implements TarantoolRequestArgument { | ||
|
||
private Object value; | ||
|
||
SimpleArgument(Object value) { | ||
Objects.requireNonNull(value); | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean isSerializable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object getValue() { | ||
return value; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Wrapper that evaluates the value each time | ||
* it is requested. | ||
* <p> | ||
* It works like a function, where {@code argument = f(key)}. | ||
*/ | ||
private static class LookupArgument implements TarantoolRequestArgument { | ||
|
||
Supplier<Object> lookup; | ||
|
||
LookupArgument(Supplier<Object> lookup) { | ||
this.lookup = Objects.requireNonNull(lookup); | ||
} | ||
|
||
@Override | ||
public boolean isSerializable() { | ||
try { | ||
lookup.get(); | ||
} catch (Exception ignored) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public synchronized Object getValue() { | ||
return lookup.get(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.