Skip to content

Commit 163b69c

Browse files
p7novandreyaksenov
andauthored
Add default values docs (#4371)
Resolves #3520 #3617 Co-authored-by: Andrey Aksenov <38073144+andreyaksenov@users.noreply.github.com>
1 parent 9e72c46 commit 163b69c

File tree

6 files changed

+228
-12
lines changed

6 files changed

+228
-12
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
local fio = require('fio')
2+
local server = require('luatest.server')
3+
local t = require('luatest')
4+
local g = t.group()
5+
g.before_each(function(cg)
6+
cg.server = server:new {
7+
box_cfg = {},
8+
workdir = fio.cwd() .. '/tmp'
9+
}
10+
cg.server:start()
11+
end)
12+
13+
g.after_each(function(cg)
14+
cg.server:drop()
15+
fio.rmtree(cg.server.workdir)
16+
end)
17+
18+
g.test_default_func = function(cg)
19+
cg.server:exec(function()
20+
21+
-- create_no_arg_function_start
22+
box.schema.func.create('current_year', {
23+
language = 'Lua',
24+
body = "function() return require('datetime').now().year end"
25+
})
26+
-- create_no_arg_function_end
27+
28+
-- format_space_default_func_start
29+
local books = box.schema.space.create('books')
30+
books:format({
31+
{ name = 'id', type = 'unsigned' },
32+
{ name = 'isbn', type = 'string' },
33+
{ name = 'title', type = 'string' },
34+
{ name = 'year', type = 'unsigned', default_func = 'current_year' }
35+
})
36+
books:create_index('primary', { parts = { 1 } })
37+
-- format_space_default_func_end
38+
39+
-- insert_ok_start
40+
books:insert { 1, '978000', 'Thinking in Java' }
41+
-- insert_ok_end
42+
43+
-- create_arg_function_start
44+
box.schema.func.create('randomize', {
45+
language = 'Lua',
46+
body = "function(limit) return math.random(limit.min, limit.max) end"
47+
})
48+
-- create_arg_function_end
49+
50+
-- reformat_space_start
51+
books:format({
52+
{ name = 'id', type = 'unsigned', default_func= 'randomize', default = {min = 0, max = 1000} },
53+
{ name = 'isbn', type = 'string' },
54+
{ name = 'title', type = 'string' },
55+
{ name = 'year', type = 'unsigned', default_func = 'current_year' }
56+
})
57+
-- reformat_space_end
58+
59+
books:insert { nil, '978001', 'How to code in Go' }
60+
61+
-- Tests --
62+
t.assert_equals(books:count(), 2)
63+
t.assert_equals(books:get(1), { 1, '978000','Thinking in Java', 2024 })
64+
end)
65+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
local fio = require('fio')
2+
local server = require('luatest.server')
3+
local t = require('luatest')
4+
local g = t.group()
5+
g.before_each(function(cg)
6+
cg.server = server:new {
7+
box_cfg = {},
8+
workdir = fio.cwd() .. '/tmp'
9+
}
10+
cg.server:start()
11+
end)
12+
13+
g.after_each(function(cg)
14+
cg.server:drop()
15+
fio.rmtree(cg.server.workdir)
16+
end)
17+
18+
g.test_default_values = function(cg)
19+
cg.server:exec(function()
20+
-- configure_space_start
21+
local books = box.schema.space.create('books')
22+
books:format({
23+
{ name = 'id', type = 'number' },
24+
{ name = 'name', type = 'string' },
25+
{ name = 'year', type = 'number', default = 2024 },
26+
})
27+
books:create_index('primary', { parts = { 1 } })
28+
-- configure_space_end
29+
30+
-- insert_ok_start
31+
books:insert { 1, 'Thinking in Java' }
32+
books:insert { 2, 'How to code in Go', nil }
33+
-- insert_ok_end
34+
35+
-- Tests --
36+
t.assert_equals(books:count(), 2)
37+
t.assert_equals(books:get(1), { 1, 'Thinking in Java', 2024 })
38+
end)
39+
end

doc/concepts/data_model/value_store.rst

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,120 @@ Charts explaining the precise differences from DUCET order are
562562
in the
563563
`Common Language Data Repository <https://unicode.org/cldr/charts/30/collation>`_.
564564

565+
566+
.. _index-defaults:
567+
568+
Default values
569+
--------------
570+
571+
*Default values* are assigned to tuple fields automatically if these fields are
572+
skipped during the tuple :ref:`insert or update <index-box_data-operations>`.
573+
574+
You can specify a default value for a field in the :ref:`space_object:format() <box_space-format>`
575+
call that defines the space format. Default values apply regardless of the field nullability:
576+
any tuple in which the field is skipped or set to `nil` receives
577+
the default value.
578+
579+
Default values can be set in two ways: explicitly or using a function.
580+
581+
.. _index-defaults-explicit:
582+
583+
Explicit default values
584+
~~~~~~~~~~~~~~~~~~~~~~~
585+
586+
Explicit default values are defined in the ``default`` parameter of the field declaration
587+
in a :ref:`space_object:format() <box_space-format>` call.
588+
589+
.. literalinclude:: /code_snippets/test/default_values/explicit_default_test.lua
590+
:language: lua
591+
:start-after: configure_space_start
592+
:end-before: configure_space_end
593+
:dedent:
594+
595+
To use a default value for a field, skip it or assign `nil`:
596+
597+
.. literalinclude:: /code_snippets/test/default_values/explicit_default_test.lua
598+
:language: lua
599+
:start-after: insert_ok_start
600+
:end-before: insert_ok_end
601+
:dedent:
602+
603+
Any Lua object that can be evaluated during the ``space_object.format()`` call
604+
may be used as a default value, for example:
605+
606+
- a constant: ``default = 100``
607+
- an initialized variable: ``default = default_size``
608+
- an expression: ``default = 10 + default_size``
609+
- a function return value: ``default = count_default()``
610+
611+
.. important::
612+
613+
Explicit default values are evaluated **only** when setting the space format.
614+
If you use a variable as a default value, its further assignments do not affect the default value.
615+
616+
To change the default values, call ``space_object:format()`` again.
617+
618+
619+
See also the :ref:`space_object:format() <box_space-format>` reference.
620+
621+
.. _index-defaults-functions:
622+
623+
Default functions
624+
~~~~~~~~~~~~~~~~~
625+
626+
A default value can be defined as a return value of a stored Lua function. To be
627+
the default, a function must be created with :ref:`box.schema.func.create() <box_schema-func_create>`
628+
with the function body and return one value of the field's type. It also must not :ref:`yield <app-yields>`.
629+
630+
.. literalinclude:: /code_snippets/test/default_values/default_functions_test.lua
631+
:language: lua
632+
:start-after: create_no_arg_function_start
633+
:end-before: create_no_arg_function_end
634+
:dedent:
635+
636+
Default functions are set in the ``default_func`` parameter of the field declaration
637+
in a ``space_object:format()`` call. To make a function with no arguments the default
638+
for a field, specify its name:
639+
640+
.. literalinclude:: /code_snippets/test/default_values/default_functions_test.lua
641+
:language: lua
642+
:start-after: format_space_default_func_start
643+
:end-before: format_space_default_func_end
644+
:dedent:
645+
646+
A default function can also have one argument.
647+
648+
.. literalinclude:: /code_snippets/test/default_values/default_functions_test.lua
649+
:language: lua
650+
:start-after: create_arg_function_start
651+
:end-before: create_arg_function_end
652+
:dedent:
653+
654+
To pass the function argument when setting the default, specify it in the ``default`` parameter
655+
of the ``space_object:format()`` call:
656+
657+
.. literalinclude:: /code_snippets/test/default_values/default_functions_test.lua
658+
:language: lua
659+
:start-after: reformat_space_start
660+
:end-before: reformat_space_end
661+
:dedent:
662+
663+
664+
.. note::
665+
666+
A key difference between a default function (``default_func = 'count_default'``)
667+
and a function return value used as a field default value (``default = count_default()``)
668+
is the following:
669+
670+
- A *default function* is called **every time a default value must be produced**,
671+
that is, a tuple is inserted or updated without specifying the field.
672+
- A return value used a field *default value*: the function is called **once**
673+
when setting the space format. Then, all tuples receive the result of
674+
this exact call if the field is not specified.
675+
676+
See also the :ref:`space_object.format() <box_space-format>` reference.
677+
678+
565679
.. _index-constraints:
566680

567681
Constraints

doc/enterprise/read_views.rst

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ Tarantool duplicates only blocks modified after a read view is created.
2121
Tarantool Enterprise Edition supports read views starting from v2.11.0 and enables the ability
2222
to work with them using both :ref:`Lua <read_views_lua_api>` and :ref:`C API <read_views_c_api>`.
2323

24-
25-
26-
27-
2824
.. _read_views_limitations:
2925

3026
Limitations
@@ -34,8 +30,7 @@ Read views have the following limitations:
3430

3531
- Only the :ref:`memtx <engines-memtx>` engine is supported.
3632
- Only TREE and HASH :ref:`indexes <index-types>` are supported.
37-
- Pagination is not supported (the :ref:`after <box_index-select>` option).
38-
33+
- Pagination (the :ref:`after <box_index-select>` option) is supported only for TREE indexes.
3934

4035

4136
.. _working_with_read_views:
@@ -73,8 +68,6 @@ After creating a read view, you can see the information about it by calling
7368
7469
To list all the created read views, call the :ref:`box.read_view.list() <reference_lua-box_read_view_list>` function.
7570

76-
77-
7871
.. _querying_data:
7972

8073
Querying data
@@ -112,6 +105,7 @@ Similarly, you can retrieve data by the specific index.
112105
...
113106
114107
108+
ADD example with fech_pos and after
115109

116110
.. _closing_read_view:
117111

@@ -134,7 +128,6 @@ After the read view is closed,
134128
its :ref:`status <read_view_object-status>` is set to ``closed``.
135129
On an attempt to use it, an error is raised.
136130

137-
138131
.. _read_views_example:
139132

140133
Example
@@ -202,7 +195,7 @@ as described in :ref:`Using data operations <box_space-operations-detailed-examp
202195
...
203196
204197
205-
198+
??? ADD example with fetch_pos and after
206199
.. toctree::
207200
:maxdepth: 2
208201
:hidden:

doc/reference/reference_lua/box_space/format.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ space_object:format()
4747
See also: :ref:`key_part.collation <key_part_collation>`.
4848
* (Optional) The ``constraint`` table specifies the :ref:`constraints <index-constraints>` that the field value must satisfy.
4949
* (Optional) The ``foreign_key`` table specifies the :ref:`foreign keys <index-box_foreign_keys>` for the field.
50+
* (Optional) The ``default`` value specifies the :ref:`explicit default value <index-defaults-explicit>` for the field
51+
or the argument of the default function if ``default_func`` is specified.
52+
* (Optional) The ``default_func`` string value specifies the name of the field's :ref:`default function <index-defaults-functions>`.
53+
To pass the default function's argument, add the ``default`` parameter.
5054

5155
It is not legal for tuples to contain values that have the wrong type.
5256
The example below will cause an error:

doc/release/3.0.0.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ It's possible to revert to the old behavior by toggling the new ``binary_data_de
447447
Default field values
448448
~~~~~~~~~~~~~~~~~~~~
449449

450-
You can now assign the default values for specific fields when defining a :ref:`space format <box_space-format>`.
450+
You can now assign the :ref:`default values <index-defaults>` for specific fields
451+
when defining a :ref:`space format <box_space-format>`.
451452
In this example, the ``isbn`` and ``title`` fields have the specified default values:
452453

453454
.. code-block:: lua
@@ -490,7 +491,7 @@ Then, assign the function name to ``default_func`` when defining a space format:
490491
{ name = 'year_of_publication', type = 'unsigned', default_func = 'current_year' }
491492
})
492493
493-
494+
Learn more in :ref:`index-defaults`.
494495

495496
.. _3-0-triggers:
496497

0 commit comments

Comments
 (0)