Elektra  0.9.4
Arrays

Currently it is inefficient to detect the length of an array and it is impossible to know if a key (without subkeys) should be an array or not.

Store length in metadata array of key, or keep metadata array empty if empty array. Only children that have # syntax are allowed in a valid array. The index start with #0. Both keyAddName("#12") or keyAddBaseName("#_12") is allowed to add the 13th index.

For example (ni syntax, sections used for metadata):

myarray/#0 = value0
myarray/#1 = value1
myarray/#2 = value2
myarray/#3 = value3
myarray/#4 = value4
myarray/#5 = value5
[myarray]
array = #5

It is not allowed to have anything else than #5 in the metadata array, e.g. even #4 would be a malformed array.

With the metadata array in spec:/ the spec plugin will add the array marker with the correct length. This needs to be happen early, so that plugins can rely on having correct arrays.

For example:

spec:/myarray # <- has array marker
user:/myarray # <- either has correct array marker or no array marker
user:/myarray/#0
user:/myarray/#1

Here, the spec plugin would add array=#1 to user:/myarray if it was not there before.

To lookup an array, first do ksLookupByName (ks, "/myarray", 0) on the parent. With the last index you get from its metadata array, iterate over the children. A cascading lookup on every individual child is also needed to make sure that overrides on individual elements are respected.

For example:

spec:/myarray # <- contains the specification for the array
spec:/myarray/# # <- contains the specification for the array elements
dir:/myarray/#0 # <- not an array, just an override for user:/myarray/#
user:/myarray # <- with metadata array=#0, this would be the array we get
user:/myarray/#0
system:/myarray # <- not found in cascading lookup, as user:/myarray exists

The spec plugin should check if it is a valid array, i.e.:

https://github.com/ElektraInitiative/libelektra/issues/182