Skip to content

Commit

Permalink
ndb: add profile flatbuffer record dump util
Browse files Browse the repository at this point in the history
I needed a way to make test profile data for notedeck

Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Apr 20, 2024
1 parent d275f9f commit 8faf6ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#use nix
use nix

export TODO_FILE=TODO
export JB55=32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245
Expand Down
43 changes: 39 additions & 4 deletions ndb.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ static int usage()
printf(" stat\n");
printf(" search [--oldest-first] [--limit 42] <fulltext query>\n");
printf(" query [-k 42] [-k 1337] [-l 42] [-a abcdef -a bcdef ...]\n");
printf(" profile <pubkey>\n");
printf(" print-search-keys\n");
printf(" print-kind-keys\n");
printf(" print-tag-keys\n");
printf(" import <line-delimited json file>\n\n");
printf("settings\n\n");
printf(" --skip-verification skip signature validation\n");
Expand Down Expand Up @@ -108,7 +112,7 @@ static void print_note(struct ndb_note *note)
int main(int argc, char *argv[])
{
struct ndb *ndb;
int i, flags, limit, count, current_field, len;
int i, flags, limit, count, current_field, len, res;
long nanos;
struct ndb_stat stat;
struct ndb_txn txn;
Expand All @@ -121,6 +125,14 @@ int main(int argc, char *argv[])
struct timespec t1, t2;
struct ndb_text_search_config search_config;
unsigned char tmp_id[32];

// profiles
const char *pk_str;
void *ptr;
size_t profile_len;
uint64_t key;

res = 0;
ndb_default_config(&config);
ndb_default_text_search_config(&search_config);
ndb_config_set_mapsize(&config, 1024ULL * 1024ULL * 1024ULL * 1024ULL /* 1 TiB */);
Expand Down Expand Up @@ -185,7 +197,8 @@ int main(int argc, char *argv[])
ndb_end_query(&txn);
} else if (argc == 2 && !strcmp(argv[1], "stat")) {
if (!ndb_stat(ndb, &stat)) {
return 3;
res = 3;
goto cleanup;
}

print_stats(&stat);
Expand Down Expand Up @@ -245,12 +258,14 @@ int main(int argc, char *argv[])
len = strlen(argv[1]);
if (len != 64 || !hex_decode(argv[1], 64, tmp_id, sizeof(tmp_id))) {
fprintf(stderr, "invalid hex pubkey\n");
return 42;
res = 42;
goto cleanup;
}

if (!ndb_filter_add_id_element(f, tmp_id)) {
fprintf(stderr, "too many author pubkeys\n");
return 43;
res = 43;
goto cleanup;
}

argv += 2;
Expand Down Expand Up @@ -301,9 +316,29 @@ int main(int argc, char *argv[])
ndb_begin_query(ndb, &txn);
ndb_print_tag_keys(&txn);
ndb_end_query(&txn);
} else if (argc == 3 && !strcmp(argv[1], "profile")) {
pk_str = argv[2];
if (!hex_decode(pk_str, strlen(pk_str), tmp_id, sizeof(tmp_id))) {
fprintf(stderr, "failed to decode hex pubkey '%s'\n", pk_str);
res = 88;
goto cleanup;
}
ndb_begin_query(ndb, &txn);
if (!(ptr = ndb_get_profile_by_pubkey(&txn, tmp_id, &profile_len, &key))) {
ndb_end_query(&txn);
fprintf(stderr, "profile not found\n");
res = 89;
goto cleanup;
}
ndb_end_query(&txn);
print_hex(ptr, profile_len);
printf("\n");
} else {
ndb_destroy(ndb);
return usage();
}

cleanup:
ndb_destroy(ndb);
return res;
}

0 comments on commit 8faf6ce

Please sign in to comment.