-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Internal Cache #4964
base: master
Are you sure you want to change the base?
Implement Internal Cache #4964
Changes from all commits
3a4e6a6
a0ca665
8d9ba0f
771b737
52daef6
574a780
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1844,6 +1844,8 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) | |||||
return -1; | ||||||
} | ||||||
|
||||||
KeySet * fromInternalCache = ksBelow (handle->allKeys, initialParent); | ||||||
|
||||||
parentKey->hasReadOnlyName = false; | ||||||
parentKey->hasReadOnlyValue = false; | ||||||
|
||||||
|
@@ -1856,19 +1858,33 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) | |||||
// Step 5: remove up-to-date backends | ||||||
for (elektraCursor i = 0; i < ksGetSize (backends); i++) | ||||||
{ | ||||||
if (keyGetMeta (ksAtCursor (backends, i), "meta:/internal/kdbneedsupdate") == NULL) | ||||||
Key * backendKey = ksAtCursor (backends, i); | ||||||
if (keyGetMeta (backendKey, "meta:/internal/kdbneedsupdate") == NULL) | ||||||
{ | ||||||
elektraKsPopAtCursor (backends, i); | ||||||
--i; | ||||||
} | ||||||
else if (fromInternalCache != NULL) | ||||||
{ | ||||||
// Remove keys from fromInternalCache that have updated data in backends | ||||||
ksDel (ksCut (fromInternalCache, backendKey)); | ||||||
} | ||||||
} | ||||||
|
||||||
if (fromInternalCache != NULL) | ||||||
{ | ||||||
// Create a deep-duplication of the cache keys | ||||||
KeySet * t = fromInternalCache; | ||||||
fromInternalCache = ksDeepDup (fromInternalCache); | ||||||
ksDel (t); | ||||||
} | ||||||
|
||||||
bool procOnly = keyGetNamespace (ksAtCursor (backends, 0)) == KEY_NS_PROC && | ||||||
keyGetNamespace (ksAtCursor (backends, ksGetSize (backends) - 1)) == KEY_NS_PROC; | ||||||
|
||||||
// Step 6: return if no backends left | ||||||
// HACK: for gopts | ||||||
if (ksGetSize (backends) == 0 || (goptsActive && procOnly && ksGetSize (backends) == 1)) | ||||||
if (ksGetSize (fromInternalCache) == 0 && (ksGetSize (backends) == 0 || (goptsActive && procOnly && ksGetSize (backends) == 1))) | ||||||
{ | ||||||
keyCopy (parentKey, initialParent, KEY_CP_NAME | KEY_CP_VALUE); | ||||||
keyDel (initialParent); | ||||||
|
@@ -1878,6 +1894,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) | |||||
|
||||||
ksDel (backends); | ||||||
ksDel (allBackends); | ||||||
ksDel (fromInternalCache); | ||||||
errno = errnosave; | ||||||
return 2; | ||||||
} | ||||||
|
@@ -1961,13 +1978,22 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) | |||||
// Step 14: run spec plugin | ||||||
parentKey->hasReadOnlyName = true; | ||||||
parentKey->hasReadOnlyValue = true; | ||||||
if (handle->hooks.spec.plugin && handle->hooks.spec.copy (handle->hooks.spec.plugin, dataKs, parentKey, true) == -1) | ||||||
|
||||||
KeySet * keysForSpec = ksNew (ksGetSize (dataKs) + ksGetSize (fromInternalCache), KS_END); | ||||||
ksAppend (keysForSpec, dataKs); | ||||||
ksAppend (keysForSpec, fromInternalCache); | ||||||
|
||||||
if (handle->hooks.spec.plugin && handle->hooks.spec.copy (handle->hooks.spec.plugin, keysForSpec, parentKey, true) == -1) | ||||||
{ | ||||||
parentKey->hasReadOnlyName = false; | ||||||
parentKey->hasReadOnlyValue = false; | ||||||
ksDel (dataKs); | ||||||
ksDel (keysForSpec); | ||||||
goto error; | ||||||
} | ||||||
|
||||||
ksDel (keysForSpec); | ||||||
|
||||||
parentKey->hasReadOnlyName = false; | ||||||
parentKey->hasReadOnlyValue = false; | ||||||
|
||||||
|
@@ -2007,6 +2033,8 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) | |||||
// Step 18: merge data into ks and return | ||||||
ksClear (dataKs); | ||||||
backendsMerge (backends, dataKs); | ||||||
|
||||||
ksAppend (ks, fromInternalCache); | ||||||
ksAppend (ks, dataKs); | ||||||
|
||||||
// TODO (atmaxinger): should we have a default:/ backend? | ||||||
|
@@ -2041,6 +2069,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) | |||||
ksAppendDup (handle->allKeys, dataKs); | ||||||
} | ||||||
ksDel (dataKs); | ||||||
ksDel (fromInternalCache); | ||||||
|
||||||
errno = errnosave; | ||||||
return procOnly ? 2 : 1; | ||||||
|
@@ -2055,6 +2084,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) | |||||
|
||||||
ksDel (backends); | ||||||
ksDel (allBackends); | ||||||
if (fromInternalCache) ksDel (fromInternalCache); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
errno = errnosave; | ||||||
return -1; | ||||||
|
@@ -2474,7 +2504,24 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey) | |||||
{ | ||||||
Key * backendKey = ksAtCursor (backends, i); | ||||||
|
||||||
KeySet * below = ksBelow (ks, backendKey); | ||||||
KeySet * below = NULL; | ||||||
if (handle->allKeys != NULL) | ||||||
{ | ||||||
below = ksBelow (handle->allKeys, backendKey); | ||||||
ksDel (ksCut (below, initialParent)); | ||||||
|
||||||
KeySet * bBackend = ksBelow (ks, backendKey); | ||||||
KeySet * bParent = ksBelow (bBackend, initialParent); | ||||||
ksAppend (below, bParent); | ||||||
|
||||||
ksDel (bParent); | ||||||
ksDel (bBackend); | ||||||
Comment on lines
+2510
to
+2518
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might be easier with |
||||||
} | ||||||
else | ||||||
{ | ||||||
below = ksBelow (ks, backendKey); | ||||||
} | ||||||
|
||||||
KeySet * deepDupedBelow = ksDeepDup (below); | ||||||
ksAppend (setKs, deepDupedBelow); | ||||||
ksDel (below); | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* @file | ||
* | ||
* @brief Test suite for internal data structures. | ||
* | ||
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org) | ||
*/ | ||
|
||
#include "kdbhelper.h" | ||
#include "kdbprivate.h" | ||
#include <tests_internal.h> | ||
|
||
static void test_doubleGet (void) | ||
{ | ||
printf ("running %s\n", __func__); | ||
|
||
// Setup | ||
Key * parentKey = keyNew ("/somewhere", KS_END); | ||
KeySet * ks = ksNew (0, KS_END); | ||
KDB * kdb = kdbOpen (ksNew (0, KS_END), parentKey); | ||
kdbGet (kdb, ks, parentKey); | ||
ksAppendKey (ks, keyNew ("user:/somewhere", KEY_VALUE, "abc", KEY_END)); | ||
ksAppendKey (ks, keyNew ("user:/somewhere/key", KEY_VALUE, "xyz", KEY_END)); | ||
kdbSet (kdb, ks, parentKey); | ||
kdbClose (kdb, parentKey); | ||
|
||
// Scenario | ||
kdb = kdbOpen (ksNew (0, KS_END), parentKey); | ||
|
||
KeySet * ks1 = ksNew (0, KS_END); | ||
KeySet * ks2 = ksNew (0, KS_END); | ||
|
||
kdbGet (kdb, ks1, parentKey); | ||
succeed_if (ksLookupByName (ks1, "/somewhere/key", 0) != NULL, "should find key (1)"); | ||
kdbGet (kdb, ks2, parentKey); | ||
succeed_if (ksLookupByName (ks2, "/somewhere/key", 0) != NULL, "should find key (2)"); | ||
|
||
kdbClose (kdb, parentKey); | ||
|
||
ksDel (ks); | ||
ksDel (ks1); | ||
ksDel (ks2); | ||
keyDel (parentKey); | ||
} | ||
|
||
static void test_get_modified_keys (void) | ||
{ | ||
printf ("running %s\n", __func__); | ||
|
||
// Setup | ||
Key * parentKey = keyNew ("/somewhere", KS_END); | ||
KeySet * ksInitial = ksNew (0, KS_END); | ||
KDB * kdb = kdbOpen (ksNew (0, KS_END), parentKey); | ||
kdbGet (kdb, ksInitial, parentKey); | ||
ksAppendKey (ksInitial, keyNew ("user:/somewhere", KEY_VALUE, "abc", KEY_END)); | ||
ksAppendKey (ksInitial, keyNew ("user:/somewhere/key", KEY_VALUE, "xyz", KEY_END)); | ||
kdbSet (kdb, ksInitial, parentKey); | ||
kdbClose (kdb, parentKey); | ||
|
||
// Scenario | ||
kdb = kdbOpen (ksNew (0, KS_END), parentKey); | ||
|
||
KeySet * ks1 = ksNew (0, KS_END); | ||
KeySet * ks2 = ksNew (0, KS_END); | ||
|
||
kdbGet (kdb, ks1, parentKey); | ||
succeed_if_keyset_contains_key_with_string (ks1, "/somewhere/key", "xyz"); | ||
ksAppendKey (ks1, keyNew ("user:/somewhere/key", KEY_VALUE, "updated", KEY_END)); | ||
ksAppendKey (ks1, keyNew ("user:/somewhere/newkey", KEY_VALUE, "new", KEY_END)); | ||
kdbSet (kdb, ks1, parentKey); | ||
|
||
kdbGet (kdb, ks2, parentKey); | ||
succeed_if_keyset_contains_key_with_string (ks2, "/somewhere/key", "updated"); | ||
succeed_if_keyset_contains_key_with_string (ks2, "/somewhere/newkey", "new"); | ||
|
||
kdbClose (kdb, parentKey); | ||
|
||
ksDel (ksInitial); | ||
ksDel (ks1); | ||
ksDel (ks2); | ||
keyDel (parentKey); | ||
} | ||
|
||
int main (int argc, char ** argv) | ||
{ | ||
printf ("INTERNAL CACHE TESTS\n"); | ||
printf ("====================\n\n"); | ||
|
||
init (argc, argv); | ||
|
||
test_doubleGet (); | ||
test_get_modified_keys (); | ||
|
||
printf ("\ntest_internalcache RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError); | ||
|
||
return nbError; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this the wrong way around? In Step 18,
fromInternalCache
is appended first