native_db_32bit/watch/query/scan.rs
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
use crate::db_type::{
DatabaseKeyDefinition, DatabaseSecondaryKeyOptions, InnerKeyValue, Input, KeyDefinition, Result,
};
use crate::watch;
use crate::watch::query::internal;
use crate::watch::MpscReceiver;
use std::ops::RangeBounds;
/// Watch multiple values.
pub struct WatchScan<'db, 'w> {
pub(crate) internal: &'w internal::InternalWatch<'db>,
}
/// Watch multiple values.
impl WatchScan<'_, '_> {
/// Watch all values.
pub fn primary(&self) -> WatchScanPrimary {
WatchScanPrimary {
internal: &self.internal,
}
}
/// Watch all values by secondary key.
pub fn secondary(
&self,
key_def: impl KeyDefinition<DatabaseSecondaryKeyOptions>,
) -> WatchScanSecondary {
WatchScanSecondary {
key_def: key_def.database_key(),
internal: &self.internal,
}
}
}
/// Watch all values.
pub struct WatchScanPrimary<'db, 'w> {
pub(crate) internal: &'w internal::InternalWatch<'db>,
}
impl WatchScanPrimary<'_, '_> {
/// Watch all values.
///
/// # Example
/// ```rust
/// use native_db::*;
/// use native_model::{native_model, Model};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize)]
/// #[native_model(id=1, version=1)]
/// #[native_db]
/// struct Data {
/// #[primary_key]
/// id: u64,
/// }
///
/// fn main() -> Result<(), db_type::Error> {
/// let mut builder = DatabaseBuilder::new();
/// builder.define::<Data>()?;
/// let db = builder.create_in_memory()?;
///
/// // Open a read transaction
/// let r = db.r_transaction()?;
///
/// // Watch all values
/// let (_recv, _id) = db.watch().scan().primary().all::<Data>()?;
/// Ok(())
/// }
/// ```
pub fn all<T: Input>(&self) -> Result<(MpscReceiver<watch::Event>, u64)> {
self.internal.watch_primary_all::<T>()
}
/// **TODO: needs to be implemented**
pub fn range<'a>(
&self,
_range: impl RangeBounds<&'a [u8]> + 'a,
) -> Result<(MpscReceiver<watch::Event>, u64)> {
todo!()
}
/// Watch all values starting with the given key.
///
/// # Example
/// ```rust
/// use native_db::*;
/// use native_model::{native_model, Model};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize)]
/// #[native_model(id=1, version=1)]
/// #[native_db]
/// struct Data {
/// #[primary_key]
/// name: String,
/// }
///
/// fn main() -> Result<(), db_type::Error> {
/// let mut builder = DatabaseBuilder::new();
/// builder.define::<Data>()?;
/// let db = builder.create_in_memory()?;
///
/// // Open a read transaction
/// let r = db.r_transaction()?;
///
/// // Watch all values starting with "test"
/// let (_recv, _id) = db.watch().scan().primary().start_with::<Data>("test")?;
/// Ok(())
/// }
/// ```
pub fn start_with<T: Input>(
&self,
start_with: impl InnerKeyValue,
) -> Result<(MpscReceiver<watch::Event>, u64)> {
self.internal.watch_primary_start_with::<T>(start_with)
}
}
/// Watch all values by secondary key.
pub struct WatchScanSecondary<'db, 'w> {
pub(crate) key_def: DatabaseKeyDefinition<DatabaseSecondaryKeyOptions>,
pub(crate) internal: &'w internal::InternalWatch<'db>,
}
impl WatchScanSecondary<'_, '_> {
/// Watch all values by secondary key.
///
/// # Example
/// ```rust
/// use native_db::*;
/// use native_model::{native_model, Model};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize)]
/// #[native_model(id=1, version=1)]
/// #[native_db]
/// struct Data {
/// #[primary_key]
/// id: u64,
/// #[secondary_key]
/// name: String,
/// }
///
/// fn main() -> Result<(), db_type::Error> {
/// let mut builder = DatabaseBuilder::new();
/// builder.define::<Data>()?;
/// let db = builder.create_in_memory()?;
///
/// // Open a read transaction
/// let r = db.r_transaction()?;
///
/// // Watch all values by secondary key "name"
/// let (_recv, _id) = db.watch().scan().secondary(DataKey::name).all::<Data>()?;
/// Ok(())
/// }
/// ```
pub fn all<'ws, T: Input>(&'ws self) -> Result<(MpscReceiver<watch::Event>, u64)> {
self.internal.watch_secondary_all::<T>(&self.key_def)
}
pub fn range<'a, 'ws>(
&'ws self,
_range: impl RangeBounds<&'a [u8]> + 'a,
) -> Result<(MpscReceiver<watch::Event>, u64)> {
todo!()
}
/// Watch all values starting with the given key.
///
/// # Example
/// ```rust
/// use native_db::*;
/// use native_model::{native_model, Model};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize)]
/// #[native_model(id=1, version=1)]
/// #[native_db]
/// struct Data {
/// #[primary_key]
/// id: u64,
/// #[secondary_key]
/// name: String,
/// }
///
/// fn main() -> Result<(), db_type::Error> {
/// let mut builder = DatabaseBuilder::new();
/// builder.define::<Data>()?;
/// let db = builder.create_in_memory()?;
///
/// // Open a read transaction
/// let r = db.r_transaction()?;
///
/// // Watch all values by secondary key "name" starting with "test"
/// let (_recv, _id) = db.watch().scan().secondary(DataKey::name).start_with::<Data>("test")?;
/// Ok(())
/// }
/// ```
pub fn start_with<T: Input>(
&self,
start_with: impl InnerKeyValue,
) -> Result<(MpscReceiver<watch::Event>, u64)> {
self.internal
.watch_secondary_start_with::<T>(&self.key_def, start_with)
}
}