$darkmode
Elektra 0.11.0
|
General methods to access the Key database. More...
Macros | |
#define | KDB_VERSION "x.y.z" |
The version information in x.y.z format as string. More... | |
#define | KDB_VERSION_MAJOR x |
The version information of the major version as number. More... | |
#define | KDB_VERSION_MINOR y |
The version information of the minor version as number. More... | |
#define | KDB_VERSION_PATCH z |
The version information of the patch version as number. More... | |
Functions | |
KeySet * | ksRenameKeys (KeySet *config, const char *name) |
Takes the first key and cuts off this common part for all other keys, instead name will be prepended. More... | |
KDB * | kdbOpen (const KeySet *contract, Key *errorKey) |
Opens the session with the Key database. More... | |
int | kdbClose (KDB *handle, Key *errorKey) |
Closes the session with the Key database. More... | |
int | kdbGet (KDB *handle, KeySet *ks, Key *parentKey) |
Retrieve Keys from the Key database in an atomic and universal way. More... | |
int | kdbSet (KDB *handle, KeySet *ks, Key *parentKey) |
Set Keys to the Key database in an atomic and universal way. More... | |
General methods to access the Key database.
To use them:
The kdb*() methods are used to access the storage, to get and set KeySets.
Parameters common for all these functions are:
KDB uses different backend implementations that 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. Backends are state-less regarding the configuration (because of that you must pass back the whole configuration for every backend), but have a state for:
As we see in the figure, kdbOpen() can be called arbitrarily often in any number of threads.
For every handle you got from kdbOpen(), for every parentKey with a different name, only the shown state transitions are valid. From a freshly opened KDB, only kdbGet() and kdbClose() are allowed, because otherwise conflicts (error C02000) would not be detected.
Once kdbGet() was called (for a specific handle+parentKey), any number of kdbGet() and kdbSet() can be used with this handle respective parentKey, unless kdbSet() had a conflict (error C02000) with another application. Every affair with KDB needs to be finished with kdbClose().
The name of the parentKey in kdbOpen() and kdbClose() does not matter.
In the usual case we just have one parentKey and one handle. In these cases we just have to remember to use kdbGet() before kdbSet():
To output warnings, you can use following code:
To output the error, you can use following code:
#define KDB_VERSION "x.y.z" |
The version information in x.y.z format as string.
To get the version at run-time, you can get the key system:/elektra/version/constants/KDB_VERSION
#define KDB_VERSION_MAJOR x |
The version information of the major version as number.
To get the version at run-time, you can get the key system:/elektra/version/constants/KDB_VERSION_MAJOR
#define KDB_VERSION_MINOR y |
The version information of the minor version as number.
To get the version at run-time, you can get the key system:/elektra/version/constants/KDB_VERSION_MINOR
#define KDB_VERSION_PATCH z |
The version information of the patch version as number.
To get the version at run-time, you can get the key system:/elektra/version/constants/KDB_VERSION_PATCH
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 are finished working with the Key database. You can manipulate Key and KeySet objects also after kdbClose(), but you must not use any kdb*() call afterwards.
The handle
parameter will be finalized and all resources associated to it will be freed. After a kdbClose(), the handle
cannot be used anymore.
handle | contains internal information of opened key database |
errorKey | the key which holds error/warning information |
0 | on success |
-1 | on NULL pointer |
int kdbGet | ( | KDB * | handle, |
KeySet * | ks, | ||
Key * | parentKey | ||
) |
Retrieve Keys from the Key database in an atomic and universal way.
handle
must be a valid KDB handle as returned from kdbOpen(). returned
must be a valid KeySet, i.e., constructed with ksNew(). returned
must contain keys only from the spec:/
, dir:/
, user:/
, system:/
, default:/
or proc:/
namespaces. parentKey
must be a valid Key, i.e., constructed with keyNew(). parentKey
must not have read-only name, value or metadata. parentKey
must use the spec:/
, dir:/
, user:/
, system:/
, default:/
, proc:/
or cascading namespace.If you pass NULL
or a key with read-only metadata as parentKey
, kdbGet() will fail immediately without doing anything. If you pass another invalid parentKey
, or NULL
as ks
or handle
, kdbGet() will set an error on parentKey
and then return immediately.
parentKey
with writable metadata, kdbGet() will always remove any existing errors and warnings from parentKey
.handle
you must make sure to pass all keys from ks
, which you do not want to remove.Not all namespace can be loaded.
spec:/
, dir:/
, user:/
and system:/
can be loaded via kdbGet().proc:/
keys can be loaded via kdbGet(), but are not persisted or cached.default:/
keys can be inserted by kdbGet() but they will always stem from a specification in spec:/
keys.ks
contains a key with any other namespace, an error will be returned.The parentKey
defines which parts of ks
will be loaded. Everything that is at or below parentKey
wil be loaded together with any key that shares a backend with such a key. Backends are always loaded as an atomic unit.
parentKey
is in the cascading namespace, keys of all loadable namespaces (see above) will be loaded. This is generally the recommended approach.Upon sucessfully returning kdbGet() also sets the value of parentKey
to the storage identifier used by the backend that contains (or would contain) parentKey
. For file-based backends this is the absolute path of the underlying file. Other backends may use different identifiers, but it always uniquely identifies the underlying storage unit.
parentKey
is in the cascading, default:/
or `proc:/ namespace, the value of parentKey
will be set to an empty string. This is done, because those namespaces are not persistable (see kdbSet()) and therfore have no storage identifier.Below or at parentKey
, the KeySet ks
will mostly contain keys loaded from backends. The only exception are proc:/
and spec:/
keys that were already present, before kdbGet() was called and do not overlap with an existing backend (for those namespaces). This can be used to provide a hard-coded fallback specifications and/or process-specific data.
Keys not below (or at) parentKey
that were present when kdbGet() was called, may still be removed. For example, this could be because they overlap with a backend that also has keys below parentKey
(backends are atomic units).
When a backend fails kdbGet() will return -1 with all error and warning information in the parentKey
. The parameter returned
will not be changed.
It is your responsibility to save the original KeySet if you need it afterwards.
If you want to be sure to get a fresh KeySet again, you need to open a second handle to the Key database using kdbOpen().
handle | contains internal information of opened key database |
parentKey | Keys below parentKey will be retrieved from handle . It is also used to add warnings and set error information. |
ks | the (pre-initialized) KeySet returned with all keys found will not be changed on error or if no update is required |
2 | if only proc:/ backends were executed. This means no data was loaded from storage. There might be warnings attached to the parentKey! Depending on your use case, you might need to treat them as errors! |
1 | if the Keys were retrieved successfully. There might be warnings attached to the parentKey! Depending on your use case, you might need to treat them as errors! |
0 | if there was no update at all - no changes are made to the KeySet then. There might be warnings attached to the parentKey! Depending on your use case, you might need to treat them as erorrs! |
-1 | on failure - no changes are made to the KeySet then |
KDB* kdbOpen | ( | const KeySet * | contract, |
Key * | errorKey | ||
) |
Opens the session with the Key database.
You must always call this method before retrieving or committing any keys to the database. At the end of a program, after using the Key database (KDB), you must not forget to call kdbClose() to free resources.
The method will bootstrap itself in 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. Then the global plugins and global keyset data from the contract
is processed. Finally, the libraries for backends will be loaded and with it the KDB
data structure will be initialized.
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 kdbOpen() if you only want to manipulate plain in-memory Key or KeySet objects.
contract | the contract that should be ensured before opening the KDB all data is copied and the KeySet can safely be used for e.g. kdbGet() later |
errorKey | the key which holds errors and warnings which were issued |
NULL | on failure |
int kdbSet | ( | KDB * | handle, |
KeySet * | ks, | ||
Key * | parentKey | ||
) |
Set Keys to the Key database in an atomic and universal way.
ks
must be a valid KeySet, i.e., constructed with ksNew(). ks
must only contain only keys in the spec:/
, dir:/
, user:/
, system:/
, default:/
or proc:/
namespaces. parentKey
must be a valid Key, e.g. constructed with keyNew(). parentKey
must not have read-only name, value or metadata. parentKey
must use the spec:/
, dir:/
, user:/
, system:/
, default:/
, proc:/
or cascading namespace.If you pass NULL
or a key with read-only metadata as parentKey
, kdbSet() will fail immediately without doing anything. If you pass another invalid parentKey
, or NULL
as ks
or handle
, kdbSet() will set an error on parentKey
and then return immediately.
parentKey
with writable metadata, kdbSet() will always remove any existing errors and warnings from parentKey
.Not all namespace can be persisted.
spec:/
, dir:/
, user:/
and system:/
will be persisted by kdbSet().default:/
and proc:/
keys are ignored by kdbSet().ks
contains a key with any other namespace, an error will be returned.In general it is recommended to use a parentKey
in the cascading namespace to cover all namespaces at once.
The parentKey
defines which parts of ks
will be stored. Everything that is at or below parentKey
will be persisted together with any key that shares a backend with such a key. Backends are always stored as an atomic unit.
parentKey
is in the cascading namespace, keys of all persistable namespaces (see above) will be stored. This is generally the recommended approach.The contents of ks
will mostly not be modified by kdbSet(). The only modifications made are those caused by applying the specification in spec:/
to dir:/
, user:/
and system:/
.
parentKey == NULL
or parentKey
has read-only metadata, kdbSet() will immediately return the error code -1. In all other error cases the following happens:In case of errors you should present the error message to the user and let the user decide what to do. Possible solutions are:
If none of the backends needs an update, kdbSet() returns 0 and does nothing.
showElektraErrorDialog() and doElektraMerge() need to be implemented by the user of Elektra. 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 | Keys below parentKey will be set to handle . It is also used to add warnings and set error information. |
1 | on success |
0 | if nothing had to be done, no changes in KDB |
-1 | on failure, no changes in KDB, an error will be set on parentKey if possible (see "Errors" above) |
handle
KeySet* ksRenameKeys | ( | KeySet * | config, |
const char * | name | ||
) |
Takes the first key and cuts off this common part for all other keys, instead name will be prepended.
The first key is removed in the resulting keyset.