|
| KeySet () |
| Creates a new empty keyset with no keys.
|
|
| KeySet (ckdb::KeySet *k) |
| Takes ownership of keyset!
|
|
| KeySet (const KeySet &other) |
| Duplicate a keyset.
|
|
| KeySet (size_t alloc,...) |
| Create a new keyset.
|
|
| KeySet (Va va, size_t alloc, va_list ap) |
| Create a new keyset.
|
|
| ~KeySet () |
| Deconstruct a keyset.
|
|
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.
|
|
void | setKeySet (ckdb::KeySet *k) |
| Take ownership of passed keyset.
|
|
KeySet & | operator= (KeySet const &other) |
| Duplicate a keyset.
|
|
ssize_t | size () const |
| The size of the keyset.
|
|
ckdb::KeySet * | dup () const |
| Duplicate a keyset.
|
|
void | copy (const KeySet &other) |
| Copy a keyset.
|
|
void | clear () |
| Clear the keyset.
|
|
ssize_t | append (const Key &toAppend) |
| append a key
|
|
ssize_t | append (const KeySet &toAppend) |
| append a keyset
|
|
Key | head () const |
| Return the first key in the KeySet.
|
|
Key | tail () const |
| Return the last key in the KeySet.
|
|
void | rewind () const |
| Rewinds the KeySet internal cursor.
|
|
Key | next () const |
| Returns the next Key in a KeySet.
|
|
Key | current () const |
| Return the current Key.
|
|
void | setCursor (cursor_t cursor) const |
| Set the KeySet internal cursor.
|
|
cursor_t | getCursor () const |
| Get the KeySet internal cursor.
|
|
Key | pop () |
| Remove and return the last key of ks .
|
|
KeySet | cut (Key k) |
| Cuts out a keyset at the cutpoint.
|
|
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 .
|
|
Key | lookup (std::string const &name, const option_t options=KDB_O_NONE) const |
| Lookup a key by name.
|
|
Key | at (cursor_t pos) const |
| Lookup a key by index.
|
|
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:
- Allows us to iterate over all keys (in any depth)
- Iteration is always sorted
- Fast key lookup
- A Key may be shared among many KeySets.
The most important methods of KeySet:
- Note
- Because the key is not copied, also the pointer to the current metadata keyNextMeta() will be shared.
With
ksRewind() and
ksNext() you can iterate through the keyset. Be assured that you will get every key of the set in a stable order (parents before children).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:
- in ksLookup(): points to the found key
- in kdbSet(): points to the key which caused an error
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:
#include <kdb.h>
KeySet *myConfig =
ksNew(20,
KS_END);
Key *current;
while ((current=
ksNext(myConfig))!=0)
{
printf(
"Key name is %s.\n",
keyName (current));
}
- Invariant
- always holds an underlying elektra keyset.
- Note
- that the cursor is mutable, so it might be changed even in const functions as described.
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
goes ok, 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 out 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:
keyNew (
"user/sw/app/fixedConfiguration/key01", KEY_SWITCH_VALUE,
"value01", 0),
keyNew (
"user/sw/app/fixedConfiguration/key02", KEY_SWITCH_VALUE,
"value02", 0),
keyNew (
"user/sw/app/fixedConfiguration/key03", KEY_SWITCH_VALUE,
"value03", 0),
keyNew (
"user/sw/app/fixedConfiguration/key15", KEY_SWITCH_VALUE,
"value15", 0),
KS_END);
If you start having 3 keys, and your application needs approximately 200-500 keys, you can use:
keyNew (
"user/sw/app/fixedConfiguration/key1", KEY_SWITCH_VALUE,
"value1", 0),
keyNew (
"user/sw/app/fixedConfiguration/key2", KEY_SWITCH_VALUE,
"value2", 0),
keyNew (
"user/sw/app/fixedConfiguration/key3", KEY_SWITCH_VALUE,
"value3", 0),
KS_END);
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.
- Postcondition
- the keyset is rewinded properly
- See Also
- ksDel() to free the KeySet afterwards
-
ksDup() to duplicate an existing KeySet
- Parameters
-
alloc | gives a hint for the size how many Keys may be stored initially |
- Returns
- a ready to use KeySet object
-
0 on memory error
kdb::KeySet::KeySet |
( |
size_t |
alloc, |
|
|
|
... |
|
) |
| |
|
inlineexplicit |
Create a new keyset.
- Parameters
-
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
goes ok, 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 out 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:
keyNew (
"user/sw/app/fixedConfiguration/key01", KEY_SWITCH_VALUE,
"value01", 0),
keyNew (
"user/sw/app/fixedConfiguration/key02", KEY_SWITCH_VALUE,
"value02", 0),
keyNew (
"user/sw/app/fixedConfiguration/key03", KEY_SWITCH_VALUE,
"value03", 0),
keyNew (
"user/sw/app/fixedConfiguration/key15", KEY_SWITCH_VALUE,
"value15", 0),
KS_END);
If you start having 3 keys, and your application needs approximately 200-500 keys, you can use:
keyNew (
"user/sw/app/fixedConfiguration/key1", KEY_SWITCH_VALUE,
"value1", 0),
keyNew (
"user/sw/app/fixedConfiguration/key2", KEY_SWITCH_VALUE,
"value2", 0),
keyNew (
"user/sw/app/fixedConfiguration/key3", KEY_SWITCH_VALUE,
"value3", 0),
KS_END);
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.
- Postcondition
- the keyset is rewinded properly
- See Also
- ksDel() to free the KeySet afterwards
-
ksDup() to duplicate an existing KeySet
- Parameters
-
alloc | gives a hint for the size how many Keys may be stored initially |
- Returns
- a ready to use KeySet object
-
0 on memory error
- Precondition
- caller must call va_start and va_end
- va the list of arguments
- Parameters
-
alloc | the allocation size |
va | the list of variable arguments |
kdb::KeySet::KeySet |
( |
Va |
va, |
|
|
size_t |
alloc, |
|
|
va_list |
av |
|
) |
| |
|
inlineexplicit |
Create a new keyset.
- Parameters
-
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
goes ok, 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 out 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:
keyNew (
"user/sw/app/fixedConfiguration/key01", KEY_SWITCH_VALUE,
"value01", 0),
keyNew (
"user/sw/app/fixedConfiguration/key02", KEY_SWITCH_VALUE,
"value02", 0),
keyNew (
"user/sw/app/fixedConfiguration/key03", KEY_SWITCH_VALUE,
"value03", 0),
keyNew (
"user/sw/app/fixedConfiguration/key15", KEY_SWITCH_VALUE,
"value15", 0),
KS_END);
If you start having 3 keys, and your application needs approximately 200-500 keys, you can use:
keyNew (
"user/sw/app/fixedConfiguration/key1", KEY_SWITCH_VALUE,
"value1", 0),
keyNew (
"user/sw/app/fixedConfiguration/key2", KEY_SWITCH_VALUE,
"value2", 0),
keyNew (
"user/sw/app/fixedConfiguration/key3", KEY_SWITCH_VALUE,
"value3", 0),
KS_END);
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.
- Postcondition
- the keyset is rewinded properly
- See Also
- ksDel() to free the KeySet afterwards
-
ksDup() to duplicate an existing KeySet
- Parameters
-
alloc | gives a hint for the size how many Keys may be stored initially |
- Returns
- a ready to use KeySet object
-
0 on memory error
- Precondition
- caller must call va_start and va_end
- va the list of arguments
- Parameters
-
alloc | the allocation size |
va | the list of variable arguments |
ssize_t kdb::KeySet::append |
( |
const Key & |
toAppend | ) |
|
|
inline |
append a key
- Parameters
-
- Returns
- number of keys in the keyset
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:
- was duplicated before inserting
- got its refcount incremented by keyIncRef() before inserting
- was also inserted into another keyset with ksAppendKey()
The reference counter of the key will be incremented to show this ownership, and thus
toAppend
is not const.
- Note
- Because the key is not copied, also the pointer to the current metadata keyNextMeta() will be shared.
- See Also
- keyGetRef().
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:
- Returns
- the size of the KeySet after insertion
-
-1 on NULL pointers
-
-1 if insertion failed, the key will be deleted then.
- Parameters
-
ks | KeySet that will receive the key |
toAppend | Key that will be appended to ks or deleted |
- See Also
- ksAppend(), keyNew(), ksDel()
-
keyIncRef()
void kdb::KeySet::copy |
( |
const KeySet & |
other | ) |
|
|
inline |
Copy a keyset.
- Parameters
-
other | other keyset to copy |
This is only a shallow copy. For a deep copy you need to dup every key.
Copy a keyset. 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.You can also use it to clear a keyset when you pass a NULL pointer as source
.Note that all keys in 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().
- Note
- Because the key is not copied, also the pointer to the current metadata keyNextMeta() will be shared.
- Parameters
-
source | has to be an initialized source KeySet or NULL |
dest | has to be an initialized KeySet where to write the keys |
- Returns
- 1 on success
-
0 if dest was cleared successfully (source is NULL)
-
-1 on NULL pointer
- See Also
- ksNew(), ksDel(), ksDup()
-
keyCopy() for copying keys
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
.
- Example:
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
.
- See Also
- kdbGet() for explanation why you might get more keys than you requested.
- Returns
- a new allocated KeySet which needs to deleted with ksDel(). The keyset consists of all keys (of the original keyset ks) below the cutpoint. If the key cutpoint exists, it will also be appended.
- Return values
-
0 | on null pointers, no key name or allocation problems |
- Parameters
-
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 |