1use crate::error::DbxResult;
4use crate::storage::StorageBackend;
5use crate::storage::encryption::wos::EncryptedWosBackend;
6use crate::storage::memory_wos::InMemoryWosBackend;
7use crate::storage::native_wos::NativeWosBackend;
8use std::sync::Arc;
9
10pub enum WosVariant {
15 Encrypted(Arc<EncryptedWosBackend>),
16 InMemory(Arc<InMemoryWosBackend>),
17 Native(Arc<NativeWosBackend>),
18}
19
20impl WosVariant {
21 pub fn insert(&self, table: &str, key: &[u8], value: &[u8]) -> DbxResult<()> {
22 match self {
23 Self::Encrypted(wos) => wos.as_ref().insert(table, key, value),
24 Self::InMemory(wos) => wos.as_ref().insert(table, key, value),
25 Self::Native(wos) => wos.as_ref().insert(table, key, value),
26 }
27 }
28
29 pub fn insert_batch(&self, table: &str, rows: Vec<(Vec<u8>, Vec<u8>)>) -> DbxResult<()> {
30 match self {
31 Self::Encrypted(wos) => wos.as_ref().insert_batch(table, rows),
32 Self::InMemory(wos) => wos.as_ref().insert_batch(table, rows),
33 Self::Native(wos) => wos.as_ref().insert_batch(table, rows),
34 }
35 }
36
37 pub fn get(&self, table: &str, key: &[u8]) -> DbxResult<Option<Vec<u8>>> {
38 match self {
39 Self::Encrypted(wos) => wos.as_ref().get(table, key),
40 Self::InMemory(wos) => wos.as_ref().get(table, key),
41 Self::Native(wos) => wos.as_ref().get(table, key),
42 }
43 }
44
45 pub fn delete(&self, table: &str, key: &[u8]) -> DbxResult<bool> {
46 match self {
47 Self::Encrypted(wos) => wos.as_ref().delete(table, key),
48 Self::InMemory(wos) => wos.as_ref().delete(table, key),
49 Self::Native(wos) => wos.as_ref().delete(table, key),
50 }
51 }
52
53 pub fn scan<R: std::ops::RangeBounds<Vec<u8>> + Clone>(
54 &self,
55 table: &str,
56 range: R,
57 ) -> DbxResult<Vec<(Vec<u8>, Vec<u8>)>> {
58 match self {
59 Self::Encrypted(wos) => wos.as_ref().scan(table, range),
60 Self::InMemory(wos) => wos.as_ref().scan(table, range),
61 Self::Native(wos) => wos.as_ref().scan(table, range),
62 }
63 }
64
65 pub fn scan_one<R: std::ops::RangeBounds<Vec<u8>> + Clone>(
66 &self,
67 table: &str,
68 range: R,
69 ) -> DbxResult<Option<(Vec<u8>, Vec<u8>)>> {
70 match self {
71 Self::Encrypted(wos) => wos.as_ref().scan_one(table, range),
72 Self::InMemory(wos) => wos.as_ref().scan_one(table, range),
73 Self::Native(wos) => wos.as_ref().scan_one(table, range),
74 }
75 }
76
77 pub fn flush(&self) -> DbxResult<()> {
78 match self {
79 Self::Encrypted(wos) => wos.as_ref().flush(),
80 Self::InMemory(wos) => wos.as_ref().flush(),
81 Self::Native(wos) => wos.as_ref().flush(),
82 }
83 }
84
85 pub fn count(&self, table: &str) -> DbxResult<usize> {
86 match self {
87 Self::Encrypted(wos) => wos.as_ref().count(table),
88 Self::InMemory(wos) => wos.as_ref().count(table),
89 Self::Native(wos) => wos.as_ref().count(table),
90 }
91 }
92
93 pub fn table_names(&self) -> DbxResult<Vec<String>> {
94 match self {
95 Self::Encrypted(wos) => wos.as_ref().table_names(),
96 Self::InMemory(wos) => wos.as_ref().table_names(),
97 Self::Native(wos) => wos.as_ref().table_names(),
98 }
99 }
100
101 pub fn save_schema_metadata(
107 &self,
108 table: &str,
109 schema: &arrow::datatypes::Schema,
110 ) -> DbxResult<()> {
111 match self {
112 Self::Native(wos) => crate::engine::metadata::save_schema(wos.as_ref(), table, schema),
113 Self::Encrypted(_) | Self::InMemory(_) => Ok(()),
114 }
115 }
116
117 pub fn delete_schema_metadata(&self, table: &str) -> DbxResult<()> {
119 match self {
120 Self::Native(wos) => crate::engine::metadata::delete_schema(wos.as_ref(), table),
121 Self::Encrypted(_) | Self::InMemory(_) => Ok(()),
122 }
123 }
124
125 pub fn save_index_metadata(
127 &self,
128 index_name: &str,
129 table: &str,
130 column: &str,
131 ) -> DbxResult<()> {
132 match self {
133 Self::Native(wos) => {
134 crate::engine::metadata::save_index(wos.as_ref(), index_name, table, column)
135 }
136 Self::Encrypted(_) | Self::InMemory(_) => Ok(()),
137 }
138 }
139
140 pub fn delete_index_metadata(&self, index_name: &str) -> DbxResult<()> {
142 match self {
143 Self::Native(wos) => crate::engine::metadata::delete_index(wos.as_ref(), index_name),
144 Self::Encrypted(_) | Self::InMemory(_) => Ok(()),
145 }
146 }
147}