nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

commit 62fd097bb3f751b51292f7a317b615030193032a
parent 8644b88408a203770693066a27cb1acc251dd5d6
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  4 Dec 2023 14:27:04 -0800

util: add print_util.h for various printing tasks

Diffstat:
Mndb.c | 35+----------------------------------
Aprint_util.h | 36++++++++++++++++++++++++++++++++++++
Mrandom.h | 6------
Mtest.c | 8+-------
4 files changed, 38 insertions(+), 47 deletions(-)

diff --git a/ndb.c b/ndb.c @@ -1,6 +1,7 @@ #include "nostrdb.h" +#include "print_util.h" #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> @@ -87,40 +88,6 @@ static void print_stats(struct ndb_stat *stat) } } -static void ndb_print_text_search_key(struct ndb_text_search_key *key) -{ - printf("K<'%.*s' %d %" PRIu64 " note_id:%" PRIu64 ">", key->str_len, key->str, - key->word_index, - key->timestamp, - key->note_id); -} - -static void print_hex(unsigned char* data, size_t size) { - size_t i; - for (i = 0; i < size; i++) { - printf("%02x", data[i]); - } -} - -static void ndb_print_text_search_result(struct ndb_txn *txn, - struct ndb_text_search_result *r) -{ - size_t len; - struct ndb_note *note; - - ndb_print_text_search_key(&r->key); - - if (!(note = ndb_get_note_by_key(txn, r->key.note_id, &len))) { - printf(": note not found"); - return; - } - - printf(" "); - print_hex(note->id, 32); - - printf("\n%s\n\n---\n", ndb_note_str(note, &note->content).str); -} - int ndb_print_search_keys(struct ndb_txn *txn); int main(int argc, char *argv[]) diff --git a/print_util.h b/print_util.h @@ -0,0 +1,36 @@ + +static void ndb_print_text_search_key(struct ndb_text_search_key *key) +{ + printf("K<'%.*s' %d %" PRIu64 " note_id:%" PRIu64 ">", key->str_len, key->str, + key->word_index, + key->timestamp, + key->note_id); +} + +static void print_hex(unsigned char* data, size_t size) { + size_t i; + for (i = 0; i < size; i++) { + printf("%02x", data[i]); + } +} + + +static void ndb_print_text_search_result(struct ndb_txn *txn, + struct ndb_text_search_result *r) +{ + size_t len; + struct ndb_note *note; + + ndb_print_text_search_key(&r->key); + + if (!(note = ndb_get_note_by_key(txn, r->key.note_id, &len))) { + printf(": note not found"); + return; + } + + printf(" "); + print_hex(note->id, 32); + + printf("\n%s\n\n---\n", ndb_note_str(note, &note->content).str); +} + diff --git a/random.h b/random.h @@ -63,9 +63,3 @@ static int fill_random(unsigned char* data, size_t size) { return 0; } -static void print_hex(unsigned char* data, size_t size) { - size_t i; - for (i = 0; i < size; i++) { - printf("%02x", data[i]); - } -} diff --git a/test.c b/test.c @@ -4,6 +4,7 @@ #include "io.h" #include "protected_queue.h" #include "memchr.h" +#include "print_util.h" #include "bindings/c/profile_reader.h" #include "bindings/c/profile_verifier.h" #include "bindings/c/meta_reader.h" @@ -17,13 +18,6 @@ static const char *test_dir = "./testdata/db"; -static void print_hex(unsigned char* data, size_t size) { - size_t i; - for (i = 0; i < size; i++) { - printf("%02x", data[i]); - } -} - static NdbProfile_table_t lookup_profile(struct ndb_txn *txn, uint64_t pk) { void *root;