Elektra
0.8.9
|
General methods to access the Key database. More...
Functions | |
KDB * | kdbOpen (Key *errorKey) |
Opens the session with the Key database. | |
int | kdbClose (KDB *handle, Key *errorKey) |
int | kdbGet (KDB *handle, KeySet *ks, Key *parentKey) |
Retrieve keys in an atomic and universal way. | |
int | kdbSet (KDB *handle, KeySet *ks, Key *parentKey) |
Set keys in an atomic and universal way. | |
General methods to access the Key database.
To use them:
The kdb*() methods are used to access the storage, to get and set KeySets .
They use some backend implementation to know the details about how to access the storage.
One backend consists of multiple plugins. See writing a new plugin for information about how to write a plugin.
int kdbClose | ( | KDB * | handle, |
Key * | errorKey | ||
) |
Closes the session with the Key database.
This is the counterpart of kdbOpen().
You must call this method when you finished your affairs with the key database. You can manipulate Key and KeySet objects also after kdbClose(), but you must not use any kdb*() call afterwards. You can run kdbClose() in the atexit() handler.
The handle
parameter will be finalized and all resources associated to it will be freed. After a kdbClose(), the handle
can't be used anymore.
handle | contains internal information of opened key database |
errorKey | the key which holds error information |
int kdbGet | ( | KDB * | handle, |
KeySet * | ks, | ||
Key * | parentKey | ||
) |
Retrieve keys in an atomic and universal way.
returned
KeySet must be a valid KeySet, e.g. constructed with ksNew().The returned
KeySet may already contain some keys from previous kdbGet() calls. The new retrieved keys will be appended using ksAppendKey().
It will fully retrieve, at least, all keys under the parentKey
folder, with all subfolders and their children.
This example demonstrates the typical usecase within an application (without error handling).
If you pass NULL on any parameter kdbGet() will fail immediately without doing anything.
When a backend fails kdbGet() will return -1 with all error and warning information in the parentKey
. The parameter returned
will not be changed.
In the first run of kdbGet all keys are retrieved. On subsequent calls only the keys are retrieved where something was changed inside the key database. The other keys stay unchanged in the keyset, even when they were manipulated.
It is your responsibility to save the original keyset if you need it afterwards.
If you must get the same keyset again, e.g. in another thread you need to open a second handle to the key database using kdbOpen().
handle | contains internal information of opened key database |
parentKey | parent key holds the information which keys should be get (it is possible that more are retrieved) - an invalid name gets all keys |
ks | the (pre-initialized) KeySet returned with all keys found will not be changed on error or if no update is required |
1 | if the keys were retrieved successfully |
0 | if there was no update - no changes are made to the keyset then |
-1 | on failure - no changes are made to the keyset then |
KDB* kdbOpen | ( | Key * | errorKey | ) |
Opens the session with the Key database.
The method will bootstrap itself the following way. The first step is to open the default backend. With it system/elektra/mountpoints will be loaded and all needed libraries and mountpoints will be determined. These libraries for backends will be loaded and with it the KDB
datastructure will be initialized.
You must always call this method before retrieving or committing any keys to the database. In the end of the program, after using the key database, you must not forget to kdbClose(). You can use the atexit () handler for it.
The pointer to the KDB
structure returned will be initialized like described above, and it must be passed along on any kdb*() method your application calls.
Get a KDB
handle for every thread using elektra. Don't share the handle across threads, and also not the pointer accessing it:
You don't need to use the kdbOpen() if you only want to manipulate plain in-memory Key or KeySet objects.
errorKey | the key which holds errors and warnings which were issued |
int kdbSet | ( | KDB * | handle, |
KeySet * | ks, | ||
Key * | parentKey | ||
) |
Set keys in an atomic and universal way.
With parentKey
you can only store a part of the given keyset. When other keys also belong to a backend, they will be used too, even when they are above parentKey
.
If you pass a parentKey without a name the whole keyset will be set in an atomic way.
Each key is checked with keyNeedSync() before being actually committed. So only changed keys are updated. If no key of a backend needs to be synced any affairs to backends omitted and 0 is returned.
If some error occurs, kdbSet() will stop. In this situation the KeySet internal cursor will be set on the key that generated the error.
None of the keys are actually commited.
You should present the error message to the user and let the user decide what to do. Possible solutions are:
showElektraErrorDialog() and doElektraMerge() need to be implemented by the user. For doElektraMerge a 3-way merge algorithm exists in libelektra-tools.
handle | contains internal information of opened key database |
ks | a KeySet which should contain changed keys, otherwise nothing is done |
parentKey | holds the information below which key keys should be set, see above |
1 | on success |
0 | if nothing had to be done |
-1 | on failure |