Elektra  0.8.10
Enumerations | Functions
KeySet

Methods to manipulate KeySets. More...

Enumerations

enum  option_t {
  KDB_O_NONE =0, KDB_O_DEL =1, KDB_O_POP =1<<1, KDB_O_NODIR =1<<2,
  KDB_O_DIRONLY =1<<3, KDB_O_NOREMOVE =1<<6, KDB_O_REMOVEONLY =1<<7, KDB_O_INACTIVE =1<<8,
  KDB_O_SYNC =1<<9, KDB_O_SORT =1<<10, KDB_O_NORECURSIVE =1<<11, KDB_O_NOCASE =1<<12,
  KDB_O_WITHOWNER =1<<13, KDB_O_NOALL =1<<14
}
 

Functions

KeySet * ksNew (size_t alloc,...)
 
KeySet * ksVNew (size_t alloc, va_list va)
 
 
KeySet * ksDup (const KeySet *source)
 
int ksCopy (KeySet *dest, const KeySet *source)
 
 
int ksDel (KeySet *ks)
 
int elektraKeyCmpOrder (const Key *ka, const Key *kb)
 
ssize_t ksGetSize (const KeySet *ks)
 
ssize_t ksAppendKey (KeySet *ks, Key *toAppend)
 
 
ssize_t ksAppend (KeySet *ks, const KeySet *toAppend)
 
 
KeySet * ksCut (KeySet *ks, const Key *cutpoint)
 
Key * ksPop (KeySet *ks)
 
int elektraKsToMemArray (KeySet *ks, Key **buffer)
 
int ksRewind (KeySet *ks)
 
Key * ksNext (KeySet *ks)
 
Key * ksCurrent (const KeySet *ks)
 
Key * ksHead (const KeySet *ks)
 
Key * ksTail (const KeySet *ks)
 
cursor_t ksGetCursor (const KeySet *ks)
 
Key * ksAtCursor (KeySet *ks, cursor_t pos)
 return key at given cursor position
 
int elektraKsFilter (KeySet *result, KeySet *input, int(*filter)(const Key *k, void *argument), void *argument)
 return only those keys from the given keyset that pass the supplied filter function with the supplied argument
 
int ksSetCursor (KeySet *ks, cursor_t cursor)
 
Key * ksLookup (KeySet *ks, Key *key, option_t options)
 
Key * ksLookupByName (KeySet *ks, const char *name, option_t options)
 

Detailed Description

Methods to manipulate KeySets.

A KeySet is a sorted set of keys. So the correct name actually would be KeyMap.

With ksNew() you can create a new KeySet.

You can add keys with ksAppendKey() in the keyset. Using ksAppend() you can append a whole keyset.

Note
Because the key is not copied, also the pointer to the current metadata keyNextMeta() will be shared.

ksGetSize() tells you the current size of the keyset.

With ksRewind() and ksNext() you can navigate through the keyset. Don't expect any particular order, but it is assured that you will get every key of the set.

KeySets have an internal cursor . This is used for ksLookup() and kdbSet().

KeySet has a fundamental meaning inside elektra. It makes it possible to get and store many keys at once inside the database. In addition to that the class can be used as high level datastructure in applications. With ksLookupByName() it is possible to fetch easily specific keys out of the list of keys.

You can easily create and iterate keys:

#include <kdb.h>
// create a new keyset with 3 keys
// with a hint that about 20 keys will be inside
KeySet *myConfig = ksNew(20,
keyNew ("user/name1", 0),
keyNew ("user/name2", 0),
keyNew ("user/name3", 0),
KS_END);
// append a key in the keyset
ksAppendKey(myConfig, keyNew("user/name4", 0));
Key *current;
ksRewind(myConfig);
while ((current=ksNext(myConfig))!=0)
{
printf("Key name is %s.\n", keyName (current));
}
ksDel (myConfig); // delete keyset and all keys appended

Enumeration Type Documentation

enum option_t

Options to change the default behavior of ksLookup() functions.

These options can be ORed. That is the |-Operator in C.

See Also
kdbGet(), kdbSet()
Enumerator:
KDB_O_NONE 

No Option set. Will be recursive with no inactive keys.

See Also
kdbGet(), kdbSet(), ksLookup()
KDB_O_DEL 

Delete parentKey key in kdbGet(), kdbSet() or ksLookup().

See Also
kdbGet(), kdbSet()
KDB_O_POP 

Pop Parent out of keyset key in kdbGet().

See Also
ksPop().
KDB_O_NODIR 

Exclude keys containing other keys in result.

Only return leaves.

See Also
keyIsDir()
KDB_O_DIRONLY 

Retrieve only directory keys (keys containing other keys). This will give you an skeleton without leaves. This must not be used together with KDB_O_NODIR.

See Also
keyIsDir()
KDB_O_NOREMOVE 

Don't remove any keys. This must not be used together with KDB_O_REMOVEONLY.

KDB_O_REMOVEONLY 

Only remove keys. This must not be used together with KDB_O_NOREMOVE.

KDB_O_INACTIVE 

Do not ignore inactive keys (that name begins with .).

See Also
keyIsInactive()
KDB_O_SYNC 

Set keys independent of sync status.

See Also
keyNeedSync()
KDB_O_SORT 

This option has no effect. KeySets are always sorted.

Deprecated:
don't use
KDB_O_NORECURSIVE 

Do not call kdbGet() for every key containing other keys (keyIsDir()).

KDB_O_NOCASE 

Ignore case.

KDB_O_WITHOWNER 

Search with owner.

KDB_O_NOALL 

Only search from start -> cursor to cursor -> end.

Function Documentation

int elektraKeyCmpOrder ( const Key *  ka,
const Key *  kb 
)

Compare the order metadata of two keys.

Returns
a number less than, equal to or greater than zero if the order of k1 is found, respectively, to be less than, to match, or be greater than the order of k2. If one key is NULL, but the other isn't, the key which is not NULL is considered to be greater. If both keys are NULL, they are considered to be equal. If one key does have an order metadata but the other has not, the key with the metadata is considered greater. If no key has metadata, they are considered to be equal.
Parameters
kakey to compare with
kbother key to compare with
int elektraKsFilter ( KeySet *  result,
KeySet *  input,
int(*)(const Key *k, void *argument)  filter,
void *  argument 
)

return only those keys from the given keyset that pass the supplied filter function with the supplied argument

Parameters
resultthe keyset that should contain the filtered keys
inputthe keyset whose keys should be filtered
filtera function pointer to a function that will be used to filter the keyset. A key will be taken if the function returns a value greater than 0.
argumentan argument that will be passed to the filter function each time it is called
Returns
the number of filtered keys if the filter function always returned a positive value, -1 otherwise
Return values
NULLon NULL pointer
int elektraKsToMemArray ( KeySet *  ks,
Key **  buffer 
)

Builds an array of pointers to the keys in the supplied keyset. The keys are not copied, calling keyDel may remove them from the keyset.

The size of the buffer can be easily allocated via ksGetSize. Example:

KeySet *ks = somekeyset;
Key **keyArray = calloc (ksGetSize(ks), sizeof (Key *));
elektraKsToMemArray (ks, keyArray);
... work with the array ...
free (keyArray);
Parameters
ksthe keyset object to work with
bufferthe buffer to put the result into
Returns
the number of elements in the array if successful
a negative number on null pointers or if an error occurred
ssize_t ksAppend ( KeySet *  ks,
const KeySet *  toAppend 
)

Append all toAppend contained keys to the end of the ks.

toAppend KeySet will be left unchanged.

If a key is both in toAppend and ks, the Key in ks will be overridden.

Note
Because the key is not copied, also the pointer to the current metadata keyNextMeta() will be shared.

Postcondition
Sorted KeySet ks with all keys it had before and additionally the keys from toAppend
Returns
the size of the KeySet after transfer
-1 on NULL pointers
Parameters
ksthe KeySet that will receive the keys
toAppendthe KeySet that provides the keys that will be transferred
See Also
ksAppendKey()
ssize_t ksAppendKey ( KeySet *  ks,
Key *  toAppend 
)

Appends a Key to the end of ks.

A pointer to the key will be stored, and not a private copy. So a future ksDel() on ks may keyDel() the toAppend object, see keyGetRef().

The reference counter of the key will be incremented, and thus toAppend is not const.

Note
Because the key is not copied, also the pointer to the current metadata keyNextMeta() will be shared.

If the keyname already existed, it will be replaced with the new key.

The KeySet internal cursor will be set to the new key.

It is save to use ksAppendKey(ks, keyNew(..)).

Returns
the size of the KeySet after insertion
-1 on NULL pointers
-1 if insertion failed, the key will be deleted then.
Parameters
ksKeySet that will receive the key
toAppendKey that will be appended to ks or deleted
See Also
ksAppend(), keyNew(), ksDel()
keyIncRef()
Key* ksAtCursor ( KeySet *  ks,
cursor_t  pos 
)

return key at given cursor position

Parameters
ksthe keyset to pop key from
poswhere to get
Returns
the key at the cursor position on success
Return values
NULLon NULL pointer, negative cursor position or a position that does not lie within the keyset
int ksCopy ( KeySet *  dest,
const KeySet *  source 
)

Copy a keyset.

Most often you may want a duplicate of a keyset, see ksDup() or append keys, see ksAppend(). But in some situations you need to copy a keyset to a existing keyset, for that this function exists.

You can also use it to clear a keyset when you pass a NULL pointer as source.

Note that all keys in dest will be deleted. Afterwards the content of the source will be added to the destination and the ksCurrent() is set properly in dest.

A flat copy is made, so the keys will not be duplicated, but there reference counter is updated, so both keysets need to be ksDel().

Note
Because the key is not copied, also the pointer to the current metadata keyNextMeta() will be shared.

int f (KeySet *ks)
{
KeySet *c = ksNew (20, ..., KS_END);
// c receives keys
ksCopy (ks, c); // pass the keyset to the caller
ksDel (c);
} // caller needs to ksDel (ks)
Parameters
sourcehas to be an initialized source KeySet or NULL
desthas to be an initialized KeySet where to write the keys
Returns
1 on success
0 if dest was cleared successfully (source is NULL)
-1 on NULL pointer
See Also
ksNew(), ksDel(), ksDup()
keyCopy() for copying keys
Key* ksCurrent ( const KeySet *  ks)

Return the current Key.

The pointer is NULL if you reached the end or after ksRewind().

Note
You must not delete the key or change the key, use ksPop() if you want to delete it.
Parameters
ksthe keyset object to work with
Returns
pointer to the Key pointed by ks's cursor
0 on NULL pointer
See Also
ksNext(), ksRewind()
KeySet* ksCut ( KeySet *  ks,
const Key *  cutpoint 
)

Cuts out a keyset at the cutpoint.

Searches for the cutpoint inside the KeySet ks. If found it cuts out everything which is below (see keyIsBelow()) this key. These keys will be missing in the keyset ks. Instead, they will be moved to the returned keyset. If cutpoint is not found an empty keyset is returned and ks is not changed.

The cursor will stay at the same key as it was before. If the cursor was inside the region of cut (moved) keys, the cursor will be set to the key before the cutpoint.

If you use ksCut() on a keyset you got from kdbGet() and plan to make a kdbSet() later, make sure that you keep all keys that should not be removed permanently. You have to keep the KeySet that was returned and the KeySet ks.

Example:

You have the keyset ks:

  • system/mountpoint/interest
  • system/mountpoint/interest/folder
  • system/mountpoint/interest/folder/key1
  • system/mountpoint/interest/folder/key2
  • system/mountpoint/other/key1

When you use

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);

Then in returned are:

  • system/mountpoint/interest
  • system/mountpoint/interest/folder
  • system/mountpoint/interest/folder/key1
  • system/mountpoint/interest/folder/key2

And in ks are:

  • system/mountpoint/other/key1

So kdbSet() permanently removes all keys below system/mountpoint/interest.

See Also
kdbGet() for explanation why you might get more keys than you requested.
Returns
a new allocated KeySet which needs to deleted with ksDel(). The keyset consists of all keys (of the original keyset ks) below the cutpoint. If the key cutpoint exists, it will also be appended.
Return values
0on null pointers, no key name or allocation problems
Parameters
ksthe keyset to cut. It will be modified by removing all keys below the cutpoint. The cutpoint itself will also be removed.
cutpointthe point where to cut out the keyset
int ksDel ( KeySet *  ks)

A destructor for KeySet objects.

Cleans all internal dynamic attributes, decrement all reference pointers to all keys and then keyDel() all contained Keys, and free()s the release the KeySet object memory (that was previously allocated by ksNew()).

Parameters
ksthe keyset object to work with
Returns
0 when the keyset was freed
-1 on null pointer
See Also
ksNew()
KeySet* ksDup ( const KeySet *  source)

Return a duplicate of a keyset.

Objects created with ksDup() must be destroyed with ksDel().

Memory will be allocated as needed for dynamic properties, so you need to ksDel() the returned pointer.

A flat copy is made, so the keys will not be duplicated, but there reference counter is updated, so both keysets need ksDel().

Parameters
sourcehas to be an initialized source KeySet
Returns
a flat copy of source on success
0 on NULL pointer
See Also
ksNew(), ksDel()
keyDup() for Key duplication
cursor_t ksGetCursor ( const KeySet *  ks)

Get the KeySet internal cursor.

Use it to get the cursor of the actual position.

Warning
Cursors are getting invalid when the key was ksPop()ed or ksLookup() with KDB_O_POP was used.

Read ahead

With the cursors it is possible to read ahead in a keyset:

cursor_t jump;
ksRewind (ks);
while ((key = keyNextMeta (ks))!=0)
{
// now mark this key
jump = ksGetCursor(ks);
//code..
keyNextMeta (ks); // now browse on
// use ksCurrent(ks) to check the keys
//code..
// jump back to the position marked before
ksSetCursor(ks, jump);
}

Restoring state

It can also be used to restore the state of a keyset in a function

int f (KeySet *ks)
{
cursor_t state = ksGetCursor(ks);
// work with keyset
// now bring the keyset to the state before
ksSetCursor (ks, state);
}

It is of course possible to make the KeySet const and cast its const away to set the cursor. Another way to achieve the same is to ksDup() the keyset, but it is not as efficient.

An invalid cursor will be returned directly after ksRewind(). When you set an invalid cursor ksCurrent() is 0 and ksNext() == ksHead().

Note
Only use a cursor for the same keyset which it was made for.
Parameters
ksthe keyset object to work with
Returns
a valid cursor on success
an invalid cursor on NULL pointer or after ksRewind()
See Also
ksNext(), ksSetCursor()
ssize_t ksGetSize ( const KeySet *  ks)

Return the number of keys that ks contains.

Parameters
ksthe keyset object to work with
Returns
the number of keys that ks contains.
-1 on NULL pointer
See Also
ksNew(0, KS_END), ksDel()
Key* ksHead ( const KeySet *  ks)

Return the first key in the KeySet.

The KeySets cursor will not be effected.

If ksCurrent()==ksHead() you know you are on the first key.

Parameters
ksthe keyset object to work with
Returns
the first Key of a keyset
0 on NULL pointer or empty keyset
See Also
ksTail() for the last Key
ksRewind(), ksCurrent() and ksNext() for iterating over the KeySet
Key* ksLookup ( KeySet *  ks,
Key *  key,
option_t  options 
)

Look for a Key contained in ks that matches the name of the key.

Introduction

ksLookup() is designed to let you work with entirely pre-loaded KeySets, so instead of kdbGetKey(), key by key, the idea is to fully kdbGet() for your application root key and process it all at once with ksLookup().

This function is very efficient by using binary search. Together with kdbGet() which can you load the whole configuration with only some communication to backends you can write very effective but short code for configuration.

Usage

If found, ks internal cursor will be positioned in the matched key (also accessible by ksCurrent()), and a pointer to the Key is returned. If not found, ks internal cursor will not move, and a NULL pointer is returned.

Cascading is done if the first character is a /. This leads to ignoring the prefix like system/ and user/.

if (kdbGet(handle, "user/myapp", myConfig, 0 ) == -1)
errorHandler ("Could not get Keys");
if (kdbGet(handle, "system/myapp", myConfig, 0 ) == -1)
errorHandler ("Could not get Keys");
if ((myKey = ksLookup(myConfig, key, 0)) == NULL)
errorHandler ("Could not Lookup Key");

This is the way multi user Programs should get there configuration and search after the values. It is guaranteed that more namespaces can be added easily and that all values can be set by admin and user.

KDB_O_NOALL

When KDB_O_NOALL is set the keyset will be only searched from ksCurrent() to ksTail(). You need to ksRewind() the keyset yourself. ksCurrent() is always set properly after searching a key, so you can go on searching another key after the found key.

When KDB_O_NOALL is not set the cursor will stay untouched and all keys are considered. A much more efficient binary search will be used then.

KDB_O_POP

When KDB_O_POP is set the key which was found will be ksPop()ed. ksCurrent() will not be changed, only iff ksCurrent() is the searched key, then the keyset will be ksRewind()ed.

Note
Like in ksPop() the popped key always needs to be keyDel() afterwards, even if it is appended to another keyset.
Warning
All cursors on the keyset will be invalid iff you use KDB_O_POP, so don't use this if you rely on a cursor, see ksGetCursor().

You can solve this problem by using KDB_O_NOALL, risking you have to iterate n^2 instead of n.

The more elegant way is to separate the keyset you use for ksLookup() and ksAppendKey():

int f(KeySet *iterator, KeySet *lookup)
{
KeySet *append = ksNew (ksGetSize(lookup), KS_END);
Key *key;
Key *current;
ksRewind(iterator);
while (current=ksNext(iterator))
{
key = ksLookup (lookup, current, KDB_O_POP);
// do something...
ksAppendKey(append, key); // now append it to append, not lookup!
keyDel (key); // make sure to ALWAYS delete poped keys.
}
ksAppend(lookup, append);
// now lookup needs to be sorted only once, append never
ksDel (append);
}
Parameters
kswhere to look for
keythe key object you are looking for
optionssome KDB_O_* option bits:
  • KDB_O_NOCASE
    Lookup ignoring case.
  • KDB_O_WITHOWNER
    Also consider correct owner.
  • KDB_O_NOALL
    Only search from ksCurrent() to end of keyset, see above text.
  • KDB_O_POP
    Pop the key which was found.
  • KDB_O_DEL
    Delete the passed key.
Returns
pointer to the Key found, 0 otherwise
0 on NULL pointers
See Also
ksLookupByName() to search by a name given by a string
ksCurrent(), ksRewind(), ksNext() for iterating over a KeySet
Key* ksLookupByName ( KeySet *  ks,
const char *  name,
option_t  options 
)

Convenience method to look for a Key contained in ks that matches name.

ksLookupByName() is designed to let you work with entirely pre-loaded KeySets, so instead of kdbGetKey(), key by key, the idea is to fully kdbGetByName() for your application root key and process it all at once with ksLookupByName().

This function is very efficient by using binary search. Together with kdbGetByName() which can you load the whole configuration with only some communication to backends you can write very effective but short code for configuration.

If found, ks internal cursor will be positioned in the matched key (also accessible by ksCurrent()), and a pointer to the Key is returned. If not found, ks internal cursor will not move, and a NULL pointer is returned. If requested to pop the key, the cursor will be rewinded.

Cascading

Cascading is done if the first character is a /. This leads to ignoring the prefix like system/ and user/.

if (kdbGet(handle, "user/sw/myapp/current", myConfig, parentKey ) == -1)
errorHandler ("Could not get Keys", parentKey);
if (kdbGet(handle, "system/sw/myapp/current", myConfig, parentKey ) == -1)
errorHandler ("Could not get Keys", parentKey);
if ((myKey = ksLookupByName (myConfig, "/myapp/current/key", 0)) == NULL)
errorHandler ("Could not Lookup Key");

This is the way multi user Programs should get there configuration and search after the values. It is guaranteed that more namespaces can be added easily and that all values can be set by admin and user.

It is up to the application to implement a sophisticated cascading algorithm, for e.g. a list of profiles (specific, group and fallback):

if ((myKey = ksLookupByName (myConfig, "/myapp/current/specific/key", 0)) == NULL)
if ((myKey = ksLookupByName (myConfig, "/myapp/current/group/key", 0)) == NULL)
if ((myKey = ksLookupByName (myConfig, "/myapp/current/fallback/key", 0)) == NULL)
errorHandler ("All fallbacks failed to lookup key");

Note that for every profile both the user and the system key are searched. The first key found will be used.

Full Search

When KDB_O_NOALL is set the keyset will be only searched from ksCurrent() to ksTail(). You need to ksRewind() the keyset yourself. ksCurrent() is always set properly after searching a key, so you can go on searching another key after the found key.

When KDB_O_NOALL is not set the cursor will stay untouched and all keys are considered. A much more efficient binary search will be used then.

Parameters
kswhere to look for
namekey name you are looking for
optionssome KDB_O_* option bits:
  • KDB_O_NOCASE
    Lookup ignoring case.
  • KDB_O_WITHOWNER
    Also consider correct owner.
  • KDB_O_NOALL
    Only search from ksCurrent() to end of keyset, see above text.
  • KDB_O_POP
    Pop the key which was found.

Currently no options supported.

Returns
pointer to the Key found, 0 otherwise
0 on NULL pointers
See Also
keyCompare() for very powerful Key lookups in KeySets
ksCurrent(), ksRewind(), ksNext()
KeySet* ksNew ( size_t  alloc,
  ... 
)

Allocate, initialize and return a new KeySet object.

Objects created with ksNew() must be destroyed with ksDel().

You can use a various long list of parameters to preload the keyset with a list of keys. Either your first and only parameter is 0 or your last parameter must be KEY_END.

So, terminate with ksNew(0, KS_END) or ksNew(20, ..., KS_END)

For most uses

KeySet *keys = ksNew(0, KS_END);
// work with it
ksDel (keys);

goes ok, the alloc size will be 16, defined in kdbprivate.h. The alloc size will be doubled whenever size reaches alloc size, so it also performs out large keysets.

But if you have any clue how large your keyset may be you should read the next statements.

If you want a keyset with length 15 (because you know of your application that you normally need about 12 up to 15 keys), use:

KeySet * keys = ksNew (15,
keyNew ("user/sw/app/fixedConfiguration/key01", KEY_SWITCH_VALUE, "value01", 0),
keyNew ("user/sw/app/fixedConfiguration/key02", KEY_SWITCH_VALUE, "value02", 0),
keyNew ("user/sw/app/fixedConfiguration/key03", KEY_SWITCH_VALUE, "value03", 0),
// ...
keyNew ("user/sw/app/fixedConfiguration/key15", KEY_SWITCH_VALUE, "value15", 0),
KS_END);
// work with it
ksDel (keys);

If you start having 3 keys, and your application needs approximately 200-500 keys, you can use:

KeySet * config = ksNew (500,
keyNew ("user/sw/app/fixedConfiguration/key1", KEY_SWITCH_VALUE, "value1", 0),
keyNew ("user/sw/app/fixedConfiguration/key2", KEY_SWITCH_VALUE, "value2", 0),
keyNew ("user/sw/app/fixedConfiguration/key3", KEY_SWITCH_VALUE, "value3", 0),
KS_END); // don't forget the KS_END at the end!
// work with it
ksDel (config);

Alloc size is 500, the size of the keyset will be 3 after ksNew. This means the keyset will reallocate when appending more than 497 keys.

The main benefit of taking a list of variant length parameters is to be able to have one C-Statement for any possible KeySet.

Postcondition
the keyset is rewinded properly
See Also
ksDel() to free the KeySet afterwards
ksDup() to duplicate an existing KeySet
Parameters
allocgives a hint for the size how many Keys may be stored initially
Returns
a ready to use KeySet object
0 on memory error
Key* ksNext ( KeySet *  ks)

Returns the next Key in a KeySet.

KeySets have an internal cursor that can be reset with ksRewind(). Every time ksNext() is called the cursor is incremented and the new current Key is returned.

You'll get a NULL pointer if the key after the end of the KeySet was reached. On subsequent calls of ksNext() it will still return the NULL pointer.

The ks internal cursor will be changed, so it is not const.

Note
You must not delete or change the key, use ksPop() if you want to delete it.
Parameters
ksthe keyset object to work with
Returns
the new current Key
0 when the end is reached
0 on NULL pointer
See Also
ksRewind(), ksCurrent()
Key* ksPop ( KeySet *  ks)

Remove and return the last key of ks.

The reference counter will be decremented by one.

The KeySets cursor will not be effected if it did not point to the popped key.

Note
You need to keyDel() the key afterwards, if you don't append it to another keyset. It has the same semantics like a key allocated with keyNew() or keyDup().
ks1=ksNew(0, KS_END);
ks2=ksNew(0, KS_END);
k1=keyNew("user/name", KEY_END); // ref counter 0
ksAppendKey(ks1, k1); // ref counter 1
ksAppendKey(ks2, k1); // ref counter 2
k1=ksPop (ks1); // ref counter 1
k1=ksPop (ks2); // ref counter 0, like after keyNew()
ksAppendKey(ks1, k1); // ref counter 1
ksDel (ks1); // key is deleted too
ksDel (ks2);
*
Returns
the last key of ks
NULL if ks is empty or on NULL pointer
Parameters
ksKeySet to work with
See Also
ksAppendKey(), ksAppend()
commandList() for an example
int ksRewind ( KeySet *  ks)

Rewinds the KeySet internal cursor.

Use it to set the cursor to the beginning of the KeySet. ksCurrent() will then always return NULL afterwards. So you want to ksNext() first.

ksRewind (ks);
while ((key = ksNext (ks))!=0) {}
Parameters
ksthe keyset object to work with
Returns
0 on success
-1 on NULL pointer
See Also
ksNext(), ksCurrent()
int ksSetCursor ( KeySet *  ks,
cursor_t  cursor 
)

Set the KeySet internal cursor.

Use it to set the cursor to a stored position. ksCurrent() will then be the position which you got with.

Warning
Cursors may get invalid when the key was ksPop()ed or ksLookup() was used together with KDB_O_POP.
cursor_t cursor;
..
// key now in any position here
cursor = ksGetCursor (ks);
while ((key = keyNextMeta (ks))!=0) {}
ksSetCursor (ks, cursor); // reset state
ksCurrent(ks); // in same position as before

An invalid cursor will set the keyset to its beginning like ksRewind(). When you set an invalid cursor ksCurrent() is 0 and ksNext() == ksHead().

Parameters
cursorthe cursor to use
ksthe keyset object to work with
Returns
0 when the keyset is ksRewind()ed
1 otherwise
-1 on NULL pointer
See Also
ksNext(), ksGetCursor()
Key* ksTail ( const KeySet *  ks)

Return the last key in the KeySet.

The KeySets cursor will not be effected.

If ksCurrent()==ksTail() you know you are on the last key. ksNext() will return a NULL pointer afterwards.

Parameters
ksthe keyset object to work with
Returns
the last Key of a keyset
0 on NULL pointer or empty keyset
See Also
ksHead() for the first Key
ksRewind(), ksCurrent() and ksNext() for iterating over the KeySet
KeySet* ksVNew ( size_t  alloc,
va_list  va 
)

Allocate, initialize and return a new KeySet object.Objects created with ksNew() must be destroyed with ksDel().You can use a various long list of parameters to preload the keyset with a list of keys. Either your first and only parameter is 0 or your last parameter must be KEY_END.So, terminate with ksNew(0, KS_END) or ksNew(20, ..., KS_END)For most uses

KeySet *keys = ksNew(0, KS_END);
// work with it
ksDel (keys);

goes ok, the alloc size will be 16, defined in kdbprivate.h. The alloc size will be doubled whenever size reaches alloc size, so it also performs out large keysets.But if you have any clue how large your keyset may be you should read the next statements.If you want a keyset with length 15 (because you know of your application that you normally need about 12 up to 15 keys), use:

KeySet * keys = ksNew (15,
keyNew ("user/sw/app/fixedConfiguration/key01", KEY_SWITCH_VALUE, "value01", 0),
keyNew ("user/sw/app/fixedConfiguration/key02", KEY_SWITCH_VALUE, "value02", 0),
keyNew ("user/sw/app/fixedConfiguration/key03", KEY_SWITCH_VALUE, "value03", 0),
// ...
keyNew ("user/sw/app/fixedConfiguration/key15", KEY_SWITCH_VALUE, "value15", 0),
KS_END);
// work with it
ksDel (keys);
If you start having 3 keys, and your application needs approximately 200-500 keys, you can use:

KeySet * config = ksNew (500,
keyNew ("user/sw/app/fixedConfiguration/key1", KEY_SWITCH_VALUE, "value1", 0),
keyNew ("user/sw/app/fixedConfiguration/key2", KEY_SWITCH_VALUE, "value2", 0),
keyNew ("user/sw/app/fixedConfiguration/key3", KEY_SWITCH_VALUE, "value3", 0),
KS_END); // don't forget the KS_END at the end!
// work with it
ksDel (config);

Alloc size is 500, the size of the keyset will be 3 after ksNew. This means the keyset will reallocate when appending more than 497 keys.The main benefit of taking a list of variant length parameters is to be able to have one C-Statement for any possible KeySet.

Postcondition
the keyset is rewinded properly
See Also
ksDel() to free the KeySet afterwards
ksDup() to duplicate an existing KeySet
Parameters
allocgives a hint for the size how many Keys may be stored initially
Returns
a ready to use KeySet object
0 on memory error

Precondition
caller must call va_start and va_end
va the list of arguments
Parameters
allocthe allocation size
vathe list of variable arguments