Skip to content

Commit 5616c49

Browse files
authored
Adds description of new key_def functions (#5131)
* Adds description of new ```key_def``` functions comparing data against the ```key_def``` object rules * Since 3.1.0, new functions are added to ```key_def``` module. * ```validate_key(key)``` - validates input key against the rules * ```validate_full_key(key)``` - validates input key's all fields against the rules * ```compare_keys(key_a, key_b)``` - Compares two keys against each other and the rules * ```validate_tuple(tuple)``` - validates the tuple against the rules * Fixes #4111
1 parent 542a4d2 commit 5616c49

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

doc/reference/reference_lua/key_def.rst

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,160 @@ to extract or compare the index key values.
230230
key_def = require('key_def')
231231
k = key_def.new({{type = 'string', fieldno = 3}})
232232
k:totable()
233+
234+
.. _key_validate_key:
235+
236+
.. method:: validate_key(key)
237+
238+
Since version :doc:`3.1.0 </release/3.1.0>`
239+
240+
Validates all parts of the specified key match the key definition. Partial keys are considered valid.
241+
Returns nothing on success.
242+
243+
If the key fails the validation, a ``box.error`` type exception is raised.
244+
245+
**Example:**
246+
247+
.. code-block:: tarantoolsession
248+
249+
-- Create a rule: key = {1 ('unsigned'), 2 (string)}
250+
-- Validate key {1001} (only id data type). Returns nothing
251+
-- Validate key {'x'}. ER_KEY_PART_TYPE is raised
252+
-- Validate key ({1000, 2000}). ER_KEY_PART_TYPE is raised
253+
-- Validate key ({1000, 'abc', 'xyz'}). ER_KEY_PART_COUNT is raised
254+
255+
tarantool> key_def = require('key_def').new({{fieldno = 1, type = 'unsigned'},
256+
> {fieldno = 2, type = 'string'}})
257+
---
258+
...
259+
260+
tarantool> key_def:validate_key({1001})
261+
---
262+
...
263+
264+
tarantool> key_def:validate_key({'x'})
265+
---
266+
- error: 'Supplied key type of part 0 does not match index part type: expected unsigned'
267+
...
268+
269+
tarantool> key_def:validate_key({1000, 2000})
270+
---
271+
- error: 'Supplied key type of part 1 does not match index part type: expected string'
272+
...
273+
274+
tarantool> key_def:validate_key({1000, 'abc', 'xyz'})
275+
---
276+
- error: 'Invalid key part count: (expected [0..2], got 3)
277+
...
278+
279+
.. _key_validate_full_key:
280+
281+
.. method:: validate_full_key(key)
282+
283+
Since version :doc:`3.1.0 </release/3.1.0>`
284+
285+
Validates whether they input ``key`` contains all fields and mathces the rules of the key definition object.
286+
Returns nothing on success.
287+
288+
If the key fails the validation, a ``box.error`` type exception is raised.
289+
290+
**Example:**
291+
292+
.. code-block:: tarantoolsession
293+
294+
-- Create a rule: key = {1 ('unsigned'), 2 (string)}
295+
-- Validate key {100, "Testuser"}. Returns nothing
296+
-- Validate key ({100}). ER_EXACT_MATCH is raised
297+
298+
tarantool> key_def = require('key_def').new({{fieldno = 1, type = 'unsigned'},
299+
> {fieldno = 2, type = 'string'}})
300+
---
301+
...
302+
303+
tarantool> key_def:validate_full_key({100, "Testuser"})
304+
---
305+
...
306+
307+
tarantool> key_def:validate_full_key({100})
308+
---
309+
- error: 'Invalid key part count in an exact match: (expected 2, got 1)
310+
...
311+
312+
.. _key_validate_tuple:
313+
314+
.. method:: validate_tuple(tuple)
315+
316+
Since version :doc:`3.1.0 </release/3.1.0>`
317+
318+
Validates whether the ``tuple`` matches the rules of the key definition object
319+
Returns nothing on success.
320+
321+
If the key fails the validation, a ``box.error`` type exception is raised.
322+
323+
**Example:**
324+
325+
.. code-block:: tarantoolsession
326+
327+
-- Create a rule: tuple = {id (number), name (string), age (number)}
328+
-- Validate tuple {1001, "Testuser", 28}. Returns nothing
329+
330+
tarantool> key_def = require('key_def').new({
331+
> {fieldno = 1, type = 'number'},
332+
> {fieldno = 2, type = 'string'},
333+
> {fieldno = 3, type = 'number'})
334+
---
335+
...
336+
337+
tarantool> key_def:validate_tuple({1001, "Testuser", 28})
338+
---
339+
...
340+
341+
.. _key_compare_keys:
342+
343+
.. method:: compare_keys(key_a, key_b)
344+
345+
Since version :doc:`3.1.0 </release/3.1.0>`
346+
347+
Compares two keys against each other and according to the key definition object.
348+
On success, returns:
349+
350+
* ``<0`` if ``key_a`` parts are less than ``key_b`` parts
351+
* ``0`` if ``key_a`` parts are equal to ``key_b`` parts
352+
* ``>0`` if ``key_a`` parts are greater than ``key_b`` parts
353+
354+
If any key does not match the key definition rules, a ``box.error`` type exception is raised.
355+
356+
**Example:**
357+
358+
.. code-block:: tarantoolsession
359+
360+
-- Create a rule: key = {1 ('unsigned'), 2 (string)}
361+
-- Validate keys ({1000, 'x'}, {1000, 'y'}). Returns -1
362+
-- Validate keys ({1000, 'x'}, {1000, 'x'}). Returns 0
363+
-- Validate keys ({1000, 'x'}, {1000}). Returns 0
364+
-- Validate keys ({2000, 'x'}, {1000, 'x'}). Returns 1
365+
366+
tarantool> key_def = require('key_def').new({{fieldno = 1, type = 'unsigned'},
367+
> {fieldno = 2, type = 'string'}})
368+
---
369+
...
370+
371+
tarantool> key_def:compare_keys({1000, 'x'}, {1000, 'y'})
372+
---
373+
- -1
374+
...
375+
376+
tarantool> key_def:compare_keys({1000, 'x'}, {1000, 'x'})
377+
---
378+
- 0
379+
...
380+
381+
tarantool> key_def:compare_keys({1000, 'x'}, {1000})
382+
---
383+
- 0
384+
...
385+
386+
tarantool> key_def:compare_keys({2000, 'x'}, {1000, 'x'})
387+
---
388+
- 1
389+
...

0 commit comments

Comments
 (0)