text_document_common/
database.rs1pub mod db_context;
4pub(crate) mod db_helpers;
5pub mod transactions;
6use anyhow::Result;
7
8pub trait CommandUnitOfWork {
9 fn begin_transaction(&mut self) -> Result<()>;
10 fn commit(&mut self) -> Result<()>;
11 fn rollback(&mut self) -> Result<()>;
12 fn create_savepoint(&self) -> Result<types::Savepoint>;
13 fn restore_to_savepoint(&mut self, savepoint: types::Savepoint) -> Result<()>;
14}
15
16pub trait QueryUnitOfWork {
17 fn begin_transaction(&self) -> Result<()>;
18 fn end_transaction(&self) -> Result<()>;
19}
20
21use redb::Key;
22use redb::{TypeName, Value};
23use serde::{Deserialize, Serialize, de::DeserializeOwned};
24use std::any::type_name;
25use std::cmp::Ordering;
26use std::fmt::Debug;
27
28use crate::types;
29
30#[derive(Debug)]
32pub struct Postcard<T>(pub T);
33
34impl<T> Value for Postcard<T>
35where
36 T: Debug + Serialize + for<'a> Deserialize<'a>,
37{
38 type SelfType<'a>
39 = T
40 where
41 Self: 'a;
42
43 type AsBytes<'a>
44 = Vec<u8>
45 where
46 Self: 'a;
47
48 fn fixed_width() -> Option<usize> {
49 None
50 }
51
52 fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a>
53 where
54 Self: 'a,
55 {
56 postcard::from_bytes(data).unwrap()
57 }
58
59 fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a>
60 where
61 Self: 'a,
62 Self: 'b,
63 {
64 postcard::to_allocvec(value).unwrap()
65 }
66
67 fn type_name() -> TypeName {
68 TypeName::new(&format!("Postcard<{}>", type_name::<T>()))
69 }
70}
71
72impl<T> Key for Postcard<T>
73where
74 T: Debug + Serialize + DeserializeOwned + Ord,
75{
76 fn compare(data1: &[u8], data2: &[u8]) -> Ordering {
77 Self::from_bytes(data1).cmp(&Self::from_bytes(data2))
78 }
79}