From 45830a7985dec1dff06a4564975ffbcf2e01903c Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sun, 30 Aug 2026 20:32:16 +0300 Subject: [PATCH 1/7] Write SqlDbLayer --- src/data.rs | 4 +- src/data/entities.rs | 8 +-- src/data/repositories.rs | 8 +-- src/data_provider/card_states.rs | 22 +++--- src/data_provider/decks.rs | 1 + src/data_provider/mod.rs | 120 +++++++++++++++++++++++++++++++ src/data_provider/sqlite.rs | 6 ++ 7 files changed, 150 insertions(+), 19 deletions(-) diff --git a/src/data.rs b/src/data.rs index 6c0c113..e575bf5 100644 --- a/src/data.rs +++ b/src/data.rs @@ -3,7 +3,7 @@ pub mod repositories; use rusqlite::ToSql; use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef}; -use std::fmt::{Display, Formatter}; +use std::fmt::{write, Display, Formatter}; use std::num::ParseIntError; use std::str::FromStr; @@ -60,4 +60,4 @@ impl Id { pub(crate) fn is_valid(self) -> bool { self.0 != 0 } -} +} \ No newline at end of file diff --git a/src/data/entities.rs b/src/data/entities.rs index 5b1b6a0..557866b 100644 --- a/src/data/entities.rs +++ b/src/data/entities.rs @@ -185,7 +185,7 @@ impl CardStatistics { #[derive(Copy)] pub struct EntityHandle { pub(crate) id: Id, - index: i32, + index: u32, pub(crate) _phantom: PhantomData, } @@ -202,7 +202,7 @@ impl EntityHandle { pub fn new(id: Id, index: usize, real: bool) -> Self { Self { id, - index: index as i32 | (!real as i32) << 31, + index: ((index as u32) << 1 >> 1) | (!real as u32) << 31, _phantom: Default::default(), } } @@ -211,7 +211,7 @@ impl EntityHandle { } pub fn set_index(&mut self, index: usize) { - self.index = self.index >> 31 << 31 | index as i32; + self.index = (self.index >> 31 << 31) | ((index as u32) << 1 >> 1); } pub fn real(&self) -> bool { @@ -219,7 +219,7 @@ impl EntityHandle { } pub fn set_real(&mut self, value: bool) { - self.index = self.index << 1 >> 1 | (!value as i32) << 31; + self.index = self.index << 1 >> 1 | (!value as u32) << 31; } } impl Eq for EntityHandle {} diff --git a/src/data/repositories.rs b/src/data/repositories.rs index 93356f0..b382460 100644 --- a/src/data/repositories.rs +++ b/src/data/repositories.rs @@ -178,8 +178,8 @@ pub trait BatchedDbLayer where T: EntityData, { - fn delete_many_entities(&mut self, ids: &[Id], connection: &Connection) -> MaybeError; - fn add_many_entities(&mut self, entity: &mut [T], connection: &Connection) -> MaybeError; + fn delete_many_entities(&mut self, ids: &[Id], connection: &mut Connection) -> MaybeError; + fn add_many_entities(&mut self, entity: &mut [T], connection: &mut Connection) -> MaybeError; } #[derive(Clone)] @@ -414,7 +414,7 @@ impl + DbLayer> BatchedRepository }); self.data_layer - .add_many_entities(&mut temp_values, &self.connection.read())?; + .add_many_entities(&mut temp_values, &mut self.connection.write())?; let start = self.cache.len(); @@ -456,7 +456,7 @@ impl + DbLayer> BatchedRepository !remove }); self.data_layer - .delete_many_entities(cached.as_slice(), &self.connection.read())?; + .delete_many_entities(cached.as_slice(), &mut self.connection.write())?; let len = cached.len(); cell = 0; self.cache.retain(|x| { diff --git a/src/data_provider/card_states.rs b/src/data_provider/card_states.rs index 248e156..0af64da 100644 --- a/src/data_provider/card_states.rs +++ b/src/data_provider/card_states.rs @@ -23,7 +23,7 @@ pub fn load_stats_of_deck( score: row.get(2)?, fsrs_opened: NaiveDate::from_epoch_days(row.get::(4)?) .expect("Incorrect date encoding"), - fsrs_memory: Some(get_memory(row.get(5)?)), + fsrs_memory: get_memory(row.get(5)?), last_open: row.get(3)?, fsrs_open_day: NaiveDate::from_epoch_days(row.get::(6)?) .expect("Incorrect date encoding"), @@ -37,14 +37,21 @@ pub fn load_stats_of_deck( Ok(buffer) } -fn get_memory(value: i64) -> MemoryState { - MemoryState { +fn get_memory(value: i64) -> Option { + if value == 0 { + return None; + } + Some(MemoryState { stability: f32::from_bits(value as u32), difficulty: f32::from_bits((value >> 32) as u32), - } + }) } -fn memory_to_i64(value: &MemoryState) -> i64 { +fn memory_to_i64(value: &Option) -> i64 { + let Some(value) = &value else { + return 0; + }; + value.stability.to_bits() as i64 | ((value.difficulty.to_bits() as i64) << 32) } @@ -69,10 +76,7 @@ pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) stat.score, stat.last_open.timestamp(), stat.fsrs_opened.to_epoch_days(), - memory_to_i64(&stat.fsrs_memory.unwrap_or(MemoryState { - difficulty: 0.0, - stability: 0.0 - })), + memory_to_i64(&stat.fsrs_memory), stat.fsrs_open_day.to_epoch_days() ]) .map_err(|_| DbSaveError)?; diff --git a/src/data_provider/decks.rs b/src/data_provider/decks.rs index b320e57..1a885a7 100644 --- a/src/data_provider/decks.rs +++ b/src/data_provider/decks.rs @@ -4,6 +4,7 @@ use crate::notification::NotificationData::{DbLoadError, DbSaveError}; use crate::notification::{MaybeError, NotificationData}; use rusqlite::Connection; + pub fn load_sets(connection: &Connection) -> Result, NotificationData> { let mut stmt = connection .prepare("SELECT id, name, forward, backward, filter FROM decks") diff --git a/src/data_provider/mod.rs b/src/data_provider/mod.rs index 86660ec..998b1f0 100644 --- a/src/data_provider/mod.rs +++ b/src/data_provider/mod.rs @@ -1,3 +1,14 @@ +use crate::core::srs::OrderMode; +use crate::data::Id; +use crate::data::entities::{DeckSettings, EntityData}; +use crate::data::repositories::{BatchedDbLayer, DbLayer}; +use crate::notification::MaybeError; +use crate::notification::NotificationData::{DbLoadError, DbSaveError}; +use log::info; +use rusqlite::{Connection, Params, Row, params}; +use std::marker::PhantomData; +use std::time::Instant; + pub(crate) mod card_states; pub(crate) mod decks; pub(crate) mod history; @@ -7,3 +18,112 @@ pub(crate) mod sqlite; pub(crate) mod voice; pub(crate) mod web_api; pub(crate) mod words; + +pub trait TableAccessQuerySet { + fn load() -> &'static str; + fn save() -> &'static str; + fn add() -> &'static str; + fn name() -> &'static str; + fn from_sql(row: &Row) -> T; + fn insert_params(entity: &T) -> impl Params; + fn update_params(entity: &T) -> impl Params; +} + +pub struct SqliteDbLayer +where + Q: TableAccessQuerySet, +{ + query_set: Q, + _phantom: PhantomData, +} + +impl> DbLayer for SqliteDbLayer { + fn write_entity(&self, entity: &T, connection: &Connection) -> MaybeError { + connection + .execute(Q::load(), Q::update_params(entity)) + .map_err(|_| DbSaveError)?; + Ok(()) + } + + fn add_entity(&mut self, entity: &mut T, connection: &Connection) -> MaybeError { + let index: u32 = connection + .query_row(Q::add(), Q::insert_params(entity), |row| row.get(0)) + .map_err(|_| DbSaveError)?; + + entity.set_id(index.into()); + Ok(()) + } + + fn delete_entity(&mut self, id: Id, connection: &Connection) -> MaybeError { + if !id.is_valid() { + return Err(DbSaveError); + } + connection + .execute( + format!("DELETE FROM {} WHERE id = ?1", Q::name()).as_str(), + (&id,), + ) + .map_err(|_| DbSaveError)?; + Ok(()) + } + + fn load_all(&self, storage: &mut Vec, connection: &Connection) -> MaybeError { + let mut stmt = connection + .prepare(Q::load()) + .expect("Failed to prepare state query"); + let iter = stmt + .query_map([], |row| Ok(Q::from_sql(row))) + .map_err(|_| DbLoadError)?; + + iter.filter_map(|res| res.ok()) + .for_each(|i| storage.push(i)); + Ok(()) + } +} + +impl> BatchedDbLayer for SqliteDbLayer { + fn delete_many_entities(&mut self, ids: &[Id], connection: &mut Connection) -> MaybeError { + for chunk in ids.chunks(1000) { + let stringed = chunk + .iter() + .map(|i| i.to_string()) + .collect::>() + .join(","); + connection + .execute( + format!("DELETE FROM {} WHERE id in ({})", Q::name(), stringed).as_str(), + (), + ) + .map_err(|_| DbSaveError)?; + } + Ok(()) + } + + fn add_many_entities(&mut self, entities: &mut [T], connection: &mut Connection) -> MaybeError { + let time = Instant::now(); + let tx = connection + .transaction() + .expect("Failed to create transaction"); + let count = entities.len(); + + { + let mut stmt = tx.prepare(Q::add()).expect("Failed to prepare state query"); + for entity in entities.iter() { + stmt.execute(Q::insert_params(entity)) + .map_err(|_| DbSaveError)?; + } + } + + let last_index: u32 = tx.last_insert_rowid() as u32; + tx.commit().expect("Failed to commit"); + + let start_index = last_index - (count as u32) + 1; + + for (index, id) in (start_index..=last_index).enumerate() { + entities[index].set_id(id.into()); + } + + info!("Added {} entities for {:?}", entities.len(), time.elapsed()); + Ok(()) + } +} diff --git a/src/data_provider/sqlite.rs b/src/data_provider/sqlite.rs index 32a7bc4..c885e9c 100644 --- a/src/data_provider/sqlite.rs +++ b/src/data_provider/sqlite.rs @@ -65,4 +65,10 @@ pub fn default_connection() -> Connection { .execute("PRAGMA foreign_keys = ON;", []) .expect("Failed to set PRAGMA"); connection + .execute("PRAGMA journal_mode = WAL;", []) + .expect("Failed to set PRAGMA"); + connection + .execute("PRAGMA synchronous = NORMAL;", []) + .expect("Failed to set PRAGMA"); + connection } From 068b9c7197d72f37930c77bd2cb3781efea6129e Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sun, 30 Aug 2026 20:43:31 +0300 Subject: [PATCH 2/7] CardStates rewritten to TableAccessQuerySet --- src/core/deck.rs | 6 +-- src/core/srs.rs | 36 +++++++++--------- src/data/entities.rs | 4 +- src/data_provider/card_states.rs | 64 ++++++++++++++++++++++++++++---- src/data_provider/mod.rs | 10 ++--- src/pages/repetition.rs | 4 +- src/pages/repetitions.rs | 4 +- 7 files changed, 88 insertions(+), 40 deletions(-) diff --git a/src/core/deck.rs b/src/core/deck.rs index 3bb2cd3..b09e41c 100644 --- a/src/core/deck.rs +++ b/src/core/deck.rs @@ -15,7 +15,7 @@ use std::sync::Arc; #[derive(Clone)] pub struct DeckData { pub(crate) words: Vec, - pub(crate) set: Vec, + pub(crate) set: Vec, current_word_index: Option, state: Arc>, order_module: OrderModule, @@ -54,7 +54,7 @@ impl DeckData { }) } - pub fn next(&mut self) -> (WordData, CardStatistics) { + pub fn next(&mut self) -> (WordData, CardState) { let index = match self.order_module { SemiRandomSRS(ref mut module) => { if !module.initialized { @@ -88,7 +88,7 @@ impl DeckData { } pub fn open(&mut self, status: WordOpenMode) -> MaybeError { if let Some(index) = self.current_word_index { - let word: &mut CardStatistics = &mut self.set[index]; + let word: &mut CardState = &mut self.set[index]; let old_score = word.score; word.update(status); let new_score = word.score; diff --git a/src/core/srs.rs b/src/core/srs.rs index 7bfd811..bd85444 100644 --- a/src/core/srs.rs +++ b/src/core/srs.rs @@ -1,4 +1,4 @@ -use crate::data::entities::CardStatistics; +use crate::data::entities::CardState; use chrono::{Local, TimeDelta}; use fsrs::FSRS; use rand::distr::weighted::WeightedIndex; @@ -31,9 +31,9 @@ pub enum OrderModule { Fsrs(FsrsModule), } pub trait SRSModule { - fn next(&mut self, set: &[CardStatistics]) -> usize; - fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &mut CardStatistics); - fn init(&mut self, set: &[CardStatistics]); + fn next(&mut self, set: &[CardState]) -> usize; + fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &mut CardState); + fn init(&mut self, set: &[CardState]); } #[derive(Clone)] pub struct RandomSRSModule { @@ -49,7 +49,7 @@ impl RandomSRSModule { } } impl SRSModule for RandomSRSModule { - fn next(&mut self, set: &[CardStatistics]) -> usize { + fn next(&mut self, set: &[CardState]) -> usize { match self.basket.pop() { Some(index) => index, None => { @@ -60,9 +60,9 @@ impl SRSModule for RandomSRSModule { } } - fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardStatistics) {} + fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardState) {} - fn init(&mut self, set: &[CardStatistics]) { + fn init(&mut self, set: &[CardState]) { self.initialized = true; self.basket = (0..set.len()).collect::>(); self.basket.shuffle(&mut rand::rng()) @@ -86,7 +86,7 @@ impl SemiRandomSRSModule { } } impl SRSModule for SemiRandomSRSModule { - fn next(&mut self, set: &[CardStatistics]) -> usize { + fn next(&mut self, set: &[CardState]) -> usize { let index = self.last_weights.sample(&mut self.generator); if self.history.contains(&index) && self.history.len() != 1 { @@ -100,13 +100,13 @@ impl SRSModule for SemiRandomSRSModule { index } - fn open(&mut self, _: WordOpenMode, index: usize, word: &mut CardStatistics) { + fn open(&mut self, _: WordOpenMode, index: usize, word: &mut CardState) { let new_weight = (100.0 / word.calculated_score()).powf(2.0); self.last_weights .update_weights(&[(index, &new_weight)]) .expect("update weights error"); } - fn init(&mut self, set: &[CardStatistics]) { + fn init(&mut self, set: &[CardState]) { self.initialized = true; let weights = set .iter() @@ -116,7 +116,7 @@ impl SRSModule for SemiRandomSRSModule { } } impl SemiRandomSRSModule { - fn history_len(&self, set: &[CardStatistics]) -> usize { + fn history_len(&self, set: &[CardState]) -> usize { ((set.len() as f32 * MAX_HISTORY_LEN_PART) as usize).clamp(1, MAX_HISTORY_LEN) } } @@ -130,7 +130,7 @@ pub struct WorstWordsSRSModule { rounds_count: u8, } impl SRSModule for WorstWordsSRSModule { - fn next(&mut self, set: &[CardStatistics]) -> usize { + fn next(&mut self, set: &[CardState]) -> usize { if self.rounds_remaining == 0 { self.fill_pool(set); self.rounds_remaining = self.rounds_count; @@ -146,9 +146,9 @@ impl SRSModule for WorstWordsSRSModule { } } - fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardStatistics) {} + fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardState) {} - fn init(&mut self, _: &[CardStatistics]) { + fn init(&mut self, _: &[CardState]) { self.initialized = true; } } @@ -163,7 +163,7 @@ impl WorstWordsSRSModule { queue: vec![], } } - fn fill_pool(&mut self, set: &[CardStatistics]) { + fn fill_pool(&mut self, set: &[CardState]) { let mut sorted = set.iter().enumerate().collect::>(); sorted.sort_by_key(|c| c.1.score); let mut worst = sorted[0..self.pool_size] @@ -187,7 +187,7 @@ pub struct FsrsModule { } impl SRSModule for FsrsModule { - fn next(&mut self, _: &[CardStatistics]) -> usize { + fn next(&mut self, _: &[CardState]) -> usize { if self.current_group.is_empty() { self.fill_partial_pool(); } @@ -200,7 +200,7 @@ impl SRSModule for FsrsModule { self.queue.pop().unwrap_or(0) } - fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &mut CardStatistics) { + fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &mut CardState) { let today = Local::now().date_naive(); let delta = today - updated_word.fsrs_opened; let delta_days = delta.num_days(); @@ -233,7 +233,7 @@ impl SRSModule for FsrsModule { } } - fn init(&mut self, set: &[CardStatistics]) { + fn init(&mut self, set: &[CardState]) { self.rng = rng(); let today = Local::now().date_naive(); self.today_set = set diff --git a/src/data/entities.rs b/src/data/entities.rs index 557866b..3c025db 100644 --- a/src/data/entities.rs +++ b/src/data/entities.rs @@ -145,7 +145,7 @@ impl DeckSettings { } #[derive(Clone, PartialEq)] -pub struct CardStatistics { +pub struct CardState { pub id: Id, pub word_id: Id, pub set_id: Id, @@ -155,7 +155,7 @@ pub struct CardStatistics { pub fsrs_memory: Option, pub fsrs_open_day: NaiveDate, } -impl CardStatistics { +impl CardState { pub fn update(&mut self, status: WordOpenMode) { match status { WordOpenMode::Easy => { diff --git a/src/data_provider/card_states.rs b/src/data_provider/card_states.rs index 0af64da..7db49eb 100644 --- a/src/data_provider/card_states.rs +++ b/src/data_provider/card_states.rs @@ -1,22 +1,72 @@ -use crate::data::entities::{CardStatistics, DeckSettings}; +use crate::data::entities::{CardState, DeckSettings}; +use crate::data_provider::TableAccessQuerySet; use crate::notification::NotificationData::{DbLoadError, DbSaveError}; use crate::notification::{MaybeError, NotificationData}; use chrono::NaiveDate; use fsrs::MemoryState; use log::info; -use rusqlite::{Connection, params}; +use rusqlite::{Connection, Error, Params, Row, params}; use std::time::Instant; +pub struct CardStatesQuerySet; +impl TableAccessQuerySet for CardStatesQuerySet { + fn load() -> &'static str { + "INSERT INTO states (word_id, set_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day) VALUES (?1, ?2, ?3, ?4)" + } + + fn save() -> &'static str { + "UPDATE states SET score = ?1, last_opened = ?2 WHERE id = ?3" + } + + fn add() -> &'static str { + todo!() + } + + fn name() -> &'static str { + "states" + } + + fn from_sql(row: &Row) -> Result { + Ok(CardState { + id: row.get(0)?, + word_id: row.get(1)?, + set_id: set.id, + score: row.get(2)?, + fsrs_opened: NaiveDate::from_epoch_days(row.get::(4)?) + .expect("Incorrect date encoding"), + fsrs_memory: get_memory(row.get(5)?), + last_open: row.get(3)?, + fsrs_open_day: NaiveDate::from_epoch_days(row.get::(6)?) + .expect("Incorrect date encoding"), + }) + } + + fn insert_params(entity: &CardState) -> impl Params { + ( + entity.word_id, + entity.set_id, + entity.score, + entity.last_open.timestamp(), + entity.fsrs_opened.to_epoch_days(), + memory_to_i64(&entity.fsrs_memory), + entity.fsrs_open_day.to_epoch_days(), + ) + } + + fn update_params(entity: &CardState) -> impl Params { + (entity.score, entity.last_open.timestamp(), entity.id) + } +} pub fn load_stats_of_deck( set: &DeckSettings, connection: &Connection, -) -> Result, NotificationData> { +) -> Result, NotificationData> { let mut stmt = connection .prepare("SELECT id, word_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day FROM states WHERE set_id = ?1") .expect("Failed to prepare state query"); let iter = stmt .query_map((set.id,), |row| { - Ok(CardStatistics { + Ok(CardState { id: row.get(0)?, word_id: row.get(1)?, set_id: set.id, @@ -55,7 +105,7 @@ fn memory_to_i64(value: &Option) -> i64 { value.stability.to_bits() as i64 | ((value.difficulty.to_bits() as i64) << 32) } -pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) -> MaybeError { +pub fn add_stat_list(stats: &mut [CardState], connection: &mut Connection) -> MaybeError { let time = Instant::now(); let tx = connection .transaction() @@ -96,7 +146,7 @@ pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) Ok(()) } -pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> MaybeError { +pub fn update_stat_score(stat: &CardState, connection: &Connection) -> MaybeError { let time = Instant::now(); connection @@ -109,7 +159,7 @@ pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> Mayb Ok(()) } -pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) -> MaybeError { +pub fn delete_stats(stat: &[CardState], connection: &Connection) -> MaybeError { if stat.is_empty() { return Ok(()); } diff --git a/src/data_provider/mod.rs b/src/data_provider/mod.rs index 998b1f0..1c54a7a 100644 --- a/src/data_provider/mod.rs +++ b/src/data_provider/mod.rs @@ -1,11 +1,10 @@ -use crate::core::srs::OrderMode; use crate::data::Id; -use crate::data::entities::{DeckSettings, EntityData}; +use crate::data::entities::{CardState, EntityData}; use crate::data::repositories::{BatchedDbLayer, DbLayer}; use crate::notification::MaybeError; use crate::notification::NotificationData::{DbLoadError, DbSaveError}; use log::info; -use rusqlite::{Connection, Params, Row, params}; +use rusqlite::{Connection, Error, Params, Row}; use std::marker::PhantomData; use std::time::Instant; @@ -24,7 +23,7 @@ pub trait TableAccessQuerySet { fn save() -> &'static str; fn add() -> &'static str; fn name() -> &'static str; - fn from_sql(row: &Row) -> T; + fn from_sql(row: &Row) -> Result; fn insert_params(entity: &T) -> impl Params; fn update_params(entity: &T) -> impl Params; } @@ -33,8 +32,7 @@ pub struct SqliteDbLayer where Q: TableAccessQuerySet, { - query_set: Q, - _phantom: PhantomData, + _phantom: PhantomData<(T, Q)>, } impl> DbLayer for SqliteDbLayer { diff --git a/src/pages/repetition.rs b/src/pages/repetition.rs index 15f0a70..cbf5d91 100644 --- a/src/pages/repetition.rs +++ b/src/pages/repetition.rs @@ -2,7 +2,7 @@ use crate::app::AppState; use crate::core::deck::DeckData; use crate::core::srs::WordOpenMode; use crate::data::Id; -use crate::data::entities::{CardStatistics, DeckSettings, WordData}; +use crate::data::entities::{CardState, DeckSettings, WordData}; use crate::data_provider::voice::get_voice; use crate::navigation::Page::PreviousPage; use crate::navigation::{KeyPressedPage, NavigatedPage, Page, RootMessage}; @@ -26,7 +26,7 @@ pub struct RepetitionState { pub settings: DeckSettings, pub set: DeckData, current_word: WordData, - current_statistic: CardStatistics, + current_statistic: CardState, open: bool, can_play: bool, sink: Arc>, diff --git a/src/pages/repetitions.rs b/src/pages/repetitions.rs index 84c4bc5..38f44d2 100644 --- a/src/pages/repetitions.rs +++ b/src/pages/repetitions.rs @@ -1,7 +1,7 @@ use crate::app::AppState; use crate::core::srs::{AppendMode, OrderMode}; use crate::data::Id; -use crate::data::entities::{CardStatistics, DeckSettings}; +use crate::data::entities::{CardState, DeckSettings}; use crate::data_provider::card_states::{add_stat_list, load_stats_of_deck}; use crate::data_provider::decks::update_deck; use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings}; @@ -585,7 +585,7 @@ impl RepetitionsState { let stats = &mut indices .map(|i| { let word = &words[i]; - CardStatistics { + CardState { id: 0.into(), word_id: word.id, last_open: Utc::now(), From c9ccacc2d14cad342236cc5789baefe9547f1f84 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sun, 30 Aug 2026 20:46:52 +0300 Subject: [PATCH 3/7] Fix CardStates --- src/data_provider/card_states.rs | 16 ++++++++-------- src/data_provider/mod.rs | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/data_provider/card_states.rs b/src/data_provider/card_states.rs index 7db49eb..effeef5 100644 --- a/src/data_provider/card_states.rs +++ b/src/data_provider/card_states.rs @@ -11,7 +11,7 @@ use std::time::Instant; pub struct CardStatesQuerySet; impl TableAccessQuerySet for CardStatesQuerySet { fn load() -> &'static str { - "INSERT INTO states (word_id, set_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day) VALUES (?1, ?2, ?3, ?4)" + "SELECT id, word_id, set_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day FROM states" } fn save() -> &'static str { @@ -19,7 +19,7 @@ impl TableAccessQuerySet for CardStatesQuerySet { } fn add() -> &'static str { - todo!() + "INSERT INTO states (word_id, set_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day) VALUES (?1, ?2, ?3, ?4)" } fn name() -> &'static str { @@ -30,13 +30,13 @@ impl TableAccessQuerySet for CardStatesQuerySet { Ok(CardState { id: row.get(0)?, word_id: row.get(1)?, - set_id: set.id, - score: row.get(2)?, - fsrs_opened: NaiveDate::from_epoch_days(row.get::(4)?) + set_id: row.get(2)?, + score: row.get(3)?, + fsrs_opened: NaiveDate::from_epoch_days(row.get::(5)?) .expect("Incorrect date encoding"), - fsrs_memory: get_memory(row.get(5)?), - last_open: row.get(3)?, - fsrs_open_day: NaiveDate::from_epoch_days(row.get::(6)?) + fsrs_memory: get_memory(row.get(6)?), + last_open: row.get(4)?, + fsrs_open_day: NaiveDate::from_epoch_days(row.get::(7)?) .expect("Incorrect date encoding"), }) } diff --git a/src/data_provider/mod.rs b/src/data_provider/mod.rs index 1c54a7a..874eae0 100644 --- a/src/data_provider/mod.rs +++ b/src/data_provider/mod.rs @@ -70,7 +70,7 @@ impl> DbLayer for SqliteDbLayer Date: Sun, 30 Aug 2026 20:50:30 +0300 Subject: [PATCH 4/7] Decks rewritten --- src/data_provider/decks.rs | 49 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/data_provider/decks.rs b/src/data_provider/decks.rs index 1a885a7..2c8f795 100644 --- a/src/data_provider/decks.rs +++ b/src/data_provider/decks.rs @@ -1,9 +1,54 @@ use crate::core::srs::OrderMode; -use crate::data::entities::DeckSettings; +use crate::data::entities::{CardState, DeckSettings}; +use crate::data_provider::TableAccessQuerySet; use crate::notification::NotificationData::{DbLoadError, DbSaveError}; use crate::notification::{MaybeError, NotificationData}; -use rusqlite::Connection; +use chrono::NaiveDate; +use rusqlite::{Connection, Error, Params, Row}; +pub struct DeckQuerySet; +impl TableAccessQuerySet for DeckQuerySet { + fn load() -> &'static str { + "SELECT id, name, forward, backward, filter FROM decks" + } + + fn save() -> &'static str { + "UPDATE decks SET name = ?1, forward = ?2, backward = ?3, filter = ?4 WHERE id = ?5" + } + + fn add() -> &'static str { + "INSERT INTO states (word_id, set_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day) VALUES (?1, ?2, ?3, ?4)" + } + + fn name() -> &'static str { + "decks" + } + + fn from_sql(row: &Row) -> Result { + Ok(DeckSettings { + id: row.get(0)?, + name: row.get(1)?, + forward: row.get(2)?, + backward: row.get(3)?, + filter: row.get(4)?, + count: None, + open_mode: OrderMode::Default, + }) + } + + fn insert_params(entity: &DeckSettings) -> impl Params { + ( + &entity.name, + &entity.forward, + &entity.backward, + &entity.filter, + ) + } + + fn update_params(entity: &DeckSettings) -> impl Params { + (&entity.name, &entity.forward, &entity.backward, &entity.filter, entity.id) + } +} pub fn load_sets(connection: &Connection) -> Result, NotificationData> { let mut stmt = connection From d9bbf9e70503a2c981c6281ea4298d4613ad5407 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sun, 30 Aug 2026 22:15:03 +0300 Subject: [PATCH 5/7] Write TableAccessQuerySet for other tables. Create Id macro --- Cargo.lock | 8 ++- Cargo.toml | 1 + src/app.rs | 1 + src/data.rs | 99 ++++++++++++++++++++----------------- src/data/entities.rs | 27 ++++++++-- src/data_provider/sqlite.rs | 9 +++- src/data_provider/words.rs | 92 +++++++++++++++++++++++++++++++++- src/notification.rs | 4 +- 8 files changed, 184 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 04b8d43..4eab091 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2456,6 +2456,7 @@ dependencies = [ "hex", "iced", "iced_core", + "litemap", "log", "mimalloc", "parking_lot", @@ -2739,9 +2740,12 @@ checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" [[package]] name = "litemap" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0" +checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" +dependencies = [ + "serde_core", +] [[package]] name = "litrs" diff --git a/Cargo.toml b/Cargo.toml index c715c06..7f51155 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ parking_lot = "0.12.5" env_logger = "0.11.11" log = "0.4.34" fsrs = "6.6.1" +litemap = { version = "0.8.3", features = ["serde"] } [profile.super-release] inherits = "release" diff --git a/src/app.rs b/src/app.rs index eaafa91..8eac209 100644 --- a/src/app.rs +++ b/src/app.rs @@ -19,6 +19,7 @@ use rusqlite::Connection; pub const USER_FONT: Font = Font::with_name("Noto Sans JP"); pub fn start() -> iced::Result { + println!("{}", size_of::()); info!("Welcome to JapLearn"); iced::application(ScreenState::boot, ScreenState::update, ScreenState::view) .window(window_settings()) diff --git a/src/data.rs b/src/data.rs index e575bf5..60f1fd8 100644 --- a/src/data.rs +++ b/src/data.rs @@ -7,57 +7,64 @@ use std::fmt::{write, Display, Formatter}; use std::num::ParseIntError; use std::str::FromStr; -#[derive(Clone, PartialEq, Eq, Debug, Hash, Copy, Ord, PartialOrd, Default)] -pub struct Id(pub(crate) u32); -pub const INVALID_ID: Id = Id(0); -impl FromStr for Id { - type Err = ParseIntError; +macro_rules! id_impl { + ($id_type:ty, $name:ident, $invalid:ident) => { + #[derive(Clone, PartialEq, Eq, Debug, Hash, Copy, Ord, PartialOrd, Default)] + pub struct $name(pub(crate) $id_type); + pub const $invalid: $name = $name(0); + impl FromStr for $name { + type Err = ParseIntError; - fn from_str(s: &str) -> Result { - Ok(Id(s.parse::()?)) - } -} + fn from_str(s: &str) -> Result { + Ok($name(s.parse::<$id_type>()?)) + } + } -impl From for Id { - fn from(value: u32) -> Self { - Id(value) - } -} + impl From<$id_type> for $name { + fn from(value: $id_type) -> Self { + $name(value) + } + } -impl From for u32 { - fn from(value: Id) -> Self { - value.0 - } -} -impl Display for Id { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} + impl From<$name> for $id_type { + fn from(value: $name) -> Self { + value.0 + } + } + impl Display for $name { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } + } -impl FromSql for Id { - fn column_result(value: ValueRef<'_>) -> FromSqlResult { - match value { - ValueRef::Integer(i) => Ok(Id(i as u32)), - _ => Err(FromSqlError::InvalidType), + impl FromSql for $name { + fn column_result(value: ValueRef<'_>) -> FromSqlResult { + match value { + ValueRef::Integer(i) => Ok($name(i as $id_type)), + _ => Err(FromSqlError::InvalidType), + } + } + } + + impl ToSql for $name { + fn to_sql(&self) -> rusqlite::Result> { + Ok(ToSqlOutput::from(self.0 as i64)) + } + } + + impl From<$name> for String { + fn from(value: $name) -> Self { + value.0.to_string() + } + } + + impl $name { + pub(crate) fn is_valid(self) -> bool { + self.0 != 0 + } } } } -impl ToSql for Id { - fn to_sql(&self) -> rusqlite::Result> { - Ok(ToSqlOutput::from(self.0 as i64)) - } -} - -impl From for String { - fn from(value: Id) -> Self { - value.0.to_string() - } -} - -impl Id { - pub(crate) fn is_valid(self) -> bool { - self.0 != 0 - } -} \ No newline at end of file +id_impl!(u32, Id, INVALID_ID); +id_impl!(u16, SmallId, INVALID_SMALL_ID); diff --git a/src/data/entities.rs b/src/data/entities.rs index 3c025db..7410848 100644 --- a/src/data/entities.rs +++ b/src/data/entities.rs @@ -7,18 +7,18 @@ use log::info; use rayon::prelude::*; use rhai::{Engine, Scope}; use std::cmp::Ordering; -use std::collections::HashMap; use std::marker::PhantomData; use std::time::Instant; +use litemap::LiteMap; #[derive(Clone, Debug)] pub struct WordData { pub id: Id, + pub group_id: Id, pub key: String, pub value: String, pub tags: String, - pub additional: HashMap, - pub group_id: Id, + pub additional: LiteMap, } impl EntityData for WordData { @@ -37,6 +37,16 @@ pub struct WordGroup { pub name: String, } +impl EntityData for WordGroup { + fn id(&self) -> Id { + self.id + } + + fn set_id(&mut self, id: Id) { + self.id = id; + } +} + impl WordData { pub fn new() -> Self { Self { @@ -155,6 +165,17 @@ pub struct CardState { pub fsrs_memory: Option, pub fsrs_open_day: NaiveDate, } + +impl EntityData for CardState { + fn id(&self) -> Id { + self.id + } + + fn set_id(&mut self, id: Id) { + self.id = id; + } +} + impl CardState { pub fn update(&mut self, status: WordOpenMode) { match status { diff --git a/src/data_provider/sqlite.rs b/src/data_provider/sqlite.rs index c885e9c..800f70f 100644 --- a/src/data_provider/sqlite.rs +++ b/src/data_provider/sqlite.rs @@ -65,8 +65,13 @@ pub fn default_connection() -> Connection { .execute("PRAGMA foreign_keys = ON;", []) .expect("Failed to set PRAGMA"); connection - .execute("PRAGMA journal_mode = WAL;", []) - .expect("Failed to set PRAGMA"); + .query_one("PRAGMA journal_mode = WAL;", [], |row| { + println!("{:?}", row); + Ok(()) + }) + .unwrap_or_else(|e| { + println!("{}", e); + }); connection .execute("PRAGMA synchronous = NORMAL;", []) .expect("Failed to set PRAGMA"); diff --git a/src/data_provider/words.rs b/src/data_provider/words.rs index 9eff98d..75587ed 100644 --- a/src/data_provider/words.rs +++ b/src/data_provider/words.rs @@ -1,8 +1,96 @@ use crate::data::entities::{WordData, WordGroup}; +use crate::data_provider::TableAccessQuerySet; use crate::notification::NotificationData::{DbLoadError, DbSaveError}; use crate::notification::{MaybeError, NotificationData}; -use rusqlite::{Connection, params}; +use rusqlite::{Connection, Error, Params, Row, params}; use std::collections::HashMap; +use litemap::LiteMap; + +pub struct WordQuerySet; +impl TableAccessQuerySet for WordQuerySet { + fn load() -> &'static str { + "SELECT id, key, value, tags, more, group_id FROM words" + } + + fn save() -> &'static str { + "UPDATE words SET key = ?1, value = ?2, tags = ?3, more = ?4 WHERE id = ?5" + } + + fn add() -> &'static str { + "INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5)" + } + + fn name() -> &'static str { + "words" + } + + fn from_sql(row: &Row) -> Result { + let additional: String = row.get(4)?; + Ok(WordData { + id: row.get(0)?, + key: row.get(1)?, + value: row.get(2)?, + tags: row.get(3)?, + additional: serde_json::from_str(&additional) + .unwrap_or_default(), + group_id: row.get(5)?, + }) + } + + fn insert_params(entity: &WordData) -> impl Params { + ( + &entity.key, + &entity.value, + &entity.tags, + serde_json::to_string(&entity.additional).unwrap_or_default(), + entity.group_id, + ) + } + + fn update_params(entity: &WordData) -> impl Params { + ( + &entity.key, + &entity.value, + &entity.tags, + serde_json::to_string(&entity.additional).unwrap_or_default(), + entity.id, + ) + } +} + +pub struct WordGroupQuerySet; +impl TableAccessQuerySet for WordGroupQuerySet { + fn load() -> &'static str { + "SELECT id, name FROM word_groups" + } + + fn save() -> &'static str { + "UPDATE word_groups SET name = ?1 WHERE id = ?2" + } + + fn add() -> &'static str { + "INSERT INTO word_groups (name) VALUES (?1)" + } + + fn name() -> &'static str { + "word_groups" + } + + fn from_sql(row: &Row) -> Result { + Ok(WordGroup { + id: row.get(0)?, + name: row.get(1)?, + }) + } + + fn insert_params(entity: &WordGroup) -> impl Params { + (&entity.name,) + } + + fn update_params(entity: &WordGroup) -> impl Params { + (&entity.name, entity.id) + } +} pub fn add_word(word: &mut WordData, connection: &Connection) -> MaybeError { let index : u32 = connection @@ -99,7 +187,7 @@ pub fn load_words(connection: &Connection) -> Result, Notification key: row.get(1)?, value: row.get(2)?, tags: row.get(3)?, - additional: serde_json::from_str::>(&additional) + additional: serde_json::from_str(&additional) .unwrap_or_default(), group_id: row.get(5)?, }) diff --git a/src/notification.rs b/src/notification.rs index d519a88..bc6c025 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -139,7 +139,7 @@ impl NotificationManager { fn draw_data<'a>(data: &NotificationData) -> Element<'a, NotificationMessage> { match data { NotificationData::DataLoaded => { - text!("Данные успешно скачаны") + text!("Данные успешно загружены на сервер") } NotificationData::ImportError => { text!("Ошибка при импорте данных из Anki") @@ -154,7 +154,7 @@ impl NotificationManager { text!("Не получается подключиться к VoiceVox") } NotificationData::PermissionDenied => { - text!("У приложение недостаточно прав") + text!("У приложение недостаточно прав для доступа в файлам") } NotificationData::WebApiUnavailable => { text!("Не получается подключиться к Api") From 6f010428d5fb55f2f1c5ad34f6a2e19823b600d3 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sun, 30 Aug 2026 22:36:43 +0300 Subject: [PATCH 6/7] Add db operation time --- src/app.rs | 1 - src/data_provider/mod.rs | 46 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/app.rs b/src/app.rs index 8eac209..eaafa91 100644 --- a/src/app.rs +++ b/src/app.rs @@ -19,7 +19,6 @@ use rusqlite::Connection; pub const USER_FONT: Font = Font::with_name("Noto Sans JP"); pub fn start() -> iced::Result { - println!("{}", size_of::()); info!("Welcome to JapLearn"); iced::application(ScreenState::boot, ScreenState::update, ScreenState::view) .window(window_settings()) diff --git a/src/data_provider/mod.rs b/src/data_provider/mod.rs index 874eae0..79e778a 100644 --- a/src/data_provider/mod.rs +++ b/src/data_provider/mod.rs @@ -37,22 +37,38 @@ where impl> DbLayer for SqliteDbLayer { fn write_entity(&self, entity: &T, connection: &Connection) -> MaybeError { + let time = Instant::now(); + connection .execute(Q::load(), Q::update_params(entity)) .map_err(|_| DbSaveError)?; + info!( + "Updated in {} for {:?}", + Q::name(), + time.elapsed() + ); Ok(()) } fn add_entity(&mut self, entity: &mut T, connection: &Connection) -> MaybeError { + let time = Instant::now(); + let index: u32 = connection .query_row(Q::add(), Q::insert_params(entity), |row| row.get(0)) .map_err(|_| DbSaveError)?; entity.set_id(index.into()); + info!( + "Added into {} for {:?}", + Q::name(), + time.elapsed() + ); Ok(()) } fn delete_entity(&mut self, id: Id, connection: &Connection) -> MaybeError { + let time = Instant::now(); + if !id.is_valid() { return Err(DbSaveError); } @@ -62,10 +78,17 @@ impl> DbLayer for SqliteDbLayer, connection: &Connection) -> MaybeError { + let time = Instant::now(); + let mut stmt = connection .prepare(Q::load()) .expect("Failed to prepare state query"); @@ -75,12 +98,21 @@ impl> DbLayer for SqliteDbLayer> BatchedDbLayer for SqliteDbLayer { fn delete_many_entities(&mut self, ids: &[Id], connection: &mut Connection) -> MaybeError { + let time = Instant::now(); + for chunk in ids.chunks(1000) { let stringed = chunk .iter() @@ -94,6 +126,13 @@ impl> BatchedDbLayer for SqliteDbLay ) .map_err(|_| DbSaveError)?; } + info!( + "Deleted {} from {} for {:?}", + ids.len(), + Q::name(), + time.elapsed() + ); + Ok(()) } @@ -121,7 +160,12 @@ impl> BatchedDbLayer for SqliteDbLay entities[index].set_id(id.into()); } - info!("Added {} entities for {:?}", entities.len(), time.elapsed()); + info!( + "Added {} into {} for {:?}", + entities.len(), + Q::name(), + time.elapsed() + ); Ok(()) } } From aa58bb2918a722195afa6b8f4ab558cd73182e18 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sun, 30 Aug 2026 23:35:32 +0300 Subject: [PATCH 7/7] Repository start method --- src/data/repositories.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/data/repositories.rs b/src/data/repositories.rs index b382460..8315de2 100644 --- a/src/data/repositories.rs +++ b/src/data/repositories.rs @@ -24,6 +24,7 @@ where fn save(&mut self, entity: &mut EntityHandle) -> MaybeError; fn delete(&mut self, handle: EntityHandle) -> MaybeError; fn init(&mut self) -> MaybeError; + fn start(&mut self) -> MaybeError; fn reload(&mut self) -> MaybeError; } @@ -375,6 +376,15 @@ impl> Repository for DataRepositoryIn Ok(()) } + fn start(&mut self) -> MaybeError { + if self.version != 0 { + return Ok(()) + } + + self.init()?; + Ok(()) + } + fn reload(&mut self) -> MaybeError { self.cache.clear(); self.temporal.clear();