1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)
}
|