commit 620d4da75570c064b8a1e532caa61fd70390e3a6
parent ceb5ab5c991d433f5188e5833971b1843d997a6e
Author: William Casarin <jb55@jb55.com>
Date: Fri, 5 Jan 2024 21:37:34 -0800
ndb: add inital query command
still very early, but works for kinds!
Diffstat:
3 files changed, 44 insertions(+), 3 deletions(-)
diff --git a/TODO b/TODO
@@ -2,3 +2,4 @@ subscription polling
execution plan for tags query
execution plan for created_at query
note kind index rebuild migration
+(A) filter from json
diff --git a/ndb.c b/ndb.c
@@ -91,10 +91,15 @@ static void print_stats(struct ndb_stat *stat)
int ndb_print_search_keys(struct ndb_txn *txn);
int ndb_print_kind_keys(struct ndb_txn *txn);
+static void print_note(struct ndb_note *note)
+{
+ printf("note[%d]: \"%s\"\n", ndb_note_kind(note), ndb_note_content(note));
+}
+
int main(int argc, char *argv[])
{
struct ndb *ndb;
- int i, flags, limit;
+ int i, flags, limit, count;
struct ndb_stat stat;
struct ndb_txn txn;
struct ndb_text_search_results results;
@@ -166,6 +171,41 @@ int main(int argc, char *argv[])
}
print_stats(&stat);
+ } else if (argc >= 3 && !strcmp(argv[1], "query")) {
+ struct ndb_filter filter, *f = &filter;
+ ndb_filter_init(f);
+
+ argv += 2;
+ argc -= 2;
+
+ for (i = 0; argc && i < 2; i++) {
+ if (!strcmp(argv[0], "-k")) {
+ ndb_filter_start_field(f, NDB_FILTER_KINDS);
+ ndb_filter_add_int_element(f, atoll(argv[1]));
+ ndb_filter_end_field(f);
+ argv += 2;
+ argc -= 2;
+ } else if (!strcmp(argv[0], "-l")) {
+ limit = atol(argv[1]);
+ ndb_filter_start_field(f, NDB_FILTER_LIMIT);
+ ndb_filter_add_int_element(f, limit);
+ ndb_filter_end_field(f);
+ argv += 2;
+ argc -= 2;
+ }
+ }
+
+ struct ndb_query_result results[500];
+ ndb_begin_query(ndb, &txn);
+ ndb_query(&txn, f, 1, results, 500, &count);
+
+ fprintf(stderr, "%d results\n", count);
+ for (i = 0; i < count; i++) {
+ print_note(results[i].note);
+ }
+
+ ndb_end_query(&txn);
+
} else if (argc == 3 && !strcmp(argv[1], "import")) {
if (!strcmp(argv[2], "-")) {
ndb_process_events_stream(ndb, stdin);
diff --git a/src/nostrdb.c b/src/nostrdb.c
@@ -1870,7 +1870,7 @@ static int ndb_ingester_process_event(secp256k1_context *ctx,
if ((int)note_size == -42) {
// we already have this!
- ndb_debug("already have id??\n");
+ //ndb_debug("already have id??\n");
goto cleanup;
} else if (note_size == 0) {
ndb_debug("failed to parse '%.*s'\n", ev->len, ev->json);
@@ -2187,7 +2187,7 @@ void *ndb_get_note_meta(struct ndb_txn *txn, const unsigned char *id, size_t *le
k.mv_size = 32;
if (mdb_get(txn->mdb_txn, txn->lmdb->dbs[NDB_DB_META], &k, &v)) {
- ndb_debug("ndb_get_note_meta: mdb_get note failed\n");
+ //ndb_debug("ndb_get_note_meta: mdb_get note failed\n");
return NULL;
}