\hypertarget{group__kdb}{}\section{K\+DB}
\label{group__kdb}\index{KDB@{KDB}}


General methods to access the Key database.  


\subsection*{Functions}
\begin{DoxyCompactItemize}
\item 
Key\+Set $\ast$ \mbox{\hyperlink{group__kdb_ga1ba80a565f54490f362df95f401bcb29}{ks\+Rename\+Keys}} (Key\+Set $\ast$config, const char $\ast$name)
\begin{DoxyCompactList}\small\item\em Takes the first key and cuts off this common part for all other keys, instead name will be prepended. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{group__kdb_ga5bfaad0230457cd6386032fe65c41576}\label{group__kdb_ga5bfaad0230457cd6386032fe65c41576}} 
int \mbox{\hyperlink{group__kdb_ga5bfaad0230457cd6386032fe65c41576}{elektra\+Open\+Bootstrap}} (K\+DB $\ast$handle, Key\+Set $\ast$keys, Key $\ast$error\+Key)
\begin{DoxyCompactList}\small\item\em Bootstrap, first phase with fallback. \end{DoxyCompactList}\item 
K\+DB $\ast$ \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open}} (Key $\ast$error\+Key)
\begin{DoxyCompactList}\small\item\em Opens the session with the Key database. \end{DoxyCompactList}\item 
int \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdb\+Close}} (K\+DB $\ast$handle, Key $\ast$error\+Key)
\begin{DoxyCompactList}\small\item\em Closes the session with the Key database. \end{DoxyCompactList}\item 
int \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get}} (K\+DB $\ast$handle, Key\+Set $\ast$ks, Key $\ast$parent\+Key)
\begin{DoxyCompactList}\small\item\em Retrieve keys in an atomic and universal way. \end{DoxyCompactList}\item 
int \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set}} (K\+DB $\ast$handle, Key\+Set $\ast$ks, Key $\ast$parent\+Key)
\begin{DoxyCompactList}\small\item\em Set keys in an atomic and universal way. \end{DoxyCompactList}\item 
int \mbox{\hyperlink{group__kdb_ga0955373877575fa21275891518f8ab31}{kdb\+Ensure}} (K\+DB $\ast$handle, Key\+Set $\ast$contract, Key $\ast$parent\+Key)
\begin{DoxyCompactList}\small\item\em This function can be used to ensure the given K\+DB {\ttfamily handle} meets certain clauses, specified in {\ttfamily contract}. \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Detailed Description}
General methods to access the Key database. 

To use them\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <kdb.h>}}
\end{DoxyCode}


The kdb$\ast$() methods are used to access the storage, to get and set \mbox{\hyperlink{group__keyset}{Key\+Sets}}.

Parameters common for all these functions are\+:


\begin{DoxyItemize}
\item {\itshape handle}, as returned by \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}}, need to be passed to every call
\item {\itshape parent\+Key} is used for every call to add warnings and set an error. For \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} / \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}} it is used to give an hint which keys should be retrieved/stored.
\end{DoxyItemize}

\begin{DoxyNote}{Note}
The parent\+Key is an obligation for you, but only an hint for K\+DB. K\+DB does not remember anything about the configuration. You need to pass the same configuration back to \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}}, otherwise parts of the configuration get lost. Only keys below the parent\+Key are subject for change, the rest must be left untouched.
\end{DoxyNote}
K\+DB uses different backend implementations that know the details about how to access the storage. One backend consists of multiple plugins. See \mbox{\hyperlink{group__plugin}{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\+:


\begin{DoxyItemize}
\item a two phase-\/commit
\item a conflict detection (error C02000) and
\item optimizations that avoid redoing already done operations.
\end{DoxyItemize}


\begin{DoxyImage}
\includegraphics[width=\textwidth,height=\textheight/2,keepaspectratio=true]{state.png}
\doxyfigcaption{State}
\end{DoxyImage}
 As we see in the figure, \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}} can be called arbitrarily often in any number of threads.

For every handle you got from \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}}, for every parent\+Key with a different name, {\itshape only} the shown state transitions are valid. From a freshly opened K\+DB, only \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} and \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdb\+Close()}} are allowed, because otherwise conflicts (error C02000) would not be detected.

Once \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} was called (for a specific handle+parent\+Key), any number of \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} and \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}} can be used with this handle respective parent\+Key, unless \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}} had a conflict (error C02000) with another application. Every affair with K\+DB needs to be finished with \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdb\+Close()}}.

The name of the parent\+Key in \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}} and \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdb\+Close()}} does not matter.

In the usual case we just have one parent\+Key and one handle. In these cases we just have to remember to use \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} before \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}}\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <kdb.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int} \mbox{\hyperlink{testio__doc_8c_a3c04138a5bfe5d72780bb7e82a18e627}{main}} (\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{        KeySet * myConfig = \mbox{\hyperlink{group__keyset_ga671e1aaee3ae9dc13b4834a4ddbd2c3c}{ksNew}} (0, \mbox{\hyperlink{kdbenum_8c_a7a28fce3773b2c873c94ac80b8b4cd54}{KS\_END}});}
\DoxyCodeLine{        Key * parentKey = \mbox{\hyperlink{group__key_gad23c65b44bf48d773759e1f9a4d43b89}{keyNew}} (\textcolor{stringliteral}{"/sw/MyApp"}, \mbox{\hyperlink{group__key_gga91fb3178848bd682000958089abbaf40afc1567f74444ff9c219f7456b652b4ec}{KEY\_CASCADING\_NAME}}, \mbox{\hyperlink{group__key_gga91fb3178848bd682000958089abbaf40aa8adb6fcb92dec58fb19410eacfdd403}{KEY\_END}});}
\DoxyCodeLine{        KDB * handle = \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdbOpen}} (parentKey);}
\DoxyCodeLine{}
\DoxyCodeLine{        \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdbGet}} (handle, myConfig, parentKey); \textcolor{comment}{// kdbGet() must be first}}
\DoxyCodeLine{        \textcolor{comment}{// now any number of any kdbGet()/kdbSet() calls are allowed, e.g.:}}
\DoxyCodeLine{        \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdbSet}} (handle, myConfig, parentKey);}
\DoxyCodeLine{}
\DoxyCodeLine{        \mbox{\hyperlink{group__keyset_ga27e5c16473b02a422238c8d970db7ac8}{ksDel}} (myConfig); \textcolor{comment}{// delete the in-memory configuration}}
\DoxyCodeLine{}
\DoxyCodeLine{        \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdbClose}} (handle, parentKey); \textcolor{comment}{// no more affairs with the key database.}}
\DoxyCodeLine{        \mbox{\hyperlink{group__key_ga3df95bbc2494e3e6703ece5639be5bb1}{keyDel}} (parentKey);          \textcolor{comment}{// working with key/ks does not need kdb}}
\DoxyCodeLine{\}}
\end{DoxyCodeInclude}


To output warnings, you can use following code\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{        \textcolor{keyword}{const} Key * metaWarnings = \mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, \textcolor{stringliteral}{"warnings"});}
\DoxyCodeLine{        \textcolor{keywordflow}{if} (!metaWarnings) \textcolor{keywordflow}{return} 1; \textcolor{comment}{/* There are no current warnings */}}
\DoxyCodeLine{}
\DoxyCodeLine{        \textcolor{keywordtype}{int} nrWarnings = atoi (\mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (metaWarnings));}
\DoxyCodeLine{}
\DoxyCodeLine{        printf (\textcolor{stringliteral}{"There are \%d warnings\(\backslash\)n"}, nrWarnings + 1);}
\DoxyCodeLine{        \textcolor{keywordflow}{for} (\textcolor{keywordtype}{int} i = 0; i <= nrWarnings; ++i)}
\DoxyCodeLine{        \{}
\DoxyCodeLine{                \textcolor{keywordtype}{char} buffer[] = \textcolor{stringliteral}{"warnings/\#00\(\backslash\)0description"};}
\DoxyCodeLine{                buffer[10] = i / 10 \% 10 + \textcolor{charliteral}{'0'};}
\DoxyCodeLine{                buffer[11] = i \% 10 + \textcolor{charliteral}{'0'};}
\DoxyCodeLine{                printf (\textcolor{stringliteral}{"buffer is: \%s\(\backslash\)n"}, buffer);}
\DoxyCodeLine{                strncat (buffer, \textcolor{stringliteral}{"/number"}, \textcolor{keyword}{sizeof} (buffer) - strlen (buffer) - 1);}
\DoxyCodeLine{                printf (\textcolor{stringliteral}{"number: \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer)));}
\DoxyCodeLine{                buffer[12] = \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{                strncat (buffer, \textcolor{stringliteral}{"/description"}, \textcolor{keyword}{sizeof} (buffer) - strlen (buffer) - 1);}
\DoxyCodeLine{                printf (\textcolor{stringliteral}{"description: \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer)));}
\DoxyCodeLine{                buffer[12] = \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{                strncat (buffer, \textcolor{stringliteral}{"/module"}, \textcolor{keyword}{sizeof} (buffer) - strlen (buffer) - 1);}
\DoxyCodeLine{                \mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer);}
\DoxyCodeLine{                printf (\textcolor{stringliteral}{"module: \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer)));}
\DoxyCodeLine{                buffer[12] = \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{                strncat (buffer, \textcolor{stringliteral}{"/file"}, \textcolor{keyword}{sizeof} (buffer) - strlen (buffer) - 1);}
\DoxyCodeLine{                \mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer);}
\DoxyCodeLine{                printf (\textcolor{stringliteral}{"file: \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer)));}
\DoxyCodeLine{                buffer[12] = \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{                strncat (buffer, \textcolor{stringliteral}{"/line"}, \textcolor{keyword}{sizeof} (buffer) - strlen (buffer) - 1);}
\DoxyCodeLine{                \mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer);}
\DoxyCodeLine{                printf (\textcolor{stringliteral}{"line: \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer)));}
\DoxyCodeLine{                buffer[12] = \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{                strncat (buffer, \textcolor{stringliteral}{"/reason"}, \textcolor{keyword}{sizeof} (buffer) - strlen (buffer) - 1);}
\DoxyCodeLine{                \mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer);}
\DoxyCodeLine{                printf (\textcolor{stringliteral}{"reason: \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer)));}
\DoxyCodeLine{                buffer[12] = \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{                strncat (buffer, \textcolor{stringliteral}{"/mountpoint"}, \textcolor{keyword}{sizeof} (buffer) - strlen (buffer) - 1);}
\DoxyCodeLine{                \mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer);}
\DoxyCodeLine{                printf (\textcolor{stringliteral}{"reason: \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer)));}
\DoxyCodeLine{                buffer[12] = \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{                strncat (buffer, \textcolor{stringliteral}{"/configfile"}, \textcolor{keyword}{sizeof} (buffer) - strlen (buffer) - 1);}
\DoxyCodeLine{                \mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer);}
\DoxyCodeLine{                printf (\textcolor{stringliteral}{"reason: \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (warningKey, buffer)));}
\DoxyCodeLine{        \}}
\end{DoxyCodeInclude}
 To output the error, you can use following code\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{        \textcolor{keyword}{const} Key * metaError = \mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (errorKey, \textcolor{stringliteral}{"error"});}
\DoxyCodeLine{        \textcolor{keywordflow}{if} (!metaError) \textcolor{keywordflow}{return} 1; \textcolor{comment}{/* There is no current error */}}
\DoxyCodeLine{}
\DoxyCodeLine{        printf (\textcolor{stringliteral}{"number: \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (errorKey, \textcolor{stringliteral}{"error/number"})));}
\DoxyCodeLine{        printf (\textcolor{stringliteral}{"description: : \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (errorKey, \textcolor{stringliteral}{"error/description"})));}
\DoxyCodeLine{        printf (\textcolor{stringliteral}{"module: : \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (errorKey, \textcolor{stringliteral}{"error/module"})));}
\DoxyCodeLine{        printf (\textcolor{stringliteral}{"at: \%s:\%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (errorKey, \textcolor{stringliteral}{"error/file"})), \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (errorKey, \textcolor{stringliteral}{"error/line"})));}
\DoxyCodeLine{        printf (\textcolor{stringliteral}{"reason: : \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (errorKey, \textcolor{stringliteral}{"error/reason"})));}
\DoxyCodeLine{        printf (\textcolor{stringliteral}{"mountpoint: : \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (errorKey, \textcolor{stringliteral}{"error/mountpoint"})));}
\DoxyCodeLine{        printf (\textcolor{stringliteral}{"configfile: : \%s\(\backslash\)n"}, \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (errorKey, \textcolor{stringliteral}{"error/configfile"})));}
\end{DoxyCodeInclude}


\subsection{Function Documentation}
\mbox{\Hypertarget{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}\label{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}} 
\index{KDB@{KDB}!kdbClose@{kdbClose}}
\index{kdbClose@{kdbClose}!KDB@{KDB}}
\subsubsection{\texorpdfstring{kdbClose()}{kdbClose()}}
{\footnotesize\ttfamily int kdb\+Close (\begin{DoxyParamCaption}\item[{K\+DB $\ast$}]{handle,  }\item[{Key $\ast$}]{error\+Key }\end{DoxyParamCaption})}



Closes the session with the Key database. 

\begin{DoxyPrecond}{Precondition}
The handle must be a valid handle as returned from \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}}

error\+Key must be a valid key, e.\+g. created with \mbox{\hyperlink{group__key_gad23c65b44bf48d773759e1f9a4d43b89}{key\+New()}}
\end{DoxyPrecond}
This is the counterpart of \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}}.

You must call this method when you finished your affairs with the key database. You can manipulate Key and Key\+Set objects also after \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdb\+Close()}}, but you must not use any kdb$\ast$() call afterwards.

The {\ttfamily handle} parameter will be finalized and all resources associated to it will be freed. After a \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdb\+Close()}}, the {\ttfamily handle} cannot be used anymore.


\begin{DoxyParams}{Parameters}
{\em handle} & contains internal information of \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{opened }} key database \\
\hline
{\em error\+Key} & the key which holds error/warning information \\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{Return values}
{\em 0} & on success \\
\hline
{\em -\/1} & on N\+U\+LL pointer \\
\hline
\end{DoxyRetVals}
\mbox{\Hypertarget{group__kdb_ga0955373877575fa21275891518f8ab31}\label{group__kdb_ga0955373877575fa21275891518f8ab31}} 
\index{KDB@{KDB}!kdbEnsure@{kdbEnsure}}
\index{kdbEnsure@{kdbEnsure}!KDB@{KDB}}
\subsubsection{\texorpdfstring{kdbEnsure()}{kdbEnsure()}}
{\footnotesize\ttfamily int kdb\+Ensure (\begin{DoxyParamCaption}\item[{K\+DB $\ast$}]{handle,  }\item[{Key\+Set $\ast$}]{contract,  }\item[{Key $\ast$}]{parent\+Key }\end{DoxyParamCaption})}



This function can be used to ensure the given K\+DB {\ttfamily handle} meets certain clauses, specified in {\ttfamily contract}. 

Currently the following clauses are supported\+:


\begin{DoxyItemize}
\item {\ttfamily system/elektra/ensure/plugins/$<$mountpoint$>$/$<$pluginname$>$} defines the state of the plugin {\ttfamily $<$pluginname$>$} for the mountpoint {\ttfamily $<$mountpoint$>$}\+:
\begin{DoxyItemize}
\item The value {\ttfamily unmounted} ensures the plugin is not mounted, at this mountpoint.
\item The value {\ttfamily mounted} ensures the plugin is mounted, at this mountpoint. If the plugin is not mounted, we will try to mount it.
\item The value {\ttfamily remount} always mounts the plugin, at this mountpoint. If it was already mounted, it will me unmounted and mounted again. This can be used to ensure the plugin is mounted with a certain configuration.
\end{DoxyItemize}
\item Keys below {\ttfamily system/elektra/ensure/plugins/$<$mountpoint$>$/$<$pluginname$>$/config} are extracted and used as the plugins config Key\+Set during mounting. {\ttfamily system/elektra/ensure/plugins/$<$mountpoint$>$/$<$pluginname$>$} will be replaced by {\ttfamily user} in the keynames. If no keys are given, an empty Key\+Set is used.
\end{DoxyItemize}

There are a few special values for {\ttfamily $<$mountpoint$>$}\+:
\begin{DoxyItemize}
\item {\ttfamily global} is used to indicate the plugin should (un)mounted as a global plugin. Currently this only supports (un)mounting plugins from/to the subposition {\ttfamily maxonce}.
\item {\ttfamily parent} is used to indicate the keyname of {\ttfamily parent\+Key} shall be used as the mountpoint.
\end{DoxyItemize}

If {\ttfamily $<$mountpoint$>$} is none of those values, it has to be valid keyname with the slashes escaped. That means it has to start with {\ttfamily /}, {\ttfamily user}, {\ttfamily system}, {\ttfamily dir} or {\ttfamily spec}.

If {\ttfamily $<$mountpoint$>$} is N\+OT {\ttfamily global}, currently only {\ttfamily unmounted} is supported (not {\ttfamily mounted} and {\ttfamily remounted}).

N\+O\+TE\+: This function only works properly, if the list plugin is mounted in all global positions. If this is not the case, 1 will be returned, because this is seen as an implicit clause in the contract. Additionally any contract that specifies clauses for the list plugin is rejected as malformed.


\begin{DoxyParams}{Parameters}
{\em handle} & contains internal information of \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{opened }} key database \\
\hline
{\em contract} & Key\+Set containing the contract described above. This will always be {\ttfamily \mbox{\hyperlink{group__keyset_ga27e5c16473b02a422238c8d970db7ac8}{ks\+Del()}}}ed. {\bfseries{Even in error cases.}} \\
\hline
{\em parent\+Key} & The parent\+Key used if the {\ttfamily parent} special value is used, otherwise only used for error reporting.\\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{Return values}
{\em 0} & on success \\
\hline
{\em 1} & if clauses of the contract are unmet \\
\hline
{\em -\/1} & on N\+U\+LL pointers, or malformed contract \\
\hline
\end{DoxyRetVals}
\mbox{\Hypertarget{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}\label{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}} 
\index{KDB@{KDB}!kdbGet@{kdbGet}}
\index{kdbGet@{kdbGet}!KDB@{KDB}}
\subsubsection{\texorpdfstring{kdbGet()}{kdbGet()}}
{\footnotesize\ttfamily int kdb\+Get (\begin{DoxyParamCaption}\item[{K\+DB $\ast$}]{handle,  }\item[{Key\+Set $\ast$}]{ks,  }\item[{Key $\ast$}]{parent\+Key }\end{DoxyParamCaption})}



Retrieve keys in an atomic and universal way. 

\begin{DoxyPrecond}{Precondition}
The {\ttfamily handle} must be passed as returned from \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}}.

The {\ttfamily returned} Key\+Set must be a valid Key\+Set, e.\+g. constructed with \mbox{\hyperlink{group__keyset_ga671e1aaee3ae9dc13b4834a4ddbd2c3c}{ks\+New()}}.

The {\ttfamily parent\+Key} Key must be a valid Key, e.\+g. constructed with \mbox{\hyperlink{group__key_gad23c65b44bf48d773759e1f9a4d43b89}{key\+New()}}.
\end{DoxyPrecond}
If you pass N\+U\+LL on any parameter \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} will fail immediately without doing anything.

The {\ttfamily returned} Key\+Set may already contain some keys, e.\+g. from previous \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} calls. The new retrieved keys will be appended using \mbox{\hyperlink{group__keyset_gaa5a1d467a4d71041edce68ea7748ce45}{ks\+Append\+Key()}}.

If not done earlier \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} will fully retrieve all keys under the {\ttfamily parent\+Key} folder recursively (See Optimization below when it will not be done).

\begin{DoxyNote}{Note}
\mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} might retrieve more keys than requested (that are not below parent\+Key). These keys must be passed to calls of \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}}, otherwise they will be lost. This stems from the fact that the user has the only copy of the whole configuration and backends only write configuration that was passed to them. For example, if you \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} \char`\"{}system/mountpoint/interest\char`\"{} you will not only get all keys below system/mountpoint/interest, but also all keys below system/mountpoint (if system/mountpoint is a mountpoint as the name suggests, but system/mountpoint/interest is not a mountpoint). Make sure to not touch or remove keys outside the keys of interest, because others may need them!
\end{DoxyNote}
\begin{DoxyParagraph}{Example\+:}
This example demonstrates the typical usecase within an application (without error handling).
\end{DoxyParagraph}

\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <kdb.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <stdio.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int} \mbox{\hyperlink{testio__doc_8c_a3c04138a5bfe5d72780bb7e82a18e627}{main}} (\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{        KeySet * myConfig = \mbox{\hyperlink{group__keyset_ga671e1aaee3ae9dc13b4834a4ddbd2c3c}{ksNew}} (0, \mbox{\hyperlink{kdbenum_8c_a7a28fce3773b2c873c94ac80b8b4cd54}{KS\_END}});}
\DoxyCodeLine{}
\DoxyCodeLine{        \textcolor{comment}{// for error handling see kdbget\_error.c}}
\DoxyCodeLine{}
\DoxyCodeLine{        \textcolor{comment}{// clang-format off}}
\DoxyCodeLine{\textcolor{comment}{}Key * key = \mbox{\hyperlink{group__key_gad23c65b44bf48d773759e1f9a4d43b89}{keyNew}} (\textcolor{stringliteral}{"/sw/tests/myapp/\#0/current/"},  \mbox{\hyperlink{group__key_gga91fb3178848bd682000958089abbaf40aa8adb6fcb92dec58fb19410eacfdd403}{KEY\_END}});}
\DoxyCodeLine{KDB * handle = \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdbOpen}} (key);}
\DoxyCodeLine{\mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdbGet}} (handle, myConfig, key);}
\DoxyCodeLine{Key * result = \mbox{\hyperlink{group__keyset_gad2e30fb6d4739d917c5abb2ac2f9c1a1}{ksLookupByName}} (myConfig, \textcolor{stringliteral}{"/sw/tests/myapp/\#0/current/testkey1"}, 0);}
\DoxyCodeLine{        \textcolor{comment}{// clang-format on}}
\DoxyCodeLine{}
\DoxyCodeLine{        \mbox{\hyperlink{group__key_ga3df95bbc2494e3e6703ece5639be5bb1}{keyDel}} (key);}
\DoxyCodeLine{}
\DoxyCodeLine{        \textcolor{keyword}{const} \textcolor{keywordtype}{char} * key\_name = \mbox{\hyperlink{group__keyname_ga8e805c726a60da921d3736cda7813513}{keyName}} (result);}
\DoxyCodeLine{        \textcolor{keyword}{const} \textcolor{keywordtype}{char} * key\_value = \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (result);}
\DoxyCodeLine{        \textcolor{keyword}{const} \textcolor{keywordtype}{char} * key\_comment = \mbox{\hyperlink{group__keyvalue_ga880936f2481d28e6e2acbe7486a21d05}{keyString}} (\mbox{\hyperlink{group__keymeta_ga9ed3875495ddb3d8a8d29158a60a147c}{keyGetMeta}} (result, \textcolor{stringliteral}{"comment"}));}
\DoxyCodeLine{        printf (\textcolor{stringliteral}{"key: \%s value: \%s comment: \%s\(\backslash\)n"}, key\_name, key\_value, key\_comment);}
\DoxyCodeLine{}
\DoxyCodeLine{        \mbox{\hyperlink{group__keyset_ga27e5c16473b02a422238c8d970db7ac8}{ksDel}} (myConfig); \textcolor{comment}{// delete the in-memory configuration}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{        \textcolor{comment}{// maybe you want kdbSet() myConfig here}}
\DoxyCodeLine{}
\DoxyCodeLine{        \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdbClose}} (handle, 0); \textcolor{comment}{// no more affairs with the key database.}}
\DoxyCodeLine{\}}
\end{DoxyCodeInclude}


When a backend fails \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} will return -\/1 with all error and warning information in the {\ttfamily parent\+Key}. The parameter {\ttfamily returned} will not be changed.

\begin{DoxyParagraph}{Optimization\+:}
In the first run of kdb\+Get all requested (or more) keys are retrieved. On subsequent calls only the keys are retrieved where something was changed inside the key database. The other keys stay in the Key\+Set returned as passed.
\end{DoxyParagraph}
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 \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}}.


\begin{DoxyParams}{Parameters}
{\em handle} & contains internal information of \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{opened }} key database \\
\hline
{\em parent\+Key} & is used to add warnings and set an error information. Additionally, its name is a hint which keys should be retrieved (it is possible that more are retrieved, see Note above).
\begin{DoxyItemize}
\item cascading keys (starting with /) will retrieve the same path in all namespaces
\item / will retrieve all keys 
\end{DoxyItemize}\\
\hline
{\em ks} & the (pre-\/initialized) Key\+Set returned with all keys found will not be changed on error or if no update is required \\
\hline
\end{DoxyParams}
\begin{DoxySeeAlso}{See also}
\mbox{\hyperlink{group__keyset_gaa34fc43a081e6b01e4120daa6c112004}{ks\+Lookup()}}, \mbox{\hyperlink{group__keyset_gad2e30fb6d4739d917c5abb2ac2f9c1a1}{ks\+Lookup\+By\+Name()}} for powerful lookups after the Key\+Set was retrieved 

\mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}} which needs to be called before 

\mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}} to save the configuration afterwards and \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdb\+Close()}} to finish affairs with the \mbox{\hyperlink{group__key}{Key}} database. 
\end{DoxySeeAlso}

\begin{DoxyRetVals}{Return values}
{\em 1} & if the keys were retrieved successfully \\
\hline
{\em 0} & if there was no update -\/ no changes are made to the keyset then \\
\hline
{\em -\/1} & on failure -\/ no changes are made to the keyset then \\
\hline
\end{DoxyRetVals}
\mbox{\Hypertarget{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}\label{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}} 
\index{KDB@{KDB}!kdbOpen@{kdbOpen}}
\index{kdbOpen@{kdbOpen}!KDB@{KDB}}
\subsubsection{\texorpdfstring{kdbOpen()}{kdbOpen()}}
{\footnotesize\ttfamily K\+DB$\ast$ kdb\+Open (\begin{DoxyParamCaption}\item[{Key $\ast$}]{error\+Key }\end{DoxyParamCaption})}



Opens the session with the Key database. 

\begin{DoxyPrecond}{Precondition}
error\+Key must be a valid key, e.\+g. created with \mbox{\hyperlink{group__key_gad23c65b44bf48d773759e1f9a4d43b89}{key\+New()}}
\end{DoxyPrecond}
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 {\ttfamily K\+DB} data structure 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 \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdb\+Close()}}.

The pointer to the {\ttfamily K\+DB} structure returned will be initialized like described above, and it must be passed along on any kdb$\ast$() method your application calls.

Get a {\ttfamily K\+DB} handle for every thread using elektra. Don\textquotesingle{}t share the handle across threads, and also not the pointer accessing it\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keywordtype}{void} thread1 (\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{        Key * parent = \mbox{\hyperlink{group__key_gad23c65b44bf48d773759e1f9a4d43b89}{keyNew}} (\textcolor{stringliteral}{"/app/part1"}, \mbox{\hyperlink{group__key_gga91fb3178848bd682000958089abbaf40afc1567f74444ff9c219f7456b652b4ec}{KEY\_CASCADING\_NAME}}, \mbox{\hyperlink{group__key_gga91fb3178848bd682000958089abbaf40aa8adb6fcb92dec58fb19410eacfdd403}{KEY\_END}});}
\DoxyCodeLine{        KDB * h = \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdbOpen}} (parent);}
\DoxyCodeLine{        \textcolor{comment}{// fetch keys and work with them}}
\DoxyCodeLine{        \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdbClose}} (h, parent);}
\DoxyCodeLine{\}}
\DoxyCodeLine{\textcolor{keywordtype}{void} thread2 (\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{        Key * parent = \mbox{\hyperlink{group__key_gad23c65b44bf48d773759e1f9a4d43b89}{keyNew}} (\textcolor{stringliteral}{"/app/part2"}, \mbox{\hyperlink{group__key_gga91fb3178848bd682000958089abbaf40afc1567f74444ff9c219f7456b652b4ec}{KEY\_CASCADING\_NAME}}, \mbox{\hyperlink{group__key_gga91fb3178848bd682000958089abbaf40aa8adb6fcb92dec58fb19410eacfdd403}{KEY\_END}});}
\DoxyCodeLine{        KDB * h = \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdbOpen}} (parent);}
\DoxyCodeLine{        \textcolor{comment}{// fetch keys and work with them}}
\DoxyCodeLine{        \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdbClose}} (h, parent);}
\DoxyCodeLine{\}}
\end{DoxyCodeInclude}
 You don\textquotesingle{}t need \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}} if you only want to manipulate plain in-\/memory Key or Key\+Set objects.

\begin{DoxyPrecond}{Precondition}
error\+Key must be a valid key, e.\+g. created with \mbox{\hyperlink{group__key_gad23c65b44bf48d773759e1f9a4d43b89}{key\+New()}}
\end{DoxyPrecond}

\begin{DoxyParams}{Parameters}
{\em error\+Key} & the key which holds errors and warnings which were issued \\
\hline
\end{DoxyParams}
\begin{DoxySeeAlso}{See also}
\mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}}, \mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdb\+Close()}} to end all affairs to the \mbox{\hyperlink{group__key}{Key}} database. 
\end{DoxySeeAlso}

\begin{DoxyRetVals}{Return values}
{\em handle} & on success \\
\hline
{\em N\+U\+LL} & on failure \\
\hline
\end{DoxyRetVals}
\mbox{\Hypertarget{group__kdb_ga11436b058408f83d303ca5e996832bcf}\label{group__kdb_ga11436b058408f83d303ca5e996832bcf}} 
\index{KDB@{KDB}!kdbSet@{kdbSet}}
\index{kdbSet@{kdbSet}!KDB@{KDB}}
\subsubsection{\texorpdfstring{kdbSet()}{kdbSet()}}
{\footnotesize\ttfamily int kdb\+Set (\begin{DoxyParamCaption}\item[{K\+DB $\ast$}]{handle,  }\item[{Key\+Set $\ast$}]{ks,  }\item[{Key $\ast$}]{parent\+Key }\end{DoxyParamCaption})}



Set keys in an atomic and universal way. 

\begin{DoxyPrecond}{Precondition}
\mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} must be called before \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}}\+:
\begin{DoxyItemize}
\item initially (after \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}})
\item after conflict errors in \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}}.
\end{DoxyItemize}

The {\ttfamily returned} Key\+Set must be a valid Key\+Set, e.\+g. constructed with \mbox{\hyperlink{group__keyset_ga671e1aaee3ae9dc13b4834a4ddbd2c3c}{ks\+New()}}.

The {\ttfamily parent\+Key} Key must be a valid Key, e.\+g. constructed with \mbox{\hyperlink{group__key_gad23c65b44bf48d773759e1f9a4d43b89}{key\+New()}}.
\end{DoxyPrecond}
If you pass N\+U\+LL on any parameter \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}} will fail immediately without doing anything.

With {\ttfamily parent\+Key} you can give an hint which part of the given keyset is of interest for you. Then you promise to only modify or remove keys below this key. All others would be passed back as they were retrieved by \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}}.

\begin{DoxyParagraph}{Errors}
If some error occurs\+:
\begin{DoxyItemize}
\item \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}} will leave the Key\+Set\textquotesingle{}s $\ast$ internal cursor on the key that generated the error.
\item Error information will be written into the metadata of the parent key.
\item None of the keys are actually committed in this situation, i.\+e. no configuration file will be modified.
\end{DoxyItemize}
\end{DoxyParagraph}
In case of errors you should present the error message to the user and let the user decide what to do. Possible solutions are\+:
\begin{DoxyItemize}
\item remove the problematic key and use \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}} again (for validation or type errors)
\item change the value of the problematic key and use \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdb\+Set()}} again (for validation errors)
\item do a \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} (for conflicts, i.\+e. error C02000) and then
\begin{DoxyItemize}
\item set the same keyset again (in favour of what was set by this user)
\item drop the old keyset (in favour of what was set from another application)
\item merge the original, your own and the other keyset
\end{DoxyItemize}
\item export the configuration into a file (for unresolvable errors)
\item repeat the same kdb\+Set might be of limited use if the user does not explicitly request it, because temporary errors are rare and its unlikely that they fix themselves (e.\+g. disc full, permission problems)
\end{DoxyItemize}

\begin{DoxyParagraph}{Optimization}
Each key is checked with \mbox{\hyperlink{group__keytest_gaf247df0de7aca04b32ef80e39ef12950}{key\+Need\+Sync()}} before being actually committed. If no key of a backend needs to be synced any affairs to backends are omitted and 0 is returned.
\end{DoxyParagraph}

\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{KeySet * myConfig = \mbox{\hyperlink{group__keyset_ga671e1aaee3ae9dc13b4834a4ddbd2c3c}{ksNew}} (0, \mbox{\hyperlink{kdbenum_8c_a7a28fce3773b2c873c94ac80b8b4cd54}{KS\_END}});}
\DoxyCodeLine{Key * parentKey = \mbox{\hyperlink{group__key_gad23c65b44bf48d773759e1f9a4d43b89}{keyNew}} (\textcolor{stringliteral}{"system/sw/MyApp"}, \mbox{\hyperlink{group__key_gga91fb3178848bd682000958089abbaf40aa8adb6fcb92dec58fb19410eacfdd403}{KEY\_END}});}
\DoxyCodeLine{KDB * handle = \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdbOpen}} (parentKey);}
\DoxyCodeLine{}
\DoxyCodeLine{\mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdbGet}} (handle, myConfig, parentKey); \textcolor{comment}{// kdbGet needs to be called first!}}
\DoxyCodeLine{KeySet * base = \mbox{\hyperlink{group__keyset_gac59e4b328245463f1451f68d5106151c}{ksDup}} (myConfig);     \textcolor{comment}{// save a copy of original keyset}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{// change the keys within myConfig}}
\DoxyCodeLine{\mbox{\hyperlink{group__keyset_gaa5a1d467a4d71041edce68ea7748ce45}{ksAppendKey}} (myConfig, \mbox{\hyperlink{group__key_gad23c65b44bf48d773759e1f9a4d43b89}{keyNew}} (\textcolor{stringliteral}{"system/sw/MyApp/Test"}, \mbox{\hyperlink{group__key_gga91fb3178848bd682000958089abbaf40ac66e4a49d09212b79f5754ca6db5bd2e}{KEY\_VALUE}}, \textcolor{stringliteral}{"5"}, \mbox{\hyperlink{group__key_gga91fb3178848bd682000958089abbaf40aa8adb6fcb92dec58fb19410eacfdd403}{KEY\_END}}));}
\DoxyCodeLine{}
\DoxyCodeLine{KeySet * ours = \mbox{\hyperlink{group__keyset_gac59e4b328245463f1451f68d5106151c}{ksDup}} (myConfig); \textcolor{comment}{// save a copy of our keyset}}
\DoxyCodeLine{KeySet * theirs;                  \textcolor{comment}{// needed for 3-way merging}}
\DoxyCodeLine{\textcolor{keywordtype}{int} ret = \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdbSet}} (handle, myConfig, parentKey);}
\DoxyCodeLine{\textcolor{keywordflow}{while} (ret == -1) \textcolor{comment}{// as long as we have an error}}
\DoxyCodeLine{\{}
\DoxyCodeLine{        \textcolor{keywordtype}{int} strategy = showElektraErrorDialog (parentKey);}
\DoxyCodeLine{        theirs = \mbox{\hyperlink{group__keyset_gac59e4b328245463f1451f68d5106151c}{ksDup}} (ours);}
\DoxyCodeLine{        \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdbGet}} (handle, theirs, parentKey); \textcolor{comment}{// refresh key database}}
\DoxyCodeLine{        KeySet * result = \mbox{\hyperlink{kdbmerge_8h_a5cb2bdc8c7e955afcd37c7fce5de6cf9}{elektraMerge}}(}
\DoxyCodeLine{                \mbox{\hyperlink{group__keyset_ga6b00cf82b59af4d883a9bad6cf4a4a4a}{ksCut}}(ours, parentKey), parentKey,}
\DoxyCodeLine{                \mbox{\hyperlink{group__keyset_ga6b00cf82b59af4d883a9bad6cf4a4a4a}{ksCut}}(theirs, parentKey), parentKey,}
\DoxyCodeLine{                \mbox{\hyperlink{group__keyset_ga6b00cf82b59af4d883a9bad6cf4a4a4a}{ksCut}}(base, parentKey), parentKey,}
\DoxyCodeLine{                parentKey, strategy, parentKey);}
\DoxyCodeLine{        \textcolor{keywordtype}{int} numberOfConflicts = \mbox{\hyperlink{kdbmerge_8h_aeeff18da6e6459f72807bdd7d4767a55}{getConflicts}} (parentKey);}
\DoxyCodeLine{        \mbox{\hyperlink{group__keyset_ga27e5c16473b02a422238c8d970db7ac8}{ksDel}} (theirs);}
\DoxyCodeLine{        \textcolor{keywordflow}{if} (result != NULL) \{}
\DoxyCodeLine{                ret = \mbox{\hyperlink{group__kdb_ga11436b058408f83d303ca5e996832bcf}{kdbSet}} (handle, result, parentKey);}
\DoxyCodeLine{        \} \textcolor{keywordflow}{else} \{}
\DoxyCodeLine{                \textcolor{comment}{// an error happened while merging}}
\DoxyCodeLine{                \textcolor{keywordflow}{if} (numberOfConflicts > 0 \&\& strategy == MERGE\_STRATEGY\_ABORT)}
\DoxyCodeLine{                \{}
\DoxyCodeLine{                        \textcolor{comment}{// Error due to merge conflicts}}
\DoxyCodeLine{                        ret = -1;}
\DoxyCodeLine{                \}}
\DoxyCodeLine{                \textcolor{keywordflow}{else}}
\DoxyCodeLine{                \{}
\DoxyCodeLine{                        \textcolor{comment}{// Internal errors, out of memory etc.}}
\DoxyCodeLine{                        ret = -1;}
\DoxyCodeLine{                \}}
\DoxyCodeLine{        \}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\mbox{\hyperlink{group__keyset_ga27e5c16473b02a422238c8d970db7ac8}{ksDel}} (ours);}
\DoxyCodeLine{\mbox{\hyperlink{group__keyset_ga27e5c16473b02a422238c8d970db7ac8}{ksDel}} (base);}
\DoxyCodeLine{\mbox{\hyperlink{group__keyset_ga27e5c16473b02a422238c8d970db7ac8}{ksDel}} (myConfig); \textcolor{comment}{// delete the in-memory configuration}}
\DoxyCodeLine{}
\DoxyCodeLine{\mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdbClose}} (handle, parentKey); \textcolor{comment}{// no more affairs with the key database.}}
\DoxyCodeLine{\mbox{\hyperlink{group__key_ga3df95bbc2494e3e6703ece5639be5bb1}{keyDel}} (parentKey);}
\end{DoxyCodeInclude}
 show\+Elektra\+Error\+Dialog() and do\+Elektra\+Merge() need to be implemented by the user of Elektra. For do\+Elektra\+Merge a 3-\/way merge algorithm exists in libelektra-\/tools.


\begin{DoxyParams}{Parameters}
{\em handle} & contains internal information of \mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{opened }} key database \\
\hline
{\em ks} & a Key\+Set which should contain changed keys, otherwise nothing is done \\
\hline
{\em parent\+Key} & is used to add warnings and set an error information. Additionally, its name is an hint which keys should be committed (it is possible that more are changed).
\begin{DoxyItemize}
\item cascading keys (starting with /) will set the path in all namespaces
\item / will commit all keys
\item metanames will be rejected (error C01320)
\item empty/invalid (error C01320) 
\end{DoxyItemize}\\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{Return values}
{\em 1} & on success \\
\hline
{\em 0} & if nothing had to be done, no changes in K\+DB \\
\hline
{\em -\/1} & on failure, no changes in K\+DB \\
\hline
\end{DoxyRetVals}
\begin{DoxySeeAlso}{See also}
\mbox{\hyperlink{group__keytest_gaf247df0de7aca04b32ef80e39ef12950}{key\+Need\+Sync()}} 

\mbox{\hyperlink{group__keyset_ga4287b9416912c5f2ab9c195cb74fb094}{ks\+Current()}} contains the error \mbox{\hyperlink{group__key}{Key}} 

\mbox{\hyperlink{group__kdb_ga6808defe5870f328dd17910aacbdc6ca}{kdb\+Open()}} and \mbox{\hyperlink{group__kdb_ga28e385fd9cb7ccfe0b2f1ed2f62453a1}{kdb\+Get()}} that must be called first 

\mbox{\hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdb\+Close()}} that must be called afterwards 
\end{DoxySeeAlso}
\mbox{\Hypertarget{group__kdb_ga1ba80a565f54490f362df95f401bcb29}\label{group__kdb_ga1ba80a565f54490f362df95f401bcb29}} 
\index{KDB@{KDB}!ksRenameKeys@{ksRenameKeys}}
\index{ksRenameKeys@{ksRenameKeys}!KDB@{KDB}}
\subsubsection{\texorpdfstring{ksRenameKeys()}{ksRenameKeys()}}
{\footnotesize\ttfamily Key\+Set$\ast$ ks\+Rename\+Keys (\begin{DoxyParamCaption}\item[{Key\+Set $\ast$}]{config,  }\item[{const char $\ast$}]{name }\end{DoxyParamCaption})}



Takes the first key and cuts off this common part for all other keys, instead name will be prepended. 

\begin{DoxyReturn}{Returns}
a new allocated keyset with keys in user namespace.
\end{DoxyReturn}
The first key is removed in the resulting keyset. 