nostrdb

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

commit 39b3288b94f5f7da3501f6a249b1b4de57568274
parent 47c98034f4f0f4a04809da7ab5b1ebc8ee0a8edf
Author: William Casarin <jb55@jb55.com>
Date:   Thu, 14 Dec 2023 18:57:38 -0800

rust: move into separate project

seems impossible to publish this way

Diffstat:
M.github/workflows/c-cpp.yml | 3---
DCargo.toml | 4----
Drust/Cargo.toml | 13-------------
Drust/build.rs | 122-------------------------------------------------------------------------------
Drust/src/bindings.rs | 1-
Drust/src/config.rs | 43-------------------------------------------
Drust/src/error.rs | 3---
Drust/src/lib.rs | 10----------
Drust/src/ndb.rs | 61-------------------------------------------------------------
Drust/src/result.rs | 3---
10 files changed, 0 insertions(+), 263 deletions(-)

diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml @@ -37,6 +37,3 @@ jobs: - name: make check run: make check - - - name: rust - run: cargo test diff --git a/Cargo.toml b/Cargo.toml @@ -1,4 +0,0 @@ -[workspace] -resolver = "2" -members = [ "rust" ] - diff --git a/rust/Cargo.toml b/rust/Cargo.toml @@ -1,13 +0,0 @@ -[package] -name = "nostrdb" -authors = ["William Casarin <jb55@jb55.com>"] -description = "An unfairly fast embedded nostr database backed by lmdb" -readme = "README.md" -version = "0.1.0" -edition = "2021" -build = "build.rs" -include = ["../deps", "src", "Cargo.toml", "build.rs", "../*.c", "../*.h"] - -[build-dependencies] -bindgen = "0.69.1" -cc = "1.0" diff --git a/rust/build.rs b/rust/build.rs @@ -1,122 +0,0 @@ -// build.rs -use cc::Build; -use std::env; -use std::path::PathBuf; - -fn secp256k1_build() { - // Actual build - let mut base_config = cc::Build::new(); - base_config - .include("../deps/secp256k1/") - .include("../deps/secp256k1/include") - .include("../deps/secp256k1/src") - .flag_if_supported("-Wno-unused-function") // some ecmult stuff is defined but not used upstream - .flag_if_supported("-Wno-unused-parameter") // patching out printf causes this warning - //.define("SECP256K1_API", Some("")) - .define("ENABLE_MODULE_ECDH", Some("1")) - .define("ENABLE_MODULE_SCHNORRSIG", Some("1")) - .define("ENABLE_MODULE_EXTRAKEYS", Some("1")) - //.define("ENABLE_MODULE_ELLSWIFT", Some("0")) - // upstream sometimes introduces calls to printf, which we cannot compile - // with WASM due to its lack of libc. printf is never necessary and we can - // just #define it away. - .define("printf(...)", Some("")); - - //if cfg!(feature = "lowmemory") { - // base_config.define("ECMULT_WINDOW_SIZE", Some("4")); // A low-enough value to consume negligible memory - // base_config.define("ECMULT_GEN_PREC_BITS", Some("2")); - //} else { - // base_config.define("ECMULT_GEN_PREC_BITS", Some("4")); - // base_config.define("ECMULT_WINDOW_SIZE", Some("15")); // This is the default in the configure file (`auto`) - //} - - //base_config.define("USE_EXTERNAL_DEFAULT_CALLBACKS", Some("1")); - //#[cfg(feature = "recovery")] - //base_config.define("ENABLE_MODULE_RECOVERY", Some("1")); - - // WASM headers and size/align defines. - if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "wasm32" { - base_config.include("wasm/wasm-sysroot").file("wasm/wasm.c"); - } - - // secp256k1 - base_config - .file("../deps/secp256k1/contrib/lax_der_parsing.c") - .file("../deps/secp256k1/src/precomputed_ecmult_gen.c") - .file("../deps/secp256k1/src/precomputed_ecmult.c") - .file("../deps/secp256k1/src/secp256k1.c"); - - if base_config.try_compile("libsecp256k1.a").is_err() { - // Some embedded platforms may not have, eg, string.h available, so if the build fails - // simply try again with the wasm sysroot (but without the wasm type sizes) in the hopes - // that it works. - base_config.include("wasm/wasm-sysroot"); - base_config.compile("libsecp256k1.a"); - } -} - -fn main() { - // Compile the C file - let mut build = Build::new(); - - build - .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", - "../deps/lmdb/mdb.c", - "../deps/lmdb/midl.c", - ]) - .include("../deps/lmdb") - .include("../deps/flatcc/include") - .include("../deps/secp256k1/include") - // Add other include paths - //.flag("-Wall") - .flag("-Wno-misleading-indentation") - .flag("-Wno-unused-function") - //.flag("-Werror") - //.flag("-g") - .compile("libnostrdb.a"); - - secp256k1_build(); - - // Re-run the build script if any of the C files or headers change - for file in &[ - "../nostrdb.c", - "../sha256.c", - "../bech32.c", - // Add all your C source files here - "../nostrdb.h", - "../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=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(out_path.join("bindings.rs")) - .expect("Couldn't write bindings!"); -} diff --git a/rust/src/bindings.rs b/rust/src/bindings.rs @@ -1 +0,0 @@ -include!(concat!(env!("OUT_DIR"), "/bindings.rs")); diff --git a/rust/src/config.rs b/rust/src/config.rs @@ -1,43 +0,0 @@ -use crate::bindings; - -// The Rust wrapper for ndb_config -pub struct NdbConfig { - pub config: bindings::ndb_config, -} - -impl NdbConfig { - // Constructor - pub fn new() -> Self { - let mut config = bindings::ndb_config { - filter_context: std::ptr::null_mut(), - ingest_filter: None, - flags: 0, - ingester_threads: 0, - mapsize: 0, - }; - - unsafe { - bindings::ndb_default_config(&mut config); - } - - NdbConfig { config } - } - - // Example setter methods - pub fn set_flags(&mut self, flags: i32) -> &mut Self { - self.config.flags = flags; - self - } - - pub fn set_ingester_threads(&mut self, threads: i32) -> &mut Self { - self.config.ingester_threads = threads; - self - } - - // Add other setter methods as needed - - // Internal method to get a raw pointer to the config, used in Ndb - pub fn as_ptr(&self) -> *const bindings::ndb_config { - &self.config - } -} diff --git a/rust/src/error.rs b/rust/src/error.rs @@ -1,3 +0,0 @@ -pub enum Error { - DbOpenFailed, -} diff --git a/rust/src/lib.rs b/rust/src/lib.rs @@ -1,10 +0,0 @@ -#[allow(non_upper_case_globals)] -#[allow(non_camel_case_types)] -#[allow(non_snake_case)] -#[allow(unused)] -mod bindings; - -mod config; -mod error; -mod ndb; -mod result; diff --git a/rust/src/ndb.rs b/rust/src/ndb.rs @@ -1,61 +0,0 @@ -use std::ffi::CString; -use std::ptr; - -use crate::bindings; -use crate::config::NdbConfig; -use crate::error::Error; -use crate::result::Result; - -pub struct Ndb { - ndb: *mut bindings::ndb, -} - -impl Ndb { - // Constructor - pub fn new(db_dir: &str, config: &NdbConfig) -> Result<Self> { - let db_dir_cstr = match CString::new(db_dir) { - Ok(cstr) => cstr, - Err(_) => return Err(Error::DbOpenFailed), - }; - let mut ndb: *mut bindings::ndb = ptr::null_mut(); - let result = unsafe { bindings::ndb_init(&mut ndb, db_dir_cstr.as_ptr(), config.as_ptr()) }; - - if result != 0 { - return Err(Error::DbOpenFailed); - } - - Ok(Ndb { ndb }) - } - - // Add other methods to interact with the library here -} - -impl Drop for Ndb { - fn drop(&mut self) { - unsafe { - bindings::ndb_destroy(self.ndb); - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use std::fs; - - fn cleanup() { - let _ = fs::remove_file("data.mdb"); - let _ = fs::remove_file("lock.mdb"); - } - - #[test] - fn ndb_init_works() { - // Initialize ndb - { - let cfg = NdbConfig::new(); - let _ = Ndb::new(".", &cfg); - } - - cleanup(); - } -} diff --git a/rust/src/result.rs b/rust/src/result.rs @@ -1,3 +0,0 @@ -use crate::error::Error; - -pub type Result<T> = std::result::Result<T, Error>;