Skip to content

Commit

Permalink
ndb: add inital query command
Browse files Browse the repository at this point in the history
still very early, but works for kinds!
  • Loading branch information
jb55 committed Jan 6, 2024
1 parent ceb5ab5 commit 620d4da
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 41 additions & 1 deletion ndb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/nostrdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 620d4da

Please sign in to comment.