summaryrefslogtreecommitdiff
path: root/c_src/permdb.c
diff options
context:
space:
mode:
Diffstat (limited to 'c_src/permdb.c')
-rw-r--r--c_src/permdb.c66
1 files changed, 32 insertions, 34 deletions
diff --git a/c_src/permdb.c b/c_src/permdb.c
index 3928e1d..c28f27f 100644
--- a/c_src/permdb.c
+++ b/c_src/permdb.c
@@ -651,10 +651,11 @@ entryoffset(node_entry entry)
UT_icd node_object_icd = {sizeof(node_object), NULL, NULL, NULL };
static char
-getpath(permdb_object *state, const unsigned char *key, UT_array *nodes)
+getpath_(permdb_object *state,
+ const unsigned char *key,
+ node_entry *entry_out,
+ UT_array *nodes_out)
{
- unsigned int level = 0;
-
node_offset rootoffset =
bf_lastcommit(state->indexfile)
- (sizeof(index_node_cookie)
@@ -674,12 +675,19 @@ getpath(permdb_object *state, const unsigned char *key, UT_array *nodes)
dprintf(READ, (stderr, "getpath: got node\n"));
+ unsigned int level = 0;
+
while (1) {
unsigned char kb = keybits(key, level);
node_entry entry = get_entry_in_node(node, kb);
- utarray_push_back(nodes, &node);
+ if (nodes_out) {
+ 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;
}
level++;
@@ -691,42 +699,32 @@ getpath(permdb_object *state, const unsigned char *key, UT_array *nodes)
return -1;
}
}
+}
+/*
+ * 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.
+ */
+static char
+getpath(permdb_object *state, const unsigned char *key, UT_array *nodes)
+{
+ return getpath_(state, key, NULL, nodes);
}
+/*
+ * Returns the entry in the node with key KEY, or
+ * NODE_ENTRY_ERROR_NODE if not found.
+ */
static node_entry
-getpathlastnode(permdb_object *state, const unsigned char *key)
+getlastnode(permdb_object *state, const unsigned char *key)
{
- unsigned int level = 0;
-
- node_offset rootoffset =
- bf_lastcommit(state->indexfile)
- - (sizeof(index_node_cookie)
- + sizeof(node_object)
- + INDEX_COMMIT_TRAILER_SIZE);
- node_object node = readnode(state, rootoffset, PS_STRING(""));
-
- if (iserrornode(node)) {
- dprintf(READ, (stderr, "getpathlastnode: no node\n"));
+ node_entry entry;
+ if (getpath_(state, key, &entry, NULL) == -1) {
return NODE_ENTRY_ERROR_NODE;
}
-
- dprintf(READ, (stderr, "getpathlastnode: got node\n"));
-
- unsigned char kb;
- while (1) {
- kb = keybits(key, level);
- node_entry entry = get_entry_in_node(node, kb);
- if (entry == 0 || isdata(entry)) {
- break;
- }
- level++;
- ps_string kp;
- keypart(key, level, &kp);
- node = readnode(state, entryoffset(entry), &kp);
- }
-
- node_entry entry = get_entry_in_node(node, kb);
return entry;
}
@@ -963,7 +961,7 @@ unsigned char *
getvalue(permdb_object *state, const unsigned char *key, size_t keylength,
size_t *datalen)
{
- node_entry entry = getpathlastnode(state, key);
+ node_entry entry = getlastnode(state, key);
if (entry == 0) {
dprintf(READ, (stderr, "getvalue: no node\n"));
return NULL;