forked from tarantool/tarantool-java
-
Notifications
You must be signed in to change notification settings - Fork 0
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. Closes: #7, tarantool#137
- Loading branch information
1 parent
b53e0ba
commit 5f2a9ec
Showing
29 changed files
with
1,656 additions
and
207 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,294 @@ | ||
package org.tarantool; | ||
|
||
import static org.tarantool.AbstractTarantoolOps.ArgumentFactory.allResolved; | ||
import static org.tarantool.AbstractTarantoolOps.ArgumentFactory.resolved; | ||
import static org.tarantool.AbstractTarantoolOps.ArgumentFactory.unresolved; | ||
|
||
public abstract class AbstractTarantoolOps<Space, Tuple, Operation, Result> | ||
implements TarantoolClientOps<Space, Tuple, Operation, Result> { | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
public abstract class AbstractTarantoolOps<Tuple, Operation, Result> | ||
implements TarantoolClientOps<Tuple, Operation, Result> { | ||
|
||
private final Function<Object, Object> defaultSpaceResolver = | ||
space -> resolveSpace((String) space); | ||
private final BiFunction<Object, Object, Object> defaultSpaceIndexResolver = | ||
(space, index) -> resolveSpaceIndex((String) space, (String) index); | ||
|
||
private Code callCode = Code.CALL; | ||
|
||
protected abstract Result exec(Code code, Object... args); | ||
protected Result exec(Code code, Object... args) { | ||
return exec(code, allResolved(args)); | ||
} | ||
|
||
protected Result exec(long timeoutMillis, Code code, Object... args) { | ||
return exec(timeoutMillis, code, allResolved(args)); | ||
} | ||
|
||
protected abstract Result exec(Code code, ResolvableOpArgument... args); | ||
|
||
protected abstract Result exec(long timeoutMillis, Code code, ResolvableOpArgument... args); | ||
|
||
protected abstract int resolveSpace(String space); | ||
|
||
protected abstract int resolveSpaceIndex(String space, String index); | ||
|
||
public Result select(Space space, Space index, Tuple key, int offset, int limit, Iterator iterator) { | ||
@Override | ||
public Result select(Integer space, Integer index, Tuple key, int offset, int limit, Iterator iterator) { | ||
return select(space, index, key, offset, limit, iterator.getValue()); | ||
} | ||
|
||
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, 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, Tuple key, int offset, int limit, int iterator) { | ||
return doSelect(resolved(space), resolved(index), key, offset, limit, iterator); | ||
} | ||
|
||
@Override | ||
public Result select(String space, String index, Tuple key, int offset, int limit, int iterator) { | ||
return doSelect( | ||
unresolved(space, defaultSpaceResolver), | ||
unresolved(index, indexName -> defaultSpaceIndexResolver.apply(space, indexName)), | ||
key, offset, limit, iterator | ||
); | ||
} | ||
|
||
public Result insert(Space space, Tuple tuple) { | ||
return exec(Code.INSERT, Key.SPACE, space, Key.TUPLE, tuple); | ||
@Override | ||
public Result insert(Integer space, Tuple tuple) { | ||
return doInsert(resolved(space), tuple); | ||
} | ||
|
||
@Override | ||
public Result insert(String space, Tuple tuple) { | ||
return doInsert(unresolved(space, defaultSpaceResolver), tuple); | ||
} | ||
|
||
public Result replace(Space space, Tuple tuple) { | ||
return exec(Code.REPLACE, Key.SPACE, space, Key.TUPLE, tuple); | ||
@Override | ||
public Result replace(Integer space, Tuple tuple) { | ||
return doReplace(resolved(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, Tuple tuple) { | ||
return doReplace(unresolved(space, defaultSpaceResolver), 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, Tuple key, Operation... args) { | ||
return doUpdate(resolved(space), key, args); | ||
} | ||
|
||
public Result delete(Space space, Tuple key) { | ||
return exec(Code.DELETE, Key.SPACE, space, Key.KEY, key); | ||
@Override | ||
public Result update(String space, Tuple key, Operation... tuple) { | ||
return doUpdate(unresolved(space, defaultSpaceResolver), key, tuple); | ||
} | ||
|
||
@Override | ||
public Result upsert(Integer space, Tuple key, Tuple defTuple, Operation... ops) { | ||
return doUpsert(resolved(space), key, defTuple, ops); | ||
} | ||
|
||
@Override | ||
public Result upsert(String space, Tuple key, Tuple defTuple, Operation... ops) { | ||
return doUpsert(unresolved(space, defaultSpaceResolver), key, defTuple, ops); | ||
} | ||
|
||
@Override | ||
public Result delete(Integer space, Tuple key) { | ||
return doDelete(resolved(space), key); | ||
} | ||
|
||
@Override | ||
public Result delete(String space, Tuple key) { | ||
return doDelete(unresolved(space, defaultSpaceResolver), key); | ||
} | ||
|
||
@Override | ||
public Result call(String function, Object... args) { | ||
return exec(callCode, Key.FUNCTION, function, Key.TUPLE, args); | ||
} | ||
|
||
@Override | ||
public Result eval(String expression, Object... args) { | ||
return exec(Code.EVAL, Key.EXPRESSION, expression, Key.TUPLE, args); | ||
} | ||
|
||
@Override | ||
public void ping() { | ||
exec(Code.PING); | ||
} | ||
|
||
public void setCallCode(Code callCode) { | ||
this.callCode = callCode; | ||
} | ||
|
||
private Result doDelete(ResolvableOpArgument space, Tuple key) { | ||
return exec(Code.DELETE, resolved(Key.SPACE), space, resolved(Key.KEY), resolved(key)); | ||
} | ||
|
||
private Result doUpsert(ResolvableOpArgument space, Tuple key, Tuple defTuple, Operation... ops) { | ||
return exec( | ||
Code.UPSERT, | ||
resolved(Key.SPACE), space, | ||
resolved(Key.KEY), resolved(key), | ||
resolved(Key.TUPLE), resolved(defTuple), | ||
resolved(Key.UPSERT_OPS), resolved(ops) | ||
); | ||
} | ||
|
||
private Result doUpdate(ResolvableOpArgument space, Tuple key, Operation... ops) { | ||
return exec( | ||
Code.UPDATE, | ||
resolved(Key.SPACE), space, | ||
resolved(Key.KEY), resolved(key), | ||
resolved(Key.TUPLE), resolved(ops) | ||
); | ||
} | ||
|
||
private Result doReplace(ResolvableOpArgument space, Tuple tuple) { | ||
return exec(Code.REPLACE, resolved(Key.SPACE), space, resolved(Key.TUPLE), resolved(tuple)); | ||
} | ||
|
||
private Result doInsert(ResolvableOpArgument space, Tuple tuple) { | ||
return exec(Code.INSERT, resolved(Key.SPACE), space, resolved(Key.TUPLE), resolved(tuple)); | ||
} | ||
|
||
private Result doSelect(ResolvableOpArgument space, | ||
ResolvableOpArgument index, | ||
Tuple key, | ||
int offset, | ||
int limit, | ||
int iterator) { | ||
return exec( | ||
Code.SELECT, | ||
resolved(Key.SPACE), space, | ||
resolved(Key.INDEX), index, | ||
resolved(Key.KEY), resolved(key), | ||
resolved(Key.ITERATOR), resolved(iterator), | ||
resolved(Key.LIMIT), resolved(limit), | ||
resolved(Key.OFFSET), resolved(offset) | ||
); | ||
} | ||
|
||
public static class ArgumentFactory { | ||
|
||
public static ResolvableOpArgument resolved(Object value) { | ||
return new ResolvedArgument(value); | ||
} | ||
|
||
public static ResolvableOpArgument unresolved(Object key, Function<Object, Object> resolver) { | ||
return new UnresolvedArgument(key, resolver); | ||
} | ||
|
||
public static ResolvableOpArgument[] allResolved(Object[] values) { | ||
return Stream.of(values).map(ArgumentFactory::resolved).toArray(ResolvableOpArgument[]::new); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Wrapper over the target argument which possibly can't | ||
* be resolved at the moment. | ||
*/ | ||
public interface ResolvableOpArgument { | ||
|
||
/** | ||
* Checks whether an argument can be obtained instantly calling | ||
* {@link #getValue()}. | ||
* | ||
* @return {@literal true} if value can be retrieved right now. | ||
*/ | ||
boolean hasValue(); | ||
|
||
/** | ||
* Gets a target argument value. It raises | ||
* an exception if the value is unavailable. | ||
* Availability can be checked via {@link #hasValue()}. | ||
* | ||
* @return wrapped argument value | ||
* @throws RuntimeException if the value is unavailable. | ||
*/ | ||
Object getValue(); | ||
|
||
} | ||
|
||
/** | ||
* Simple wrapper that holds the original value. | ||
*/ | ||
public static class ResolvedArgument implements ResolvableOpArgument { | ||
|
||
private final Object value; | ||
|
||
private ResolvedArgument(Object value) { | ||
Objects.requireNonNull(value); | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean hasValue() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ResolvedArgument{" + | ||
"value=" + ((value instanceof Object[]) ? Arrays.toString((Object[])value) : value) + | ||
'}'; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Wrapper that evaluates the value each time | ||
* it is requested. | ||
* <p> | ||
* It works like a function, where {@code argument = f(key)}. | ||
*/ | ||
public static class UnresolvedArgument implements ResolvableOpArgument { | ||
|
||
private final Object key; | ||
private final Function<Object, Object> resolver; | ||
|
||
private UnresolvedArgument(Object key, Function<Object, Object> resolver) { | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(resolver); | ||
|
||
this.key = key; | ||
this.resolver = resolver; | ||
} | ||
|
||
@Override | ||
public boolean hasValue() { | ||
try { | ||
resolver.apply(key); | ||
} catch (Exception ignored) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object getValue() { | ||
return resolver.apply(key); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "UnresolvedArgument{" + | ||
"key=" + key + | ||
'}'; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.