//! Embedded local stores: LMDB, DuckDB and SQLite. //! //! These complement the MariaDB-backed REST endpoints with local, //! file-backed (or in-memory) stores: LMDB as a key/value cache, DuckDB //! for analytical queries, SQLite for a small relational mirror. Each //! helper below opens the store, does a trivial write/read round-trip //! and returns the result, so a test can prove the native libraries are //! linked and usable without needing a server or a database. //! //! The crates link the *system* libraries found via pkg-config (and, //! for DuckDB, `DUCKDB_LIB_DIR`/`DUCKDB_INCLUDE_DIR`); the `bundled` //! features are deliberately not enabled so the Guix build reuses the //! distro's liblmdb/libsqlite3/libduckdb rather than compiling its own. use std::path::Path; /// LMDB: open (creating if needed) an environment in `dir`, store /// `value` under `key`, and read it back. pub fn lmdb_roundtrip(dir: &Path, key: &[u8], value: &[u8]) -> Result, String> { use lmdb::{DatabaseFlags, Environment, Transaction, WriteFlags}; std::fs::create_dir_all(dir).map_err(|e| e.to_string())?; let env = Environment::new().open(dir).map_err(|e| e.to_string())?; let db = env .create_db(None, DatabaseFlags::empty()) .map_err(|e| e.to_string())?; { let mut txn = env.begin_rw_txn().map_err(|e| e.to_string())?; txn.put(db, &key, &value, WriteFlags::empty()) .map_err(|e| e.to_string())?; txn.commit().map_err(|e| e.to_string())?; } let txn = env.begin_ro_txn().map_err(|e| e.to_string())?; let got = txn.get(db, &key).map_err(|e| e.to_string())?; Ok(got.to_vec()) } /// DuckDB: run an in-memory analytical query and return the row count. pub fn duckdb_roundtrip() -> Result { let conn = duckdb::Connection::open_in_memory().map_err(|e| e.to_string())?; conn.execute_batch( "CREATE TABLE gn4_embedded(id INTEGER, name VARCHAR); \ INSERT INTO gn4_embedded VALUES (1, 'a'), (2, 'b');", ) .map_err(|e| e.to_string())?; let n: i64 = conn .query_row("SELECT COUNT(*) FROM gn4_embedded", [], |row| row.get(0)) .map_err(|e| e.to_string())?; Ok(n) } /// SQLite: open (creating if needed) a database at `path`, insert three /// rows and return the row count. pub fn sqlite_roundtrip(path: &Path) -> Result { let conn = rusqlite::Connection::open(path).map_err(|e| e.to_string())?; conn.execute_batch( "CREATE TABLE gn4_embedded(id INTEGER); \ INSERT INTO gn4_embedded VALUES (1), (2), (3);", ) .map_err(|e| e.to_string())?; let n: i64 = conn .query_row("SELECT COUNT(*) FROM gn4_embedded", [], |row| row.get(0)) .map_err(|e| e.to_string())?; Ok(n) }