Skip to content

Commit

Permalink
search: remove result printing, move to util/ndb
Browse files Browse the repository at this point in the history
  • Loading branch information
jb55 committed Dec 1, 2023
1 parent f277cc7 commit 6651511
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 41 deletions.
33 changes: 33 additions & 0 deletions ndb.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,38 @@ 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;
int i, flags;
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;
Expand Down Expand Up @@ -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)) {
Expand Down
41 changes: 0 additions & 41 deletions nostrdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -2738,13 +2704,6 @@ int ndb_text_search(struct ndb_txn *txn, const char *query,

}

// 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;
Expand Down

0 comments on commit 6651511

Please sign in to comment.