Skip to content

Commit

Permalink
Change behaviour of provide with data in the DB
Browse files Browse the repository at this point in the history
Previously we returned the DB data in preference to
provided data. We now always return provided data
if a provider exists for the path.
  • Loading branch information
carlgsmith authored and blairsteven committed May 30, 2023
1 parent 1f69952 commit c791bbb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
11 changes: 9 additions & 2 deletions apteryx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1232,11 +1232,18 @@ apteryx_path_to_node (GNode* root, const char *path, const char *value)
next = strchr (path, '/');
if (!next)
{
if (value)
rnode = apteryx_find_child (root, path);
if (rnode && value)
{
/* We already have this leaf - replace the value */
free (g_node_first_child (rnode)->data);
g_node_first_child (rnode)->data = strdup (value);
}
else if (value)
{
rnode = APTERYX_LEAF (root, strdup (path), strdup (value));
}
else
else if (!rnode)
{
rnode = APTERYX_NODE (root, strdup (path));
}
Expand Down
14 changes: 10 additions & 4 deletions apteryxd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,10 @@ handle_set (rpc_message msg, bool ack)
ERROR ("SET: Failed to decode message\n");
return false;
}
if (apteryx_debug && root) {
DEBUG ("SET:\n");
apteryx_print_tree (root, stdout);
}

/* Figure out if we need the lists for checking callbacks */
_node_to_path(root, &root_path);
Expand Down Expand Up @@ -1296,11 +1300,11 @@ get_value (const char *path)
/* Call refreshers */
call_refreshers (path, false);

/* Database second */
if (!db_get (path, (unsigned char**)&value, &vsize))
/* Provide second */
if ((value = provide_get (path)) == NULL)
{
/* Provide third */
if ((value = provide_get (path)) == NULL)
/* Database third */
if (!db_get (path, (unsigned char**)&value, &vsize))
{
DEBUG ("GET: not in database or provided or proxied\n");
}
Expand Down Expand Up @@ -1737,6 +1741,7 @@ handle_traverse (rpc_message msg)
{
path = (char *) ipath->data;
value = (char *) ivalue->data;
/* Overwrite any database values with those from provide */
apteryx_path_to_node (root, path, value);
}
}
Expand Down Expand Up @@ -1969,6 +1974,7 @@ handle_query (rpc_message msg)
{
path = (char *) ipath->data;
value = (char *) ivalue->data;
/* Overwrite any database values with those from provide */
apteryx_path_to_node (root, path, value);
}
}
Expand Down
24 changes: 12 additions & 12 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3258,7 +3258,7 @@ test_provide_search_root ()
char *
test_provide_cb(const char *path)
{
return strdup("tmp");
return strdup("provided");
}

void
Expand Down Expand Up @@ -3308,15 +3308,15 @@ test_provide_search_db ()
}

void
test_provide_after_db ()
test_provide_before_db ()
{
const char *path = TEST_PATH"/interfaces/eth0/state";
const char *value = NULL;

CU_ASSERT (apteryx_set (path, "down"));
CU_ASSERT (apteryx_provide (path, test_provide_callback_up));
CU_ASSERT (( value = apteryx_get (path)) != NULL);
CU_ASSERT (value && strcmp (value, "down") == 0);
CU_ASSERT (value && strcmp (value, "up") == 0);
if (value)
free ((void *) value);
apteryx_unprovide (path, test_provide_callback_up);
Expand Down Expand Up @@ -3743,7 +3743,7 @@ test_get_tree_provided ()
}

void
test_get_tree_provided_after_db ()
test_get_tree_provided_before_db ()
{
const char *path = TEST_PATH"/interfaces/eth0/state";
GNode *root = NULL;
Expand All @@ -3757,7 +3757,7 @@ test_get_tree_provided_after_db ()
CU_ASSERT (node && g_node_n_children (node) == 1);
node = node ? g_node_first_child (node) : NULL;
CU_ASSERT (node && strcmp (APTERYX_NAME (node), "state") == 0);
CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "up") == 0);
CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "provided") == 0);

CU_ASSERT (apteryx_set (path, NULL));
CU_ASSERT (apteryx_unprovide (path, test_provide_cb));
Expand Down Expand Up @@ -3805,7 +3805,7 @@ char *
test_provide_writes_cb (const char *path)
{
apteryx_set (TEST_PATH"/unimportant", NULL);
return strdup ("tmp");
return strdup ("provided");
}

void
Expand Down Expand Up @@ -4528,7 +4528,7 @@ test_query_provided ()
}

void
test_query_provided_after_db ()
test_query_provided_before_db ()
{
const char *path = TEST_PATH"/system/state";
GNode *root = NULL;
Expand All @@ -4547,7 +4547,7 @@ test_query_provided_after_db ()
CU_ASSERT (node && g_node_n_children (node) == 1);
node = node ? g_node_first_child (node) : NULL;
CU_ASSERT (node && strcmp (APTERYX_NAME (node), "state") == 0);
CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "up") == 0);
CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "provided") == 0);
apteryx_free_tree (rroot);
apteryx_free_tree (root);

Expand All @@ -4560,7 +4560,7 @@ test_query_provided_after_db ()
CU_ASSERT (node && g_node_n_children (node) == 1);
node = node ? g_node_first_child (node) : NULL;
CU_ASSERT (node && strcmp (APTERYX_NAME (node), "state") == 0);
CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "up") == 0);
CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "provided") == 0);
apteryx_free_tree (rroot);
apteryx_free_tree (root);

Expand Down Expand Up @@ -7780,7 +7780,7 @@ static CU_TestInfo tests_api_provide[] = {
{ "provide search root", test_provide_search_root },
{ "provide wildcard + search", test_provider_wildcard_search },
{ "provide and db search", test_provide_search_db },
{ "provide after db", test_provide_after_db },
{ "provide before db", test_provide_before_db },
{ "provider wildcard", test_provider_wildcard },
{ "provider wildcard internal", test_provider_wildcard_internal },
{ "provider search exact provide", test_search_of_provide },
Expand Down Expand Up @@ -7826,7 +7826,7 @@ static CU_TestInfo tests_api_tree[] = {
{ "get tree indexed/provided wildcards", test_get_tree_indexed_provided_wildcards },
{ "get tree indexed/provided deeper", test_get_tree_indexed_provided_two_levels },
{ "get tree provided", test_get_tree_provided },
{ "get tree provided after db", test_get_tree_provided_after_db },
{ "get tree provided before db", test_get_tree_provided_before_db },
{ "get tree provided + set", test_get_tree_provided_plus_set },
{ "get tree provider writes", test_get_tree_provider_write },
{ "get tree thrashing" , test_get_tree_while_thrashing },
Expand All @@ -7850,7 +7850,7 @@ static CU_TestInfo tests_api_tree[] = {
{ "query null values", test_query_null_values},
{ "query two branches", test_query_two_branches},
{ "query provided", test_query_provided},
{ "query provided after db", test_query_provided_after_db},
{ "query provided before db", test_query_provided_before_db},
{ "query provided and refreshed", test_query_provided_refreshed},
{ "query provided trunk request", test_query_trunk_provided},
{ "query provided wildcard", test_query_provided_wildcard},
Expand Down

0 comments on commit c791bbb

Please sign in to comment.