summaryrefslogtreecommitdiff
path: root/c_src/permdb.c
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2016-02-14 09:35:26 +0100
committerLinus Nordberg <linus@nordu.net>2016-04-25 13:14:11 +0200
commit2776d2972d30e3391569401fdce57a4452fc76a7 (patch)
tree97dc0e47bad0b1f3feec8eea85d41ff527ac4082 /c_src/permdb.c
parentb2a3e6dc5cf7c987c1f2ac1349d26ad76fb55d3c (diff)
Make debug printing prettier.
Diffstat (limited to 'c_src/permdb.c')
-rw-r--r--c_src/permdb.c179
1 files changed, 42 insertions, 137 deletions
diff --git a/c_src/permdb.c b/c_src/permdb.c
index 77a249c..dde863b 100644
--- a/c_src/permdb.c
+++ b/c_src/permdb.c
@@ -53,53 +53,40 @@ struct nodecache {
static node_object
get_node_from_cache(permdb_object *state, const char *key)
{
-#if DEBUG_CACHE
- fprintf(stderr, "getting cache key %s, ", key);
-#endif
+ dprintf(CACHE, (stderr, "getting cache key %s, ", key));
struct nodecache *node;
HASH_FIND(hh, state->nodecache, key, strlen(key), node);
if (node == NULL) {
-#if DEBUG_CACHE
- fprintf(stderr, "found nothing in cache\n");
-#endif
+ dprintf(CACHE, (stderr, "found nothing in cache\n"));
return errornode;
}
-#if DEBUG_CACHE
- fprintf(stderr, "got cache key %s: ", node->key);
- print_hex(&node->value, sizeof(struct node_object));
-#endif
+ dprintf(CACHE, (stderr, "got cache key %s: ", node->key));
+ dprinthex(CACHE, &node->value, sizeof(struct node_object));
return node->value;
}
static node_object
get_node_from_dirtynodes(permdb_object *state, const char *key)
{
-#if DEBUG_CACHE
- fprintf(stderr, "getting key %s, ", key);
-#endif
+ dprintf(CACHE, (stderr, "getting key %s, ", key));
struct nodecache *node;
HASH_FIND(hh, state->dirtynodes, key, strlen(key), node);
if (node == NULL) {
-#if DEBUG_CACHE
- fprintf(stderr, "found nothing\n");
-#endif
+ dprintf(CACHE, (stderr, "found nothing\n"));
return errornode;
}
-#if DEBUG_CACHE
- fprintf(stderr, "got key %s: ", node->key);
- print_hex(&node->value, sizeof(struct node_object));
-#endif
+ dprintf(CACHE, (stderr, "got key %s: ", node->key));
+ dprinthex(CACHE, &node->value, sizeof(struct node_object));
return node->value;
}
static void
put_node_in_cache(permdb_object *state, const char *key, node_object value)
{
-#if DEBUG_CACHE
- fprintf(stderr, "putting cache key %s: ", key);
- print_hex(&value, sizeof(node_object));
-#endif
+ dprintf(CACHE, (stderr, "putting cache key %s: ", key));
+ dprinthex(CACHE, &value, sizeof(node_object));
+
struct nodecache *node;
HASH_FIND(hh, state->nodecache, key, strlen(key), node);
if (node) {
@@ -116,10 +103,8 @@ put_node_in_cache(permdb_object *state, const char *key, node_object value)
static void
put_node_in_dirtynodes(permdb_object *state, const char *key, node_object value)
{
-#if DEBUG_CACHE
- fprintf(stderr, "putting key %s: ", key);
- print_hex(&value, sizeof(node_object));
-#endif
+ dprintf(CACHE, (stderr, "putting key %s: ", key));
+ dprinthex(CACHE, &value, sizeof(node_object));
struct nodecache *node;
HASH_FIND(hh, state->dirtynodes, key, strlen(key), node);
if (node) {
@@ -412,9 +397,7 @@ permdb_alloc(const char *dbpath)
state->dirtynodes = NULL;
state->error = NULL;
if (bf_total_length(state->datafile) == 0 && bf_total_length(state->indexfile) == 0) {
-#if DEBUG_WRITE
- fprintf(stderr, "writing header\n");
-#endif
+ dprintf(WRITE, (stderr, "writing header\n"));
indexfile_add_header(state->indexfile);
datafile_add_header(state->datafile);
initial_commit(state);
@@ -524,9 +507,7 @@ unsigned char *
read_internal_data(permdb_object *state, node_offset offset, size_t length)
{
buffered_file *file = state->datafile;
-#if DEBUG_READ
- fprintf(stderr, "reading data: offset %llu\n", offset);
-#endif
+ dprintf(READ, (stderr, "reading data: offset %llu\n", offset));
return bf_read(file, offset, length, &state->error);
}
@@ -543,31 +524,24 @@ iserrornode(node_object node)
node_object
readnode(permdb_object *state, node_offset offset, const char *cachekey)
{
-#if DEBUG_READ
- fprintf(stderr, "reading node: offset %llu cachekey '%s'\n", offset, cachekey ? cachekey : "none");
-#endif
+ dprintf(READ, (stderr, "reading node: offset %llu cachekey '%s'\n", offset, cachekey ? cachekey : "none"));
+
if (cachekey) {
node_object dirtynode = get_node_from_dirtynodes(state, cachekey);
if (!iserrornode(dirtynode)) {
-#if DEBUG_READ
- fprintf(stderr, "reading node: found node in dirty nodes\n");
-#endif
+ dprintf(READ, (stderr, "reading node: found node in dirty nodes\n"));
return dirtynode;
}
if (offset == NODE_ENTRY_DIRTY_NODE) {
set_error(&state->error, "referring to dirty node at key %s, but node not in dirtynodes\n", cachekey);
-#if DEBUG_READ
- fprintf(stderr, "reading node: referring to dirty node at key %s, but node not in dirtynodes\n", cachekey);
-#endif
+ dprintf(READ, (stderr, "reading node: referring to dirty node at key %s, but node not in dirtynodes\n", cachekey));
return errornode;
}
node_object cachednode = get_node_from_cache(state, cachekey);
if (!iserrornode(cachednode)) {
-#if DEBUG_READ
- fprintf(stderr, "reading node: found node in cache\n");
-#endif
+ dprintf(READ, (stderr, "reading node: found node in cache\n"));
return cachednode;
}
}
@@ -587,9 +561,7 @@ readnode(permdb_object *state, node_offset offset, const char *cachekey)
put_node_in_cache(state, cachekey, result);
}
-#if DEBUG_READ
- fprintf(stderr, "reading node: success\n");
-#endif
+ dprintf(READ, (stderr, "reading node: success\n"));
return result;
}
@@ -639,13 +611,6 @@ getpath(permdb_object *state, const unsigned char *key, struct nodelist *nodes)
{
unsigned int level = 0;
-#if 0
- if (bf_lastcommit(state->indexfile) < (sizeof(index_node_cookie) + sizeof(node_object) + INDEX_COMMIT_TRAILER_SIZE)) {
- fprintf(stderr, "No commit exists (lastcommit %llu)\n", (long long unsigned int) bf_lastcommit(state->indexfile));
- return -1;
- }
-#endif
-
node_offset rootoffset = bf_lastcommit(state->indexfile) - (sizeof(index_node_cookie) + sizeof(node_object) + INDEX_COMMIT_TRAILER_SIZE);
node_object node = readnode(state, rootoffset, "");
@@ -655,9 +620,7 @@ getpath(permdb_object *state, const unsigned char *key, struct nodelist *nodes)
return -1;
}
-#if DEBUG_READ
- fprintf(stderr, "getpath: got node\n");
-#endif
+ dprintf(READ, (stderr, "getpath: got node\n"));
while (1) {
unsigned char kb = keybits(key, level);
@@ -668,9 +631,7 @@ getpath(permdb_object *state, const unsigned char *key, struct nodelist *nodes)
}
add_entry_to_nodelist(nodes, node);
if (entry == 0 || isdata(entry)) {
-#if DEBUG_READ
- fprintf(stderr, "getpath: return node\n");
-#endif
+ dprintf(READ, (stderr, "getpath: return node\n"));
return (char) kb;
}
level++;
@@ -678,9 +639,7 @@ getpath(permdb_object *state, const unsigned char *key, struct nodelist *nodes)
node = readnode(state, entryoffset(entry), kp);
if (iserrornode(node)) {
free(kp);
-#if DEBUG_READ
- fprintf(stderr, "getpath: not found\n");
-#endif
+ dprintf(READ, (stderr, "getpath: not found\n"));
return -1;
}
free(kp);
@@ -697,15 +656,11 @@ getpathlastnode(permdb_object *state, const unsigned char *key)
node_object node = readnode(state, rootoffset, "");
if (iserrornode(node)) {
-#if DEBUG_READ
- fprintf(stderr, "getpathlastnode: no node\n");
-#endif
+ dprintf(READ, (stderr, "getpathlastnode: no node\n"));
return NODE_ENTRY_ERROR_NODE;
}
-#if DEBUG_READ
- fprintf(stderr, "getpathlastnode: got node\n");
-#endif
+ dprintf(READ, (stderr, "getpathlastnode: got node\n"));
unsigned char kb;
while (1) {
@@ -731,9 +686,7 @@ writenode(permdb_object *state, node_object node, const char *cachekey)
put_node_in_cache(state, cachekey, node);
-#if DEBUG_WRITE
- fprintf(stderr, "writing node: offset %llu\n", offset);
-#endif
+ dprintf(WRITE, (stderr, "writing node: offset %llu\n", offset));
bf_add(state->indexfile, &index_node_cookie, sizeof(index_node_cookie));
bf_add(state->indexfile, &node, sizeof(node_object));
@@ -819,9 +772,7 @@ writedata(permdb_object *state, const unsigned char *key, const unsigned char *d
}
node_offset offset = bf_total_length(state->datafile);
-#if DEBUG_WRITE
- fprintf(stderr, "writing data: offset %llu\n", offset);
-#endif
+ dprintf(WRITE, (stderr, "writing data: offset %llu\n", offset));
bf_add(state->datafile, &data_entry_cookie, sizeof(data_entry_cookie));
bf_add(state->datafile, key, keylen);
bf_add_be16(state->datafile, 1);
@@ -1015,15 +966,11 @@ getvalue(permdb_object *state, const unsigned char *key, size_t keylength, size_
{
node_entry entry = getpathlastnode(state, key);
if (entry == 0) {
-#if DEBUG_READ
- fprintf(stderr, "getvalue: no node\n");
-#endif
+ dprintf(READ, (stderr, "getvalue: no node\n"));
return NULL;
}
-#if DEBUG_READ
- fprintf(stderr, "getvalue: got node\n");
-#endif
+ dprintf(READ, (stderr, "getvalue: got node\n"));
node_offset olddataoffset = entryoffset(entry);
@@ -1039,24 +986,6 @@ getvalue(permdb_object *state, const unsigned char *key, size_t keylength, size_
return readdata(state, olddataoffset, *datalen);
}
-struct keylist {
- char **keys;
- int pos;
-};
-
-#if 0
-static int
-add_entry_to_keylist(void *ptr, void *arg)
-{
- struct keylist *list = (struct keylist *) arg;
- struct nodecache *node = (struct nodecache *)ptr;
-
- list->keys[list->pos] = strdup(node->key);
- list->pos++;
- return 0;
-}
-#endif
-
static int
string_length_comparison(struct nodecache *a, struct nodecache *b) {
size_t a_len = strlen(a->key);
@@ -1086,9 +1015,7 @@ committree(permdb_object *state)
return -1;
}
-#if DEBUG_WRITE
- fprintf(stderr, "committing %d dirty nodes at offset %llu\n", ncommits, bf_total_length(state->indexfile));
-#endif
+ dprintf(WRITE, (stderr, "committing %d dirty nodes at offset %llu\n", HASH_COUNT(state->dirtynodes), bf_total_length(state->indexfile)));
struct nodecache *node, *tmp;
HASH_ITER(hh, state->dirtynodes, node, tmp) {
assert(get_entry_in_node(node->value, 0) != NODE_ENTRY_DIRTY_NODE);
@@ -1108,9 +1035,7 @@ committree(permdb_object *state)
}
}
-#if DEBUG_WRITE
- fprintf(stderr, "writing data commit trailer at offset %llu\n", bf_total_length(state->datafile));
-#endif
+ dprintf(WRITE, (stderr, "writing data commit trailer at offset %llu\n", bf_total_length(state->datafile)));
int data_commit_padding_size = calc_padding(bf_total_length(state->datafile), 4);
uint8_t padding[4] = {0, 0, 0, 0};
@@ -1122,18 +1047,14 @@ committree(permdb_object *state)
bf_add(state->datafile, data_commit_checksum, SHA256_DIGEST_SIZE);
bf_add(state->datafile, &data_commit_end_cookie, sizeof(data_commit_end_cookie));
-#if DEBUG_WRITE
- fprintf(stderr, "finished writing data commit trailer at offset %llu\n", bf_total_length(state->datafile));
-#endif
+ dprintf(WRITE, (stderr, "finished writing data commit trailer at offset %llu\n", bf_total_length(state->datafile)));
if (bf_flush(state->datafile) == -1) {
set_error(&state->error, "data file flushing failed\n");
return -1;
}
-#if DEBUG_WRITE
- fprintf(stderr, "writing index commit trailer at offset %llu\n", bf_total_length(state->indexfile));
-#endif
+ dprintf(WRITE, (stderr, "writing index commit trailer at offset %llu\n", bf_total_length(state->indexfile)));
uint64_t index_commit_length = bf_total_length(state->indexfile) - bf_lastcommit(state->indexfile) + sizeof(uint64_t);
unsigned char index_commit_checksum[SHA256_DIGEST_SIZE];
@@ -1142,9 +1063,7 @@ committree(permdb_object *state)
bf_add(state->indexfile, index_commit_checksum, SHA256_DIGEST_SIZE);
bf_add(state->indexfile, &index_commit_cookie, sizeof(index_commit_cookie));
-#if DEBUG_WRITE
- fprintf(stderr, "finished writing index commit trailer at offset %llu\n", bf_total_length(state->indexfile));
-#endif
+ dprintf(WRITE, (stderr, "finished writing index commit trailer at offset %llu\n", bf_total_length(state->indexfile)));
if (bf_flush(state->indexfile) == -1) {
set_error(&state->error, "index file flushing failed\n");
@@ -1164,9 +1083,7 @@ portloop(permdb_object *state)
ssize_t len;
while ((len = read_command(buf, sizeof(buf)-1, 4)) > 0) {
if (buf[0] == 0) {
-#if DEBUG_PORT
- fprintf(stderr, "get\n");
-#endif
+ dprintf(PORT, (stderr, "get\n"));
if (len != keylen+1) {
write_reply(NULL, 0, 4);
continue;
@@ -1181,14 +1098,10 @@ portloop(permdb_object *state)
} else {
write_reply(result, datalen, 4);
}
-#if DEBUG_PORT
- fprintf(stderr, "get reply\n");
-#endif
+ dprintf(PORT, (stderr, "get reply\n"));
free(result);
} else if (buf[0] == 1) {
-#if DEBUG_PORT
- fprintf(stderr, "add\n");
-#endif
+ dprintf(PORT, (stderr, "add\n"));
if (len < (keylen + 1)) {
write_reply(NULL, 0, 4);
fprintf(stderr, "invalid addvalue command, length was %zd\n", len);
@@ -1207,13 +1120,9 @@ portloop(permdb_object *state)
unsigned char result_byte = (unsigned char) result;
write_reply(&result_byte, 1, 4);
}
-#if DEBUG_PORT
- fprintf(stderr, "add reply\n");
-#endif
+ dprintf(PORT, (stderr, "add reply\n"));
} else if (buf[0] == 2) {
-#if DEBUG_PORT
- fprintf(stderr, "commit\n");
-#endif
+ dprintf(PORT, (stderr, "commit\n"));
if (len != 1) {
write_reply(NULL, 0, 4);
fprintf(stderr, "invalid commit command, length was %zd\n", len);
@@ -1229,13 +1138,9 @@ portloop(permdb_object *state)
unsigned char result_byte = (unsigned char) result;
write_reply(&result_byte, 1, 4);
}
-#if DEBUG_PORT
- fprintf(stderr, "commit reply\n");
-#endif
+ dprintf(PORT, (stderr, "commit reply\n"));
} else {
-#if DEBUG_PORT
- fprintf(stderr, "unknown command\n");
-#endif
+ dprintf(PORT, (stderr, "unknown command\n"));
write_reply(NULL, 0, 4);
}
}