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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use crate::watch;
use crate::{Error, KeyDefinition, ReadOnlyTransaction, Result, SDBItem, Transaction};
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::Path;
use std::sync::atomic::AtomicU64;
use std::sync::{Arc, Mutex, RwLock};
use std::u64;
use crate::watch::MpscReceiver;
use crate::builder::Builder;
/// The `Db` struct represents a database instance. It allows add **schema**, create **transactions** and **watcher**.
pub struct Db {
pub(crate) instance: redb::Database,
pub(crate) table_definitions:
HashMap<&'static str, redb::TableDefinition<'static, &'static [u8], &'static [u8]>>,
pub(crate) watchers: Arc<RwLock<watch::Watchers>>,
pub(crate) watchers_counter_id: AtomicU64,
}
impl Db {
/// Creates a new [Db] instance using the given path.
///
/// Use [redb::Builder.create(...)](https://docs.rs/redb/latest/redb/struct.Builder.html#method.create)
pub fn create(path: impl AsRef<Path>) -> Result<Self> {
Builder::new().create(path)
}
/// Creates a new [Db] instance using a temporary directory with the given path.
///
/// Example: `Db::create_tmp('project/my_db')` will create the db to `/tmp/project/my_db`.
///
/// Use [redb::Builder.create(...)](https://docs.rs/redb/latest/redb/struct.Builder.html#method.create)
pub fn create_tmp(path: impl AsRef<Path>) -> Result<Self> {
Builder::new().create_tmp(path)
}
/// Opens an existing [Db] instance using the given path.
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
Builder::new().open(path)
}
/// Opens an existing [Db] instance using a temporary directory with the given path.
pub fn open_tmp(path: impl AsRef<Path>) -> Result<Self> {
Builder::new().open_tmp(path)
}
/// Defines a table using the given schema.
///
/// # Example
/// ```
/// use serde::{Deserialize, Serialize};
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
/// #[struct_db(fn_primary_key(p_key))]
/// struct Data(u32);
/// impl Data {pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()}}
///
/// fn main() {
/// let mut db = Db::create_tmp("my_db_as").unwrap();
/// // Initialize the table
/// db.define::<Data>();
/// }
pub fn define<T: SDBItem>(&mut self) {
let schema = T::struct_db_schema();
let main_table_name = schema.table_name;
let main_table_definition = redb::TableDefinition::new(main_table_name);
self.table_definitions
.insert(main_table_name, main_table_definition);
for secondary_table_name in schema.secondary_tables_name {
let secondary_table_definition = redb::TableDefinition::new(secondary_table_name);
self.table_definitions
.insert(secondary_table_name, secondary_table_definition);
}
}
}
impl Db {
/// Creates a new read-write transaction.
///
/// # Example
/// ```
/// use serde::{Deserialize, Serialize};
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
/// #[struct_db(fn_primary_key(p_key))]
/// struct Data(u32);
/// impl Data {pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()}}
///
/// fn main() {
/// let mut db = Db::create_tmp("my_db_t").unwrap();
/// db.define::<Data>();
///
/// // Use transaction to insert a new data
/// let mut txn = db.transaction().unwrap();
/// {
/// let mut data = Data(42);
/// let mut tables = txn.tables();
/// tables.insert(&txn, data).unwrap();
/// }
/// txn.commit().unwrap(); // /!\ Don't forget to commit
///
/// // Use transaction to update a data
/// let mut txn = db.transaction().unwrap();
/// {
/// let mut tables = txn.tables();
/// let (new_data, old_data) = {
/// let old_data = tables.primary_get::<Data>(&txn, &42_u32.to_be_bytes()).unwrap().unwrap();
/// let mut new_data = old_data.clone();
/// new_data.0 = 43;
/// (new_data, old_data)
/// };
/// tables.update(&txn, old_data, new_data).unwrap();
/// }
/// txn.commit().unwrap(); // /!\ Don't forget to commit
///
/// // Use transaction to delete a data
/// let mut txn = db.transaction().unwrap();
/// {
/// let mut tables = txn.tables();
/// let data = tables.primary_get::<Data>(&txn, &43_u32.to_be_bytes()).unwrap().unwrap();
/// tables.remove(&txn, data).unwrap();
/// }
/// txn.commit().unwrap(); // /!\ Don't forget to commit
/// }
pub fn transaction(&self) -> Result<Transaction> {
let txn = self.instance.begin_write()?;
let write_txn = Transaction {
table_definitions: &self.table_definitions,
txn,
watcher: &self.watchers,
batch: RefCell::new(watch::Batch::new()),
};
Ok(write_txn)
}
/// Creates a new read-only transaction.
///
/// # Example
/// ```
/// use serde::{Deserialize, Serialize};
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
/// #[struct_db(fn_primary_key(p_key))]
/// struct Data(u32);
/// impl Data {pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()}}
///
/// fn main() {
/// let mut db = Db::create_tmp("my_db_rt").unwrap();
/// db.define::<Data>();
///
/// // Insert a new data
/// let mut txn = db.transaction().unwrap();
/// {
/// let mut tables = txn.tables();
/// tables.insert(&txn, Data(42)).unwrap();
/// }
/// txn.commit().unwrap(); // /!\ Don't forget to commit
///
/// let txn_read = db.read_transaction().unwrap();
/// let mut tables = txn_read.tables();
/// let len = tables.len::<Data>(&txn_read).unwrap();
/// assert_eq!(len, 1);
/// }
pub fn read_transaction(&self) -> Result<ReadOnlyTransaction> {
let txn = self.instance.begin_read()?;
let read_txn = ReadOnlyTransaction {
table_definitions: &self.table_definitions,
txn,
};
Ok(read_txn)
}
}
impl Db {
fn generate_watcher_id(&self) -> Result<u64> {
let value = self
.watchers_counter_id
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
if value == u64::MAX {
Err(Error::MaxWatcherReached.into())
} else {
Ok(value)
}
}
fn watch_generic(
&self,
table_filter: watch::TableFilter,
) -> Result<(MpscReceiver<watch::Event>, u64)> {
#[cfg(not(feature = "tokio"))]
let (event_sender, event_receiver) = std::sync::mpsc::channel();
#[cfg(feature = "tokio")]
let (event_sender, event_receiver) = tokio::sync::mpsc::unbounded_channel();
let event_sender = Arc::new(Mutex::new(event_sender));
let id = self.generate_watcher_id()?;
let mut watchers = self.watchers.write().unwrap();
watchers.add_sender(id, &table_filter, Arc::clone(&event_sender));
drop(watchers);
Ok((event_receiver, id))
}
/// Watches for changes in the specified table for the given primary key.
/// If the argument `key` is `None` you will receive all the events from the table.
/// Otherwise you will receive only the events for the given primary key.
///
/// Supported channels to to receive changes:
/// - [`std::sync::mpsc::Receiver`](https://doc.rust-lang.org/std/sync/mpsc/struct.Receiver.html) by default
/// - [`tokio::sync::mpsc::UnboundedReceiver`](https://docs.rs/tokio/latest/tokio/sync/mpsc/struct.UnboundedReceiver.html) with the feature (`async_tokio`).
///
/// To unregister the watcher you need to call [`unwatch`](Db::unwatch)
/// with the returned `id`.
///
/// Get data from the event, use `inner()` method on:
/// - [`watch::Insert::inner`](watch::Insert::inner)
/// - [`watch::Update::inner_new`](watch::Update::inner_new) to get the updated data
/// - [`watch::Update::inner_old`](watch::Update::inner_old) to get the old data
/// - [`watch::Delete::inner`](watch::Delete::inner)
///
/// # Example
/// ```
/// use serde::{Deserialize, Serialize};
/// use struct_db::*;
///
/// #[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
/// #[struct_db(fn_primary_key(p_key))]
/// struct Data(u32);
/// impl Data {pub fn p_key(&self) -> Vec<u8> {self.0.to_be_bytes().to_vec()}}
///
/// fn main() {
/// let mut db = Db::create_tmp("my_db").unwrap();
/// db.define::<Data>();
///
/// // None you will receive all the events from Data.
/// let (event_receiver, _id) = db.primary_watch::<Data>(None).unwrap();
///
/// // Add a new data
/// let mut txn = db.transaction().unwrap();
/// {
/// let mut tables = txn.tables();
/// tables.insert(&txn, Data(42)).unwrap();
/// }
/// txn.commit().unwrap(); // /!\ Don't forget to commit
///
/// // Wait for the event
/// for _ in 0..1 {
/// // With the feature "async_tokio" you can use async/await pattern
/// let event = event_receiver.recv().unwrap();
/// if let watch::Event::Insert(insert) = event {
/// let data = insert.inner::<Data>();
/// assert_eq!(data, Data(42));
/// }
/// }
/// }
pub fn primary_watch<T: SDBItem>(
&self,
key: Option<&[u8]>,
) -> Result<(MpscReceiver<watch::Event>, u64)> {
let table_name = T::struct_db_schema().table_name;
let table_filter = watch::TableFilter::new_primary(table_name.as_bytes(), key);
self.watch_generic(table_filter)
}
/// Watches for changes in the specified table for the given prefix.
/// You will receive all the events for the given prefix.
///
/// To unregister the watcher you need to call [`unwatch`](Db::unwatch)
/// with the returned `id`.
///
/// # Example
/// - Similar to [`primary_watch`](#method.primary_watch) but with a prefix.
pub fn primary_watch_start_with<T: SDBItem>(
&self,
key_prefix: &[u8],
) -> Result<(MpscReceiver<watch::Event>, u64)> {
let table_name = T::struct_db_schema().table_name;
let table_filter =
watch::TableFilter::new_primary_start_with(table_name.as_bytes(), key_prefix);
self.watch_generic(table_filter)
}
/// Watches for changes in the specified table for the given secondary key.
/// If the argument `key` is `None` you will receive all the events from the table.
/// Otherwise you will receive only the events for the given secondary key.
///
/// To unregister the watcher you need to call [`unwatch`](Db::unwatch)
/// with the returned `id`.
///
/// # Example
/// - Similar to [`primary_watch`](#method.primary_watch) but with a secondary key.
pub fn secondary_watch<T: SDBItem>(
&self,
key_def: impl KeyDefinition,
key: Option<&[u8]>,
) -> Result<(MpscReceiver<watch::Event>, u64)> {
let table_name = T::struct_db_schema().table_name;
let table_filter = watch::TableFilter::new_secondary(table_name.as_bytes(), key_def, key);
self.watch_generic(table_filter)
}
/// Watches for changes in the specified table for the given prefix.
/// You will receive all the events for the given prefix.
///
/// To unregister the watcher you need to call [`unwatch`](Db::unwatch)
/// with the returned `id`.
///
/// # Example
/// - Similar to [`primary_watch`](#method.primary_watch) but with a secondary key and a prefix.
pub fn secondary_watch_start_with<T: SDBItem>(
&self,
key_def: impl KeyDefinition,
key_prefix: &[u8],
) -> Result<(MpscReceiver<watch::Event>, u64)> {
let table_name = T::struct_db_schema().table_name;
let table_filter = watch::TableFilter::new_secondary_start_with(
table_name.as_bytes(),
key_def,
key_prefix,
);
self.watch_generic(table_filter)
}
/// Unwatch the given `id`.
/// You can get the `id` from the return value of [`primary_watch`](#method.primary_watch).
/// If the `id` is not valid anymore, this function will do nothing.
/// If the `id` is valid, the corresponding watcher will be removed.
pub fn unwatch(&self, id: u64) -> Result<()> {
let mut watchers = self.watchers.write().unwrap();
watchers.remove_sender(id);
Ok(())
}
}