nostrdb

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

commit 044ff0ab60be78cd3c0150b28e2c84a6df0d1bac
parent 827f876c7c634406f257633eee480fc489ce38a0
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 11 Feb 2024 14:07:38 -0800

Merge remote-tracking branch 'github/master'

Diffstat:
MMakefile | 12++++++++++--
Msrc/nostrdb.c | 8+++++---
Msrc/random.h | 10++++++----
3 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/Makefile b/Makefile @@ -18,6 +18,14 @@ C_BINDINGS_COMMON=$(BINDINGS)/c/flatbuffers_common_builder.h $(BINDINGS)/c/flatb C_BINDINGS=$(C_BINDINGS_COMMON) $(C_BINDINGS_PROFILE) $(C_BINDINGS_META) BIN=ndb +# Detect operating system +UNAME_S := $(shell uname -s) + +# macOS-specific flags +ifeq ($(UNAME_S),Darwin) + LDFLAGS += -framework Security +endif + CHECKDATA=testdata/db/v0/data.mdb all: $(BIN) lib bench @@ -31,7 +39,7 @@ libnostrdb.a: $(OBJS) lib: libnostrdb.a ndb: ndb.c $(DEPS) - $(CC) $(CFLAGS) ndb.c $(LDS) -o $@ + $(CC) $(CFLAGS) ndb.c $(LDS) $(LDFLAGS) -o $@ bindings: bindings-swift bindings-rust bindings-c @@ -179,6 +187,6 @@ testdata/db/.dir: touch testdata/db/.dir test: test.c $(DEPS) testdata/db/.dir - $(CC) $(CFLAGS) test.c $(LDS) -o $@ + $(CC) $(CFLAGS) test.c $(LDS) $(LDFLAGS) -o $@ .PHONY: tags clean fake diff --git a/src/nostrdb.c b/src/nostrdb.c @@ -2612,7 +2612,7 @@ static int ndb_query_plan_execute_ids(struct ndb_txn *txn, struct ndb_query_result res; struct ndb_tsid tsid, *ptsid; uint64_t note_id, until, *pint; - uint64_t note_size; + size_t note_size; unsigned char *id; matched = 0; @@ -2717,7 +2717,8 @@ static int ndb_query_plan_execute_tags(struct ndb_txn *txn, MDB_dbi db; MDB_val k, v; int len, taglen, rc, i; - uint64_t *pint, until, note_id, note_size; + uint64_t *pint, until, note_id; + size_t note_size; unsigned char key_buffer[255]; struct ndb_note *note; struct ndb_filter_elements *tags; @@ -2800,7 +2801,8 @@ static int ndb_query_plan_execute_kinds(struct ndb_txn *txn, struct ndb_u64_tsid tsid, *ptsid; struct ndb_filter_elements *kinds; struct ndb_query_result res; - uint64_t kind, note_id, note_size, until, *pint; + uint64_t kind, note_id, until, *pint; + size_t note_size; int i, rc; // we should have kinds in a kinds filter! diff --git a/src/random.h b/src/random.h @@ -24,10 +24,12 @@ #include <fcntl.h> #include <unistd.h> #include <errno.h> -#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) +#elif defined(__linux__) || defined(__FreeBSD__) #include <sys/random.h> #elif defined(__OpenBSD__) #include <unistd.h> +#elif defined(__APPLE__) +#include <Security/SecRandom.h> #else #error "Couldn't identify the OS" #endif @@ -66,7 +68,7 @@ static int fill_random(unsigned char* data, size_t size) { } close(fd); return 1; -#elif defined(__linux__) || defined(__FreeBSD__) +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) /* If `getrandom(2)` is not available you should fallback to /dev/urandom */ ssize_t res = getrandom(data, size, 0); if (res < 0 || (size_t)res != size ) { @@ -74,10 +76,10 @@ static int fill_random(unsigned char* data, size_t size) { } else { return 1; } -#elif defined(__APPLE__) || defined(__OpenBSD__) +#elif defined(__APPLE__) /* If `getentropy(2)` is not available you should fallback to either * `SecRandomCopyBytes` or /dev/urandom */ - int res = getentropy(data, size); + int res = SecRandomCopyBytes(kSecRandomDefault, size, data); if (res == 0) { return 1; } else {