|
1 | 1 | package org.tarantool;
|
2 | 2 |
|
| 3 | +import static org.tarantool.dsl.Requests.callRequest; |
| 4 | +import static org.tarantool.dsl.Requests.deleteRequest; |
| 5 | +import static org.tarantool.dsl.Requests.evalRequest; |
| 6 | +import static org.tarantool.dsl.Requests.insertRequest; |
| 7 | +import static org.tarantool.dsl.Requests.pingRequest; |
| 8 | +import static org.tarantool.dsl.Requests.replaceRequest; |
| 9 | +import static org.tarantool.dsl.Requests.selectRequest; |
| 10 | +import static org.tarantool.dsl.Requests.updateRequest; |
| 11 | +import static org.tarantool.dsl.Requests.upsertRequest; |
3 | 12 |
|
4 |
| -public abstract class AbstractTarantoolOps<Space, Tuple, Operation, Result> |
5 |
| - implements TarantoolClientOps<Space, Tuple, Operation, Result> { |
| 13 | +import org.tarantool.dsl.Operation; |
| 14 | +import org.tarantool.dsl.TarantoolRequestConvertible; |
| 15 | +import org.tarantool.schema.TarantoolSchemaMeta; |
| 16 | + |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +public abstract class AbstractTarantoolOps<Result> |
| 21 | + implements TarantoolClientOps<List<?>, Object, Result> { |
6 | 22 |
|
7 | 23 | private Code callCode = Code.CALL;
|
8 | 24 |
|
9 |
| - protected abstract Result exec(Code code, Object... args); |
| 25 | + protected abstract Result exec(TarantoolRequest request); |
| 26 | + |
| 27 | + protected abstract TarantoolSchemaMeta getSchemaMeta(); |
| 28 | + |
| 29 | + public Result select(Integer space, Integer index, List<?> key, int offset, int limit, Iterator iterator) { |
| 30 | + return execute( |
| 31 | + selectRequest(space, index) |
| 32 | + .key(key) |
| 33 | + .offset(offset).limit(limit) |
| 34 | + .iterator(iterator) |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Result select(String space, String index, List<?> key, int offset, int limit, Iterator iterator) { |
| 40 | + return execute( |
| 41 | + selectRequest(space, index) |
| 42 | + .key(key) |
| 43 | + .offset(offset).limit(limit) |
| 44 | + .iterator(iterator) |
| 45 | + ); |
| 46 | + } |
10 | 47 |
|
11 |
| - public Result select(Space space, Space index, Tuple key, int offset, int limit, Iterator iterator) { |
12 |
| - return select(space, index, key, offset, limit, iterator.getValue()); |
| 48 | + @Override |
| 49 | + public Result select(Integer space, Integer index, List<?> key, int offset, int limit, int iterator) { |
| 50 | + return execute( |
| 51 | + selectRequest(space, index) |
| 52 | + .key(key) |
| 53 | + .offset(offset).limit(limit) |
| 54 | + .iterator(iterator) |
| 55 | + ); |
13 | 56 | }
|
14 | 57 |
|
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 |
| 58 | + @Override |
| 59 | + public Result select(String space, String index, List<?> key, int offset, int limit, int iterator) { |
| 60 | + return execute( |
| 61 | + selectRequest(space, index) |
| 62 | + .key(key) |
| 63 | + .offset(offset).limit(limit) |
| 64 | + .iterator(iterator) |
24 | 65 | );
|
25 | 66 | }
|
26 | 67 |
|
27 |
| - public Result insert(Space space, Tuple tuple) { |
28 |
| - return exec(Code.INSERT, Key.SPACE, space, Key.TUPLE, tuple); |
| 68 | + @Override |
| 69 | + public Result insert(Integer space, List<?> tuple) { |
| 70 | + return execute(insertRequest(space, tuple)); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Result insert(String space, List<?> tuple) { |
| 75 | + return execute(insertRequest(space, tuple)); |
29 | 76 | }
|
30 | 77 |
|
31 |
| - public Result replace(Space space, Tuple tuple) { |
32 |
| - return exec(Code.REPLACE, Key.SPACE, space, Key.TUPLE, tuple); |
| 78 | + @Override |
| 79 | + public Result replace(Integer space, List<?> tuple) { |
| 80 | + return execute(replaceRequest(space, tuple)); |
33 | 81 | }
|
34 | 82 |
|
35 |
| - public Result update(Space space, Tuple key, Operation... args) { |
36 |
| - return exec(Code.UPDATE, Key.SPACE, space, Key.KEY, key, Key.TUPLE, args); |
| 83 | + @Override |
| 84 | + public Result replace(String space, List<?> tuple) { |
| 85 | + return execute(replaceRequest(space, tuple)); |
37 | 86 | }
|
38 | 87 |
|
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); |
| 88 | + @Override |
| 89 | + public Result update(Integer space, List<?> key, Object... operations) { |
| 90 | + Operation[] ops = Arrays.stream(operations) |
| 91 | + .map(Operation::fromArray) |
| 92 | + .toArray(org.tarantool.dsl.Operation[]::new); |
| 93 | + return execute(updateRequest(space, key, ops)); |
41 | 94 | }
|
42 | 95 |
|
43 |
| - public Result delete(Space space, Tuple key) { |
44 |
| - return exec(Code.DELETE, Key.SPACE, space, Key.KEY, key); |
| 96 | + @Override |
| 97 | + public Result update(String space, List<?> key, Object... operations) { |
| 98 | + Operation[] ops = Arrays.stream(operations) |
| 99 | + .map(Operation::fromArray) |
| 100 | + .toArray(org.tarantool.dsl.Operation[]::new); |
| 101 | + return execute(updateRequest(space, key, ops)); |
45 | 102 | }
|
46 | 103 |
|
| 104 | + @Override |
| 105 | + public Result upsert(Integer space, List<?> key, List<?> defTuple, Object... operations) { |
| 106 | + Operation[] ops = Arrays.stream(operations) |
| 107 | + .map(Operation::fromArray) |
| 108 | + .toArray(Operation[]::new); |
| 109 | + return execute(upsertRequest(space, key, defTuple, ops)); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public Result upsert(String space, List<?> key, List<?> defTuple, Object... operations) { |
| 114 | + Operation[] ops = Arrays.stream(operations) |
| 115 | + .map(Operation::fromArray) |
| 116 | + .toArray(Operation[]::new); |
| 117 | + return execute(upsertRequest(space, key, defTuple, ops)); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Result delete(Integer space, List<?> key) { |
| 122 | + return execute(deleteRequest(space, key)); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public Result delete(String space, List<?> key) { |
| 127 | + return execute(deleteRequest(space, key)); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
47 | 131 | public Result call(String function, Object... args) {
|
48 |
| - return exec(callCode, Key.FUNCTION, function, Key.TUPLE, args); |
| 132 | + return execute( |
| 133 | + callRequest(function) |
| 134 | + .arguments(args) |
| 135 | + .useCall16(callCode == Code.OLD_CALL) |
| 136 | + ); |
49 | 137 | }
|
50 | 138 |
|
| 139 | + @Override |
51 | 140 | public Result eval(String expression, Object... args) {
|
52 |
| - return exec(Code.EVAL, Key.EXPRESSION, expression, Key.TUPLE, args); |
| 141 | + return execute(evalRequest(expression).arguments(args)); |
53 | 142 | }
|
54 | 143 |
|
| 144 | + @Override |
55 | 145 | public void ping() {
|
56 |
| - exec(Code.PING); |
| 146 | + execute(pingRequest()); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public Result execute(TarantoolRequestConvertible requestSpec) { |
| 151 | + return exec(requestSpec.toTarantoolRequest(getSchemaMeta())); |
57 | 152 | }
|
58 | 153 |
|
59 | 154 | public void setCallCode(Code callCode) {
|
60 | 155 | this.callCode = callCode;
|
61 | 156 | }
|
| 157 | + |
62 | 158 | }
|
0 commit comments