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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::sync::Arc;
use holo_hash::WasmHash;
use holochain_sqlite::rusqlite::named_params;
use holochain_sqlite::rusqlite::OptionalExtension;
use holochain_sqlite::rusqlite::Transaction;
use holochain_types::prelude::*;
use crate::mutations;
use crate::prelude::StateMutationResult;
use crate::prelude::StateQueryResult;
pub fn get(txn: &Transaction<'_>, hash: &WasmHash) -> StateQueryResult<Option<DnaWasmHashed>> {
let item = txn
.query_row(
"SELECT hash, blob FROM Wasm WHERE hash = :hash",
named_params! {
":hash": hash
},
|row| {
let hash: WasmHash = row.get("hash")?;
let wasm: Vec<u8> = row.get("blob")?;
Ok((hash, wasm))
},
)
.optional()?;
match item {
Some((hash, wasm)) => Ok(Some(DnaWasmHashed::with_pre_hashed(
DnaWasm {
code: Arc::new(wasm.into_boxed_slice()),
},
hash,
))),
None => Ok(None),
}
}
pub fn contains(txn: &Transaction<'_>, hash: &WasmHash) -> StateQueryResult<bool> {
Ok(txn.query_row(
"SELECT EXISTS(SELECT 1 FROM Wasm WHERE hash = :hash)",
named_params! {
":hash": hash
},
|row| row.get(0),
)?)
}
pub fn put(txn: &mut Transaction, wasm: DnaWasmHashed) -> StateMutationResult<()> {
mutations::insert_wasm(txn, wasm)
}
#[cfg(test)]
mod tests {
use super::*;
use holo_hash::HasHash;
use holochain_sqlite::prelude::DatabaseResult;
use holochain_types::dna::wasm::DnaWasm;
#[tokio::test(flavor = "multi_thread")]
async fn wasm_store_round_trip() -> DatabaseResult<()> {
use holochain_sqlite::prelude::*;
observability::test_run().ok();
let db = crate::test_utils::test_wasm_db();
let wasm =
DnaWasmHashed::from_content(DnaWasm::from(holochain_wasm_test_utils::TestWasm::Foo))
.await;
db.conn()?
.with_commit_sync(|txn| put(txn, wasm.clone()))
.unwrap();
fresh_reader_test!(db, |txn| {
assert!(contains(&txn, &wasm.as_hash()).unwrap());
let ret = get(&txn, &wasm.as_hash()).unwrap().unwrap();
assert_eq!(ret, wasm);
});
Ok(())
}
}