about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-11 11:17:55 +0000
committerPjotr <wrk@bosa.lan>2026-10-11 11:17:55 +0000
commitab13b3dcca0d832434e190bd716b213e6d8d64f0 (patch)
treef58a3462cacfd26747aa11f2138470f93a13bfe7 /src
parent69aac61b0c5b2d8a4f909de6a6d6af8d6ee293a3 (diff)
downloadgenenetwork4-ab13b3dcca0d832434e190bd716b213e6d8d64f0.tar.gz
Embedded stores: add LMDB, DuckDB and SQLite support
Add src/embedded.rs with lmdb/duckdb/sqlite round-trip helpers and
tests/embedded.rs integration tests: an LMDB put/get (surviving a
fresh environment handle), an in-memory DuckDB query, and a SQLite
query.

Depend on the system libraries rather than bundled builds:
duckdb 1.10506.0, lmdb 0.8.0 and rusqlite 0.40.2 with their default
features, so the Guix build reuses libduckdb/liblmdb/libsqlite3 instead
of compiling its own.

Guix wiring (guix.scm): add the duckdb, lmdb and sqlite inputs
((gnu packages duckdb)/(gnu packages databases)/(gnu packages sqlite)),
a set-duckdb-paths phase that points libduckdb-sys at the system
libduckdb via DUCKDB_LIB_DIR/DUCKDB_INCLUDE_DIR (there is no
duckdb.pc), and the 79 new crate sources in rust-package.scm
(regenerated with `guix import crate -f Cargo.lock genenetwork4`).
Diffstat (limited to 'src')
-rw-r--r--src/embedded.rs65
-rw-r--r--src/lib.rs1
2 files changed, 66 insertions, 0 deletions
diff --git a/src/embedded.rs b/src/embedded.rs
new file mode 100644
index 0000000..efb9d30
--- /dev/null
+++ b/src/embedded.rs
@@ -0,0 +1,65 @@
+//! 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<Vec<u8>, 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<i64, String> {
+    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<i64, String> {
+    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)
+}
diff --git a/src/lib.rs b/src/lib.rs
index 42b2da7..cfe2c59 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -8,6 +8,7 @@ use utoipa_axum::routes;
 pub mod auth;
 pub mod config;
 mod db;
+pub mod embedded;
 mod error;
 pub mod response;
 mod routes;