Partial dictionary rewrite

This commit is contained in:
micialware committed 2026-09-02 00:19:58 +03:00
1 parent 1509947cb0
commit f2035ca749
12 files changed
+304 -363

No files matched your search

+10 -51
View File
@@ -6,9 +6,9 @@ use chrono::{DateTime, NaiveDate, Utc};
use fsrs::MemoryState;
use litemap::LiteMap;
use log::info;
use rayon::prelude::*;
use rhai::{Engine, Scope};
use rhai::Engine;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::time::Instant;
@@ -77,8 +77,6 @@ pub struct DeckSettings {
pub forward: String,
pub backward: String,
pub filter: String,
pub count: Option<usize>,
pub open_mode: OrderMode,
}
impl EntityData for DeckSettings {
@@ -99,8 +97,6 @@ impl DeckSettings {
forward: "".to_string(),
backward: "".to_string(),
filter: "true".to_string(),
count: None,
open_mode: OrderMode::Default,
}
}
pub fn check_filter(&self) -> bool {
@@ -110,50 +106,6 @@ impl DeckSettings {
info!("Compile time is {:?}", time.elapsed());
ast.is_ok()
}
pub fn get_word_list(&self, state: &AppState) -> Vec<usize> {
todo!()
// let time = Instant::now();
// let mut list = vec![];
// let engine = Engine::new();
//
// let ast = engine.compile(&self.filter);
// if let Ok(ast) = ast {
// let groups = state.word_groups.read();
// let query = state
// .dictionary.read().query_par(| word: &WordData|{
// let mut more = rhai::Map::new();
// for iced in &word.additional {
// more.insert(iced.0.clone().into(), iced.1.clone().into());
// }
// let mut scope = Scope::new();
// let group_name = groups.get_ref(&mut groups.handle_by_id(word.group_id)).unwrap().name;
//
// scope
// .push_constant("id", word.id.0 as i64)
// .push_constant("key", word.key.clone())
// .push_constant("value", word.value.clone())
// .push_constant("tags", word.tags.clone())
// .push_constant("more", more)
// .push_constant(
// "group",
// group_name,
// );
//
// let result = engine.eval_ast_with_scope::<bool>(&mut scope, &ast);
// if let Ok(result) = result {
// return result;
// }
// false
// });
//
//
// info!("Collecting available words is {:?}", time.elapsed());
//
// list
// } else {
// list
// }
}
pub fn require_speech(&self) -> bool {
self.forward == "speech" || self.backward == "speech"
}
@@ -213,7 +165,6 @@ pub struct CustomField {
pub name: String,
}
#[derive(Copy)]
pub struct EntityHandle<E> {
pub(crate) id: Id,
index: u32,
@@ -229,6 +180,7 @@ impl<T> Clone for EntityHandle<T> {
}
}
}
impl<T> Copy for EntityHandle<T> {}
impl<T> EntityHandle<T> {
pub fn new(id: Id, index: usize, real: bool) -> Self {
Self {
@@ -280,6 +232,13 @@ impl<T> Ord for EntityHandle<T> {
}
}
impl<T> Hash for EntityHandle<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
self.real().hash(state);
}
}
pub trait EntityData {
fn id(&self) -> Id;
fn set_id(&mut self, id: Id);
+95 -2
View File
@@ -8,8 +8,9 @@ use rayon::iter::IntoParallelRefIterator;
use rayon::iter::ParallelIterator;
use rusqlite::Connection;
use std::marker::PhantomData;
use std::ops::Deref;
use std::ops::{Deref, Index, IndexMut};
use std::rc::Rc;
use std::slice::Iter;
use std::sync::Arc;
use crate::data_provider::sqlite::SqliteConnection;
@@ -20,6 +21,9 @@ where
fn get(&self, handle: &mut EntityHandle<T>) -> Option<T>;
fn get_ref(&self, handle: &mut EntityHandle<T>) -> Option<&T>;
fn get_mut(&mut self, handle: &mut EntityHandle<T>) -> Option<&mut T>;
fn get_static(&self, handle: EntityHandle<T>) -> Option<T>;
fn get_ref_static(&self, handle: EntityHandle<T>) -> Option<&T>;
fn get_mut_static(&mut self, handle: EntityHandle<T>) -> Option<&mut T>;
fn handle_by_id(&self, id: Id) -> EntityHandle<T>;
fn create(&mut self, new: T) -> EntityHandle<T>;
fn create_now(&mut self, new: T) -> Result<EntityHandle<T>, NotificationData>;
@@ -30,6 +34,8 @@ where
fn start(&mut self) -> MaybeError;
fn reload(&mut self) -> MaybeError;
fn shifts(&self) -> usize;
fn iter<'a>(&'a self) -> impl Iterator<Item = &'a T> where T: 'a;
fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T> where T: 'a;
}
struct QueryManger<T, R, D>
@@ -122,10 +128,25 @@ impl<T: EntityData + Clone + Send + Sync, R: Repository<T, D>, D: DbLayer<T>> Qu
.filter_map(move |e| data_repository_inner.get_ref(e))
}
pub fn for_each_mut<F>(&mut self, repo: &mut R, mut f: F)
pub fn iter_with_handles<'a>(&mut self, data_repository_inner: &'a DataRepositoryInner<T, D>) -> impl Iterator<Item = (&'a T, EntityHandle<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| Some((data_repository_inner.get_ref(e)?, e.clone())))
}
pub fn for_each_mut<F>(&mut self, repo: &mut DataRepositoryInner<T, D>, mut f: F)
where
F: FnMut(&mut T),
{
if self.shift_stamp != repo.shifts() {
self.load(repo);
}
for handle in &mut self.cache {
if let Some(entity) = repo.get_mut(handle) {
f(entity);
@@ -166,6 +187,28 @@ impl<T: EntityData + Clone + Send + Sync, R: Repository<T, D>, D: DbLayer<T>> Qu
.collect::<Vec<_>>(),
);
}
pub fn handles(&self, data_repository: &DataRepositoryInner<T, D>) -> impl Iterator<Item = EntityHandle<T>> {
self.cache.iter().cloned()
}
pub fn len(&self) -> usize {
self.cache.len()
}
}
impl<T: EntityData + Clone + Send + Sync, R: Repository<T, D>, D: DbLayer<T>> Index<usize> for Query<T, R, D> {
type Output = EntityHandle<T>;
fn index(&self, index: usize) -> &Self::Output {
&self.cache[index]
}
}
impl<T: EntityData + Clone + Send + Sync, R: Repository<T, D>, D: DbLayer<T>> IndexMut<usize> for Query<T, R, D> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.cache[index]
}
}
pub trait BatchedRepository<T, D>
@@ -300,6 +343,30 @@ impl<T: EntityData + Clone, D: DbLayer<T>> DataRepositoryInner<T, D> {
shift_stamp: self.shifts,
}
}
pub fn empty_query(&self) -> Query<T, DataRepositoryInner<T, D>, D> {
Query{
cache: vec![],
rule: Arc::new(|_| false),
parallel: false,
_marker: Default::default(),
shift_stamp: 0,
}
}
pub fn empty_query_par(&self) -> Query<T, DataRepositoryInner<T, D>, D> {
Query{
cache: vec![],
rule: Arc::new(|_| false),
parallel: true,
_marker: Default::default(),
shift_stamp: 0,
}
}
pub fn len(&self) -> usize {
self.cache.len() + self.temporal.len()
}
}
impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryInner<T, D> {
@@ -333,6 +400,18 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
Some(&mut slice[index])
}
fn get_static(&self, mut handle: EntityHandle<T>) -> Option<T> {
self.get(&mut handle)
}
fn get_ref_static(&self, mut handle: EntityHandle<T>) -> Option<&T> {
self.get_ref(&mut handle)
}
fn get_mut_static(&mut self, mut handle: EntityHandle<T>) -> Option<&mut T> {
self.get_mut(&mut handle)
}
fn handle_by_id(&self, id: Id) -> EntityHandle<T> {
EntityHandle::new(id, id.0 as usize, true)
}
@@ -439,6 +518,20 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
fn shifts(&self) -> usize {
self.shifts
}
fn iter<'a>(&'a self) -> impl Iterator<Item=&'a T>
where
T: 'a
{
self.cache.iter().chain(self.temporal.iter())
}
fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item=&'a mut T>
where
T: 'a
{
self.cache.iter_mut().chain(self.temporal.iter_mut())
}
}
impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository<T, D>
for DataRepositoryInner<T, D>