$darkmode
Elektra 0.10.0
Enumerations | Functions
kdbmerge.h File Reference

Kdb merge tool. More...

#include "kdb.h"
#include "kdbtypes.h"
Include dependency graph for kdbmerge.h:

Enumerations

enum  MergeStrategy { MERGE_STRATEGY_ABORT = 1 , MERGE_STRATEGY_OUR = 3 , MERGE_STRATEGY_THEIR = 4 }
 The strategy to use when a merge conflict occurs. More...
 

Functions

KeySet * elektraMerge (KeySet *our, Key *ourRoot, KeySet *their, Key *theirRoot, KeySet *base, Key *baseRoot, Key *resultKey, enum MergeStrategy strategy, Key *informationKey)
 This function can incorporate changes from two modified versions (our and their) into a common preceding version (base) of a key set. More...
 
int elektraMergeGetConflicts (Key *informationKey)
 This function returns the number of conflicts that is store in the key. More...
 
KeySet * elektraMergeGetConflictingKeys (Key *informationKey, Key *root)
 Returns a keyset with all conflicting keys under the specified root. More...
 
bool elektraMergeIsKeyConflicting (Key *informationKey, Key *root, Key *key)
 Check whether the given key was part of a conflict. More...
 

Detailed Description

Kdb merge tool.

Enumeration Type Documentation

◆ MergeStrategy

The strategy to use when a merge conflict occurs.

Enumerator
MERGE_STRATEGY_ABORT 

Abort merging if a conflict occurs

MERGE_STRATEGY_OUR 

Prefer our keys in case of conflict

MERGE_STRATEGY_THEIR 

Prefer their keys in case of conflict

Function Documentation

◆ elektraMerge()

KeySet* elektraMerge ( KeySet *  our,
Key *  ourRoot,
KeySet *  their,
Key *  theirRoot,
KeySet *  base,
Key *  baseRoot,
Key *  resultRoot,
enum MergeStrategy  strategy,
Key *  informationKey 
)

This function can incorporate changes from two modified versions (our and their) into a common preceding version (base) of a key set.

This lets you merge the sets of changes represented by the two newer key sets. This is called a three-way merge between key sets.

#include <kdb.h>
#include <kdbmerge.h>
#include <stdio.h>
static void print_results (KeySet * result, Key * resultRoot, Key * informationKey)
{
KeySet * conflictingKeys = elektraMergeGetConflictingKeys (informationKey, resultRoot);
for (elektraCursor i = 0; i < ksGetSize (conflictingKeys); i++)
{
printf ("Conflict in key %s\n", keyName (ksAtCursor (conflictingKeys, i)));
}
printf ("Result:\n");
for (elektraCursor i = 0; i < ksGetSize (result); i++)
{
Key * k = ksAtCursor (result, i);
printf ("%s = %s\n", keyName (k), keyString (k));
}
if (result == NULL)
{
printf ("Aborted.\n");
}
ksDel (conflictingKeys);
}
int main (void)
{
Key * baseRoot = keyNew ("user:/screen", KEY_END);
KeySet * base = ksNew (1, keyNew ("user:/screen/background", KEY_VALUE, "blue", KEY_END), KS_END);
Key * theirRoot = keyDup (baseRoot, KEY_CP_ALL);
KeySet * their = ksNew (2, keyNew ("user:/screen/background", KEY_VALUE, "red", KEY_END),
keyNew ("user:/screen/brightness", KEY_VALUE, "100", KEY_END), KS_END);
Key * ourRoot = keyDup (baseRoot, KEY_CP_ALL);
KeySet * our = ksNew (2, keyNew ("user:/screen/background", KEY_VALUE, "purple", KEY_END),
keyNew ("user:/screen/standby", KEY_VALUE, "on", KEY_END), KS_END);
Key * resultRoot = keyDup (baseRoot, KEY_CP_ALL);
Key * informationKey = keyDup (baseRoot, KEY_CP_ALL);
// Strategy: Abort
// Will abort due to user:/screen/background changes in both their and our
printf ("Trying merge strategy ABORT\n");
KeySet * result = elektraMerge (our, ourRoot, their, theirRoot, base, baseRoot, resultRoot, MERGE_STRATEGY_ABORT, informationKey);
print_results (result, resultRoot, informationKey);
ksDel (result);
// Strategy: Our
// Will take value of our for user:/screen/background
printf ("\nTrying merge strategy OUR\n");
result = elektraMerge (our, ourRoot, their, theirRoot, base, baseRoot, resultRoot, MERGE_STRATEGY_OUR, informationKey);
print_results (result, resultRoot, informationKey);
ksDel (result);
// Strategy: Their
// Will take value of their for user:/screen/background
printf ("\nTrying merge strategy THEIR\n");
result = elektraMerge (our, ourRoot, their, theirRoot, base, baseRoot, resultRoot, MERGE_STRATEGY_THEIR, informationKey);
print_results (result, resultRoot, informationKey);
ksDel (result);
ksDel (base);
ksDel (their);
ksDel (our);
keyDel (baseRoot);
keyDel (theirRoot);
keyDel (ourRoot);
keyDel (informationKey);
keyDel (resultRoot);
return 0;
}
int keyDel(Key *key)
A destructor for Key objects.
Definition: key.c:459
Key * keyNew(const char *name,...)
A practical way to fully create a Key object in one step.
Definition: key.c:144
@ KEY_END
Definition: kdbenum.c:95
@ KEY_VALUE
Definition: kdbenum.c:88
@ KEY_CP_ALL
Definition: kdbenum.c:110
const char * keyName(const Key *key)
Returns a pointer to the abbreviated real internal key name.
Definition: elektra/keyname.c:432
int ksDel(KeySet *ks)
A destructor for KeySet objects.
Definition: keyset.c:521
KeySet * ksNew(size_t alloc,...)
Allocate, initialize and return a new KeySet object.
Definition: keyset.c:282
ssize_t ksGetSize(const KeySet *ks)
Return the number of Keys that ks contains.
Definition: keyset.c:791
#define KS_END
End of a list of keys.
Definition: kdbenum.c:156
Key * ksAtCursor(const KeySet *ks, elektraCursor pos)
Return Key at given position pos.
Definition: keyset.c:1978
const char * keyString(const Key *key)
Get a pointer to the c-string representing the value.
Definition: keyvalue.c:208
Kdb merge tool.
@ MERGE_STRATEGY_OUR
Definition: kdbmerge.h:28
@ MERGE_STRATEGY_THEIR
Definition: kdbmerge.h:29
@ MERGE_STRATEGY_ABORT
Definition: kdbmerge.h:26
KeySet * elektraMerge(KeySet *our, Key *ourRoot, KeySet *their, Key *theirRoot, KeySet *base, Key *baseRoot, Key *resultKey, enum MergeStrategy strategy, Key *informationKey)
This function can incorporate changes from two modified versions (our and their) into a common preced...
Definition: kdbmerge.c:1177
KeySet * elektraMergeGetConflictingKeys(Key *informationKey, Key *root)
Returns a keyset with all conflicting keys under the specified root.
Definition: kdbmerge.c:318
int main(int argc, char **argv)
[kdbio testsuite main]
Definition: testio_doc.c:48

Join three key sets together

Parameters
ourour key set
ourRootkey that has the root of our as name
theirtheir key set
theirRootkey that has the root of their as name
basebase key set
baseRootkey that has the root of base as name
resultRootthe name of this key determines where the resulting key set will be stored
strategyspecify which merge strategy to choose in case of a conflict
informationKeystores errors as well as statistics
Returns
the merged key set and NULL on error

◆ elektraMergeGetConflictingKeys()

KeySet* elektraMergeGetConflictingKeys ( Key *  informationKey,
Key *  root 
)

Returns a keyset with all conflicting keys under the specified root.

#include <kdb.h>
#include <kdbmerge.h>
#include <stdio.h>
static void print_results (KeySet * result, Key * resultRoot, Key * informationKey)
{
KeySet * conflictingKeys = elektraMergeGetConflictingKeys (informationKey, resultRoot);
for (elektraCursor i = 0; i < ksGetSize (conflictingKeys); i++)
{
printf ("Conflict in key %s\n", keyName (ksAtCursor (conflictingKeys, i)));
}
printf ("Result:\n");
for (elektraCursor i = 0; i < ksGetSize (result); i++)
{
Key * k = ksAtCursor (result, i);
printf ("%s = %s\n", keyName (k), keyString (k));
}
if (result == NULL)
{
printf ("Aborted.\n");
}
ksDel (conflictingKeys);
}
int main (void)
{
Key * baseRoot = keyNew ("user:/screen", KEY_END);
KeySet * base = ksNew (1, keyNew ("user:/screen/background", KEY_VALUE, "blue", KEY_END), KS_END);
Key * theirRoot = keyDup (baseRoot, KEY_CP_ALL);
KeySet * their = ksNew (2, keyNew ("user:/screen/background", KEY_VALUE, "red", KEY_END),
keyNew ("user:/screen/brightness", KEY_VALUE, "100", KEY_END), KS_END);
Key * ourRoot = keyDup (baseRoot, KEY_CP_ALL);
KeySet * our = ksNew (2, keyNew ("user:/screen/background", KEY_VALUE, "purple", KEY_END),
keyNew ("user:/screen/standby", KEY_VALUE, "on", KEY_END), KS_END);
Key * resultRoot = keyDup (baseRoot, KEY_CP_ALL);
Key * informationKey = keyDup (baseRoot, KEY_CP_ALL);
// Strategy: Abort
// Will abort due to user:/screen/background changes in both their and our
printf ("Trying merge strategy ABORT\n");
KeySet * result = elektraMerge (our, ourRoot, their, theirRoot, base, baseRoot, resultRoot, MERGE_STRATEGY_ABORT, informationKey);
print_results (result, resultRoot, informationKey);
ksDel (result);
// Strategy: Our
// Will take value of our for user:/screen/background
printf ("\nTrying merge strategy OUR\n");
result = elektraMerge (our, ourRoot, their, theirRoot, base, baseRoot, resultRoot, MERGE_STRATEGY_OUR, informationKey);
print_results (result, resultRoot, informationKey);
ksDel (result);
// Strategy: Their
// Will take value of their for user:/screen/background
printf ("\nTrying merge strategy THEIR\n");
result = elektraMerge (our, ourRoot, their, theirRoot, base, baseRoot, resultRoot, MERGE_STRATEGY_THEIR, informationKey);
print_results (result, resultRoot, informationKey);
ksDel (result);
ksDel (base);
ksDel (their);
ksDel (our);
keyDel (baseRoot);
keyDel (theirRoot);
keyDel (ourRoot);
keyDel (informationKey);
keyDel (resultRoot);
return 0;
}
Parameters
informationKeystores errors as well as statistics
rootThe root key (either for base, theirs, ours or result) that was used in the merge
Returns
KeySet containing all keys that are part of a merge conflict. Will be empty if the specified root key was not part of the merge.

◆ elektraMergeGetConflicts()

int elektraMergeGetConflicts ( Key *  informationKey)

This function returns the number of conflicts that is store in the key.

Parameters
informationKeycontains the statistics in its meta information
Returns
the number of conflicts stored in the key

◆ elektraMergeIsKeyConflicting()

bool elektraMergeIsKeyConflicting ( Key *  informationKey,
Key *  root,
Key *  key 
)

Check whether the given key was part of a conflict.

NOTE: Even if the conflict was resolved via the provided conflict strategy, the key will still be marked as being part of a conflict.

Parameters
informationKeystores errors as well as statistics
rootThe root key (either for base, theirs, ours or result) that was used in the merge
keyThat key that should be checked. Must be located under the specified root key
Return values
1if the key was part of a conflict
0if the key was not part of a conflict or the given root was not part of the merge