$darkmode
Elektra 0.11.0
Arrays

Problem

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.

Constraints

Assumptions

Considered Alternatives

Decision

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):

1 myarray/#0 = value0
2 myarray/#1 = value1
3 myarray/#2 = value2
4 myarray/#3 = value3
5 myarray/#4 = value4
6 myarray/#5 = value5
7 [myarray]
8  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 happening early, so that plugins can rely on having correct arrays.

For example:

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

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

To look up 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:

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

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

Rationale

Implications

Related Decisions

Notes

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