Elektra
0.8.20
|
A keyset holds together a set of keys. More...
#include <keyset.hpp>
Public Member Functions | |
KeySet () | |
Creates a new empty keyset with no keys. More... | |
KeySet (ckdb::KeySet *k) | |
Takes ownership of keyset! More... | |
KeySet (const KeySet &other) | |
Duplicate a keyset. More... | |
KeySet (size_t alloc,...) ELEKTRA_SENTINEL | |
Create a new keyset. More... | |
KeySet (VaAlloc alloc, va_list ap) | |
Create a new keyset. More... | |
~KeySet () | |
Deconstruct a keyset. More... | |
ckdb::KeySet * | release () |
If you don't want destruction of keyset at the end you can release the pointer. | |
ckdb::KeySet * | getKeySet () const |
Passes out the raw keyset pointer. More... | |
void | setKeySet (ckdb::KeySet *k) |
Take ownership of passed keyset. More... | |
KeySet & | operator= (KeySet const &other) |
Duplicate a keyset. More... | |
ssize_t | size () const |
The size of the keyset. More... | |
ckdb::KeySet * | dup () const |
Duplicate a keyset. More... | |
void | copy (const KeySet &other) |
Copy a keyset. More... | |
void | clear () |
Clear the keyset. More... | |
ssize_t | append (const Key &toAppend) |
append a key More... | |
ssize_t | append (const KeySet &toAppend) |
append a keyset More... | |
Key | head () const |
Return the first key in the KeySet. More... | |
Key | tail () const |
Return the last key in the KeySet. More... | |
void | rewind () const |
Rewinds the KeySet internal cursor. More... | |
Key | next () const |
Returns the next Key in a KeySet. More... | |
Key | current () const |
Return the current Key. More... | |
void | setCursor (cursor_t cursor) const |
Set the KeySet internal cursor. More... | |
cursor_t | getCursor () const |
Get the KeySet internal cursor. More... | |
Key | pop () |
Remove and return the last key of ks . More... | |
Key | at (cursor_t pos) const |
Lookup a key by index. More... | |
KeySet | cut (Key k) |
Cuts out a keyset at the cutpoint. More... | |
Key | lookup (const Key &k, const option_t options=KDB_O_NONE) const |
Look for a Key contained in ks that matches the name of the key . More... | |
Key | lookup (std::string const &name, const option_t options=KDB_O_NONE) const |
Lookup a key by name. More... | |
template<typename T > | |
T | get (std::string const &name, const option_t options=KDB_O_NONE) const |
Generic lookup+get for keysets. More... | |
A keyset holds together a set of keys.
Methods to manipulate KeySets. A KeySet is a set of keys.
Most important properties of a KeySet:
The most important methods of KeySet:
KeySets have an internal cursor . Methods should avoid to change this cursor, unless they want to communicate something with it. The internal cursor is used:
KeySet is the most important data structure in Elektra. It makes it possible to get and store many keys at once inside the database. In addition to that, the class can be used as high level datastructure in applications and it can be used in plugins to manipulate or check configuration.
With ksLookupByName() it is possible to fetch easily specific keys out of the list of keys.
You can easily create and iterate keys:
|
inline |
Creates a new empty keyset with no keys.
Allocate, initialize and return a new KeySet object. Objects created with ksNew() must be destroyed with ksDel().
You can use a various long list of parameters to preload the keyset with a list of keys. Either your first and only parameter is 0 or your last parameter must be KEY_END.
So, terminate with ksNew(0, KS_END) or ksNew(20, ..., KS_END)
For most uses
will be fine, the alloc size will be 16, defined in kdbprivate.h. The alloc size will be doubled whenever size reaches alloc size, so it also performs well with large keysets.
But if you have any clue how large your keyset may be you should read the next statements.
If you want a keyset with length 15 (because you know of your application that you normally need about 12 up to 15 keys), use:
If you start having 3 keys, and your application needs approximately 200-500 keys, you can use:
Alloc size is 500, the size of the keyset will be 3 after ksNew. This means the keyset will reallocate when appending more than 497 keys.
The main benefit of taking a list of variant length parameters is to be able to have one C-Statement for any possible KeySet. If you prefer, you can always create an empty KeySet and use ksAppendKey().
alloc | gives a hint for the size how many Keys may be stored initially |
0 | on memory error |
|
inline |
Takes ownership of keyset!
Keyset will be destroyed at destructor you cant continue to use keyset afterwards!
Use KeySet::release() to avoid destruction.
k | the keyset to take the ownership from |
|
inline |
Duplicate a keyset.
This keyset will be a duplicate of the other afterwards.
So it is shallow copy, to create a deep copy you have to dup() every key (it still won't copy metadata, but they are COW):
|
inlineexplicit |
Create a new keyset.
alloc | minimum number of keys to allocate |
... | variable argument list |
Allocate, initialize and return a new KeySet object. Objects created with ksNew() must be destroyed with ksDel().
You can use a various long list of parameters to preload the keyset with a list of keys. Either your first and only parameter is 0 or your last parameter must be KEY_END.
So, terminate with ksNew(0, KS_END) or ksNew(20, ..., KS_END)
For most uses
will be fine, the alloc size will be 16, defined in kdbprivate.h. The alloc size will be doubled whenever size reaches alloc size, so it also performs well with large keysets.
But if you have any clue how large your keyset may be you should read the next statements.
If you want a keyset with length 15 (because you know of your application that you normally need about 12 up to 15 keys), use:
If you start having 3 keys, and your application needs approximately 200-500 keys, you can use:
Alloc size is 500, the size of the keyset will be 3 after ksNew. This means the keyset will reallocate when appending more than 497 keys.
The main benefit of taking a list of variant length parameters is to be able to have one C-Statement for any possible KeySet. If you prefer, you can always create an empty KeySet and use ksAppendKey().
alloc | gives a hint for the size how many Keys may be stored initially |
0 | on memory error |
alloc | the allocation size |
va | the list of variable arguments |
|
inlineexplicit |
Create a new keyset.
alloc | minimum number of keys to allocate |
ap | variable arguments list |
Use va as first argument to use this constructor, e.g.:
Allocate, initialize and return a new KeySet object. Objects created with ksNew() must be destroyed with ksDel().
You can use a various long list of parameters to preload the keyset with a list of keys. Either your first and only parameter is 0 or your last parameter must be KEY_END.
So, terminate with ksNew(0, KS_END) or ksNew(20, ..., KS_END)
For most uses
will be fine, the alloc size will be 16, defined in kdbprivate.h. The alloc size will be doubled whenever size reaches alloc size, so it also performs well with large keysets.
But if you have any clue how large your keyset may be you should read the next statements.
If you want a keyset with length 15 (because you know of your application that you normally need about 12 up to 15 keys), use:
If you start having 3 keys, and your application needs approximately 200-500 keys, you can use:
Alloc size is 500, the size of the keyset will be 3 after ksNew. This means the keyset will reallocate when appending more than 497 keys.
The main benefit of taking a list of variant length parameters is to be able to have one C-Statement for any possible KeySet. If you prefer, you can always create an empty KeySet and use ksAppendKey().
alloc | gives a hint for the size how many Keys may be stored initially |
0 | on memory error |
alloc | the allocation size |
va | the list of variable arguments |
|
inline |
Deconstruct a keyset.
A destructor for KeySet objects. Cleans all internal dynamic attributes, decrement all reference pointers to all keys and then keyDel() all contained Keys, and elektraFree ()s the release the KeySet object memory (that was previously allocated by ksNew()).
ks | the keyset object to work with |
0 | when the keyset was freed |
-1 | on null pointer |
|
inline |
append a key
toAppend | key to append |
Appends a Key to the end of ks
. Will take ownership of the key toAppend
. That means ksDel(ks) will remove the key unless the key:
The reference counter of the key will be incremented to show this ownership, and thus toAppend
is not const.
If the keyname already existed in the keyset, it will be replaced with the new key.
The KeySet internal cursor will be set to the new key.
It is save to directly append newly created keys:
If you want the key to outlive the keyset, make sure to do proper ref counting:
Or if you want to avoid aliasing at all, you can duplicate the key. But then key in the keyset has another identity:
-1 | on NULL pointers |
-1 | if insertion failed, the key will be deleted then. |
|
inline |
append a keyset
toAppend | keyset to append |
Append all toAppend
contained keys to the end of the ks
. toAppend
KeySet will be left unchanged.
If a key is both in toAppend and ks, the Key in ks will be overridden.
-1 | on NULL pointers |
ks | the KeySet that will receive the keys |
toAppend | the KeySet that provides the keys that will be transferred |
|
inline |
Lookup a key by index.
pos | cursor position |
|
inline |
Clear the keyset.
Keyset will have no keys afterwards.
|
inline |
Copy a keyset.
other | other keyset to copy |
This is only a shallow copy. For a deep copy you need to dup every key.
Replace the content of a keyset with another one. Most often you may want a duplicate of a keyset, see ksDup() or append keys, see ksAppend(). But in some situations you need to copy a keyset to a existing keyset, for that this function exists.
source
.dest
will be deleted. Afterwards the content of the source will be added to the destination and the ksCurrent() is set properly in dest
.A flat copy is made, so the keys will not be duplicated, but there reference counter is updated, so both keysets need to be ksDel().
source | has to be an initialized source KeySet or NULL |
dest | has to be an initialized KeySet where to write the keys |
1 | on success |
0 | if dest was cleared successfully (source is NULL) |
-1 | on NULL pointer |
|
inline |
Return the current Key.
The pointer is NULL if you reached the end or after ksRewind().
ks | the keyset object to work with |
ks's
cursor 0 | on NULL pointer |
Cuts out a keyset at the cutpoint.
Searches for the cutpoint inside the KeySet ks. If found it cuts out everything which is below (see keyIsBelow()) this key. These keys will be missing in the keyset ks
. Instead, they will be moved to the returned keyset. If cutpoint
is not found an empty keyset is returned and ks
is not changed.
The cursor will stay at the same key as it was before. If the cursor was inside the region of cut (moved) keys, the cursor will be set to the key before the cutpoint.
If you use ksCut() on a keyset you got from kdbGet() and plan to make a kdbSet() later, make sure that you keep all keys that should not be removed permanently. You have to keep the KeySet that was returned and the KeySet ks
.
You have the keyset ks:
system/mountpoint/interest
system/mountpoint/interest/folder
system/mountpoint/interest/folder/key1
system/mountpoint/interest/folder/key2
system/mountpoint/other/key1
When you use
Then in returned
are:
system/mountpoint/interest
system/mountpoint/interest/folder
system/mountpoint/interest/folder/key1
system/mountpoint/interest/folder/key2
And in ks
are:
system/mountpoint/other/key1
So kdbSet() permanently removes all keys below system/mountpoint/interest
.
0 | on null pointers, no key name or allocation problems |
ks | the keyset to cut. It will be modified by removing all keys below the cutpoint. The cutpoint itself will also be removed. |
cutpoint | the point where to cut out the keyset |
|
inline |
Duplicate a keyset.
This is only a shallow copy. For a deep copy you need to dup every key.
Return a duplicate of a keyset. Objects created with ksDup() must be destroyed with ksDel().
Memory will be allocated as needed for dynamic properties, so you need to ksDel() the returned pointer.
A flat copy is made, so the keys will not be duplicated, but there reference counter is updated, so both keysets need ksDel().
source | has to be an initialized source KeySet |
0 | on NULL pointer |
|
inline |
Generic lookup+get for keysets.
name | the key name to get |
options | the options to be passed to lookup() |
KeyNotFoundException | if no key found |
Use
to include specializations for std types.
|
inline |
Get the KeySet internal cursor.
Use it to get the cursor of the actual position.
With the cursors it is possible to read ahead in a keyset:
It can also be used to restore the state of a keyset in a function
It is of course possible to make the KeySet const and cast its const away to set the cursor. Another way to achieve the same is to ksDup() the keyset, but it is not as efficient.
An invalid cursor will be returned directly after ksRewind(). When you set an invalid cursor ksCurrent() is 0 and ksNext() == ksHead().
ks | the keyset object to work with |
|
inline |
Passes out the raw keyset pointer.
|
inline |
Return the first key in the KeySet.
The KeySets cursor will not be affected.
If ksCurrent()==ksHead() you know you are on the first key.
ks | the keyset object to work with |
0 | on NULL pointer or empty keyset |
|
inline |
Look for a Key contained in ks
that matches the name of the key
.
/
). Furthermore, a lookup should be done for every key (also when iterating over keys) so that the specifications are honored correctly. Keys of all namespaces need to be present so that ksLookup() can work correctly, so make sure to also use kdbGet() with a cascading key.ksLookup()
is designed to let you work with a KeySet containing all keys of the application. The idea is to fully kdbGet() the whole configuration of your application and process it all at once with many ksLookup()
.
This function is efficient (at least using binary search). Together with kdbGet() which can you load the whole configuration you can write very effective but short code for configuration:
This is the way programs should get their configuration and search after the values. It is guaranteed that more namespaces can be added easily and that all values can be set by admin and user. Furthermore, using the kdb-tool, it is possible to introspect which values an application will get (by doing the same cascading lookup).
If found, ks
internal cursor will be positioned in the matched key (also accessible by ksCurrent()), and a pointer to the Key is returned. If not found, ks
internal cursor will not move, and a NULL pointer is returned.
Cascading lookups will by default search in all namespaces (proc/, dir/, user/ and system/), but will also correctly consider the specification (=metadata) in spec/:
override/#
will make sure that another key is considered beforenamespace/#
will change the number and/or order in which the namespaces are searchedfallback/#
will search for other keys when the other possibilities up to now were not successfuldefault
to return the given value when not even fallback
keys were found.This process is very flexible, but it would be boring to manually follow all this links to find out which key will be taken in the end. Use kdb get -v
to trace the keys.
The invalidation of cursors does not matter if you use multiple keysets, e.g. by using ksDup(). E.g., to separate ksLookup() with KDB_O_POP and ksAppendKey():
This is also a nice example how a complete application with ksLookup() can look like.
ks | where to look for |
key | the key object you are looking for |
options | of type option_t with some KDB_O_* option bits as explained above |
0 | on NULL pointers |
|
inline |
|
inline |
Returns the next Key in a KeySet.
KeySets have an internal cursor that can be reset with ksRewind(). Every time ksNext() is called the cursor is incremented and the new current Key is returned.
You'll get a NULL pointer if the key after the end of the KeySet was reached. On subsequent calls of ksNext() it will still return the NULL pointer.
The ks
internal cursor will be changed, so it is not const.
ks | the keyset object to work with |
0 | when the end is reached |
0 | on NULL pointer |
Duplicate a keyset.
This keyset will be a duplicate of the other afterwards.
|
inline |
Remove and return the last key of ks
.
The reference counter will be decremented by one.
The KeySets cursor will not be affected if it did not point to the popped key.
ks
NULL | if ks is empty or on NULL pointer |
ks | KeySet to work with |
|
inline |
Rewinds the KeySet internal cursor.
Use it to set the cursor to the beginning of the KeySet. ksCurrent() will then always return NULL afterwards. So you want to ksNext() first.
ks | the keyset object to work with |
0 | on success |
-1 | on NULL pointer |
|
inline |
Set the KeySet internal cursor.
Use it to set the cursor to a stored position. ksCurrent() will then be the position which you got with.
An invalid cursor will set the keyset to its beginning like ksRewind(). When you set an invalid cursor ksCurrent() is 0 and ksNext() == ksHead().
cursor | the cursor to use |
ks | the keyset object to work with |
0 | when the keyset is ksRewind()ed |
1 | otherwise |
-1 | on NULL pointer |
|
inline |
Take ownership of passed keyset.
k | the keyset to take ownership from |
|
inline |
The size of the keyset.
|
inline |
Return the last key in the KeySet.
The KeySets cursor will not be affected.
If ksCurrent()==ksTail() you know you are on the last key. ksNext() will return a NULL pointer afterwards.
ks | the keyset object to work with |
0 | on NULL pointer or empty keyset |