Query partial code
This commit is contained in:
1 parent
0f2090af77
commit
89aa1abc13
1 file changed
+37
-4
@@ -5,6 +5,8 @@ use parking_lot::{Mutex, RwLock};
|
||||
use rusqlite::Connection;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
use std::rc::Weak;
|
||||
use std::slice::Iter;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub trait Repository<T, D>
|
||||
@@ -20,6 +22,32 @@ where
|
||||
fn delete(&mut self, handle: EntityHandle<T>) -> MaybeError;
|
||||
fn init(&mut self) -> MaybeError;
|
||||
fn reload(&mut self) -> MaybeError;
|
||||
fn query(&mut self, rule: fn(&T) -> bool) -> Query<T, Self, D> where Self: Sized;
|
||||
}
|
||||
|
||||
struct QueryManger<T, R, D>
|
||||
where
|
||||
R: Repository<T, D>,
|
||||
D: DbLayer<T>,
|
||||
{
|
||||
queries: Mutex<Vec<Weak<Query<T, R, D>>>>,
|
||||
}
|
||||
|
||||
pub struct Query<T, R, D>
|
||||
where
|
||||
R: Repository<T, D>, D: DbLayer<T>
|
||||
{
|
||||
cache: Vec<EntityHandle<T>>,
|
||||
rule: fn(&T) -> bool,
|
||||
repo: Arc<R>,
|
||||
_marker: PhantomData<D>,
|
||||
}
|
||||
|
||||
impl<T, R: Repository<T, D>, D: DbLayer<T>> Query<T, R, D>
|
||||
{
|
||||
fn iter<'a>(&mut self, data_repository_inner: &'a R) -> Iter<'_, &'a T> {
|
||||
self.cache.iter_mut().filter_map(|e| data_repository_inner.get_ref(e))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait BatchedRepository<T, D>
|
||||
@@ -47,19 +75,20 @@ where
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DataRepository<T, D> {
|
||||
pub struct DataRepository<T: std::clone::Clone + EntityData, D: DbLayer<T>> {
|
||||
inner: Arc<RwLock<DataRepositoryInner<T, D>>>,
|
||||
}
|
||||
|
||||
pub struct DataRepositoryInner<T, D> {
|
||||
pub struct DataRepositoryInner<T: std::clone::Clone + EntityData, D: DbLayer<T>> {
|
||||
cache: Vec<T>,
|
||||
temporal: Vec<T>,
|
||||
next_temporal: u32,
|
||||
connection: Arc<Mutex<Connection>>,
|
||||
data_layer: D,
|
||||
query_manger: QueryManger<T, Self, D>
|
||||
}
|
||||
|
||||
impl<T, D> Deref for DataRepository<T, D> {
|
||||
impl<T: std::clone::Clone + EntityData, D: DbLayer<T>> Deref for DataRepository<T, D> {
|
||||
type Target = Arc<RwLock<DataRepositoryInner<T, D>>>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
@@ -196,9 +225,13 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
|
||||
self.init()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn query(&mut self, rule: fn(&T) -> bool) -> Query<T, Self, D> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository<T, D>
|
||||
for DataRepositoryInner<T, D>
|
||||
for DataRepositoryInner<T, D>
|
||||
{
|
||||
fn save_many(&mut self, handles: &mut [EntityHandle<T>]) -> MaybeError {
|
||||
let mut temp = Vec::with_capacity(handles.len() / 2);
|
||||
|
||||
Reference in new issue
Block a user