nostrdb

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

commit 8eb0af2f554b700c93f3a5ffb649f862862f60a3
parent 6651511d65a284f4965837e9a9233c55d8df1db6
Author: William Casarin <jb55@jb55.com>
Date:   Fri,  1 Dec 2023 16:21:25 -0800

search: add limit param

If we only care to have a certain number of results

Diffstat:
Mndb.c | 2+-
Mnostrdb.c | 6++++--
Mnostrdb.h | 2+-
3 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/ndb.c b/ndb.c @@ -154,7 +154,7 @@ 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); + ndb_text_search(&txn, argv[2], &results, 999); // print results for now for (i = 0; i < results.num_results; i++) { diff --git a/nostrdb.c b/nostrdb.c @@ -2567,7 +2567,7 @@ static void ndb_text_search_results_init( } int ndb_text_search(struct ndb_txn *txn, const char *query, - struct ndb_text_search_results *results) + struct ndb_text_search_results *results, int limit) { unsigned char buffer[1024], *buf; unsigned char saved_buf[1024], *saved; @@ -2600,8 +2600,10 @@ int ndb_text_search(struct ndb_txn *txn, const char *query, return 0; } + limit = min(MAX_TEXT_SEARCH_RESULTS, limit); + // for each word, we recursively find all of the submatches - while (results->num_results < MAX_TEXT_SEARCH_RESULTS) { + while (results->num_results < limit) { last_result = NULL; result = &results->results[results->num_results]; diff --git a/nostrdb.h b/nostrdb.h @@ -377,7 +377,7 @@ void ndb_filter_end_field(struct ndb_filter *); void ndb_filter_free(struct ndb_filter *filter); // FULLTEXT SEARCH -int ndb_text_search(struct ndb_txn *txn, const char *query, struct ndb_text_search_results *); +int ndb_text_search(struct ndb_txn *txn, const char *query, struct ndb_text_search_results *, int limit); // stats int ndb_stat(struct ndb *ndb, struct ndb_stat *stat);