|
1 | 1 | package org.tarantool;
|
2 | 2 |
|
| 3 | +import static org.tarantool.AbstractTarantoolOps.ArgumentFactory.allResolved; |
| 4 | +import static org.tarantool.AbstractTarantoolOps.ArgumentFactory.resolved; |
| 5 | +import static org.tarantool.AbstractTarantoolOps.ArgumentFactory.unresolved; |
3 | 6 |
|
4 |
| -public abstract class AbstractTarantoolOps<Space, Tuple, Operation, Result> |
5 |
| - implements TarantoolClientOps<Space, Tuple, Operation, Result> { |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Objects; |
| 9 | +import java.util.function.BiFunction; |
| 10 | +import java.util.function.Function; |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +public abstract class AbstractTarantoolOps<Tuple, Operation, Result> |
| 14 | + implements TarantoolClientOps<Tuple, Operation, Result> { |
| 15 | + |
| 16 | + private final Function<Object, Object> defaultSpaceResolver = |
| 17 | + space -> resolveSpace((String) space); |
| 18 | + private final BiFunction<Object, Object, Object> defaultSpaceIndexResolver = |
| 19 | + (space, index) -> resolveSpaceIndex((String) space, (String) index); |
6 | 20 |
|
7 | 21 | private Code callCode = Code.CALL;
|
8 | 22 |
|
9 |
| - protected abstract Result exec(Code code, Object... args); |
| 23 | + protected Result exec(Code code, Object... args) { |
| 24 | + return exec(code, allResolved(args)); |
| 25 | + } |
| 26 | + |
| 27 | + protected Result exec(long timeoutMillis, Code code, Object... args) { |
| 28 | + return exec(timeoutMillis, code, allResolved(args)); |
| 29 | + } |
| 30 | + |
| 31 | + protected abstract Result exec(Code code, ResolvableOpArgument... args); |
| 32 | + |
| 33 | + protected abstract Result exec(long timeoutMillis, Code code, ResolvableOpArgument... args); |
| 34 | + |
| 35 | + protected abstract int resolveSpace(String space); |
| 36 | + |
| 37 | + protected abstract int resolveSpaceIndex(String space, String index); |
10 | 38 |
|
11 |
| - public Result select(Space space, Space index, Tuple key, int offset, int limit, Iterator iterator) { |
| 39 | + @Override |
| 40 | + public Result select(Integer space, Integer index, Tuple key, int offset, int limit, Iterator iterator) { |
12 | 41 | return select(space, index, key, offset, limit, iterator.getValue());
|
13 | 42 | }
|
14 | 43 |
|
15 |
| - public Result select(Space space, Space index, Tuple key, int offset, int limit, int iterator) { |
16 |
| - return exec( |
17 |
| - Code.SELECT, |
18 |
| - Key.SPACE, space, |
19 |
| - Key.INDEX, index, |
20 |
| - Key.KEY, key, |
21 |
| - Key.ITERATOR, iterator, |
22 |
| - Key.LIMIT, limit, |
23 |
| - Key.OFFSET, offset |
| 44 | + @Override |
| 45 | + public Result select(String space, String index, Tuple key, int offset, int limit, Iterator iterator) { |
| 46 | + return select(space, index, key, offset, limit, iterator.getValue()); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Result select(Integer space, Integer index, Tuple key, int offset, int limit, int iterator) { |
| 51 | + return doSelect(resolved(space), resolved(index), key, offset, limit, iterator); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Result select(String space, String index, Tuple key, int offset, int limit, int iterator) { |
| 56 | + return doSelect( |
| 57 | + unresolved(space, defaultSpaceResolver), |
| 58 | + unresolved(index, indexName -> defaultSpaceIndexResolver.apply(space, indexName)), |
| 59 | + key, offset, limit, iterator |
24 | 60 | );
|
25 | 61 | }
|
26 | 62 |
|
27 |
| - public Result insert(Space space, Tuple tuple) { |
28 |
| - return exec(Code.INSERT, Key.SPACE, space, Key.TUPLE, tuple); |
| 63 | + @Override |
| 64 | + public Result insert(Integer space, Tuple tuple) { |
| 65 | + return doInsert(resolved(space), tuple); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Result insert(String space, Tuple tuple) { |
| 70 | + return doInsert(unresolved(space, defaultSpaceResolver), tuple); |
29 | 71 | }
|
30 | 72 |
|
31 |
| - public Result replace(Space space, Tuple tuple) { |
32 |
| - return exec(Code.REPLACE, Key.SPACE, space, Key.TUPLE, tuple); |
| 73 | + @Override |
| 74 | + public Result replace(Integer space, Tuple tuple) { |
| 75 | + return doReplace(resolved(space), tuple); |
33 | 76 | }
|
34 | 77 |
|
35 |
| - public Result update(Space space, Tuple key, Operation... args) { |
36 |
| - return exec(Code.UPDATE, Key.SPACE, space, Key.KEY, key, Key.TUPLE, args); |
| 78 | + @Override |
| 79 | + public Result replace(String space, Tuple tuple) { |
| 80 | + return doReplace(unresolved(space, defaultSpaceResolver), tuple); |
37 | 81 | }
|
38 | 82 |
|
39 |
| - public Result upsert(Space space, Tuple key, Tuple def, Operation... args) { |
40 |
| - return exec(Code.UPSERT, Key.SPACE, space, Key.KEY, key, Key.TUPLE, def, Key.UPSERT_OPS, args); |
| 83 | + @Override |
| 84 | + public Result update(Integer space, Tuple key, Operation... args) { |
| 85 | + return doUpdate(resolved(space), key, args); |
41 | 86 | }
|
42 | 87 |
|
43 |
| - public Result delete(Space space, Tuple key) { |
44 |
| - return exec(Code.DELETE, Key.SPACE, space, Key.KEY, key); |
| 88 | + @Override |
| 89 | + public Result update(String space, Tuple key, Operation... tuple) { |
| 90 | + return doUpdate(unresolved(space, defaultSpaceResolver), key, tuple); |
45 | 91 | }
|
46 | 92 |
|
| 93 | + @Override |
| 94 | + public Result upsert(Integer space, Tuple key, Tuple defTuple, Operation... ops) { |
| 95 | + return doUpsert(resolved(space), key, defTuple, ops); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Result upsert(String space, Tuple key, Tuple defTuple, Operation... ops) { |
| 100 | + return doUpsert(unresolved(space, defaultSpaceResolver), key, defTuple, ops); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Result delete(Integer space, Tuple key) { |
| 105 | + return doDelete(resolved(space), key); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Result delete(String space, Tuple key) { |
| 110 | + return doDelete(unresolved(space, defaultSpaceResolver), key); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
47 | 114 | public Result call(String function, Object... args) {
|
48 | 115 | return exec(callCode, Key.FUNCTION, function, Key.TUPLE, args);
|
49 | 116 | }
|
50 | 117 |
|
| 118 | + @Override |
51 | 119 | public Result eval(String expression, Object... args) {
|
52 | 120 | return exec(Code.EVAL, Key.EXPRESSION, expression, Key.TUPLE, args);
|
53 | 121 | }
|
54 | 122 |
|
| 123 | + @Override |
55 | 124 | public void ping() {
|
56 | 125 | exec(Code.PING);
|
57 | 126 | }
|
58 | 127 |
|
59 | 128 | public void setCallCode(Code callCode) {
|
60 | 129 | this.callCode = callCode;
|
61 | 130 | }
|
| 131 | + |
| 132 | + private Result doDelete(ResolvableOpArgument space, Tuple key) { |
| 133 | + return exec(Code.DELETE, resolved(Key.SPACE), space, resolved(Key.KEY), resolved(key)); |
| 134 | + } |
| 135 | + |
| 136 | + private Result doUpsert(ResolvableOpArgument space, Tuple key, Tuple defTuple, Operation... ops) { |
| 137 | + return exec( |
| 138 | + Code.UPSERT, |
| 139 | + resolved(Key.SPACE), space, |
| 140 | + resolved(Key.KEY), resolved(key), |
| 141 | + resolved(Key.TUPLE), resolved(defTuple), |
| 142 | + resolved(Key.UPSERT_OPS), resolved(ops) |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + private Result doUpdate(ResolvableOpArgument space, Tuple key, Operation... ops) { |
| 147 | + return exec( |
| 148 | + Code.UPDATE, |
| 149 | + resolved(Key.SPACE), space, |
| 150 | + resolved(Key.KEY), resolved(key), |
| 151 | + resolved(Key.TUPLE), resolved(ops) |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + private Result doReplace(ResolvableOpArgument space, Tuple tuple) { |
| 156 | + return exec(Code.REPLACE, resolved(Key.SPACE), space, resolved(Key.TUPLE), resolved(tuple)); |
| 157 | + } |
| 158 | + |
| 159 | + private Result doInsert(ResolvableOpArgument space, Tuple tuple) { |
| 160 | + return exec(Code.INSERT, resolved(Key.SPACE), space, resolved(Key.TUPLE), resolved(tuple)); |
| 161 | + } |
| 162 | + |
| 163 | + private Result doSelect(ResolvableOpArgument space, |
| 164 | + ResolvableOpArgument index, |
| 165 | + Tuple key, |
| 166 | + int offset, |
| 167 | + int limit, |
| 168 | + int iterator) { |
| 169 | + return exec( |
| 170 | + Code.SELECT, |
| 171 | + resolved(Key.SPACE), space, |
| 172 | + resolved(Key.INDEX), index, |
| 173 | + resolved(Key.KEY), resolved(key), |
| 174 | + resolved(Key.ITERATOR), resolved(iterator), |
| 175 | + resolved(Key.LIMIT), resolved(limit), |
| 176 | + resolved(Key.OFFSET), resolved(offset) |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + public static class ArgumentFactory { |
| 181 | + |
| 182 | + public static ResolvableOpArgument resolved(Object value) { |
| 183 | + return new ResolvedArgument(value); |
| 184 | + } |
| 185 | + |
| 186 | + public static ResolvableOpArgument unresolved(Object key, Function<Object, Object> resolver) { |
| 187 | + return new UnresolvedArgument(key, resolver); |
| 188 | + } |
| 189 | + |
| 190 | + public static ResolvableOpArgument[] allResolved(Object[] values) { |
| 191 | + return Stream.of(values).map(ArgumentFactory::resolved).toArray(ResolvableOpArgument[]::new); |
| 192 | + } |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Wrapper over the target argument which possibly can't |
| 198 | + * be resolved at the moment. |
| 199 | + */ |
| 200 | + public interface ResolvableOpArgument { |
| 201 | + |
| 202 | + /** |
| 203 | + * Checks whether an argument can be obtained instantly calling |
| 204 | + * {@link #getValue()}. |
| 205 | + * |
| 206 | + * @return {@literal true} if value can be retrieved right now. |
| 207 | + */ |
| 208 | + boolean hasValue(); |
| 209 | + |
| 210 | + /** |
| 211 | + * Gets a target argument value. It raises |
| 212 | + * an exception if the value is unavailable. |
| 213 | + * Availability can be checked via {@link #hasValue()}. |
| 214 | + * |
| 215 | + * @return wrapped argument value |
| 216 | + * @throws RuntimeException if the value is unavailable. |
| 217 | + */ |
| 218 | + Object getValue(); |
| 219 | + |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Simple wrapper that holds the original value. |
| 224 | + */ |
| 225 | + public static class ResolvedArgument implements ResolvableOpArgument { |
| 226 | + |
| 227 | + private final Object value; |
| 228 | + |
| 229 | + private ResolvedArgument(Object value) { |
| 230 | + Objects.requireNonNull(value); |
| 231 | + this.value = value; |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public boolean hasValue() { |
| 236 | + return true; |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public Object getValue() { |
| 241 | + return value; |
| 242 | + } |
| 243 | + |
| 244 | + @Override |
| 245 | + public String toString() { |
| 246 | + return "ResolvedArgument{" + |
| 247 | + "value=" + ((value instanceof Object[]) ? Arrays.toString((Object[])value) : value) + |
| 248 | + '}'; |
| 249 | + } |
| 250 | + |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Wrapper that evaluates the value each time |
| 255 | + * it is requested. |
| 256 | + * <p> |
| 257 | + * It works like a function, where {@code argument = f(key)}. |
| 258 | + */ |
| 259 | + public static class UnresolvedArgument implements ResolvableOpArgument { |
| 260 | + |
| 261 | + private final Object key; |
| 262 | + private final Function<Object, Object> resolver; |
| 263 | + |
| 264 | + private UnresolvedArgument(Object key, Function<Object, Object> resolver) { |
| 265 | + Objects.requireNonNull(key); |
| 266 | + Objects.requireNonNull(resolver); |
| 267 | + |
| 268 | + this.key = key; |
| 269 | + this.resolver = resolver; |
| 270 | + } |
| 271 | + |
| 272 | + @Override |
| 273 | + public boolean hasValue() { |
| 274 | + try { |
| 275 | + resolver.apply(key); |
| 276 | + } catch (Exception ignored) { |
| 277 | + return false; |
| 278 | + } |
| 279 | + return true; |
| 280 | + } |
| 281 | + |
| 282 | + @Override |
| 283 | + public Object getValue() { |
| 284 | + return resolver.apply(key); |
| 285 | + } |
| 286 | + |
| 287 | + @Override |
| 288 | + public String toString() { |
| 289 | + return "UnresolvedArgument{" + |
| 290 | + "key=" + key + |
| 291 | + '}'; |
| 292 | + } |
| 293 | + } |
62 | 294 | }
|
0 commit comments