native_db_32bit/watch/query/
scan.rs

1use crate::db_type::{
2    DatabaseKeyDefinition, DatabaseSecondaryKeyOptions, InnerKeyValue, Input, KeyDefinition, Result,
3};
4use crate::watch;
5use crate::watch::query::internal;
6use crate::watch::MpscReceiver;
7use std::ops::RangeBounds;
8
9/// Watch multiple values.
10pub struct WatchScan<'db, 'w> {
11    pub(crate) internal: &'w internal::InternalWatch<'db>,
12}
13
14/// Watch multiple values.
15impl WatchScan<'_, '_> {
16    /// Watch all values.
17    pub fn primary(&self) -> WatchScanPrimary {
18        WatchScanPrimary {
19            internal: &self.internal,
20        }
21    }
22
23    /// Watch all values by secondary key.
24    pub fn secondary(
25        &self,
26        key_def: impl KeyDefinition<DatabaseSecondaryKeyOptions>,
27    ) -> WatchScanSecondary {
28        WatchScanSecondary {
29            key_def: key_def.database_key(),
30            internal: &self.internal,
31        }
32    }
33}
34
35/// Watch all values.
36pub struct WatchScanPrimary<'db, 'w> {
37    pub(crate) internal: &'w internal::InternalWatch<'db>,
38}
39
40impl WatchScanPrimary<'_, '_> {
41    /// Watch all values.
42    ///
43    /// # Example
44    /// ```rust
45    /// use native_db::*;
46    /// use native_model::{native_model, Model};
47    /// use serde::{Deserialize, Serialize};
48    ///
49    /// #[derive(Serialize, Deserialize)]
50    /// #[native_model(id=1, version=1)]
51    /// #[native_db]
52    /// struct Data {
53    ///     #[primary_key]
54    ///     id: u64,
55    /// }
56    ///
57    /// fn main() -> Result<(), db_type::Error> {
58    ///     let mut builder = DatabaseBuilder::new();
59    ///     builder.define::<Data>()?;
60    ///     let db = builder.create_in_memory()?;
61    ///     
62    ///     // Open a read transaction
63    ///     let r = db.r_transaction()?;
64    ///     
65    ///     // Watch all values
66    ///     let (_recv, _id) = db.watch().scan().primary().all::<Data>()?;
67    ///     Ok(())
68    /// }
69    /// ```
70    pub fn all<T: Input>(&self) -> Result<(MpscReceiver<watch::Event>, u64)> {
71        self.internal.watch_primary_all::<T>()
72    }
73
74    /// **TODO: needs to be implemented**
75    pub fn range<'a>(
76        &self,
77        _range: impl RangeBounds<&'a [u8]> + 'a,
78    ) -> Result<(MpscReceiver<watch::Event>, u64)> {
79        todo!()
80    }
81
82    /// Watch all values starting with the given key.
83    ///
84    /// # Example
85    /// ```rust
86    /// use native_db::*;
87    /// use native_model::{native_model, Model};
88    /// use serde::{Deserialize, Serialize};
89    ///
90    /// #[derive(Serialize, Deserialize)]
91    /// #[native_model(id=1, version=1)]
92    /// #[native_db]
93    /// struct Data {
94    ///     #[primary_key]
95    ///     name: String,
96    /// }
97    ///
98    /// fn main() -> Result<(), db_type::Error> {
99    ///     let mut builder = DatabaseBuilder::new();
100    ///     builder.define::<Data>()?;
101    ///     let db = builder.create_in_memory()?;
102    ///     
103    ///     // Open a read transaction
104    ///     let r = db.r_transaction()?;
105    ///     
106    ///     // Watch all values starting with "test"
107    ///     let (_recv, _id) = db.watch().scan().primary().start_with::<Data>("test")?;
108    ///     Ok(())
109    /// }
110    /// ```
111    pub fn start_with<T: Input>(
112        &self,
113        start_with: impl InnerKeyValue,
114    ) -> Result<(MpscReceiver<watch::Event>, u64)> {
115        self.internal.watch_primary_start_with::<T>(start_with)
116    }
117}
118
119/// Watch all values by secondary key.
120pub struct WatchScanSecondary<'db, 'w> {
121    pub(crate) key_def: DatabaseKeyDefinition<DatabaseSecondaryKeyOptions>,
122    pub(crate) internal: &'w internal::InternalWatch<'db>,
123}
124
125impl WatchScanSecondary<'_, '_> {
126    /// Watch all values by secondary key.
127    ///
128    /// # Example
129    /// ```rust
130    /// use native_db::*;
131    /// use native_model::{native_model, Model};
132    /// use serde::{Deserialize, Serialize};
133    ///
134    /// #[derive(Serialize, Deserialize)]
135    /// #[native_model(id=1, version=1)]
136    /// #[native_db]
137    /// struct Data {
138    ///     #[primary_key]
139    ///     id: u64,
140    ///     #[secondary_key]
141    ///     name: String,
142    /// }
143    ///
144    /// fn main() -> Result<(), db_type::Error> {
145    ///     let mut builder = DatabaseBuilder::new();
146    ///     builder.define::<Data>()?;
147    ///     let db = builder.create_in_memory()?;
148    ///     
149    ///     // Open a read transaction
150    ///     let r = db.r_transaction()?;
151    ///     
152    ///     // Watch all values by secondary key "name"
153    ///     let (_recv, _id) = db.watch().scan().secondary(DataKey::name).all::<Data>()?;
154    ///     Ok(())
155    /// }
156    /// ```
157    pub fn all<'ws, T: Input>(&'ws self) -> Result<(MpscReceiver<watch::Event>, u64)> {
158        self.internal.watch_secondary_all::<T>(&self.key_def)
159    }
160
161    pub fn range<'a, 'ws>(
162        &'ws self,
163        _range: impl RangeBounds<&'a [u8]> + 'a,
164    ) -> Result<(MpscReceiver<watch::Event>, u64)> {
165        todo!()
166    }
167
168    /// Watch all values starting with the given key.
169    ///
170    /// # Example
171    /// ```rust
172    /// use native_db::*;
173    /// use native_model::{native_model, Model};
174    /// use serde::{Deserialize, Serialize};
175    ///
176    /// #[derive(Serialize, Deserialize)]
177    /// #[native_model(id=1, version=1)]
178    /// #[native_db]
179    /// struct Data {
180    ///     #[primary_key]
181    ///     id: u64,
182    ///     #[secondary_key]
183    ///     name: String,
184    /// }
185    ///
186    /// fn main() -> Result<(), db_type::Error> {
187    ///     let mut builder = DatabaseBuilder::new();
188    ///     builder.define::<Data>()?;
189    ///     let db = builder.create_in_memory()?;
190    ///     
191    ///     // Open a read transaction
192    ///     let r = db.r_transaction()?;
193    ///     
194    ///     // Watch all values by secondary key "name" starting with "test"
195    ///     let (_recv, _id) = db.watch().scan().secondary(DataKey::name).start_with::<Data>("test")?;
196    ///     Ok(())
197    /// }
198    /// ```
199    pub fn start_with<T: Input>(
200        &self,
201        start_with: impl InnerKeyValue,
202    ) -> Result<(MpscReceiver<watch::Event>, u64)> {
203        self.internal
204            .watch_secondary_start_with::<T>(&self.key_def, start_with)
205    }
206}