Skip to content

Commit

Permalink
add "import -"
Browse files Browse the repository at this point in the history
Closes: #21
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
mattn authored and jb55 committed Dec 13, 2023
1 parent 6117380 commit 8376e5b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ndb.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,13 @@ 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);
ndb_process_client_events(ndb, (const char *)data, data_len);
if (!strcmp(argv[2], "-")) {
ndb_process_events_stream(ndb, stdin);
} else {
map_file(argv[2], &data, &data_len);
ndb_process_events(ndb, (const char *)data, data_len);
ndb_process_client_events(ndb, (const char *)data, data_len);
}
} else if (argc == 2 && !strcmp(argv[1], "print-search-keys")) {
ndb_begin_query(ndb, &txn);
ndb_print_search_keys(&txn);
Expand Down
18 changes: 18 additions & 0 deletions nostrdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3236,6 +3236,24 @@ int _ndb_process_events(struct ndb *ndb, const char *ldjson, size_t json_len, in
return 1;
}

int ndb_process_events_stream(struct ndb *ndb, FILE* fp)
{
char *line = NULL;
size_t len = 0;
ssize_t nread;

while ((nread = getline(&line, &len, stdin)) != -1) {
if (line == NULL)
break;
ndb_process_event(ndb, line, len);
}

if (line)
free(line);

return 1;
}

int ndb_process_client_events(struct ndb *ndb, const char *ldjson, size_t json_len)
{
return _ndb_process_events(ndb, ldjson, json_len, 1);
Expand Down
1 change: 1 addition & 0 deletions nostrdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ int ndb_init(struct ndb **ndb, const char *dbdir, struct ndb_config *);
int ndb_db_version(struct ndb *ndb);
int ndb_process_event(struct ndb *, const char *json, int len);
int ndb_process_events(struct ndb *, const char *ldjson, size_t len);
int ndb_process_events_stream(struct ndb *, FILE* fp);
int ndb_process_client_event(struct ndb *, const char *json, int len);
int ndb_process_client_events(struct ndb *, const char *json, size_t len);
int ndb_begin_query(struct ndb *, struct ndb_txn *);
Expand Down

0 comments on commit 8376e5b

Please sign in to comment.