Skip to content

Commit

Permalink
Soft automatic schema reload
Browse files Browse the repository at this point in the history
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
nicktorwald committed Jul 1, 2019
1 parent b53e0ba commit 5f2a9ec
Show file tree
Hide file tree
Showing 29 changed files with 1,656 additions and 207 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ makes possible for the client to configure a socket provider.
* `ComposableAsyncOps` - return the operation result as a `CompletionStage`
* `FireAndForgetOps` - returns the query ID

Each operation that requires space or index to be executed, can work with
number ID as well as string name of a space or an index.
Assume, we have `my_space` space with space ID `512` and its primary index
`primary` with index ID `0`. Then, for instance, `select` operations can be
performed as the following:

```java
client.syncOps().select(512, 0, Collections.singletonList(1), 0, 1, Iterator.EQ);
// or using more convenient way
client.syncOps().select("my_space", "primary", Collections.singletonList(1), 0, 1, Iterator.EQ);
```

Feel free to override any method of `TarantoolClientImpl`. For example, to hook
all the results, you could override this:

Expand Down
278 changes: 255 additions & 23 deletions src/main/java/org/tarantool/AbstractTarantoolOps.java
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 +
'}';
}
}
}
12 changes: 1 addition & 11 deletions src/main/java/org/tarantool/TarantoolBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

public abstract class TarantoolBase<Result> extends AbstractTarantoolOps<Integer, List<?>, Object, Result> {
public abstract class TarantoolBase<Result> extends AbstractTarantoolOps<List<?>, Object, Result> {
protected String serverVersion;
protected MsgPackLite msgPackLite = MsgPackLite.INSTANCE;
protected AtomicLong syncId = new AtomicLong();
Expand Down Expand Up @@ -42,16 +42,6 @@ protected void closeChannel(SocketChannel channel) {
}
}

protected void validateArgs(Object[] args) {
if (args != null) {
for (int i = 0; i < args.length; i += 2) {
if (args[i + 1] == null) {
throw new NullPointerException(((Key) args[i]).name() + " should not be null");
}
}
}
}

public void setInitialRequestSize(int initialRequestSize) {
this.initialRequestSize = initialRequestSize;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/tarantool/TarantoolClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import java.util.concurrent.TimeUnit;

public interface TarantoolClient {
TarantoolClientOps<Integer, List<?>, Object, List<?>> syncOps();
TarantoolClientOps<List<?>, Object, List<?>> syncOps();

TarantoolClientOps<Integer, List<?>, Object, Future<List<?>>> asyncOps();
TarantoolClientOps<List<?>, Object, Future<List<?>>> asyncOps();

TarantoolClientOps<Integer, List<?>, Object, CompletionStage<List<?>>> composableAsyncOps();
TarantoolClientOps<List<?>, Object, CompletionStage<List<?>>> composableAsyncOps();

TarantoolClientOps<Integer, List<?>, Object, Long> fireAndForgetOps();
TarantoolClientOps<List<?>, Object, Long> fireAndForgetOps();

TarantoolSQLOps<Object, Long, List<Map<String, Object>>> sqlSyncOps();

Expand Down
Loading

0 comments on commit 5f2a9ec

Please sign in to comment.