nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

commit 7cd176001388fd977a56bc2c64781a382a69c2ad
parent 0aaf1f67ce93840f9bd0c8bb78346215e0eb98d1
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 29 Nov 2023 12:04:01 -0800

util/ndb: add import command

So that we can import notes for testing. This currently assumes the
client ingest format:

["EVENT", "", {}]

Which is a bit strange, but a lot of our test data is in this format at
the moment. Will need to create new nostrdb functions for ingesting
plain json notes. Right now we have:

ndb_process_events: ["EVENT", "", note]
ndb_process_client_events: ["EVENT", note]

we still need:

ndb_process_notes: <line delimited json notes>

Diffstat:
Mndb.c | 27+++++++++++++++++++++++++++
1 file changed, 27 insertions(+), 0 deletions(-)

diff --git a/ndb.c b/ndb.c @@ -2,6 +2,10 @@ #include "nostrdb.h" #include <stdio.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <unistd.h> +#include <fcntl.h> static int usage() { @@ -9,9 +13,26 @@ static int usage() printf("commands\n\n"); printf(" stat\n"); printf(" search <fulltext query>\n"); + printf(" import <line-delimited json file>\n"); return 1; } + +static int map_file(const char *filename, unsigned char **p, size_t *flen) +{ + struct stat st; + int des; + stat(filename, &st); + *flen = st.st_size; + + des = open(filename, O_RDONLY); + + *p = mmap(NULL, *flen, PROT_READ, MAP_PRIVATE, des, 0); + close(des); + + return *p != MAP_FAILED; +} + static inline void print_stat_counts(struct ndb_stat_counts *counts) { printf("%zu\t%zu\t%zu\t%zu\n", @@ -71,6 +92,8 @@ int main(int argc, char *argv[]) struct ndb_txn txn; struct ndb_text_search_results results; const char *dir; + unsigned char *data; + size_t data_len; size_t mapsize = 1024ULL * 1024ULL * 1024ULL * 1024ULL; // 1 TiB if (argc < 2) { @@ -100,8 +123,12 @@ int main(int argc, char *argv[]) } print_stats(&stat); + } else if (argc == 3 && !strcmp(argv[1], "import")) { + map_file(argv[2], &data, &data_len); + ndb_process_events(ndb, (const char *)data, data_len); } else { return usage(); } + ndb_destroy(ndb); }