46 lines
1.3 KiB
Rust
46 lines
1.3 KiB
Rust
use crate::data::Id;
|
|||
|
|
use crate::data::entities::{Entity, EntityInner, Repository, WordData};
|
||
|
|
use crate::notification::MaybeError;
|
||
|
|
use parking_lot::{Mutex, RwLock};
|
||
|
|
use rusqlite::Connection;
|
||
|
|
use std::marker::PhantomData;
|
||
|
|
use std::sync::Arc;
|
||
|
|
|
||
|
|
#[derive(Clone)]
|
||
|
|
pub struct WordRepository {
|
||
|
|
inner: Arc<RwLock<WordRepositoryInner>>,
|
||
|
|
}
|
||
|
|
|
||
|
|
pub struct WordRepositoryInner {
|
||
|
|
cache: Vec<WordData>,
|
||
|
|
temporal: Vec<Entity<WordData, WordRepository>>,
|
||
|
|
next_temporal: u32,
|
||
|
|
connection: Arc<Mutex<Connection>>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Repository<WordData> for WordRepository {
|
||
|
|
fn get(&self, id: Id) -> Option<WordData> {
|
||
|
|
let inner = self.inner.read();
|
||
|
|
Some(inner.cache[inner.cache.binary_search_by(|w| w.id.cmp(&id)).ok()?].clone())
|
||
|
|
}
|
||
|
|
|
||
|
|
fn create(&mut self, new: WordData) -> Entity<WordData, WordRepository> {
|
||
|
|
let mut inner = self.inner.write();
|
||
|
|
let next_temporal = inner.next_temporal;
|
||
|
|
inner.next_temporal += 1;
|
||
|
|
drop(inner);
|
||
|
|
let entity = Entity {
|
||
|
|
inner: EntityInner {
|
||
|
|
id: next_temporal.into(),
|
||
|
|
real: false,
|
||
|
|
},
|
||
|
|
repo: self.clone(),
|
||
|
|
_phantom: PhantomData,
|
||
|
|
};
|
||
|
|
entity
|
||
|
|
}
|
||
|
|
|
||
|
|
fn save(&mut self, entity: EntityInner) -> MaybeError {
|
||
|
|
todo!()
|
||
|
|
}
|
||
|
|
}
|