gluesql_redb_storage/
lib.rs

1#![deny(clippy::str_to_string)]
2
3mod core;
4mod error;
5
6use {
7    async_trait::async_trait,
8    core::StorageCore,
9    gluesql_core::{
10        data::{Key, Schema},
11        error::Result,
12        store::{
13            AlterTable, CustomFunction, CustomFunctionMut, DataRow, Index, IndexMut, Metadata,
14            RowIter, Store, StoreMut, Transaction,
15        },
16    },
17    redb::Database,
18    std::path::Path,
19};
20
21pub struct RedbStorage(StorageCore);
22
23impl RedbStorage {
24    pub fn new<P: AsRef<Path>>(filename: P) -> Result<Self> {
25        StorageCore::new(filename).map(Self).map_err(Into::into)
26    }
27
28    pub fn from_database(db: Database) -> Self {
29        Self(StorageCore::from_database(db))
30    }
31}
32
33#[async_trait]
34impl Store for RedbStorage {
35    async fn fetch_all_schemas(&self) -> Result<Vec<Schema>> {
36        self.0.fetch_all_schemas().map_err(Into::into)
37    }
38
39    async fn fetch_schema(&self, table_name: &str) -> Result<Option<Schema>> {
40        self.0.fetch_schema(table_name).map_err(Into::into)
41    }
42
43    async fn fetch_data(&self, table_name: &str, key: &Key) -> Result<Option<DataRow>> {
44        self.0.fetch_data(table_name, key).map_err(Into::into)
45    }
46
47    async fn scan_data<'a>(&'a self, table_name: &str) -> Result<RowIter<'a>> {
48        self.0.scan_data(table_name).map_err(Into::into)
49    }
50}
51
52#[async_trait]
53impl StoreMut for RedbStorage {
54    async fn insert_schema(&mut self, schema: &Schema) -> Result<()> {
55        self.0.insert_schema(schema).map_err(Into::into)
56    }
57
58    async fn delete_schema(&mut self, table_name: &str) -> Result<()> {
59        self.0.delete_schema(table_name).map_err(Into::into)
60    }
61
62    async fn append_data(&mut self, table_name: &str, rows: Vec<DataRow>) -> Result<()> {
63        self.0.append_data(table_name, rows).map_err(Into::into)
64    }
65
66    async fn insert_data(&mut self, table_name: &str, rows: Vec<(Key, DataRow)>) -> Result<()> {
67        self.0.insert_data(table_name, rows).map_err(Into::into)
68    }
69
70    async fn delete_data(&mut self, table_name: &str, keys: Vec<Key>) -> Result<()> {
71        self.0.delete_data(table_name, keys).map_err(Into::into)
72    }
73}
74
75#[async_trait]
76impl Transaction for RedbStorage {
77    async fn begin(&mut self, autocommit: bool) -> Result<bool> {
78        self.0.begin(autocommit).map_err(Into::into)
79    }
80
81    async fn rollback(&mut self) -> Result<()> {
82        self.0.rollback().map_err(Into::into)
83    }
84
85    async fn commit(&mut self) -> Result<()> {
86        self.0.commit().map_err(Into::into)
87    }
88}
89
90impl AlterTable for RedbStorage {}
91impl Index for RedbStorage {}
92impl IndexMut for RedbStorage {}
93impl Metadata for RedbStorage {}
94impl CustomFunction for RedbStorage {}
95impl CustomFunctionMut for RedbStorage {}