From 8cb49bd7170991493cbb12a5a1231f4332c43624 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Wed, 4 May 2016 14:18:15 +0200 Subject: Refactor getpath/getlast even more. --- c_src/permdb.c | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) (limited to 'c_src') diff --git a/c_src/permdb.c b/c_src/permdb.c index c28f27f..f740dd8 100644 --- a/c_src/permdb.c +++ b/c_src/permdb.c @@ -650,10 +650,9 @@ entryoffset(node_entry entry) UT_icd node_object_icd = {sizeof(node_object), NULL, NULL, NULL }; -static char +static node_entry getpath_(permdb_object *state, const unsigned char *key, - node_entry *entry_out, UT_array *nodes_out) { node_offset rootoffset = @@ -666,14 +665,14 @@ getpath_(permdb_object *state, if (iserrornode(node)) { fprintf(stderr, - "cannot find root node at offset %llu " + "getpath_: cannot find root node at offset %llu " "(lastcommit %llu)\n", (long long unsigned int) rootoffset, (long long unsigned int) bf_lastcommit(state->indexfile)); - return -1; + return NODE_ENTRY_ERROR_NODE; } - dprintf(READ, (stderr, "getpath: got node\n")); + dprintf(READ, (stderr, "getpath_: got root node\n")); unsigned int level = 0; @@ -684,19 +683,16 @@ getpath_(permdb_object *state, utarray_push_back(nodes_out, &node); } if (entry == 0 || isdata(entry)) { - dprintf(READ, (stderr, "getpath: return node\n")); - if (entry_out) { - *entry_out = entry; - } - return (char) kb; + dprintf(READ, (stderr, "getpath_: return node\n")); + return entry; } level++; ps_string kp; keypart(key, level, &kp); node = readnode(state, entryoffset(entry), &kp); if (iserrornode(node)) { - dprintf(READ, (stderr, "getpath: not found\n")); - return -1; + dprintf(READ, (stderr, "getpath_: not found\n")); + return NODE_ENTRY_ERROR_NODE; } } } @@ -705,13 +701,13 @@ getpath_(permdb_object *state, * Writes the nodes in the path from the root to the node with key KEY * to NODES. * - * Returns keybits for KEY at the last level on success, or -1 if not - * found. + * Returns the entry in the last node, i.e. the node with key KEY, or + * NODE_ENTRY_ERROR_NODE if not found. */ -static char +static node_entry getpath(permdb_object *state, const unsigned char *key, UT_array *nodes) { - return getpath_(state, key, NULL, nodes); + return getpath_(state, key, nodes); } /* @@ -721,11 +717,7 @@ getpath(permdb_object *state, const unsigned char *key, UT_array *nodes) static node_entry getlastnode(permdb_object *state, const unsigned char *key) { - node_entry entry; - if (getpath_(state, key, &entry, NULL) == -1) { - return NODE_ENTRY_ERROR_NODE; - } - return entry; + return getpath_(state, key, NULL); } static node_offset @@ -871,26 +863,23 @@ addvalue(permdb_object *state, const unsigned char *key, unsigned int keylength, UT_array *nodes; utarray_new(nodes, &node_object_icd); - char kb = getpath(state, key, nodes); + node_entry lastentry = getpath(state, key, nodes); - if (kb == -1) { + if (lastentry == NODE_ENTRY_ERROR_NODE) { utarray_free(nodes); return -1; } unsigned int foundlevel = utarray_len(nodes) - 1; - node_object lastnode = *(node_object *) utarray_back(nodes); - if (get_entry_in_node(lastnode, (unsigned char) kb) == 0) { + if (lastentry == 0) { if (data != NULL) { dataoffset = writedata(state, key, data, datalength); } addentry(&lastnode, keybits(key, foundlevel), buildentry(1, dataoffset)); } else { - node_offset olddataoffset = - entryoffset(get_entry_in_node(lastnode, - (unsigned char) kb)); + node_offset olddataoffset = entryoffset(lastentry); unsigned char *olddatakey = readdatakey(state, olddataoffset); if (olddatakey == NULL) { utarray_free(nodes); -- cgit v1.1