commit 68e997d91f73030df3d43816233601ea621538b2
parent 3fb90ccf247fba0177e5877801c1d73d11147a13
Author: William Casarin <jb55@jb55.com>
Date: Tue, 14 Nov 2023 09:47:39 -0800
cli: add ndb tool
This adds a command-lind utility for interacting with nostrdb databases.
Right now the only function is "stat" which calls the new ndb_stat
feature.
Changelog-Added: Add nostrdb command line tool "ndb"
Diffstat:
M | .gitignore | | | 1 | + |
M | Makefile | | | 6 | +++++- |
A | ndb.c | | | 76 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 82 insertions(+), 1 deletion(-)
diff --git a/.gitignore b/.gitignore
@@ -19,3 +19,4 @@ libsecp256k1.a
.direnv/
deps/
testdata/db/*.mdb
+ndb
diff --git a/Makefile b/Makefile
@@ -14,13 +14,17 @@ C_BINDINGS_META=bindings/c/meta_builder.h bindings/c/meta_reader.h bindings/c/me
C_BINDINGS_COMMON=bindings/c/flatbuffers_common_builder.h bindings/c/flatbuffers_common_reader.h
C_BINDINGS=$(C_BINDINGS_COMMON) $(C_BINDINGS_PROFILE) $(C_BINDINGS_META)
BINDINGS=bindings
+BIN=ndb
CHECKDATA=testdata/db/v0/data.mdb
-all: lib bindings
+all: $(BIN) lib bindings
lib: benches test
+ndb: ndb.c $(DEPS)
+ $(CC) $(CFLAGS) ndb.c $(LDS) -o $@
+
bindings: bindings-swift bindings-rust bindings-c
check: test $(CHECKDATA)
diff --git a/ndb.c b/ndb.c
@@ -0,0 +1,76 @@
+
+
+#include "nostrdb.h"
+#include <stdio.h>
+
+static int usage()
+{
+ printf("usage: ndb stat [db-dir]\n");
+ return 1;
+}
+
+static inline void print_stat_counts(struct ndb_stat_counts *counts)
+{
+ printf("%zu\t%zu\t%zu\t%zu\n",
+ counts->count,
+ counts->key_size,
+ counts->value_size,
+ counts->key_size + counts->value_size);
+}
+
+static void print_stats(struct ndb_stat *stat)
+{
+ int i;
+ const char *name;
+ struct ndb_stat_counts *c;
+
+ printf("name\tcount\tkey_bytes\tvalue_bytes\ttotal_bytes\n");
+ printf("dbs\n---\n");
+ for (i = 0; i < NDB_DBS; i++) {
+ name = ndb_db_name(i);
+
+ printf("%s\t", name);
+ print_stat_counts(&stat->dbs[i]);
+ }
+
+ printf("kinds\n-----\n");
+ for (i = 0; i < NDB_CKIND_COUNT; i++) {
+ c = &stat->common_kinds[i];
+ if (c->count == 0)
+ continue;
+
+ printf("%s\t", ndb_kind_name(i));
+ print_stat_counts(c);
+ }
+
+ if (stat->other_kinds.count != 0) {
+ printf("other\t");
+ print_stat_counts(&stat->other_kinds);
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ struct ndb *ndb;
+ int threads = 8;
+ int flags = 0;
+ struct ndb_stat stat;
+ const char *dir;
+ size_t mapsize = 1024ULL * 1024ULL * 1024ULL * 1024ULL; // 1 TiB
+
+ if (argc < 2 && strcmp(argv[1], "stat")) {
+ return usage();
+ }
+
+ dir = argv[2] == NULL ? "." : argv[2];
+
+ if (!ndb_init(&ndb, dir, mapsize, threads, flags)) {
+ return 2;
+ }
+
+ if (!ndb_stat(ndb, &stat)) {
+ return 3;
+ }
+
+ print_stats(&stat);
+}