Skip to content

Commit 45e4b39

Browse files
committed
dsl: introduce Tarantool client DSL
This DSL 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: #212
1 parent 061c8d4 commit 45e4b39

20 files changed

+1319
-114
lines changed

README.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,194 @@ all the results, you could override this:
134134
protected void complete(TarantoolPacket packet, CompletableFuture<?> future);
135135
```
136136

137+
### Supported operation types
138+
139+
Given a tarantool space as:
140+
141+
```lua
142+
box.schema.space.create('cars', { format =
143+
{ {name='id', type='integer'},"
144+
{name='name', type='string'},"
145+
{name='max_mph', type='integer'} }
146+
});
147+
box.space.cars:create_index('pk', { type='TREE', parts={'id'} });
148+
box.space.cars:create_index('speed_idx', { type='TREE', unique=false, parts={'max_mph', type='unsigned'} });
149+
```
150+
151+
and a stored function as well:
152+
153+
```lua
154+
function getVehiclesSlowerThan(max_mph, max_size)
155+
return box.space.cars.index.speed_idx:select(max_mph, {iterator='LT', limit=max_size});
156+
end;
157+
```
158+
159+
Let's have a look what sort of operations we can apply to it using a connector.
160+
*Note*: assume Tarantool generated id equal `512` for the newly created `cars` space.
161+
162+
* SELECT (find tuples matching the search pattern)
163+
164+
For instance, we can get a single tuple by id like
165+
166+
```java
167+
ops.select(512, 0, Collections.singletonList(1), 0, 1, Iterator.EQ);
168+
```
169+
170+
or using more readable lookup names
171+
172+
```java
173+
ops.select("cars", "pk", Collections.singletonList(1), 0, 1, Iterator.EQ);
174+
```
175+
176+
or even using builder-way to construct a query part-by-part
177+
178+
```java
179+
import static org.tarantool.dsl.Requests.selectRequest;
180+
181+
ops.execute(
182+
selectRequest("cars", "pk")
183+
.iterator(Iterator.EQ)
184+
.limit(1)
185+
);
186+
```
187+
188+
* INSERT (put a tuple in the space)
189+
190+
Let's insert a new tuple into the space
191+
192+
```java
193+
ops.insert(512, Arrays.asList(1, "Lada Niva", 81));
194+
```
195+
196+
do the same using names
197+
198+
```java
199+
ops.insert("cars", Arrays.asList(1, "Lada Niva", 81));
200+
```
201+
202+
or using DSL
203+
204+
```java
205+
import static org.tarantool.dsl.Requests.insertRequest;
206+
207+
ops.execute(
208+
insertRequest("cars", Arrays.asList(1, "Lada Niva", 81))
209+
);
210+
```
211+
212+
* REPLACE (insert a tuple into the space or replace an existing one)
213+
214+
The syntax is quite similar to insert operation
215+
216+
```java
217+
import static org.tarantool.dsl.Requests.replaceRequest;
218+
219+
ops.replace(512, Arrays.asList(2, "UAZ-469", 60));
220+
ops.replace("cars", Arrays.asList(2, "UAZ-469", 60));
221+
ops.execute(
222+
replaceRequest("cars", Arrays.asList(2, "UAZ-469", 60))
223+
);
224+
```
225+
226+
* UPDATE (update a tuple)
227+
228+
Let's modify one of existing tuples
229+
230+
```java
231+
ops.update(512, Collections.singletonList(1), Arrays.asList("=", 1, "Lada 4×4"));
232+
```
233+
234+
Lookup way:
235+
236+
```java
237+
ops.update("cars", Collections.singletonList(1), Arrays.asList("=", 1, "Lada 4×4"));
238+
```
239+
240+
or using DSL
241+
242+
```java
243+
import static org.tarantool.dsl.Operations.assign;
244+
import static org.tarantool.dsl.Requests.updateRequest;
245+
246+
ops.execute(
247+
updateRequest("cars", Collections.singletonList(1), assign(1, "Lada 4×4"))
248+
);
249+
```
250+
251+
*Note*: since Tarantool 2.3.x you can refer to tuple fields by name:
252+
253+
```java
254+
ops.update(512, Collections.singletonList(1), Arrays.asList("=", "name", "Lada 4×4"));
255+
```
256+
257+
* UPSERT (update a tuple if it exists, otherwise try to insert it as a new tuple)
258+
259+
An example looks as a mix of both insert and update operations:
260+
261+
```java
262+
import static org.tarantool.dsl.Operations.assign;
263+
import static org.tarantool.dsl.Requests.upsertRequest;
264+
265+
ops.upsert(512, Collections.singletonList(3), Arrays.asList(3, "KAMAZ-65224", 65), Arrays.asList("=", 2, 65));
266+
ops.upsert("cars", Collections.singletonList(3), Arrays.asList(3, "KAMAZ-65224", 65), Arrays.asList("=", 2, 65));
267+
ops.execute(
268+
upsertRequest("cars", Collections.singletonList(3), assign(2, 65))
269+
);
270+
```
271+
272+
*Note*: since Tarantool 2.3.x you can refer to tuple fields by name:
273+
274+
```java
275+
ops.upsert("cars", Collections.singletonList(3), Arrays.asList(3, "KAMAZ-65224", 65), Arrays.asList("=", "max_mph", 65));
276+
```
277+
278+
* DELETE (delete a tuple)
279+
280+
Remove a tuple using one of the following forms:
281+
282+
```java
283+
import static org.tarantool.dsl.Requests.deleteRequest;
284+
285+
ops().delete(512, Collections.singletonList(1));
286+
// same via lookup
287+
ops().delete("cars", Collections.singletonList(1));
288+
// same via DSL
289+
ops.execute(deleteRequest("cars", Collections.singletonList(1)));
290+
```
291+
292+
* CALL / CALL v1.6 (call a stored function)
293+
294+
Let's invoke the predefined function to fetch slower enough vehicles:
295+
296+
```java
297+
import static org.tarantool.dsl.Requests.callRequest;
298+
299+
ops().call("getVehiclesSlowerThan", 80, 10);
300+
// same via DSL
301+
ops.execute(callRequest("getVehiclesSlowerThan").arguments(80, 10));
302+
```
303+
304+
*NOTE*: to use obsolete Tarantool v1.6 operation, configure it as follows:
305+
306+
```java
307+
ops().setCallCode(Code.OLD_CALL);
308+
ops().call("getVehiclesSlowerThan", 80, 10);
309+
// same via DSL
310+
ops.execute(callRequest("getVehiclesSlowerThan").arguments(80, 10).useCall16(true));
311+
``` \
312+
313+
* EVAL (evaluate a Lua expression)
314+
315+
To evaluate expressions using Lua, you can invoke the following operation:
316+
317+
```java
318+
import static org.tarantool.dsl.Requests.evalRequest;
319+
320+
ops().eval("return getVehiclesSlowerThan(...)", 90, 50);
321+
// same via DSL
322+
ops.execute(evalRequest("return getVehiclesSlowerThan(...)")).arguments(90, 50));
323+
```
324+
137325
### Client config options
138326

139327
The client configuration options are represented through the `TarantoolClientConfig` class.

0 commit comments

Comments
 (0)