Git init
[pkgs/e/elektra.git] / doc / elektra-api / man / man3 / backend.3
1 .TH "KDB Backends :: Elektra framework for pluggable backends" 3 "30 Jun 2009" "Elektra Projekt" \" -*- nroff -*-
2 .ad l
3 .nh
4 .SH NAME
5 KDB Backends :: Elektra framework for pluggable backends \- The tactics to create pluggable backends to libelektra.so.  
6
7 .PP
8 .SS "Enumerations"
9
10 .in +1c
11 .ti -1c
12 .RI "enum \fBbackend_t\fP { \fBKDB_BE_OPEN\fP = 1, \fBKDB_BE_CLOSE\fP = 1<<1, \fBKDB_BE_GET\fP = 1<<2, \fBKDB_BE_SET\fP = 1<<3, \fBKDB_BE_VERSION\fP = 1<<4, \fBKDB_BE_DESCRIPTION\fP = 1<<5, \fBKDB_BE_AUTHOR\fP = 1<<6, \fBKDB_BE_LICENCE\fP = 1<<7, \fBKDB_BE_END\fP = 0 }"
13 .br
14 .in -1c
15 .SS "Functions"
16
17 .in +1c
18 .ti -1c
19 .RI "KDB * \fBkdbBackendExport\fP (const char *backendName,...)"
20 .br
21 .ti -1c
22 .RI "int \fBkdbOpen_backend\fP (KDB *handle)"
23 .br
24 .ti -1c
25 .RI "int \fBkdbClose_backend\fP (KDB *handle)"
26 .br
27 .ti -1c
28 .RI "ssize_t \fBkdbGet_backend\fP (KDB *handle, KeySet *returned, const Key *parentKey)"
29 .br
30 .ti -1c
31 .RI "ssize_t \fBkdbSet_backend\fP (KDB *handle, KeySet *returned, const Key *parentKey)"
32 .br
33 .ti -1c
34 .RI "\fBKDBEXPORT\fP (backend)"
35 .br
36 .in -1c
37 .SH "Detailed Description"
38 .PP 
39 The tactics to create pluggable backends to libelektra.so. 
40 .PP
41 .SH "Introduction"
42 .PP
43 \fBSince:\fP
44 .RS 4
45 Since version 0.4.9, Elektra can dynamically load different key storage backends.
46 .PP
47 Since version 0.7.0 Elektra can have multiple storage backends, called just backends henceforth, at once for different purposes.
48 .RE
49 .PP
50 \fBDefinition: You refers to the implementation of the function in this specification.\fP.RS 4
51 If you read the documentation about \fBkdbGet_backend()\fP, then the caller is \fBkdbGet()\fP which is the only function which can and will call (invoke) you. The Preconditions will always be met by the caller, you can count on them. But you (as said before we speak about the function) need to take care that all Postconditions are met.
52 .RE
53 .PP
54 .SS "Overview"
55 The methods of class KDB that are backend dependent are only \fBkdbOpen_backend()\fP, \fBkdbClose_backend()\fP, \fBkdbGet_backend()\fP, \fBkdbSet_backend()\fP and \fBKDBEXPORT()\fP to export these methods. A backend must implement each of them. A detailed specification of these methods and methods needed in that context follows in this Documentation Module.
56 .PP
57 The other KDB methods are higher level. They use the above methods to do their job, and generally don't have to be reimplemented for a different backend, but there might be a solution to do so for higher performance in future. kdbh* methods are for access to the internals of KDB, which will be passed to all functions.
58 .SS "Include Files"
59 The backend implementation must include: 
60 .PP
61 .nf
62 #include <kdbbackend.h>
63
64 .fi
65 .PP
66  to have direct access to the structs, which is currently needed to access the capability structure.
67 .PP
68 Don't include kdb.h, it will be automatically included and some macros will avoid redefining structs where you have more insight from a backend than you would normally have. Additionally you get the declaration of all functions described here, except the one you have to implement.
69 .SS "Dynamic Mounting"
70 An elektrified program will use elektra/libelektra-default.so as its default backend. This backend provides the system/ hierarchy and some base configuration in system/elektra for elektra itself. Everything below system/ and the other hierarchies can be stored in any different backend. This is allowed through the technique mounting. A backend can be mounted to any path except system/ and system/elektra.
71 .PP
72 A backends is guaranteed to be loaded whenever calling \fBkdbGet()\fP or \fBkdbSet()\fP requires the backend, but may already be loaded at \fBkdbOpen()\fP. It might be loaded explizit by \fBkdbMount()\fP at any time after \fBkdbOpen()\fP. Backends get a chance to initialize by calling \fBkdbOpen_backend()\fP whenever they are loaded.
73 .PP
74 Using \fBkdbUnmount()\fP a backend may closed during runtime. All backends will be closed when \fBkdbClose()\fP is called. Backends might be unloaded after some time of inactivity or other reasons. After loading backends get a chance to cleanup by calling \fBkdbClose_backend()\fP.
75 .PP
76 That means it is not guaranteed that the backend live the whole time nor it will be loaded only one time. A tactic to handle this well is to build stateless backends referring to \fBkdbGet_backend()\fP and \fBkdbSet_backend()\fP. That means that there is no more information present than in the storage itself. Be aware that you must not have any global variables in your backend. Read more about that in \fBkdbOpen_backend()\fP. But to be stateless you also have to consider not to store any other than caching information into \fBkdbhGetBackendData()\fP. I repeat: it must be possible to restore everything dynamically stored without exception.
77 .SS "Library Names"
78 Elektra source code or development package provides a skeleton and Makefile to implement a backend. Copy src/backends/template to have a good starting point. See the CODING document to know how to integrate the backend in the build system or how to compile it external.
79 .PP
80 A backend is defined by a single name, for example \fCBACKENDNAME\fP, that causes libelektra.so look for its library as \fClibelektra-BACKENDNAME.so\fP.
81 .PP
82 \fBExample of a complete backend:\fP.RS 4
83
84 .PP
85 .nf
86 //
87 // This is my implementation for an Elektra backend storage.
88 //
89 // To compile it:
90 // $ cc -fpic `pkg-config --cflags elektra` -o myback.o -c myback.c
91 // $ cc -shared -fpic `pkg-config --libs elektra` -o libelektra-myback.so myback.o
92 //
93 // To use it:
94 // $ preload mount myback system/myback myback /tmp/nofile
95 // $ kdb ls system/myback
96 // $ kdb set system/myback/key 'value'
97 // $ kdb get system/myback/key
98 //
99
100 #include <kdbbackend.h>
101
102 #define BACKENDNAME 'backend'
103
104
105 int kdbOpen_backend(KDB *handle) {...}
106 int kdbClose_backend(KDB *handle) {...}
107 int kdbGet_backend(KDB handle, KeySet *returned, Key *key) {...}
108 int kdbSet_backend(KDB handle, KeySet *returned, Key *key) {...}
109
110 KDBEXPORT(backend) {
111         return kdbBackendExport(BACKENDNAME,
112                 KDB_BE_OPEN,  &kdbOpen_backend,
113                 KDB_BE_CLOSE, &kdbClose_backend,
114                 KDB_BE_GET,   &kdbGet_backend,
115                 KDB_BE_SET,   &kdbSet_backend,
116                 KDB_BE_END);
117 }
118
119 .fi
120 .PP
121 .RE
122 .PP
123 In the example, the *_backend() methods can have other random names, since you'll correctly pass them later to \fBkdbBackendExport()\fP. It is recommended to use names according to your backendname to avoid name clashes. Be aware that every symbol name in the linked application must be unique.
124 .PP
125 Don't copy above example out, use src/backends/template, it does compile as-is and does some initialization and cleanup already.
126 .PP
127 Elektra source code tree includes several backend implementations https://svn.libelektra.org/svn/elektra/trunk/src/backends/ that can also be used as a reference.
128 .SH "Details"
129 .PP
130 .SS "Introduction"
131 Capabilities may make your live much easier. If it is impossible, very hard or would impact performance badly you may leave out some parts described here, but need to declare that you have done so with capabilites.
132 .PP
133 It is allowed to provide additional information, even if you declared you don't have it. If you declare that you are capable of doing something, you must provide it without exceptions.
134 .SS "Owner"
135 You need to set the owner of keys by \fBkeySetOwner()\fP. Owner is the name to whom a specific key of the user/ hierarchy belongs. If you declare kdbcGetnoOwner() you need not to set the owner of the keys. It also means that even if you want to get keys from another user hierarchy you get yours.
136 .SS "Values"
137 Values are the central information of keys next to the name describing what informations it holds. Parse them out of your backend and put them into the key with \fBkeySetString()\fP. The information will be duplicated, so you might need to free() your string. Don't try to directly access key->data, things may change there and your backend might be compiled with a different libc than elektra. If you support types, you might want to use keySetRaw() to not change the key type. If you don't support values for all keys declare kdbcGetnoValue().
138 .SS "IDs"
139 You need to set uid respective gid for any key not having the uid and gid of the current process. This will be set by default in every key. You can do it with \fBkeySetUID()\fP and \fBkeySetGID()\fP. Declaring kdbcGetnoUID() and kdbcGetnoGID() you need not set uid and gid.
140 .SS "Mode"
141 Mode shows what can be done with the key having or not having the above uid and gid. Use \fBkeySetMode()\fP to set the correct mode description, read the description in \fBkeySetMode()\fP for the semantics of the 3 octal representation. Declaring kdbcGetnoMode() means mode will remain default.
142 .PP
143 The very related method \fBkeySetDir()\fP sets the executable bits of mode. Even if your backend does not support mode, it might support directories, meaning that keys have the mode 0664 or 0775 for directories. Declaring kdbcGetnoDir() means that the backend is flat, no key will be true for \fBkeyIsDir()\fP and so can't have any subkeys.
144 .SS "Timing"
145 Keys should have exact timing information of their modification and access times. Use \fBkeySetATime()\fP, \fBkeySetMTime()\fP and \fBkeySetCTime()\fP to store appropriate information. ATime need to be stored in database, if you stat a key the backend need to return the time \fBkdbGet()\fP was last used for the keys. If you don't support this, declare kdbcGetnoATime() and simple store time(0) in the atime. This must be the same for every key for a single \fBkdbGet_backend()\fP. If you only stat keys with \fBkdbGet()\fP, see below, then the access time should not be updated. MTime is the last modification time of value or comment. If you don't support this, declare kdbcGetnoMTime() and simple store time(0) in the mtime. This must be the same for every key for a single \fBkdbGet_backend()\fP. CTime is the last change time of any metadata or add/remove of subkeys. If you don't support this, declare kdbcGetnoCTime() and simple store time(0) in the ctime. This must be the same for every key for a single \fBkdbGet_backend()\fP.
146 .SS "Types"
147 Keys having value and comment can be one of two fundamental types, string or binary, both called value. While string is a null terminated utf8 character sequence, binary is any data of a specific length. Be sure to use \fBkeySetString()\fP for string and \fBkeySetBinary()\fP if you want to store binary data. If you do not support one of these, be sure to declare kdbcGetnoBinary() or kdbcGetnoString(), if you don't support both make sure to also declare kdbcGetnoValue().
148 .PP
149 Using keySetRaw() does not set the type, be sure to use \fBkeySetType()\fP afterwards. This can be KEY_TYPE_STRING and KEY_TYPE_BINARY or any other type in type_t, leading to same results as explained above, but also any other number in the range of type_t. Declare kdbcGetnoTypes() when your backend does not support arbitrary types. 
150 .SH "Enumeration Type Documentation"
151 .PP 
152 .SS "enum \fBbackend_t\fP"
153 .PP
154 Switches to denote the backend methods. Used in calls to \fBkdbBackendExport()\fP. 
155 .PP
156 \fBEnumerator: \fP
157 .in +1c
158 .TP
159 \fB\fIKDB_BE_OPEN \fP\fP
160 Next arg is backend for \fBkdbOpen()\fP 
161 .TP
162 \fB\fIKDB_BE_CLOSE \fP\fP
163 Next arg is backend for \fBkdbClose()\fP 
164 .TP
165 \fB\fIKDB_BE_GET \fP\fP
166 Next arg is backend for \fBkdbGet()\fP 
167 .TP
168 \fB\fIKDB_BE_SET \fP\fP
169 Next arg is backend for \fBkdbSet()\fP 
170 .TP
171 \fB\fIKDB_BE_VERSION \fP\fP
172 Next arg is char * for Version 
173 .TP
174 \fB\fIKDB_BE_DESCRIPTION \fP\fP
175 Next arg is char * for Description 
176 .TP
177 \fB\fIKDB_BE_AUTHOR \fP\fP
178 Next arg is char * for Author 
179 .TP
180 \fB\fIKDB_BE_LICENCE \fP\fP
181 Next arg is char * for Licence 
182 .TP
183 \fB\fIKDB_BE_END \fP\fP
184 End of arguments 
185 .SH "Function Documentation"
186 .PP 
187 .SS "KDB* kdbBackendExport (const char * backendName,  ...)"
188 .PP
189 This function must be called by a backend's kdbBackendFactory() to define the backend's methods that will be exported.
190 .PP
191 See \fBKDBEXPORT()\fP how to use it for backends.
192 .PP
193 The order and number of arguments are flexible (as in \fBkeyNew()\fP and \fBksNew()\fP) to let libelektra.so evolve without breaking its ABI compatibility with backends. So for each method a backend must export, there is a flag defined by \fBbackend_t\fP. Each flag tells \fBkdbBackendExport()\fP which method comes next. A backend can have no implementation for a few methods that have default inefficient high-level implementations and to use these defaults, simply don't pass anything to \fBkdbBackendExport()\fP about them.
194 .PP
195 \fBParameters:\fP
196 .RS 4
197 \fIbackendName\fP a simple name for this backend 
198 .RE
199 .PP
200 \fBReturns:\fP
201 .RS 4
202 an object that contains all backend informations needed by libelektra.so 
203 .RE
204 .PP
205
206 .SS "int kdbClose_backend (KDB * handle)"
207 .PP
208 Finalize the backend. Called prior to unloading the backend dynamic module. Should ensure that no functions or static/global variables from the module will ever be accessed again.
209 .PP
210 Make sure to free all memory that your backend requested at runtime.
211 .PP
212 Specifically make sure to capDel() all capabilites and free your backendData in \fBkdbhGetBackendData()\fP.
213 .PP
214 After this call, libelektra.so will unload the backend library, so this is the point to shutdown any affairs with the storage.
215 .PP
216 \fBParameters:\fP
217 .RS 4
218 \fIhandle\fP contains internal information of \fBopened \fP key database 
219 .RE
220 .PP
221 \fBReturns:\fP
222 .RS 4
223 0 on success, anything else otherwise. 
224 .RE
225 .PP
226 \fBSee also:\fP
227 .RS 4
228 \fBkdbClose()\fP 
229 .RE
230 .PP
231
232 .SS "KDBEXPORT (backend)"
233 .PP
234 All KDB methods implemented by the backend can have random names, except kdbBackendFactory(). This is the single symbol that will be looked up when loading the backend, and the first method of the backend implementation that will be called.
235 .PP
236 Its purpose is to publish the exported methods for libelektra.so. The implementation inside the provided skeleton is usually enough: simply call \fBkdbBackendExport()\fP with all methods that must be exported.
237 .PP
238 The first paramter is the name of the backend. Then every backend must have: \fCKDB_BE_OPEN\fP, \fCKDB_BE_CLOSE\fP, \fCKDB_BE_GET\fP and \fCKDB_BE_SET\fP 
239 .PP
240 You might also give following information by char *: \fCKDB_BE_VERSION\fP, \fCKDB_BE_AUTHOR\fP, \fCKDB_BE_LICENCE\fP and \fCKDB_BE_DESCRIPTION\fP 
241 .PP
242 You must use static 'char arrays' in a read only segment. Don't allocate storage, it won't be freed.
243 .PP
244 With capability you can get that information on runtime from any backend with kdbGetCapability().
245 .PP
246 The last parameter must be \fCKDB_BE_END\fP.
247 .PP
248 \fBReturns:\fP
249 .RS 4
250 \fBkdbBackendExport()\fP with the above described parameters. 
251 .RE
252 .PP
253 \fBSee also:\fP
254 .RS 4
255 \fBkdbBackendExport()\fP for an example 
256 .PP
257 kdbOpenBackend() 
258 .RE
259 .PP
260
261 .SS "ssize_t kdbGet_backend (KDB * handle, KeySet * returned, const Key * parentKey)"
262 .PP
263 Retrieve information from a permanent storage to construct a keyset.
264 .SH "Introduction"
265 .PP
266 This function does everything related to get keys out from a backend. There is only one function for that purpose to make implementation and locking much easier.
267 .PP
268 The keyset \fCreturned\fP needs to be filled with information so that the application using elektra can access it. See the live cycle of a comment to understand: 
269 .PP
270 .nf
271 kdbGet_backend(KDB *handle, KeySet *returned, Key *parentKey)
272 {
273         // the task of kdbGet_backend is to retrieve the comment out of the permanent storage
274         Key *key = keyDup (parentKey); // generate a new key to hold the information
275         char *comment;
276         loadfromdisc (comment);
277         keySetComment (key, comment, size); // set the information
278         ksAppendKey(returned, key);
279 }
280
281 // Now return to kdbGet
282 int kdbGet(KDB *handle, KeySet *keyset, Key *parentKey, options)
283 {
284         kdbGet_backend (handle, keyset, 0);
285         // postprocess the keyset and return it
286 }
287
288 // Now return to usercode, waiting for the comment
289 void usercode (Key *key)
290 {
291         kdbGet (handle, keyset, parentKey, 0);
292         key = ksCurrent (keyset, key); // lookup the key from the keyset
293         keyGetComment (key); // now the usercode retrieves the comment
294 }
295
296 .fi
297 .PP
298  Of course not only the comment, but all information of every key in the keyset \fCreturned\fP need to be fetched from permanent storage and stored in the key. So this specification needs to give an exhaustive list of information present in a key.
299 .SH "Conditions"
300 .PP
301 \fBPrecondition:\fP
302 .RS 4
303 The caller \fBkdbGet()\fP will make sure before you are called that the parentKey:
304 .IP "\(bu" 2
305 is a valid key (means that it is a system or user key).
306 .IP "\(bu" 2
307 is below (see \fBkeyIsBelow()\fP) your mountpoint and that your backend is responsible for it.
308 .IP "\(bu" 2
309 has \fBkeyNeedStat()\fP set when you should only stat keys (see later). and that the returned:
310 .IP "\(bu" 2
311 is a valid keyset.
312 .IP "\(bu" 2
313 has \fCall\fP keys with the flag KEY_FLAG_SYNC set.
314 .IP "\(bu" 2
315 \fCmay\fP has keys with the flag KEY_FLAG_STAT set.
316 .IP "\(bu" 2
317 contains only valid keys direct below (see \fBkeyIsDirectBelow()\fP) your parentKey. That also means, that the parentKey will not be in that keyset.
318 .IP "\(bu" 2
319 have keyIsStat() set when the value/comment information is not necessary.
320 .IP "\(bu" 2
321 is in a sorted order, see \fBksSort()\fP. and that the handle:
322 .IP "  \(bu" 4
323 is a valid KDB for your backend.
324 .IP "  \(bu" 4
325 that kdbhGetBackendHandle() contains the same handle for lifetime \fBkdbOpen_backend()\fP until \fBkdbClose_backend()\fP was called.
326 .PP
327
328 .PP
329 .PP
330 The caller \fBkdbGet()\fP will make sure that afterwards you were called, whenever the user requested it with the options, that:
331 .IP "\(bu" 2
332 hidden keys they will be thrown away.
333 .IP "\(bu" 2
334 dirs or only dirs \fBkdbGet()\fP will remove the other.
335 .IP "\(bu" 2
336 you will be called again recursively with all subdirectories.
337 .IP "\(bu" 2
338 the keyset will be sorted when needed.
339 .IP "\(bu" 2
340 the keys in returned having KEY_FLAG_SYNC will be sorted out.
341 .PP
342 .RE
343 .PP
344 \fBInvariant:\fP
345 .RS 4
346 There are no global variables and \fBkdbhGetBackendData()\fP only stores information which can be regenerated any time. The handle is the same when it is the same backend.
347 .RE
348 .PP
349 \fBPostcondition:\fP
350 .RS 4
351 The keyset \fCreturned\fP has the \fCparentKey\fP and all keys direct below (\fBkeyIsDirectBelow()\fP) with all information from the storage. Make sure to return all keys, all directories and also all hidden keys. If some of them are not wished, the caller \fBkdbGet()\fP will drop these keys, see above.
352 .RE
353 .PP
354 .SH "Details"
355 .PP
356 Now lets look at an example how the typical \fBkdbGet_backend()\fP might be implemented. To explain we introduce some pseudo functions which do all the work with the storage (which is of course 90% of the work for a real backend):
357 .IP "\(bu" 2
358 find_key() gets an key out from the storage and memorize the position.
359 .IP "\(bu" 2
360 next_key() will find the next key and return it (with the name).
361 .IP "\(bu" 2
362 fetch_key() gets out all information of a key from storage (details see below example). It removes the \fBkeyNeedStat()\fP and \fBkeyNeedSync()\fP flag afterwards.
363 .IP "\(bu" 2
364 stat_key() gets all meta information (everything but value and comment). It removes the key \fBkeyNeedSync()\fP flag afterwards. returns the next key out from the storage. The typical loop now will be like: 
365 .PP
366 .nf
367 ssize_t kdbGet_backend(KDB *handle, KeySet *update, const Key *parentKey) {
368         Key * current;
369         KeySet *returned = ksNew(ksGetSize(update)*2, KS_END);
370
371         find_key (parentKey);
372         current = keyDup (parentKey);
373         if (keyNeedStat(parentKey))
374         {
375                 current = stat_key(current);
376         } else {
377                 current = fetch_key(current);
378         }
379         clear_bit (KEY_FLAG_SYNC, current->flags);
380         ksAppendKey(returned, current);
381
382         while ((current = next_key()) != 0)
383         {
384                 // search if key was passed in update by caller
385                 Key * tmp = ksLookup (update, current, KDB_O_WITHOWNER|KDB_O_POP);
386                 if (tmp) current = tmp; // key was passed, so use it
387                 if (keyNeedStat(parentKey) || keyNeedStat(current))
388                 {
389                         current = stat_key (current);
390                         set_bit (KEY_FLAG_STAT, current->flags);
391                 } else {
392                         current = fetch_key(current);
393                 }
394                 clear_bit (KEY_FLAG_SYNC, current->flags);
395                 ksAppendKey(returned, current);
396                 // TODO: delete lookup key
397         }
398
399         if (error_happened())
400         {
401                 errno = restore_errno();
402                 return -1;
403         }
404
405         ksClear (update); // the rest of update keys is not in storage anymore
406         ksAppend(update, returned); // append the keys
407         ksDel (returned);
408
409         return nr_keys();
410 }
411
412 .fi
413 .PP
414
415 .PP
416 .PP
417 \fBNote:\fP
418 .RS 4
419 - returned and update are separated, for details why see \fBksLookup()\fP
420 .IP "\(bu" 2
421 the bit KEY_FLAG_SYNC is always cleared, see postconditions
422 .PP
423 .RE
424 .PP
425 So your mission is simple: Search the \fCparentKey\fP and add it and then search all keys below and add them too, of course with all requested information (which is only depended on \fBkeyNeedStat()\fP).
426 .SH "Stat"
427 .PP
428 Sometimes value and comment are not of interest, but metadata. To avoid a potential time-consuming \fBkdbGet()\fP you can \fBkeyNeedStat()\fP the \fCparentKey\fP. If the backend supports a less time-consuming method to just get names and metadata, implement it, otherwise declare kdbcGetnoStat().
429 .PP
430 The implementation works as follows: When the \fCparentKey\fP has \fBkeyNeedStat()\fP set, all keys need to be stated instead of getting them. So the keys you \fBksAppendKey()\fP don't have a value nor a comment and make sure that KEY_FLAG_SYNC is not set, but \fBkeyNeedStat()\fP must be set for all keys which are only stated.
431 .PP
432 The keys in \fCreturned\fP may already have \fBkeyNeedStat()\fP set. These keys must keep the status \fBkeyNeedStat()\fP and you don't need to get the value and comment. See the example above for code.
433 .SH "Updating"
434 .PP
435 To get all keys out of the storage over and over again can be very inefficient. You might know a more efficient method to know if the key needs update or not, e.g. by stating it or by an external time stamp info. In that case you can make use of \fCreturned\fP KeySet. There are following possibilities:
436 .IP "\(bu" 2
437 The key is in returned and up to date. You just need to remove the KEY_FLAG_SYNC flag.
438 .IP "\(bu" 2
439 The key is in returned but \fBkeyNeedStat()\fP is true. You just need to stat the key and remove the KEY_FLAG_SYNC flag and set the KEY_FLAG_STAT flag.
440 .IP "\(bu" 2
441 The key is in returned, \fBkeyNeedStat()\fP is false (for the key and the \fCparentKey\fP) and you know that the key has changed. You need to fully retrieve the key and remove the KEY_FLAG_SYNC flag.
442 .IP "\(bu" 2
443 The key is not in returned, the \fCparentKey\fP has \fBkeyNeedStat()\fP. You just need to stat the key. Make sure that KEY_FLAG_SYNC is not set, but KEY_FLAG_STAT needs to be set. Append the key to \fCreturned\fP.
444 .IP "\(bu" 2
445 The key is not in returned and the \fCparentKey\fP \fBkeyNeedStat()\fP is false. You need to fully retrieve the key out of storage, clear KEY_FLAG_STAT and KEY_FLAG_SYNC and \fBksAppendKey()\fP it to the \fCreturned\fP keyset.
446 .PP
447 .PP
448 \fBNote:\fP
449 .RS 4
450 You must clear the flag KEY_FLAG_SYNC at the very last point where no more modification on the key will take place, because any modification on the key will set the KEY_FLAG_SYNC flag again. With that \fBkeyNeedSync()\fP will return true and the caller will sort this key out.
451 .RE
452 .PP
453 .SH "only Full Get"
454 .PP
455 In some backends it is not useful to get only a part of the configuration, because getting all keys would take as long as getting some. For this situation, you can declare onlyFullGet, see kdbcGetonlyFullGet().
456 .PP
457 The only valid call for your backend is then that \fCparentKey\fP equals the \fCmountpoint\fP. For all other \fCparentKey\fP you must, add nothing and just return 0.
458 .PP
459 .PP
460 .nf
461 if (strcmp (keyName(kdbhGetMountpoint(handle)), keyName(parentKey))) return 0;
462 .fi
463 .PP
464 .PP
465 If the \fCparentKey\fP is your mountpoint you will of course fetch all keys, and not only the keys direct below the \fCparentKey\fP. So \fCreturned\fP is valid iff:
466 .IP "\(bu" 2
467 every key is below ( \fBkeyIsBelow()\fP) the parentKey
468 .IP "\(bu" 2
469 every key has a direct parent (\fBkeyIsDirectBelow()\fP) in the keyset
470 .PP
471 .PP
472 \fBNote:\fP
473 .RS 4
474 This statement is only valid for backends with kdbcGetonlyFullGet() set.
475 .PP
476 If any calls you use change errno, make sure to restore the old errno.
477 .RE
478 .PP
479 \fBSee also:\fP
480 .RS 4
481 \fBkdbGet()\fP for caller.
482 .RE
483 .PP
484 \fBParameters:\fP
485 .RS 4
486 \fIhandle\fP contains internal information of \fBopened \fP key database 
487 .br
488 \fIreturned\fP contains a keyset where the function need to append the keys got from the storage. There might be also some keys inside it, see conditions. You may use them to support efficient updating of keys, see \fBUpdating\fP. 
489 .br
490 \fIparentKey\fP contains the information below which key the keys should be gotten.
491 .RE
492 .PP
493 \fBReturns:\fP
494 .RS 4
495 Return how many keys you added.
496 .PP
497 -1 on failure, the current key in returned shows the position. In normal execution cases a positive value will be returned. But in some cases you are not able to get keys and have to return -1. If you declare kdbcGetnoError() you are done, but otherwise you have to set the cause of the error. (Will be added in 0.7.1) 
498 .RE
499 .PP
500
501 .SS "int kdbOpen_backend (KDB * handle)"
502 .PP
503 Initialize the backend. This is the first method kdbOpenBackend() calls after dynamically loading the backend library.
504 .PP
505 This method is responsible of:
506 .IP "\(bu" 2
507 backend's specific configuration gathering
508 .IP "\(bu" 2
509 all backend's internal structs initialization
510 .IP "\(bu" 2
511 initial setup of all I/O details such as opening a file, connecting to a database, etc
512 .PP
513 .PP
514 If your backend does not support all aspects described in \fBkdbGet_backend()\fP and \fBkdbSet_backend()\fP you need capabilities to export this information. Per default you declare to be fully compliant to the specification given here, to change it get a pointer to KDBCap structure by using \fBkdbhGetCapability()\fP.
515 .PP
516 You may also read the configuration you can get with \fBkdbhGetConfig()\fP and transform it into other structures used by your backend.
517 .PP
518 But be aware that you don't have any global variables. If you do your backend will not be threadsafe. You can use \fBkdbhSetBackendData()\fP and \fBkdbhGetBackendData()\fP to store and get any information related to your backend.
519 .PP
520 The correct substitute for global variables will be: 
521 .PP
522 .nf
523 struct _GlobalData{ int global; };
524 typedef struct _GlobalData GlobalData;
525 int kdbOpen_backend(KDB *handle) {
526         PasswdData *data;
527         data=malloc(sizeof(PasswdData));
528         data.global = 20;
529         kdbhSetBackendData(handle,data);
530 }
531
532 .fi
533 .PP
534 .PP
535 Make sure to free everything in \fBkdbClose_backend()\fP.
536 .PP
537 \fBReturns:\fP
538 .RS 4
539 0 on success 
540 .RE
541 .PP
542 \fBParameters:\fP
543 .RS 4
544 \fIhandle\fP contains internal information of \fBopened \fP key database 
545 .RE
546 .PP
547 \fBSee also:\fP
548 .RS 4
549 \fBkdbOpen()\fP 
550 .RE
551 .PP
552
553 .SS "ssize_t kdbSet_backend (KDB * handle, KeySet * returned, const Key * parentKey)"
554 .PP
555 Store a keyset permanently.
556 .PP
557 This function does everything related to set and remove keys in a backend. There is only one function for that purpose to make implementation and locking much easier.
558 .PP
559 The keyset \fCreturned\fP was filled in with information from the application using elektra and the task of this function is to store it in a permanent way so that a subsequent call of \fBkdbGet_backend()\fP can rebuild the keyset as it was before. See the live cycle of a comment to understand: 
560 .PP
561 .nf
562 void usercode (Key *key)
563 {
564         keySetComment (key, 'mycomment'); // the usercode stores a comment for the key
565         ksAppendKey(keyset, key); // append the key to the keyset
566         kdbSet (handle, keyset, 0, 0);
567 }
568
569 // so now kdbSet is called
570 int kdbSet(KDB *handle, KeySet *keyset, Key *parentKey, options)
571 {
572         // find appropriate backend
573         kdbSet_backend (handle, keyset, 0); // the keyset with the key will be passed to this function
574 }
575
576 // so now kdbSet_backend(), which is the function described here, is called
577 kdbSet_backend(KDB *handle, KeySet *keyset, Key *parentKey)
578 {
579         // the task of kdbSet_backend is now to store the comment
580         Key *key = ksCurrent (keyset); // get out the key where the user set the comment before
581         char *comment = allocate(size);
582         keyGetComment (key, comment, size);
583         savetodisc (comment);
584 }
585
586 .fi
587 .PP
588  Of course not only the comment, but all information of every key in the keyset \fCreturned\fP need to be stored permanetly. So this specification needs to give an exhaustive list of information present in a key.
589 .PP
590 \fBPrecondition:\fP
591 .RS 4
592 The keyset \fCreturned\fP holds all keys which must be saved permanently for this keyset. The keyset is sorted and rewinded. All keys having children must be true for \fBkeyIsDir()\fP.
593 .PP
594 The \fCparentKey\fP is the key which is the ancestor for all other keys in the keyset. The first key of the keyset \fCreturned\fP has the same keyname. The parentKey is below the mountpoint, see \fBkdbhGetMountpoint()\fP.
595 .PP
596 The caller kdbSet will fulfill following parts:
597 .IP "\(bu" 2
598 If the user does not want hidden keys they will be thrown away. All keys in \fCreturned\fP need to be stored permanently.
599 .IP "\(bu" 2
600 If the user does not want dirs or only dirs \fBkdbGet()\fP will remove the other.
601 .IP "\(bu" 2
602 Sorting of the keyset. It is not important in which order the keys are appended. So make sure to set all keys, all directories and also all hidden keys. If some of them are not wished, the caller \fBkdbSet()\fP will sort them out.
603 .PP
604 .RE
605 .PP
606 \fBInvariant:\fP
607 .RS 4
608 There are no global variables and \fBkdbhGetBackendData()\fP only stores information which can be regenerated any time. The handle is the same when it is the same backend.
609 .RE
610 .PP
611 \fBPostcondition:\fP
612 .RS 4
613 The information of the keyset \fCreturned\fP is stored permanently.
614 .RE
615 .PP
616 When some keys have KEY_FLAG_REMOVE set, that means return true for \fBkeyNeedRemove()\fP, remove the keys instead of getting them. In this case the sorting order will be the reverse way, first will be the children, then the parentKey when iterating over the KeySet returned.
617 .PP
618 Lock your permanent storage in an exclusive way, no access of a concurrent \fBkdbSet_backend()\fP or \fBkdbGet_backend()\fP is possible and these methods block until the function has finished. Otherwise declare kdbcGetnoLock().
619 .PP
620 \fBSee also:\fP
621 .RS 4
622 \fBkdbSet()\fP for caller.
623 .RE
624 .PP
625 \fBParameters:\fP
626 .RS 4
627 \fIhandle\fP contains internal information of \fBopened \fP key database 
628 .br
629 \fIreturned\fP contains a keyset with relevant keys 
630 .br
631 \fIparentKey\fP contains the information where to set the keys
632 .RE
633 .PP
634 \fBReturns:\fP
635 .RS 4
636 When everything works gracefully return the number of keys you set. The cursor position and the keys remaining in the keyset are not important.
637 .PP
638 Return 0 on success with no changed key in database
639 .PP
640 Return -1 on failure.
641 .RE
642 .PP
643 \fBNote:\fP
644 .RS 4
645 If any calls you use change errno, make sure to restore the old errno.
646 .RE
647 .PP
648 \fBError\fP
649 .RS 4
650 In normal execution cases a positive value will be returned. But in some cases you are not able to set keys and have to return -1. If you declare kdbcGetnoError() you are done, but otherwise you have to set the cause of the error. (Will be added with 0.7.1)
651 .RE
652 .PP
653 You also have to make sure that \fBksGetCursor()\fP shows to the position where the error appeared.