Example #1
0
int main()
{
	KeySet *myConfig = ksNew(0, KS_END);
	Key *key = keyNew("system/test/myapp",KEY_END);
	KDB *handle = kdbOpen(key);

	kdbGet(handle, myConfig, key);

	keySetName(key, "user/test/myapp");
	kdbGet(handle, myConfig, key);

	// check for errors in key
	keyDel(key);

	key = ksLookupByName(myConfig,"/test/myapp/key", 0);

	// check if key is not 0 and work with it...
	if (key)
	{
		printf("%s\n", keyString(key));
	}

	ksDel (myConfig); // delete the in-memory configuration


	// maybe you want kdbSet() myConfig here

	kdbClose(handle, 0); // no more affairs with the key database.
	return 0;
}
Example #2
0
int main ()
{
	// clang-format off
//! [set]
KeySet * myConfig = ksNew (0, KS_END);
Key * parentKey = keyNew ("system/sw/MyApp", KEY_END);
KDB * handle = kdbOpen (parentKey);

kdbGet (handle, myConfig, parentKey); // kdbGet needs to be called first!
KeySet * base = ksDup (myConfig);     // save a copy of original keyset

// change the keys within myConfig

KeySet * ours = ksDup (myConfig); // save a copy of our keyset
KeySet * theirs;		  // needed for 3-way merging
int ret = kdbSet (handle, myConfig, parentKey);
while (ret == -1) // as long as we have an error
{
	// We got an error. Warn user.
	Key * problemKey = ksCurrent (myConfig);
	// parentKey has the errorInformation
	// problemKey is the faulty key (may be null)
	int userInput = showElektraErrorDialog (parentKey, problemKey);
	switch (userInput)
	{
	case INPUT_USE_OURS:
		kdbGet (handle, myConfig, parentKey); // refresh key database
		ksDel (myConfig);
		myConfig = ours;
		break;
	case INPUT_DO_MERGE:
		theirs = ksDup (ours);
		kdbGet (handle, theirs, parentKey); // refresh key database
		KeySet * res = doElektraMerge (ours, theirs, base);
		ksDel (theirs);
		myConfig = res;
		break;
	case INPUT_USE_THEIRS:
		// should always work, we just write what we got
		// but to be sure always give the user another way
		// to exit the loop
		kdbGet (handle, myConfig, parentKey); // refresh key database
		break;
		// other cases ...
	}
	ret = kdbSet (handle, myConfig, parentKey);
}

ksDel (ours);
ksDel (base);
ksDel (myConfig); // delete the in-memory configuration

kdbClose (handle, parentKey); // no more affairs with the key database.
keyDel (parentKey);
//! [set]
}
Example #3
0
void kdbGetByName(KDB *kdb, KeySet *conf, Key *parentKey, char *where)
{
	keySetName(parentKey, "system");
	keyAddBaseName(parentKey, where);
	kdbGet(kdb, conf, parentKey);

	keySetName(parentKey, "user");
	keyAddBaseName(parentKey, where);
	kdbGet(kdb, conf, parentKey);
}
Example #4
0
/**
 * @brief Bootstrap, first phase with fallback
 * @internal
 *
 * @param handle already allocated, but without defaultBackend
 * @param [out] keys for bootstrapping
 * @param errorKey key to add errors too
 *
 * @retval -1 failure: cannot initialize defaultBackend
 * @retval 0 warning: could not get initial config
 * @retval 1 success
 * @retval 2 success in fallback mode
 */
int elektraOpenBootstrap (KDB * handle, KeySet * keys, Key * errorKey)
{
	handle->defaultBackend = backendOpenDefault (handle->modules, KDB_DB_INIT, errorKey);
	if (!handle->defaultBackend) return -1;

	handle->split = splitNew ();
	splitAppend (handle->split, handle->defaultBackend, keyNew (KDB_SYSTEM_ELEKTRA, KEY_END), 2);

	keySetName (errorKey, KDB_SYSTEM_ELEKTRA);
	keySetString (errorKey, "kdbOpen(): get");

	int funret = 1;
	int ret = kdbGet (handle, keys, errorKey);
	int fallbackret = 0;
	if (ret == 0 || ret == -1)
	{
		// could not get KDB_DB_INIT, try KDB_DB_FILE
		// first cleanup:
		ksClear (keys);
		backendClose (handle->defaultBackend, errorKey);
		splitDel (handle->split);

		// then create new setup:
		handle->defaultBackend = backendOpenDefault (handle->modules, KDB_DB_FILE, errorKey);
		if (!handle->defaultBackend)
		{
			elektraRemoveMetaData (errorKey, "error"); // fix errors from kdbGet()
			return -1;
		}
		handle->split = splitNew ();
		splitAppend (handle->split, handle->defaultBackend, keyNew (KDB_SYSTEM_ELEKTRA, KEY_END), 2);

		keySetName (errorKey, KDB_SYSTEM_ELEKTRA);
		keySetString (errorKey, "kdbOpen(): get fallback");
		fallbackret = kdbGet (handle, keys, errorKey);
		keySetName (errorKey, "system/elektra/mountpoints");

		KeySet * cutKeys = ksCut (keys, errorKey);
		if (fallbackret == 1 && ksGetSize (cutKeys) != 0)
		{
			funret = 2;
		}
		ksAppend (keys, cutKeys);
		ksDel (cutKeys);
	}

	if (ret == -1 && fallbackret == -1)
	{
		funret = 0;
	}

	elektraRemoveMetaData (errorKey, "error"); // fix errors from kdbGet()
	return funret;
}
Example #5
0
int main()
{
	KeySet *myConfig = ksNew(0, KS_END);
	Key *key = keyNew("/sw/MyApp", KEY_CASCADING_NAME, KEY_END);
	KDB *handle = kdbOpen(key);

	kdbGet(handle, myConfig, key);
	// to get an intention of proper error handling see kdbget_error.c

	keyDel (key);

	Key * result = ksLookupByName (myConfig,"/sw/MyApp/Tests/TestKey1", 0);

	const char * key_name = keyName(result);
	const char * key_value = keyString(result);
	const char * key_comment = keyString(keyGetMeta(result, "comment"));
	printf("key: %s value: %s comment: %s\n", key_name, key_value, key_comment);

	ksDel (myConfig); // delete the in-memory configuration


	// maybe you want kdbSet() myConfig here

	kdbClose(handle, 0); // no more affairs with the key database.
}
Example #6
0
static void test_registerInt (void)
{
	printf ("test elektraNotificationRegisterInt\n");

	Key * key = keyNew ("system/elektra/version/constants", KEY_END);
	Key * valueKey = keyNew ("system/elektra/version/constants/KDB_VERSION_MAJOR", KEY_END);

	int startValue = -1;
	int value = startValue;

	KDB * kdb = kdbOpen (key);

	succeed_if (elektraNotificationRegisterInt (kdb, valueKey, &value) == 0, "register should fail before open");

	elektraNotificationOpen (kdb);

	succeed_if (elektraNotificationRegisterInt (kdb, valueKey, &value), "register failed");

	// call kdbGet; value gets automatically updated
	KeySet * config = ksNew (0, KS_END);
	succeed_if (kdbGet (kdb, config, key), "kdbGet failed");

	succeed_if (value != startValue, "value was not changed");

	// cleanup
	ksDel (config);
	elektraNotificationClose (kdb);
	kdbClose (kdb, key);
	keyDel (key);
	keyDel (valueKey);
}
Example #7
0
static int basicUse (int argc, const char ** argv)
{
	Key * parentKey = keyNew ("/sw/org/example/#0/current", KEY_END);
	//! [basic use]
	KDB * kdb = kdbOpen (parentKey);
	KeySet * ks = ksNew (0, KS_END);

	kdbGet (kdb, ks, parentKey);

	int result = elektraGetOpts (ks, argc, argv, (const char **) environ, parentKey);
	if (result == -1)
	{
		fprintf (stderr, "ERROR: %s\n", keyString (keyGetMeta (parentKey, "error/reason")));
		keyDel (parentKey);
		ksDel (ks);
		return EXIT_FAILURE;
	}

	if (result == 1)
	{
		char * help = elektraGetOptsHelpMessage (parentKey, NULL, NULL);
		fprintf (stderr, "%s\n", help);
		elektraFree (help);
		keyDel (parentKey);
		ksDel (ks);
		return EXIT_SUCCESS;
	}

	//! [basic use]
	ksDel (ks);
	kdbClose (kdb, parentKey);
	keyDel (parentKey);
	return EXIT_SUCCESS;
}
Example #8
0
// typical usage of Elektra
int main()
{
	Key * error_key = keyNew(KEY_END);
	KDB * kdb_handle = kdbOpen(error_key);
	Key * top = keyNew(KEY_END);
	keySetName(top, "user/sw/MyApp");

	KeySet * ks = ksNew(0);
	kdbGet(kdb_handle, ks, top);

	Key * key = keyNew(KEY_END);
	keySetName(key, "user/sw/MyApp/Tests/TestKey1"); // == 31
	keySetString(key, "NULLTestValue"); // == 14
	keySetMeta(key, "comment", "NULLTestComment"); // == 16
	ksAppendKey(ks, key); // == 1
	keyNeedSync(key);
	kdbSet(kdb_handle, ks, top); // == -1
	print_warnings(top);
	keyDel(top);
	ksDel(ks);
	kdbClose(kdb_handle, error_key);
	keyDel(error_key);

	check_key();

	return 0;
}
Example #9
0
int main ()
{
	// clang-format off
//! [cut]
Key * parentKey = keyNew ("system/mountpoint/interest", KEY_END);
KDB * kdb = kdbOpen (parentKey);
KeySet * ks = ksNew (0, KS_END);
kdbGet (kdb, ks, parentKey);
KeySet * returned = ksCut (ks, parentKey);
kdbSet (kdb, ks, parentKey); // all keys below cutpoint are now removed
kdbClose (kdb, parentKey);
//! [cut]
outputKeySet (returned);
outputKeySet (ks);
}
Example #10
0
/** After writing the key this function rereads the key and print it*/
void check_key()
{
	Key * error_key = keyNew(KEY_END);
	KDB * kdb_handle = kdbOpen(error_key);
	Key * top = keyNew(KEY_END);
	keySetName(top, "user/sw/MyApp"); // == 14
	KeySet * ks = ksNew(0);
	kdbGet(kdb_handle, ks, top);
	Key * key = keyNew(KEY_END);
	keySetName(key, "user/sw/MyApp/Tests/TestKey1"); // == 14
	Key * result = ksLookup(ks, key, KDB_O_NONE);
	const char * key_name = keyName(result);
	const char * key_value = keyString(result);
	const char * key_comment = keyString(keyGetMeta(result, "comment"));
	printf("key: %s value: %s comment: %s", key_name, key_value, key_comment);
	ksDel(ks);
	keyDel(key);
	keyDel(top);
	kdbClose(kdb_handle, error_key);
	keyDel(error_key);
}
Example #11
0
int main (void)
{
	KeySet * myConfig = ksNew (0, KS_END);
	Key * key = keyNew ("/sw/MyApp", KEY_CASCADING_NAME, KEY_END);
	KDB * handle = kdbOpen (key);

	if (!handle) printError (key);


	printWarnings (key);

	if (kdbGet (handle, myConfig, key) < 0) printError (key);


	printWarnings (key);

	keyDel (key);

	// lookup
	Key * result = ksLookupByName (myConfig, "/sw/MyApp/Tests/TestKey1", 0);
	if (!result)
		printf ("Key not found in KeySet\n");
	else
	{
		// do something with the key
		const char * key_name = keyName (result);
		const char * key_value = keyString (result);
		const char * key_comment = keyString (keyGetMeta (result, "comment"));
		printf ("key: %s value: %s comment: %s\n", key_name, key_value, key_comment);
	}

	ksDel (myConfig); // delete the in-memory configuration


	// maybe you want kdbSet() myConfig here

	kdbClose (handle, 0); // no more affairs with the key database.
}
Example #12
0
/**
 * @see kdbnotificationinternal.h ::ElektraNotificationKdbUpdate
 */
static void elektraNotificationKdbUpdate (KDB * kdb, Key * changedKey)
{
	KeySet * ks = ksNew (0, KS_END);
	kdbGet (kdb, ks, changedKey);
	ksDel (ks);
}
Example #13
0
void benchmarkReread ()
{
	kdbGet (kdb, large, key);
}
Example #14
0
void benchmarkReadin ()
{
	KeySet * n = ksNew (0, KS_END);
	kdbGet (kdb, n, key);
	ksDel (n);
}
Example #15
0
int main (void)
{
	printf ("lightweight zeromq message hub\n");

	// exit on SIGINT
	signal (SIGINT, onSignal);

	KeySet * config = ksNew (2, KS_END);

	Key * parentKey = keyNew ("/sw/elektra/hub-zeromq/#0/current", KEY_END);
	Key * configXSubEndpoint = keyDup (parentKey);
	keyAddBaseName (configXSubEndpoint, "bind_xsub");
	Key * configXPubEndpoint = keyDup (parentKey);
	keyAddBaseName (configXPubEndpoint, "bind_xpub");
	KDB * kdb = kdbOpen (parentKey);
	if (kdb == NULL)
	{
		printf ("could not open KDB. aborting\n");
		return -1;
	}

	const char * xSubEndpoint = "tcp://127.0.0.1:6000";
	const char * xPubEndpoint = "tcp://127.0.0.1:6001";
	kdbGet (kdb, config, parentKey);
	Key * xSubEndpointKey = ksLookup (config, configXSubEndpoint, 0);
	if (xSubEndpointKey)
	{
		xSubEndpoint = keyString (xSubEndpointKey);
	}
	Key * xPubEndpointKey = ksLookup (config, configXPubEndpoint, 0);
	if (xPubEndpointKey)
	{
		xPubEndpoint = keyString (xPubEndpointKey);
	}

	keyDel (configXSubEndpoint);
	keyDel (configXPubEndpoint);
	kdbClose (kdb, parentKey);
	keyDel (parentKey);

	context = zmq_ctx_new ();
	xSubSocket = zmq_socket (context, ZMQ_XSUB);
	xPubSocket = zmq_socket (context, ZMQ_XPUB);
	int result;
	result = zmq_bind (xSubSocket, xSubEndpoint);
	if (result != 0)
	{
		printf ("could not bind XSUB on %s socket: %s\n", xSubEndpoint, zmq_strerror (zmq_errno ()));
		zmq_close (xSubSocket);
		zmq_close (xPubSocket);
		zmq_ctx_destroy (context);
		return -1;
	}
	result = zmq_bind (xPubSocket, xPubEndpoint);
	if (result != 0)
	{
		printf ("could not bind XPUB on %s socket: %s\n", xPubEndpoint, zmq_strerror (zmq_errno ()));
		zmq_close (xSubSocket);
		zmq_close (xPubSocket);
		zmq_ctx_destroy (context);
		return -1;
	}

	printf ("listening on %s (XSUB for zeromqsend)\n", xSubEndpoint);
	printf ("listening on %s (XPUB for zeromqrecv)\n", xPubEndpoint);
	ksDel (config);

	// forward messages between sockets
	// will return on zmq_ctx_destroy()
	zmq_proxy (xPubSocket, xSubSocket, NULL);

	return 0;
}
Example #16
0
gint gelektra_kdb_get (GElektraKdb * kdb, GElektraKeySet * returned, GElektraKey * parent)
{
	return kdbGet (kdb->handle, returned->keyset, parent->key);
}
Example #17
0
#include <sys/wait.h>

pthread_barrier_t *bar;

void * writer (void * pV_data ELEKTRA_UNUSED)
{
	Key * parent = keyNew ("user/test/race", KEY_END);
	KDB *h = kdbOpen(parent);
	char buffer[4096];
	unsigned long tid = (unsigned long) pthread_self();
	int pid = getpid();
	sprintf(buffer, "user/test/race/keys/%d/%lu", pid, tid);
	KeySet * ks = ksNew(20,
		KS_END);

	int retg = kdbGet(h, ks, parent);
	ksAppendKey(ks, 
		keyNew (buffer, KEY_VALUE, "a value", KEY_END));

	pthread_barrier_wait(bar);
	int rets = kdbSet (h, ks, parent);

	if (rets != -1)
	{
		int retg2 = kdbGet(h, ks, parent);
		printf ("I (%d/%lu) won the race! Got return values from first get %d,"
			" from set %d, from second get %d\n",
			pid, tid, retg, rets, retg2);
		ksRewind(ks);
		/*
		Key * c;