Elektra
0.9.6
|
The main building block of Elektra’s database are hierarchical key-value pairs. You can create such a pair using `kdb set`:
. The command above created a key user:/tests/parent
with the value value
. Since Elektra uses a hierarchical database we can also create keys below user:/tests/parent
:
. We can check the hierarchy of the keys using kdb ls
and retrieve data using kdb get
:
Since Elektra sorts keys alphabetically we can use the key-value pair structure described above to store sequences of values (aka. arrays).
For an empty array ([]
) we just add the metakey array
:
To create an array element we start the basename of a key with the #
character and add the index of the array element afterwards. For example, the commands below adds three elements to our array user:/tests/sequence
:
. As you can see above arrays can contain “empty fields”: The key user:/tests/sequence/#2
is missing.
For array elements with an index larger than 9
we must add underscores (_
) to the basename, so we do not destroy the alphabetic order of the array. For example, to add a eleventh element to our array we use the following command:
. The order of the array sequence is still correct afterwards, as the following command shows:
. For larger indices we add one underscore less, than the number of digits of the index. For example, to add a element with index 1337
(4
digits) we use the basename #___1337
. We can also generate the basename programmatically:
Elektra’s arrays require that you always add the metakey array
to the array parent. Otherwise the values below the parent will not be interpreted as array elements, but rather as normal key-value pairs. To make the array
metakey more useful storage plugins should save the basename of the last key in the array parent. This of course works only, if the plugins already stores this information in the config file, either
array
directly in the config file). Either way, in some situations you might have to add this value manually via kdb meta-set
:
. This way you can always retrieve the last element of an array easily:
We close this tutorial by removing the data created by the commands above: