| Elektra 0.11.0

Enumerations | Functions
kdbmerge.h File Reference

Kdb merge tool. More...

`#include "kdb.h"`
`#include "kdbtypes.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.
int | elektraMergeGetConflicts (Key *informationKey)
| This function returns the number of conflicts that is store in the key.
KeySet * | elektraMergeGetConflictingKeys (Key *informationKey, Key *root)
| Returns a keyset with all conflicting keys under the specified root.
bool | elektraMergeIsKeyConflicting (Key *informationKey, Key *root, Key *key)
| Check whether the given key was part of a conflict.

## Detailed Description

Kdb merge tool.

Copyright
  BSD License (see LICENSE.md or https://ww.libelektra.org)

## Enumeration Type Documentation

## ◆ MergeStrategy

enum 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;
}
keyDel
int keyDel(Key *key)
A destructor for Key objects.
Definition key.c:459
keyNew
Key * keyNew(const char *name,...)
A practical way to fully create a Key object in one step.
Definition key.c:144
KEY_CP_ALL
@ KEY_CP_ALL
Definition kdbenum.c:110
keyName
const char * keyName(const Key *key)
Returns a pointer to the abbreviated real internal key name.
Definition elektra/keyname.c:429
ksDel
int ksDel(KeySet *ks)
A destructor for KeySet objects.
Definition keyset.c:521
ksGetSize
ssize_t ksGetSize(const KeySet *ks)
Return the number of Keys that ks contains.
Definition keyset.c:791
KS_END
#define KS_END
End of a list of keys.
Definition kdbenum.c:156
ksAtCursor
Key * ksAtCursor(const KeySet *ks, elektraCursor pos)
Return Key at given position pos.
Definition keyset.c:1978
ksNew
KeySet * ksNew(size_t alloc,...)
Allocate, initialize and return a new KeySet object.
Definition keyset.c:282
keyString
const char * keyString(const Key *key)
Get a pointer to the c-string representing the value.
Definition keyvalue.c:208
kdbmerge.h
Kdb merge tool.
MERGE_STRATEGY_OUR
@ MERGE_STRATEGY_OUR
Definition kdbmerge.h:28
MERGE_STRATEGY_THEIR
@ MERGE_STRATEGY_THEIR
Definition kdbmerge.h:29
MERGE_STRATEGY_ABORT
@ MERGE_STRATEGY_ABORT
Definition kdbmerge.h:26

Join three key sets together

Parameters

  our | our key set
  ourRoot | key that has the root of our as name
  their | their key set
  theirRoot | key that has the root of their as name
  base | base key set
  baseRoot | key that has the root of base as name
  resultRoot | the name of this key determines where the resulting key set will be stored
  strategy | specify which merge strategy to choose in case of a conflict
  informationKey | stores 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

  informationKey | stores errors as well as statistics
  root | The 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

  informationKey | contains 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

  informationKey | stores errors as well as statistics
  root | The root key (either for base, theirs, ours or result) that was used in the merge
  key | That key that should be checked. Must be located under the specified root key

Return values

  1 | if the key was part of a conflict
  0 | if the key was not part of a conflict or the given root was not part of the merge