New repository methods

This commit is contained in:
micialware committed 2026-08-31 14:35:11 +03:00
1 parent 11d5144db5
commit 55bdc5a7ad
5 files changed
+63 -41

No files matched your search

+30 -5
View File
@@ -1,5 +1,5 @@
use crate::data::Id;
use crate::data::entities::{EntityData, EntityHandle};
use crate::data::entities::{EntityData, EntityHandle, WordData};
use crate::helpers::into_result;
use crate::notification::{MaybeError, NotificationData};
use parking_lot::{Mutex, RwLock};
@@ -24,9 +24,11 @@ where
fn create_now(&mut self, new: T) -> Result<EntityHandle<T>, NotificationData>;
fn save(&mut self, entity: &mut EntityHandle<T>) -> MaybeError;
fn delete(&mut self, handle: EntityHandle<T>) -> MaybeError;
fn find<F>(&self, predicate: F) -> Option<EntityHandle<T>> where F : Fn(&T) -> bool;
fn init(&mut self) -> MaybeError;
fn start(&mut self) -> MaybeError;
fn reload(&mut self) -> MaybeError;
fn shifts(&self) -> usize;
}
struct QueryManger<T, R, D>
@@ -101,14 +103,17 @@ where
rule: Box<dyn Fn(&T) -> bool + Send + Sync>,
parallel: bool,
_marker: PhantomData<(R, D)>,
shift_stamp: u16,
shift_stamp: usize,
}
impl<T: EntityData + Clone, R: Repository<T, D>, D: DbLayer<T>> Query<T, R, D> {
pub fn iter<'a>(&mut self, data_repository_inner: &'a R) -> impl Iterator<Item = &'a T>
impl<T: EntityData + Clone + Send + Sync, R: Repository<T, D>, D: DbLayer<T>> Query<T, R, D> {
pub fn iter<'a>(&mut self, data_repository_inner: &'a DataRepositoryInner<T, D>) -> impl Iterator<Item = &'a T>
where
T: 'a,
{
if self.shift_stamp != data_repository_inner.shifts() {
self.load(data_repository_inner);
}
self.cache
.iter_mut()
.filter_map(move |e| data_repository_inner.get_ref(e))
@@ -197,7 +202,7 @@ pub struct DataRepositoryInner<T: std::clone::Clone + EntityData, D: DbLayer<T>>
data_layer: D,
query_manger: QueryManger<T, Self, D>,
version: u16,
shifts: u16,
shifts: usize,
}
impl<T: std::clone::Clone + EntityData, D: DbLayer<T> + std::default::Default> DataRepository<T, D> {
@@ -389,6 +394,22 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
Ok(())
}
fn find<F>(&self, predicate: F) -> Option<EntityHandle<T>>
where
F: Fn(&T) -> bool
{
if let Some(index) = self.cache.iter().position(|entity| predicate(entity)){
let value = &self.cache[index];
return Some(EntityHandle::new(value.id(), index, true))
}
if let Some(index) = self.temporal.iter().position(|entity| predicate(entity)){
let value = &self.temporal[index];
return Some(EntityHandle::new(value.id(), index, false))
}
None
}
fn init(&mut self) -> MaybeError {
self.data_layer
.load_all(&mut self.cache, &self.connection.read())?;
@@ -411,6 +432,10 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
self.init()?;
Ok(())
}
fn shifts(&self) -> usize {
self.shifts
}
}
impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository<T, D>
for DataRepositoryInner<T, D>