forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdb.hpp
407 lines (370 loc) · 9.89 KB
/
kdb.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifndef ELEKTRA_KDB_HPP
#define ELEKTRA_KDB_HPP
#include <string>
#include <vector>
#include <elektradiff.hpp>
#include <kdbexcept.hpp>
#include <key.hpp>
#include <keyset.hpp>
#include <kdb.h>
#include <kdbchangetracking.h>
#include <kdbgopts.h>
/**
* @brief This is the main namespace for the C++ binding and libraries.
*
* Classes or Functions directly below this namespace are header-only.
* Sub namespaces are intended for libraries and you need to link
* the library if you want to use them.
* - @see kdb::tools
*/
namespace kdb
{
/**
* @copydoc KDB
*
* @brief Access to the key database.
*
* @invariant the object holds a valid connection to the key database
* or is empty
*/
class KDB
{
public:
KDB ();
explicit KDB (Key & errorKey);
explicit KDB (KeySet & contract);
KDB (KeySet & contract, Key & errorKey);
virtual ~KDB () throw ()
{
close ();
}
virtual inline void open (Key & errorKey);
virtual inline void open (KeySet & contract, Key & errorKey);
virtual inline void close () throw ();
virtual inline void close (Key & errorKey) throw ();
virtual inline int get (KeySet & returned, std::string const & keyname);
virtual inline int get (KeySet & returned, Key & parentKey);
virtual inline int set (KeySet & returned, std::string const & keyname);
virtual inline int set (KeySet & returned, Key & parentKey);
virtual inline ElektraDiff calculateChanges (KeySet & changedKeySet, std::string const & parentKeyName);
virtual inline ElektraDiff calculateChanges (KeySet & changedKeySet, Key & parentKey);
inline ckdb::KDB * getKdb () const;
inline ckdb::KDB * operator* () const;
private:
ckdb::KDB * handle; ///< holds an kdb handle
};
/**
* Constructs a class KDB.
*
* @throw KDBException if database could not be opened
*
* @copydoc kdbOpen
*/
inline KDB::KDB ()
{
Key errorKey;
open (errorKey);
}
/**
* Constructs a class KDB.
*
* @param errorKey is useful if you want to get the warnings in
* the successful case, when no exception is thrown.
*
* @throw KDBException if database could not be opened
*
* @copydoc kdbOpen
*/
inline KDB::KDB (Key & errorKey)
{
open (errorKey);
}
/**
* Constructs a class KDB.
*
* @param contract the contract that should be ensured
* @param errorKey is useful if you want to get the warnings in
* the successful case, when no exception is thrown.
*
* @throw KDBException if database could not be opened
*
* @copydoc kdbOpen
*/
inline KDB::KDB (KeySet & contract)
{
Key errorKey;
open (contract, errorKey);
}
/**
* Constructs a class KDB.
*
* @param contract the contract that should be ensured
* @param errorKey is useful if you want to get the warnings in
* the successful case, when no exception is thrown.
*
* @throw KDBException if database could not be opened
*
* @copydoc kdbOpen
*/
inline KDB::KDB (KeySet & contract, Key & errorKey)
{
open (contract, errorKey);
}
/**
* Open the database
*
* @param errorKey is useful if you want to get the warnings in
* the successful case, when no exception is thrown.
*
* @copydoc kdbOpen
*/
inline void KDB::open (Key & errorKey)
{
handle = ckdb::kdbOpen (NULL, errorKey.getKey ());
if (!handle)
{
throw kdb::KDBException (errorKey);
}
}
/**
* Open the database
*
* @param contract the contract that should be ensured
* @param errorKey is useful if you want to get the warnings in
* the successful case, when no exception is thrown.
*
* @copydoc kdbOpen
*/
inline void KDB::open (KeySet & contract, Key & errorKey)
{
handle = ckdb::kdbOpen (contract.getKeySet (), errorKey.getKey ());
if (!handle)
{
throw kdb::KDBException (errorKey);
}
}
/**
* Close the database.
*
* The return value does not matter because its only a null pointer check.
*
* @copydoc kdbClose
*/
inline void KDB::close () throw ()
{
Key errorKey;
ckdb::kdbClose (handle, errorKey.getKey ());
handle = nullptr;
}
/**
* Close the database.
*
* The return value does not matter because its only a null pointer check.
*
* @param errorKey is useful if you want to get the warnings
*
* @copydoc kdbClose
*/
inline void KDB::close (Key & errorKey) throw ()
{
ckdb::kdbClose (handle, errorKey.getKey ());
handle = nullptr;
}
/**
* @class doxygenKDBReturn
* @brief
*
* @retval 0 if no key was updated
* @retval 1 if user or system keys were updated
* @retval 2 if user and system keys were updated
*/
/**
* Get all keys below keyname inside returned.
*
* @copydoc kdbGet
*
* @include cpp_example_get.cpp
*
* @param returned the keyset where the keys will be in
* @param keyname the root keyname which should be used to get keys below it
*
* @copydetails doxygenKDBReturn
*
* @throw KDBException if there were problems with the database
*
* @see KDB::get (KeySet & returned, Key & parentKey)
*/
inline int KDB::get (KeySet & returned, std::string const & keyname)
{
Key parentKey (keyname.c_str (), KEY_END);
return get (returned, parentKey);
}
/**
* Get all keys below parentKey inside returned.
*
* @copydoc kdbGet
*
* @param returned the keyset where the keys will be in
* @param parentKey the parentKey of returned
*
* @copydetails doxygenKDBReturn
*
* @throw KDBException if there were problems with the database
*/
inline int KDB::get (KeySet & returned, Key & parentKey)
{
int ret = ckdb::kdbGet (handle, returned.getKeySet (), parentKey.getKey ());
if (ret == -1)
{
throw KDBException (parentKey);
}
return ret;
}
/**
* Set all keys below keyname.
*
* If the keyname of the parentKey is invalid (e.g. empty) all keys will be set.
*
* @copydoc kdbSet
*
* @copydetails doxygenKDBReturn
*
* @param returned the keyset where the keys will be in
* @param keyname the keyname below the names should be set
*
* @throw KDBException if there were problems with the database
*/
inline int KDB::set (KeySet & returned, std::string const & keyname)
{
Key parentKey (keyname.c_str (), KEY_END);
return set (returned, parentKey);
}
/**
* Set all keys below parentKey.
*
* If the keyname of the parentKey is invalid (e.g. empty) all keys will be set.
*
* @copydoc kdbSet
*
* @copydetails doxygenKDBReturn
*
* @param returned the keyset where the keys are passed to the user
* @param parentKey the parentKey of returned
*
* @throw KDBException if there were problems with the database
*/
inline int KDB::set (KeySet & returned, Key & parentKey)
{
int ret = ckdb::kdbSet (handle, returned.getKeySet (), parentKey.getKey ());
if (ret == -1)
{
throw KDBException (parentKey);
}
return ret;
}
/**
* Calculates the changes between the provided KeySet and the current state of the KDB
*
* @param changedKeySet the keyset that should be used to diff
* @param parentKeyName only changes same or below this keys are calculated
*
* @return a diff with all the changes
*/
inline ElektraDiff KDB::calculateChanges (KeySet & changedKeySet, std::string const & parentKeyName)
{
Key parentKey (parentKeyName, KEY_END);
return calculateChanges (changedKeySet, parentKey);
}
/**
* Calculates the changes between the provided KeySet and the current state of the KDB
*
* @param changedKeySet the keyset that should be used to diff
* @param parentKey only changes same or below this keys are calculated
*
* @return a diff with all the changes
*/
inline ElektraDiff KDB::calculateChanges (KeySet & changedKeySet, Key & parentKey)
{
const ckdb::ChangeTrackingContext * context = ckdb::elektraChangeTrackingGetContextFromKdb (handle);
ckdb::ElektraDiff * diff = ckdb::elektraChangeTrackingCalculateDiff (changedKeySet.getKeySet (), context, parentKey.getKey ());
return ElektraDiff (diff);
}
/**
* Passes out the raw kdb pointer.
*
* This pointer can be used to directly interact with the underlying kdb instance
*
* \note that the ownership remains in the object
*/
inline ckdb::KDB * KDB::getKdb () const
{
return handle;
}
/**
* Is an abbreviation for getKdb.
*
* @copydoc getKdb
*
* @see getKdb()
*/
inline ckdb::KDB * KDB::operator* () const
{
return handle;
}
/**
* @see elektraGOptsContract
*/
inline int goptsContract (kdb::KeySet & contract, int argc, const char * const * argv, const char * const * envp,
const kdb::Key & parentKey, kdb::KeySet & goptsConfig)
{
return ckdb::elektraGOptsContract (contract.getKeySet (), argc, argv, envp, parentKey.getKey (), goptsConfig.getKeySet ());
}
/**
* Prefer to use goptsContract with argc, argv and envp if possible
* (especially when you are calling this in your main function)
*
* This function mainly exists for use from language bindings.
*
* @see elektraGOptsContractFromStrings
*/
inline int goptsContract (kdb::KeySet & contract, const std::string & argsString, const std::string & envString, const kdb::Key & parentKey,
kdb::KeySet & goptsConfig)
{
return ckdb::elektraGOptsContractFromStrings (contract.getKeySet (), argsString.size (), argsString.c_str (), envString.size (),
envString.c_str (), parentKey.getKey (), goptsConfig.getKeySet ());
}
/**
* Prefer to use goptsContract with argc, argv and envp if possible
* (especially when you are calling this in your main function)
*
* This function mainly exists for use from language bindings.
*
* @see elektraGOptsContractFromStrings
*/
inline int goptsContract (kdb::KeySet & contract, const std::vector<std::string> & args, const std::vector<std::string> & env,
const kdb::Key & parentKey, kdb::KeySet & goptsConfig)
{
std::stringstream argStringStream;
for (auto && arg : args)
{
argStringStream << arg << '\0';
}
std::string argString = argStringStream.str ();
std::stringstream envStringStream;
for (auto && envvar : env)
{
envStringStream << envvar << '\0';
}
std::string envString = envStringStream.str ();
return ckdb::elektraGOptsContractFromStrings (contract.getKeySet (), argString.size (), argString.c_str (), envString.size (),
envString.c_str (), parentKey.getKey (), goptsConfig.getKeySet ());
}
} // end of namespace kdb
#endif