nostrdb

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

commit 6651511d65a284f4965837e9a9233c55d8df1db6
parent f277cc768d39d38d089969aba442354b183dc6d8
Author: William Casarin <jb55@jb55.com>
Date:   Fri,  1 Dec 2023 15:37:03 -0800

search: remove result printing, move to util/ndb

Diffstat:
Mndb.c | 33+++++++++++++++++++++++++++++++++
Mnostrdb.c | 41-----------------------------------------
2 files changed, 33 insertions(+), 41 deletions(-)

diff --git a/ndb.c b/ndb.c @@ -86,6 +86,30 @@ 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 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("\n%s\n\n---\n", ndb_note_str(note, &note->content).str); +} + int main(int argc, char *argv[]) { struct ndb *ndb; @@ -93,6 +117,7 @@ int main(int argc, char *argv[]) struct ndb_stat stat; struct ndb_txn txn; struct ndb_text_search_results results; + struct ndb_text_search_result *result; const char *dir; unsigned char *data; size_t data_len; @@ -130,6 +155,14 @@ int main(int argc, char *argv[]) if (argc == 3 && !strcmp(argv[1], "search")) { ndb_begin_query(ndb, &txn); ndb_text_search(&txn, argv[2], &results); + + // print results for now + for (i = 0; i < results.num_results; i++) { + result = &results.results[i]; + printf("[%02d] ", i+1); + ndb_print_text_search_result(&txn, result); + } + ndb_end_query(&txn); } else if (argc == 2 && !strcmp(argv[1], "stat")) { if (!ndb_stat(ndb, &stat)) { diff --git a/nostrdb.c b/nostrdb.c @@ -2561,30 +2561,6 @@ static int ndb_text_search_next_word(MDB_cursor *cursor, MDB_cursor_op op, return 1; } -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 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("\n%s\n\n---\n", ndb_note_str(note, &note->content).str); -} - static void ndb_text_search_results_init( struct ndb_text_search_results *results) { results->num_results = 0; @@ -2624,9 +2600,6 @@ int ndb_text_search(struct ndb_txn *txn, const char *query, return 0; } - fprintf(stderr, "search query: '%s'\n", query); - - // for each word, we recursively find all of the submatches while (results->num_results < MAX_TEXT_SEARCH_RESULTS) { last_result = NULL; @@ -2635,13 +2608,6 @@ int ndb_text_search(struct ndb_txn *txn, const char *query, // if we have saved, then we continue from the last root search // sequence if (saved) { - /* - fprintf(stderr, "continuing from "); - if (ndb_unpack_text_search_key(saved_buf, saved_size, &search_key)) { - ndb_print_text_search_key(&search_key); - } - fprintf(stderr, "\n"); - */ buf = saved_buf; saved = NULL; keysize = saved_size; @@ -2738,13 +2704,6 @@ cont: } - // print results for now - for (j = 0; j < results->num_results; j++) { - result = &results->results[j]; - printf("[%02d] ", j+1); - ndb_print_text_search_result(txn, result); - } - mdb_cursor_close(cursor); return 1;