From 01c239c0ebb4548fffce89235fdfe7fc3494bf6d Mon Sep 17 00:00:00 2001 From: William Casarin Date: Fri, 1 Dec 2023 16:21:25 -0800 Subject: [PATCH] nostrdb/search: add limit param If we only care to have a certain number of results Signed-off-by: William Casarin --- nostrdb/nostrdb.c | 6 ++++-- nostrdb/nostrdb.h | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/nostrdb/nostrdb.c b/nostrdb/nostrdb.c index 277d1811..8428d558 100644 --- a/nostrdb/nostrdb.c +++ b/nostrdb/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/nostrdb.h b/nostrdb/nostrdb.h index 9b4c0ff9..ee0a51a5 100644 --- a/nostrdb/nostrdb.h +++ b/nostrdb/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);