Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement nip50 fulltext searching #68

Merged
merged 8 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 13 additions & 39 deletions ndb.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ static int usage()
printf("usage: ndb [--skip-verification] [-d db_dir] <command>\n\n");
printf("commands\n\n");
printf(" stat\n");
printf(" search [--oldest-first] [--limit 42] <fulltext query>\n");
printf(" query [-k 42] [-k 1337] [-l 42] [-e abcdef...] [-a abcdef... -a bcdef...]\n");
printf(" query [-k 42] [-k 1337] [--search term] [-l 42] [-e abcdef...] [-a abcdef... -a bcdef...]\n");
printf(" profile <pubkey>\n");
printf(" print-search-keys\n");
printf(" print-kind-keys\n");
Expand Down Expand Up @@ -136,14 +135,11 @@ int main(int argc, char *argv[])
long nanos;
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;
struct ndb_config config;
struct timespec t1, t2;
struct ndb_text_search_config search_config;
unsigned char tmp_id[32];

// profiles
Expand All @@ -154,7 +150,6 @@ int main(int argc, char *argv[])

res = 0;
ndb_default_config(&config);
ndb_default_text_search_config(&search_config);
ndb_config_set_mapsize(&config, 1024ULL * 1024ULL * 1024ULL * 1024ULL /* 1 TiB */);

if (argc < 2) {
Expand Down Expand Up @@ -184,38 +179,7 @@ int main(int argc, char *argv[])
return 2;
}

if (argc >= 3 && !strcmp(argv[1], "search")) {
for (i = 0; i < 2; i++) {
if (!strcmp(argv[2], "--oldest-first")) {
ndb_text_search_config_set_order(&search_config, NDB_ORDER_ASCENDING);
argv++;
argc--;
} else if (!strcmp(argv[2], "--limit") || !strcmp(argv[2], "-l")) {
limit = atoi(argv[3]);
ndb_text_search_config_set_limit(&search_config, limit);
argv += 2;
argc -= 2;
}
}

ndb_begin_query(ndb, &txn);
clock_gettime(CLOCK_MONOTONIC, &t1);
ndb_text_search(&txn, argv[2], &results, &search_config);
clock_gettime(CLOCK_MONOTONIC, &t2);

nanos = (t2.tv_sec - t1.tv_sec) * (long)1e9 + (t2.tv_nsec - t1.tv_nsec);

fprintf(stderr, "%d results in %f ms\n", results.num_results, nanos/1000000.0);

// print results for now
for (i = 0; i < results.num_results; i++) {
result = &results.results[i];
//fprintf(stderr, "[%02d] ", i+1);
ndb_print_text_search_result(&txn, result);
}

ndb_end_query(&txn);
} else if (argc == 2 && !strcmp(argv[1], "stat")) {
if (argc == 2 && !strcmp(argv[1], "stat")) {
if (!ndb_stat(ndb, &stat)) {
res = 3;
goto cleanup;
Expand All @@ -240,7 +204,7 @@ int main(int argc, char *argv[])
ndb_filter_add_int_element(f, atoll(argv[1]));
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "-l")) {
} else if (!strcmp(argv[0], "-l") || !strcmp(argv[0], "--limit")) {
limit = atol(argv[1]);
if (current_field) {
ndb_filter_end_field(f);
Expand Down Expand Up @@ -282,6 +246,16 @@ int main(int argc, char *argv[])
ndb_filter_end_field(f);
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "--search") || !strcmp(argv[0], "-S")) {
if (current_field) {
ndb_filter_end_field(f);
current_field = 0;
}
ndb_filter_start_field(f, NDB_FILTER_SEARCH);
ndb_filter_add_str_element(f, argv[1]);
ndb_filter_end_field(f);
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "-e")) {
if (current_field != 'e') {
if (!ndb_filter_start_tag_field(f, 'e')) {
Expand Down
Loading
Loading