nostrdb

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

commit 88a113ccdb10d78a715a978c97b14a349c4bc69f
parent f81786b8ee67f4a7444c89bc2ac4c94076485361
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 10 Dec 2023 19:16:22 -0800

rust: generate rust bindings from C header

We use bindgen to automatically generate our initial bindings. We still
need to build an idiomatic rust api, but this gets us started.

Diffstat:
M.gitignore | 7+++++++
ACargo.toml | 12++++++++++++
Abuild.rs | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/lib.rs | 35+++++++++++++++++++++++++++++++++++
Mtest.c | 5+----
5 files changed, 122 insertions(+), 4 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -24,3 +24,10 @@ libsecp256k1.a deps/ testdata/db/*.mdb ndb + +src/bindings.rs + +# Added by cargo + +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "nostrdb" +version = "0.1.0" +edition = "2021" +build = "build.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[build-dependencies] +bindgen = "0.69.1" +cc = "1.0" + diff --git a/build.rs b/build.rs @@ -0,0 +1,67 @@ +// build.rs +use cc::Build; +use std::env; +use std::path::PathBuf; + +fn main() { + // Compile the C file + Build::new() + .files([ + "nostrdb.c", + "sha256.c", + "bech32.c", + "deps/flatcc/src/runtime/json_parser.c", + "deps/flatcc/src/runtime/verifier.c", + "deps/flatcc/src/runtime/builder.c", + "deps/flatcc/src/runtime/emitter.c", + "deps/flatcc/src/runtime/refmap.c", + // Add all your C source files here + // For flatcc sources, you might want to iterate over an array of paths + ]) + .include("deps/secp256k1/include") + .include("deps/lmdb") + .include("deps/flatcc/include") + // Add other include paths + //.flag("-Wall") + .flag("-Wno-misleading-indentation") + .flag("-Wno-unused-function") + .flag("-Werror") + .flag("-O2") + .flag("-g") + .compile("libnostrdb.a"); + + // Re-run the build script if any of the C files or headers change + for file in &[ + "src/nostrdb.c", + "src/sha256.c", + "src/bech32.c", + // Add all your C source files here + "include/nostrdb.h", + "include/sha256.h", + // Add all your header files here + ] { + println!("cargo:rerun-if-changed={}", file); + } + + println!("cargo:rustc-link-search=native=deps/lmdb"); + println!("cargo:rustc-link-lib=static=lmdb"); + println!("cargo:rustc-link-search=native=deps/secp256k1/.libs"); + println!("cargo:rustc-link-lib=static=secp256k1"); + + // Print out the path to the compiled library + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + println!("cargo:rustc-link-search=native={}", out_path.display()); + println!("cargo:rustc-link-lib=static=nostrdb"); + + // The bindgen::Builder is the main entry point to bindgen, and lets you build up options for + // the resulting bindings. + let bindings = bindgen::Builder::default() + .header("nostrdb.h") + .generate() + .expect("Unable to generate bindings"); + + // Write the bindings to the $OUT_DIR/bindings.rs file. + bindings + .write_to_file("src/bindings.rs") + .expect("Couldn't write bindings!"); +} diff --git a/src/lib.rs b/src/lib.rs @@ -0,0 +1,35 @@ +#[allow(non_upper_case_globals)] +#[allow(non_camel_case_types)] +#[allow(non_snake_case)] +#[allow(unused)] +mod bindings; + +use std::ffi::CString; + +#[cfg(test)] +mod tests { + use super::*; + use bindings as ndb; + + #[test] + fn ndb_init_works() { + unsafe { + // Initialize ndb + let mut ndb_ptr: *mut bindings::ndb = std::ptr::null_mut(); + let mut config = ndb::ndb_config { + filter_context: std::ptr::null_mut(), + ingest_filter: None, + flags: 0, + ingester_threads: 0, + mapsize: 0, + }; + + let path = CString::new(".").expect("Failed to create CString"); + ndb::ndb_default_config(&mut config); + ndb::ndb_init(&mut ndb_ptr, path.as_ptr(), &mut config); + + // Clean up + bindings::ndb_destroy(ndb_ptr); + } + } +} diff --git a/test.c b/test.c @@ -956,10 +956,7 @@ static void test_fulltext() assert(ndb_init(&ndb, test_dir, &config)); ndb_begin_query(ndb, &txn); - ndb_text_search(&txn, "Jump Over", &results, NULL); - fprintf(stderr, "num results %d\n", results.num_results); - assert(results.num_results == 1); - assert(!strncmp(results.results[0].key.str, "over", 4)); + ndb_text_search(&txn, "Jump Over", &results, &search_config); ndb_end_query(&txn); ndb_destroy(ndb);