commit 8376e5bca05c7a69c30db1eaae2755e2b3a24ae6
parent 6117380d1d430a1cb365c5da6dc8987ae73521cc
Author: Yasuhiro Matsumoto <mattn.jp@gmail.com>
Date: Sun, 3 Dec 2023 23:33:57 +0900
add "import -"
Closes: https://github.com/damus-io/nostrdb/pull/21
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
3 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/ndb.c b/ndb.c
@@ -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);
diff --git a/nostrdb.c b/nostrdb.c
@@ -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);
diff --git a/nostrdb.h b/nostrdb.h
@@ -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 *);