1mod alter_table;
2mod data_row;
3mod function;
4mod index;
5mod metadata;
6mod transaction;
7
8pub trait GStore: Store + Index + Metadata + CustomFunction {}
9impl<S: Store + Index + Metadata + CustomFunction> GStore for S {}
10
11pub trait GStoreMut:
12 StoreMut + IndexMut + AlterTable + Transaction + CustomFunction + CustomFunctionMut
13{
14}
15impl<S: StoreMut + IndexMut + AlterTable + Transaction + CustomFunction + CustomFunctionMut>
16 GStoreMut for S
17{
18}
19
20pub use {
21 alter_table::{AlterTable, AlterTableError},
22 data_row::DataRow,
23 function::{CustomFunction, CustomFunctionMut},
24 index::{Index, IndexError, IndexMut},
25 metadata::{MetaIter, Metadata},
26 transaction::Transaction,
27};
28
29use {
30 crate::{
31 data::{Key, Schema},
32 executor::Referencing,
33 result::{Error, Result},
34 },
35 async_trait::async_trait,
36 futures::stream::Stream,
37 std::pin::Pin,
38};
39
40pub type RowIter<'a> = Pin<Box<dyn Stream<Item = Result<(Key, DataRow)>> + Send + 'a>>;
41
42#[async_trait]
44pub trait Store: Send + Sync {
45 async fn fetch_schema(&self, table_name: &str) -> Result<Option<Schema>>;
46
47 async fn fetch_all_schemas(&self) -> Result<Vec<Schema>>;
48
49 async fn fetch_data(&self, table_name: &str, key: &Key) -> Result<Option<DataRow>>;
50
51 async fn scan_data<'a>(&'a self, table_name: &str) -> Result<RowIter<'a>>;
52
53 async fn fetch_referencings(&self, table_name: &str) -> Result<Vec<Referencing>> {
54 let schemas = self.fetch_all_schemas().await?;
55
56 Ok(schemas
57 .into_iter()
58 .flat_map(|schema| {
59 let Schema {
60 table_name: referencing_table_name,
61 foreign_keys,
62 ..
63 } = schema;
64
65 foreign_keys.into_iter().filter_map(move |foreign_key| {
66 (foreign_key.referenced_table_name == table_name
67 && referencing_table_name != table_name)
68 .then_some(Referencing {
69 table_name: referencing_table_name.clone(),
70 foreign_key,
71 })
72 })
73 })
74 .collect())
75 }
76}
77
78#[async_trait]
81pub trait StoreMut: Send + Sync {
82 async fn insert_schema(&mut self, _schema: &Schema) -> Result<()> {
83 let msg = "[Storage] StoreMut::insert_schema is not supported".to_owned();
84
85 Err(Error::StorageMsg(msg))
86 }
87
88 async fn delete_schema(&mut self, _table_name: &str) -> Result<()> {
89 let msg = "[Storage] StoreMut::delete_schema is not supported".to_owned();
90
91 Err(Error::StorageMsg(msg))
92 }
93
94 async fn append_data(&mut self, _table_name: &str, _rows: Vec<DataRow>) -> Result<()> {
95 let msg = "[Storage] StoreMut::append_data is not supported".to_owned();
96
97 Err(Error::StorageMsg(msg))
98 }
99
100 async fn insert_data(&mut self, _table_name: &str, _rows: Vec<(Key, DataRow)>) -> Result<()> {
101 let msg = "[Storage] StoreMut::insert_data is not supported".to_owned();
102
103 Err(Error::StorageMsg(msg))
104 }
105
106 async fn delete_data(&mut self, _table_name: &str, _keys: Vec<Key>) -> Result<()> {
107 let msg = "[Storage] StoreMut::delete_data is not supported".to_owned();
108
109 Err(Error::StorageMsg(msg))
110 }
111}