use crate::db_type::{
check_key_type, check_key_type_from_key_definition, KeyDefinition, KeyOptions, Result, ToInput,
ToKey, ToKeyDefinition,
};
use crate::watch;
use crate::watch::query::internal;
use crate::watch::MpscReceiver;
use std::ops::RangeBounds;
pub struct WatchScan<'db, 'w> {
pub(crate) internal: &'w internal::InternalWatch<'db>,
}
impl WatchScan<'_, '_> {
pub fn primary(&self) -> WatchScanPrimary {
WatchScanPrimary {
internal: self.internal,
}
}
pub fn secondary(&self, key_def: impl ToKeyDefinition<KeyOptions>) -> WatchScanSecondary {
WatchScanSecondary {
key_def: key_def.key_definition(),
internal: self.internal,
}
}
}
pub struct WatchScanPrimary<'db, 'w> {
pub(crate) internal: &'w internal::InternalWatch<'db>,
}
impl WatchScanPrimary<'_, '_> {
pub fn all<T: ToInput>(&self) -> Result<(MpscReceiver<watch::Event>, u64)> {
self.internal.watch_primary_all::<T>()
}
pub fn range<'a>(
&self,
_range: impl RangeBounds<&'a [u8]> + 'a,
) -> Result<(MpscReceiver<watch::Event>, u64)> {
todo!()
}
pub fn start_with<T: ToInput>(
&self,
start_with: impl ToKey,
) -> Result<(MpscReceiver<watch::Event>, u64)> {
let model = T::native_db_model();
check_key_type(&model, &start_with)?;
self.internal.watch_primary_start_with::<T>(start_with)
}
}
pub struct WatchScanSecondary<'db, 'w> {
pub(crate) key_def: KeyDefinition<KeyOptions>,
pub(crate) internal: &'w internal::InternalWatch<'db>,
}
impl WatchScanSecondary<'_, '_> {
pub fn all<T: ToInput>(&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!()
}
pub fn start_with<T: ToInput>(
&self,
start_with: impl ToKey,
) -> Result<(MpscReceiver<watch::Event>, u64)> {
check_key_type_from_key_definition(&self.key_def, &start_with)?;
self.internal
.watch_secondary_start_with::<T>(&self.key_def, start_with)
}
}