21 Commits
Author SHA1 Message Date
micialware 29a90d48d9 Reformat code 2026-08-31 11:26:05 +03:00
micialware 2fc58fcc2d Custom fields table 2026-08-31 11:25:41 +03:00
micialware 6d363c8148 Custom fields table 2026-08-31 11:23:41 +03:00
micialware ba834ac9b1 Impl Id convert methods 2026-08-31 11:21:01 +03:00
micialware 97e301b0d6 Merge pull request 'Db layers' (#14) from db_layers into master
Reviewed-on: https://gitea.micialware.ru/micialware/jap_learn/pulls/14
2026-08-30 23:38:33 +03:00
micialware aa58bb2918 Repository start method 2026-08-30 23:35:32 +03:00
micialware 6f010428d5 Add db operation time 2026-08-30 22:36:43 +03:00
micialware d9bbf9e705 Write TableAccessQuerySet for other tables. Create Id macro 2026-08-30 22:15:03 +03:00
micialware 9a5b64a4fe Decks rewritten 2026-08-30 20:50:30 +03:00
micialware c9ccacc2d1 Fix CardStates 2026-08-30 20:46:52 +03:00
micialware 068b9c7197 CardStates rewritten to TableAccessQuerySet 2026-08-30 20:43:31 +03:00
micialware 45830a7985 Write SqlDbLayer 2026-08-30 20:32:16 +03:00
micialware 8ec7c8f05d New repo method 2026-08-30 19:30:43 +03:00
micialware 18aa748451 Merge remote-tracking branch 'origin/repository'
# Conflicts:
#	src/data/entities.rs
2026-08-30 19:25:38 +03:00
micialware d0eabbe1f8 Merge pull request 'Fsrs' (#12) from fsrs into master
Reviewed-on: https://gitea.micialware.ru/micialware/jap_learn/pulls/12
2026-08-29 22:05:16 +03:00
micialware d5a2a575fe Cleanup and format 2026-08-29 22:02:01 +03:00
micialware af6cf10765 rename data provider file names 2026-08-29 21:32:54 +03:00
micialware a642a8fda4 rename tables 2026-08-29 21:31:54 +03:00
micialware 188a26e77d Change date type 2026-08-28 21:47:47 +03:00
micialware e7019903b0 Create fsrs upadte method 2026-08-28 18:52:38 +03:00
micialware 663fa377b1 Add history table and modify card_stats 2026-08-28 17:44:14 +03:00
28 changed files with 993 additions and 216 deletions

No files matched your search

Generated
+128 -4
View File
@@ -1003,7 +1003,7 @@ dependencies = [
"ciborium",
"clap",
"criterion-plot",
"itertools",
"itertools 0.13.0",
"num-traits",
"oorandom",
"page_size",
@@ -1023,7 +1023,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8d80a2f4f5b554395e47b5d8305bc3d27813bacb73493eb1001e8f76dae29ea"
dependencies = [
"cast",
"itertools",
"itertools 0.13.0",
]
[[package]]
@@ -1529,6 +1529,23 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
[[package]]
name = "fsrs"
version = "6.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8a99ea3dec9af37c9ed3835463ff6820a578a0026a8e370b4c1c0db48dfab3f"
dependencies = [
"itertools 0.14.0",
"log",
"ndarray",
"priority-queue",
"rand",
"rayon",
"serde",
"snafu",
"strum",
]
[[package]]
name = "futures"
version = "0.3.31"
@@ -2411,6 +2428,15 @@ dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.17"
@@ -2425,10 +2451,12 @@ dependencies = [
"criterion",
"dirs",
"env_logger",
"fsrs",
"hashbrown 0.17.1",
"hex",
"iced",
"iced_core",
"litemap",
"log",
"mimalloc",
"parking_lot",
@@ -2712,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"
@@ -2776,6 +2807,16 @@ dependencies = [
"libc",
]
[[package]]
name = "matrixmultiply"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f607c237553f086e7043417a51df26b2eb899d3caff94e6a67592ff992fedc7"
dependencies = [
"autocfg",
"rawpointer",
]
[[package]]
name = "memchr"
version = "2.8.0"
@@ -2912,6 +2953,21 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "ndarray"
version = "0.17.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "520080814a7a6b4a6e9070823bb24b4531daac8c4627e08ba5de8c5ef2f2752d"
dependencies = [
"matrixmultiply",
"num-complex",
"num-integer",
"num-traits",
"portable-atomic",
"portable-atomic-util",
"rawpointer",
]
[[package]]
name = "ndk"
version = "0.9.0"
@@ -2961,6 +3017,15 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
dependencies = [
"num-traits",
]
[[package]]
name = "num-conv"
version = "0.2.2"
@@ -3773,6 +3838,17 @@ dependencies = [
"syn 2.0.114",
]
[[package]]
name = "priority-queue"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93980406f12d9f8140ed5abe7155acb10bb1e69ea55c88960b9c2f117445ef96"
dependencies = [
"equivalent",
"indexmap",
"serde",
]
[[package]]
name = "proc-macro-crate"
version = "3.4.0"
@@ -3951,6 +4027,12 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539"
[[package]]
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
[[package]]
name = "rayon"
version = "1.12.0"
@@ -4716,6 +4798,27 @@ dependencies = [
"serde",
]
[[package]]
name = "snafu"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e45cb604038abb7b926b679887b3226d8d0f23874b66623625a0454be425a4b7"
dependencies = [
"snafu-derive",
]
[[package]]
name = "snafu-derive"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "287f59010008f0d7cf5e3b03196d666c1acc46c8d3e9cf34c28a1a7157601e72"
dependencies = [
"heck 0.5.0",
"proc-macro2",
"quote",
"syn 2.0.114",
]
[[package]]
name = "socket2"
version = "0.6.3"
@@ -4805,6 +4908,27 @@ dependencies = [
"float-cmp",
]
[[package]]
name = "strum"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9628de9b8791db39ceda2b119bbe13134770b56c138ec1d3af810d045c04f9bd"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab85eea0270ee17587ed4156089e10b9e6880ee688791d45a905f5b1ca36f664"
dependencies = [
"heck 0.5.0",
"proc-macro2",
"quote",
"syn 2.0.114",
]
[[package]]
name = "subtle"
version = "2.6.1"
+3 -1
View File
@@ -26,13 +26,15 @@ rayon = "1.12.0"
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"
codegen-units = 1
lto = true
opt-level = 3
strip = true
#strip = true
panic = "abort"
[dev-dependencies]
+4
View File
@@ -0,0 +1,4 @@
create table custom_fields(
id INTEGER PRIMARY KEY,
name TEXT not null
);
+1 -1
View File
@@ -1,4 +1,4 @@
create table card_set
create table decks
(
id INTEGER
primary key autoincrement,
+12
View File
@@ -0,0 +1,12 @@
create table history
(
id INTEGER
primary key autoincrement,
set_id INTEGER not null references decks on delete cascade,
stat_id INTEGER not null references states on delete cascade,
timestamp INTEGER not null,
answer INTEGER not null,
before INTEGER not null,
after INTEGER not null,
fsrs INTEGER not null
);
+6 -3
View File
@@ -1,4 +1,4 @@
create table card_stats
create table states
(
id INTEGER
primary key autoincrement,
@@ -6,8 +6,11 @@ create table card_stats
references words
on delete cascade,
set_id INTEGER not null
references card_set
references decks
on delete cascade,
score INTEGER default 1 not null,
last_opened INTEGER not null
last_opened INTEGER not null,
fsrs_opened INTEGER not null,
fsrs_memory INTEGER not null,
fsrs_open_day INTEGER not null
);
+1 -1
View File
@@ -1,4 +1,4 @@
create table word_group
create table word_groups
(
id INTEGER
primary key autoincrement,
+2 -2
View File
@@ -7,7 +7,7 @@ create table words
tags TEXT not null,
more TEXT,
group_id integer default 1 not null
constraint words_word_group_id_fk
references word_group
constraint words_word_groups_id_fk
references word_groups
on update cascade on delete cascade
);
+1 -1
View File
@@ -1,7 +1,7 @@
use crate::data::Id;
use crate::data::entities::DeckSettings;
use crate::data::entities::{WordData, WordGroup};
use crate::data_provider::card_sets::load_sets;
use crate::data_provider::decks::load_sets;
use crate::data_provider::settings::get_setting;
use crate::data_provider::sqlite::default_connection;
use crate::data_provider::words::{load_word_groups, load_words};
+36 -19
View File
@@ -1,10 +1,11 @@
use crate::app::AppState;
use crate::core::srs::OrderModule::{Fsrs, RandomSRS, SemiRandomSRS, WorstWordsSRS};
use crate::core::srs::{
OrderMode, OrderModule, RandomSRSModule, SRSModule, SemiRandomSRSModule, WordOpenMode,
WorstWordsSRSModule,
FsrsModule, OrderMode, OrderModule, RandomSRSModule, SRSModule, SemiRandomSRSModule,
WordOpenMode, WorstWordsSRSModule,
};
use crate::data::entities::*;
use crate::data_provider::card_stats::{load_stats_of_deck, update_stat_score};
use crate::data_provider::card_states::{load_stats_of_deck, update_stat_score};
use crate::data_provider::history::push_note;
use crate::notification::{MaybeError, NotificationData};
use chrono::Utc;
@@ -14,10 +15,11 @@ use std::sync::Arc;
#[derive(Clone)]
pub struct DeckData {
pub(crate) words: Vec<WordData>,
pub(crate) set: Vec<CardStatistics>,
pub(crate) set: Vec<CardState>,
current_word_index: Option<usize>,
state: Arc<Mutex<AppState>>,
order_module: OrderModule,
open_mode: OrderMode,
settings: DeckSettings,
}
impl DeckData {
@@ -46,32 +48,34 @@ impl DeckData {
words,
current_word_index: None,
state: state_for,
order_module: match settings.open_mode {
OrderMode::Default => OrderModule::SemiRandomSRS(SemiRandomSRSModule::new()),
OrderMode::TrainWorstFirst => {
OrderModule::WorstWordsSRS(WorstWordsSRSModule::new())
}
OrderMode::FullRandom => OrderModule::RandomSRS(RandomSRSModule::new()),
},
order_module: Fsrs(FsrsModule::new()),
open_mode: settings.open_mode,
settings: settings.clone(),
})
}
pub fn next(&mut self) -> (WordData, CardStatistics) {
pub fn next(&mut self) -> (WordData, CardState) {
let index = match self.order_module {
OrderModule::SemiRandomSRS(ref mut module) => {
SemiRandomSRS(ref mut module) => {
if !module.initialized {
module.init(self.set.as_slice())
}
module.next(self.set.as_slice())
}
OrderModule::RandomSRS(ref mut module) => {
RandomSRS(ref mut module) => {
if !module.initialized {
module.init(self.set.as_slice())
}
module.next(self.set.as_slice())
}
OrderModule::WorstWordsSRS(ref mut module) => {
WorstWordsSRS(ref mut module) => {
if !module.initialized {
module.init(self.set.as_slice())
}
module.next(self.set.as_slice())
}
Fsrs(ref mut module) => {
if !module.initialized {
module.init(self.set.as_slice())
}
@@ -84,21 +88,34 @@ 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;
match self.order_module {
OrderModule::SemiRandomSRS(ref mut module) => {
SemiRandomSRS(ref mut module) => {
module.open(status, index, word);
}
OrderModule::RandomSRS(ref mut module) => {
RandomSRS(ref mut module) => {
module.open(status, index, word);
}
OrderModule::WorstWordsSRS(ref mut module) => {
WorstWordsSRS(ref mut module) => {
module.open(status, index, word);
}
Fsrs(ref mut module) => {
module.open(status, index, word);
}
}
if let Fsrs(ref fsrs) = self.order_module
&& fsrs.finished()
{
self.order_module = match self.open_mode {
OrderMode::Default => SemiRandomSRS(SemiRandomSRSModule::new()),
OrderMode::TrainWorstFirst => WorstWordsSRS(WorstWordsSRSModule::new()),
OrderMode::FullRandom => RandomSRS(RandomSRSModule::new()),
}
}
update_stat_score(word, &self.state.lock().connection)?;
let _ = push_note(
+127 -18
View File
@@ -1,14 +1,16 @@
use crate::data::entities::CardStatistics;
use crate::data::entities::CardState;
use chrono::{Local, TimeDelta};
use fsrs::FSRS;
use rand::distr::weighted::WeightedIndex;
use rand::prelude::ThreadRng;
use rand::prelude::{Distribution, SliceRandom};
use rand::rng;
use rand::{RngExt, rng};
const MAX_HISTORY_LEN: usize = 20;
const MAX_HISTORY_LEN_PART: f32 = 0.33;
pub const MAX_SCORE: u8 = 25;
pub const FADE_PER_DAY: f32 = 0.95;
const PARTIAL_POOL_SIZE: usize = 10;
#[derive(Clone, PartialEq, Copy, Eq)]
pub enum OrderMode {
Default,
@@ -26,11 +28,12 @@ pub enum OrderModule {
SemiRandomSRS(SemiRandomSRSModule),
RandomSRS(RandomSRSModule),
WorstWordsSRS(WorstWordsSRSModule),
Fsrs(FsrsModule),
}
pub trait SRSModule {
fn next(&mut self, set: &[CardStatistics]) -> usize;
fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &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 {
@@ -46,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 => {
@@ -57,9 +60,9 @@ impl SRSModule for RandomSRSModule {
}
}
fn open(&mut self, _: WordOpenMode, _: usize, _: &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::<Vec<usize>>();
self.basket.shuffle(&mut rand::rng())
@@ -83,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 {
@@ -97,13 +100,13 @@ impl SRSModule for SemiRandomSRSModule {
index
}
fn open(&mut self, _: WordOpenMode, index: usize, word: &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()
@@ -113,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)
}
}
@@ -127,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;
@@ -143,9 +146,9 @@ impl SRSModule for WorstWordsSRSModule {
}
}
fn open(&mut self, _: WordOpenMode, _: usize, _: &CardStatistics) {}
fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardState) {}
fn init(&mut self, _: &[CardStatistics]) {
fn init(&mut self, _: &[CardState]) {
self.initialized = true;
}
}
@@ -153,14 +156,14 @@ impl WorstWordsSRSModule {
pub(crate) fn new() -> WorstWordsSRSModule {
WorstWordsSRSModule {
initialized: false,
pool_size: 15,
pool_size: PARTIAL_POOL_SIZE,
rounds_count: 2,
rounds_remaining: 0,
pool: vec![],
queue: vec![],
}
}
fn fill_pool(&mut self, set: &[CardStatistics]) {
fn fill_pool(&mut self, set: &[CardState]) {
let mut sorted = set.iter().enumerate().collect::<Vec<_>>();
sorted.sort_by_key(|c| c.1.score);
let mut worst = sorted[0..self.pool_size]
@@ -172,6 +175,112 @@ impl WorstWordsSRSModule {
}
}
#[derive(Clone)]
pub struct FsrsModule {
today_set: Vec<usize>,
solved: Vec<usize>,
current_group: Vec<usize>,
queue: Vec<usize>,
rng: ThreadRng,
fsrs: FSRS,
pub(crate) initialized: bool,
}
impl SRSModule for FsrsModule {
fn next(&mut self, _: &[CardState]) -> usize {
if self.current_group.is_empty() {
self.fill_partial_pool();
}
if self.queue.is_empty() {
self.fill_queue();
self.queue.shuffle(&mut self.rng);
}
self.queue.pop().unwrap_or(0)
}
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();
let memory = self
.fsrs
.next_states(updated_word.fsrs_memory, 0.9, delta_days as u32)
.expect("Wrong next stage calculation");
let next = match status {
WordOpenMode::Easy => memory.easy,
WordOpenMode::Ok => memory.good,
WordOpenMode::Hard => memory.hard,
WordOpenMode::None => memory.again,
};
let mut next_day = today + TimeDelta::days(next.interval.round() as i64);
if (next_day - today).num_days() >= 5 {
let offset = ((rng().random::<f32>() - 0.5) * 3.0).round() as i32;
next_day += TimeDelta::days(offset as i64);
}
updated_word.fsrs_memory = Some(next.memory);
updated_word.fsrs_open_day = next_day;
updated_word.fsrs_opened = updated_word.last_open.date_naive();
if updated_word.fsrs_open_day != today {
self.solved.push(index);
let index = self.current_group.iter().position(|x| *x == index).unwrap();
self.current_group.remove(index);
}
}
fn init(&mut self, set: &[CardState]) {
self.rng = rng();
let today = Local::now().date_naive();
self.today_set = set
.iter()
.enumerate()
.filter(|(_, c)| c.fsrs_open_day <= today)
.map(|(index, _)| index)
.collect();
self.today_set.shuffle(&mut self.rng);
self.fill_partial_pool();
self.initialized = true;
}
}
impl FsrsModule {
pub fn new() -> Self {
Self {
today_set: vec![],
current_group: vec![],
queue: vec![],
solved: vec![],
rng: Default::default(),
fsrs: FSRS::default(),
initialized: false,
}
}
fn fill_partial_pool(&mut self) {
self.current_group.extend(
self.today_set
.iter()
.filter(|x| !self.solved.contains(*x))
.take(PARTIAL_POOL_SIZE),
);
self.queue.clear();
self.queue.extend(self.current_group.clone());
}
fn fill_queue(&mut self) {
self.queue.extend(self.current_group.clone());
}
pub fn finished(&self) -> bool {
self.today_set.len() == self.solved.len()
}
}
#[derive(Clone, Copy)]
pub enum WordOpenMode {
Easy,
+36 -17
View File
@@ -3,61 +3,80 @@ pub mod repositories;
use rusqlite::ToSql;
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
use std::fmt::{Display, Formatter};
use std::fmt::{Display, Formatter, write};
use std::num::ParseIntError;
use std::str::FromStr;
macro_rules! id_impl {
($id_type:ty, $name:ident, $invalid:ident) => {
#[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 {
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<Self, Self::Err> {
Ok(Id(s.parse::<u32>()?))
Ok($name(s.parse::<$id_type>()?))
}
}
impl From<u32> 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<Id> for u32 {
fn from(value: Id) -> Self {
impl From<$name> for $id_type {
fn from(value: $name) -> Self {
value.0
}
}
impl Display for Id {
impl Display for $name {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromSql for Id {
impl FromSql for $name {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
match value {
ValueRef::Integer(i) => Ok(Id(i as u32)),
ValueRef::Integer(i) => Ok($name(i as $id_type)),
_ => Err(FromSqlError::InvalidType),
}
}
}
impl ToSql for Id {
impl ToSql for $name {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
Ok(ToSqlOutput::from(self.0 as i64))
}
}
impl From<Id> for String {
fn from(value: Id) -> Self {
impl From<$name> for String {
fn from(value: $name) -> Self {
value.0.to_string()
}
}
impl Id {
impl $name {
pub(crate) fn is_valid(self) -> bool {
self.0 != 0
}
}
};
}
id_impl!(u32, Id, INVALID_ID);
id_impl!(u16, SmallId, INVALID_SMALL_ID);
impl From<SmallId> for Id {
fn from(value: SmallId) -> Self {
Self(value.0 as u32)
}
}
impl From<Id> for SmallId {
fn from(value: Id) -> Self {
Self(value.0 as u16)
}
}
+35 -11
View File
@@ -1,24 +1,24 @@
use crate::app::AppState;
use crate::core::srs::{FADE_PER_DAY, MAX_SCORE, OrderMode, WordOpenMode};
use crate::data::{INVALID_ID, Id};
use crate::notification::MaybeError;
use chrono::{DateTime, Utc};
use chrono::{DateTime, NaiveDate, Utc};
use fsrs::MemoryState;
use litemap::LiteMap;
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;
#[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<String, String>,
pub group_id: Id,
pub additional: LiteMap<String, String>,
}
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 {
@@ -145,14 +155,28 @@ impl DeckSettings {
}
#[derive(Clone, PartialEq)]
pub struct CardStatistics {
pub struct CardState {
pub id: Id,
pub word_id: Id,
pub set_id: Id,
pub last_open: DateTime<Utc>,
pub score: u8,
pub fsrs_opened: NaiveDate,
pub fsrs_memory: Option<MemoryState>,
pub fsrs_open_day: NaiveDate,
}
impl CardStatistics {
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 {
WordOpenMode::Easy => {
@@ -182,7 +206,7 @@ impl CardStatistics {
#[derive(Copy)]
pub struct EntityHandle<E> {
pub(crate) id: Id,
index: i32,
index: u32,
pub(crate) _phantom: PhantomData<E>,
}
@@ -199,7 +223,7 @@ impl<T> EntityHandle<T> {
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(),
}
}
@@ -208,7 +232,7 @@ impl<T> EntityHandle<T> {
}
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 {
@@ -216,7 +240,7 @@ impl<T> EntityHandle<T> {
}
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<T> Eq for EntityHandle<T> {}
+24 -4
View File
@@ -20,9 +20,11 @@ where
fn get_mut(&mut self, handle: &mut 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>;
fn save(&mut self, entity: &mut EntityHandle<T>) -> MaybeError;
fn delete(&mut self, handle: EntityHandle<T>) -> MaybeError;
fn init(&mut self) -> MaybeError;
fn start(&mut self) -> MaybeError;
fn reload(&mut self) -> MaybeError;
}
@@ -177,8 +179,8 @@ pub trait BatchedDbLayer<T>
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)]
@@ -317,6 +319,15 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
entity
}
fn create_now(&mut self, mut new: T) -> Result<EntityHandle<T>, NotificationData> {
self.data_layer
.add_entity(&mut new, &self.connection.read())?;
let index = self.cache.len();
let handle = EntityHandle::new(new.id(), index, true);
self.cache.push(new);
Ok(handle)
}
fn save(&mut self, entity: &mut EntityHandle<T>) -> MaybeError {
let original_state = entity.clone();
if entity.real() {
@@ -366,6 +377,15 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> 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();
@@ -405,7 +425,7 @@ impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> 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();
@@ -447,7 +467,7 @@ impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> 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| {
+178
View File
@@ -0,0 +1,178 @@
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, Error, Params, Row, params};
use std::time::Instant;
pub struct CardStatesQuerySet;
impl TableAccessQuerySet<CardState> for CardStatesQuerySet {
fn load() -> &'static str {
"SELECT id, word_id, set_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day FROM states"
}
fn save() -> &'static str {
"UPDATE states SET score = ?1, last_opened = ?2 WHERE id = ?3"
}
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 {
"states"
}
fn from_sql(row: &Row) -> Result<CardState, Error> {
Ok(CardState {
id: row.get(0)?,
word_id: row.get(1)?,
set_id: row.get(2)?,
score: row.get(3)?,
fsrs_opened: NaiveDate::from_epoch_days(row.get::<usize, i32>(5)?)
.expect("Incorrect date encoding"),
fsrs_memory: get_memory(row.get(6)?),
last_open: row.get(4)?,
fsrs_open_day: NaiveDate::from_epoch_days(row.get::<usize, i32>(7)?)
.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<Vec<CardState>, 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(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::<usize, i32>(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::<usize, i32>(6)?)
.expect("Incorrect date encoding"),
})
})
.map_err(|_| DbLoadError)?;
let mut buffer = Vec::with_capacity(512);
iter.filter_map(|res| res.ok()).for_each(|i| buffer.push(i));
Ok(buffer)
}
fn get_memory(value: i64) -> Option<MemoryState> {
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: &Option<MemoryState>) -> i64 {
let Some(value) = &value else {
return 0;
};
value.stability.to_bits() as i64 | ((value.difficulty.to_bits() as i64) << 32)
}
pub fn add_stat_list(stats: &mut [CardState], connection: &mut Connection) -> MaybeError {
let time = Instant::now();
let tx = connection
.transaction()
.expect("Failed to create transaction");
let count = stats.len();
{
let mut stmt = tx
.prepare(
"INSERT INTO states (word_id, set_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day) VALUES (?1, ?2, ?3, ?4)",
)
.expect("Failed to prepare state query");
for stat in stats.iter() {
stmt.execute(params![
stat.word_id,
stat.set_id,
stat.score,
stat.last_open.timestamp(),
stat.fsrs_opened.to_epoch_days(),
memory_to_i64(&stat.fsrs_memory),
stat.fsrs_open_day.to_epoch_days()
])
.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() {
stats[index].id = id.into();
}
info!("Added {} cards for {:?}", stats.len(), time.elapsed());
Ok(())
}
pub fn update_stat_score(stat: &CardState, connection: &Connection) -> MaybeError {
let time = Instant::now();
connection
.execute(
"UPDATE states SET score = ?1, last_opened = ?2 WHERE id = ?3",
(&stat.score, &stat.last_open.timestamp(), &stat.id),
)
.map_err(|_| DbSaveError)?;
info!("Updated stat: {}", time.elapsed().as_millis());
Ok(())
}
pub fn delete_stats(stat: &[CardState], connection: &Connection) -> MaybeError {
if stat.is_empty() {
return Ok(());
}
let ids = stat
.iter()
.map(|s| s.id.into())
.collect::<Vec<String>>()
.join(", ");
connection
.execute(
format!("DELETE FROM states WHERE id in ({ids})").as_str(),
(),
)
.map_err(|_| DbSaveError)?;
Ok(())
}
-100
View File
@@ -1,100 +0,0 @@
use crate::data::entities::{CardStatistics, DeckSettings};
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
use crate::notification::{MaybeError, NotificationData};
use log::info;
use rusqlite::{Connection, params};
use std::time::Instant;
pub fn load_stats_of_deck(
set: &DeckSettings,
connection: &Connection,
) -> Result<Vec<CardStatistics>, NotificationData> {
let mut stmt = connection
.prepare("SELECT id, word_id, score, last_opened FROM card_stats WHERE set_id = ?1")
.expect("Failed to prepare state query");
let iter = stmt
.query_map((set.id,), |row| {
Ok(CardStatistics {
id: row.get(0)?,
word_id: row.get(1)?,
set_id: set.id,
score: row.get(2)?,
last_open: row.get(3)?,
})
})
.map_err(|_| DbLoadError)?;
let mut buffer = Vec::with_capacity(512);
iter.filter_map(|res| res.ok()).for_each(|i| buffer.push(i));
Ok(buffer)
}
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) -> MaybeError {
let time = Instant::now();
let tx = connection
.transaction()
.expect("Failed to create transaction");
let count = stats.len();
{
let mut stmt = tx
.prepare(
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4)",
)
.expect("Failed to prepare state query");
for stat in stats.iter() {
stmt.execute(params![
stat.word_id,
stat.set_id,
stat.score,
stat.last_open.timestamp()
])
.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() {
stats[index].id = id.into();
}
info!("Added {} cards for {:?}", stats.len(), time.elapsed());
Ok(())
}
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> MaybeError {
let time = Instant::now();
connection
.execute(
"UPDATE card_stats SET score = ?1, last_opened = ?2 WHERE id = ?3",
(&stat.score, &stat.last_open.timestamp(), &stat.id),
)
.map_err(|_| DbSaveError)?;
info!("Updated stat: {}", time.elapsed().as_millis());
Ok(())
}
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) -> MaybeError {
if stat.is_empty() {
return Ok(());
}
let ids = stat
.iter()
.map(|s| s.id.into())
.collect::<Vec<String>>()
.join(", ");
connection
.execute(
format!("DELETE FROM card_stats WHERE id in ({ids})").as_str(),
(),
)
.map_err(|_| DbSaveError)?;
Ok(())
}
+37
View File
@@ -0,0 +1,37 @@
use crate::data::entities::WordGroup;
use crate::data_provider::TableAccessQuerySet;
use rusqlite::{Error, Params, Row};
pub struct CustomFieldsQuerySet;
impl TableAccessQuerySet<WordGroup> for CustomFieldsQuerySet {
fn load() -> &'static str {
"SELECT id, name FROM custom_fields"
}
fn save() -> &'static str {
"UPDATE custom_fields SET name = ?1 WHERE id = ?2"
}
fn add() -> &'static str {
"INSERT INTO word_groups (name) VALUES (?1)"
}
fn name() -> &'static str {
"custom_fields"
}
fn from_sql(row: &Row) -> Result<WordGroup, Error> {
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)
}
}
@@ -1,13 +1,64 @@
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<DeckSettings> 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<DeckSettings, Error> {
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<Vec<DeckSettings>, NotificationData> {
let mut stmt = connection
.prepare("SELECT id, name, forward, backward, filter FROM card_set")
.prepare("SELECT id, name, forward, backward, filter FROM decks")
.expect("Failed to prepare state query");
let iter = stmt
.query_map([], |row| {
@@ -31,7 +82,7 @@ pub fn load_sets(connection: &Connection) -> Result<Vec<DeckSettings>, Notificat
pub fn add_set(set: &mut DeckSettings, connection: &Connection) -> MaybeError {
let index : u32 = connection
.query_row(
"INSERT INTO card_set (name, forward, backward, filter) VALUES (?1, ?2, ?3, ?4) RETURNING id",
"INSERT INTO decks (name, forward, backward, filter) VALUES (?1, ?2, ?3, ?4) RETURNING id",
(
&set.name,
&set.forward,
@@ -52,7 +103,7 @@ pub fn update_deck(set: &mut DeckSettings, connection: &Connection) -> MaybeErro
} else {
connection
.execute(
"UPDATE card_set SET name = ?1, forward = ?2, backward = ?3, filter = ?4 WHERE id = ?5",
"UPDATE decks SET name = ?1, forward = ?2, backward = ?3, filter = ?4 WHERE id = ?5",
(
&set.name,
&set.forward,
@@ -71,7 +122,7 @@ pub fn delete_set(set: &DeckSettings, connection: &Connection) -> MaybeError {
return Err(DbSaveError);
}
connection
.execute("DELETE FROM card_set WHERE id = ?1", (&set.id,))
.execute("DELETE FROM decks WHERE id = ?1", (&set.id,))
.map_err(|_| DbSaveError)?;
Ok(())
}
+9 -1
View File
@@ -4,12 +4,20 @@ use crate::data::entities::HistoryItem;
use crate::fs::*;
use crate::notification::NotificationData::PermissionDenied;
use crate::notification::{MaybeError, NotificationData};
use chrono::DateTime;
use chrono::{DateTime, NaiveDate};
use rusqlite::Connection;
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
pub fn get_open_count_of_set(
id: Id,
connection: &Connection,
) -> Result<Vec<(NaiveDate, u32)>, NotificationData> {
todo!()
}
pub fn get_history_of_set(id: Id) -> Result<Vec<HistoryItem>, NotificationData> {
let app_dir = history_dir()?;
let head = app_dir.clone().join(format!("set_{}_history.csv", id));
+154 -2
View File
@@ -1,9 +1,161 @@
pub(crate) mod card_sets;
pub(crate) mod card_stats;
use crate::data::Id;
use crate::data::entities::{CardState, EntityData};
use crate::data::repositories::{BatchedDbLayer, DbLayer};
use crate::notification::MaybeError;
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
use log::debug;
use rusqlite::{Connection, Error, Params, Row};
use std::marker::PhantomData;
use std::time::Instant;
pub(crate) mod card_states;
pub mod custom_fields;
pub(crate) mod decks;
pub(crate) mod history;
pub(crate) mod import;
pub(crate) mod settings;
pub(crate) mod sqlite;
pub(crate) mod voice;
pub(crate) mod web_api;
pub mod word_groups;
pub(crate) mod words;
pub trait TableAccessQuerySet<T> {
fn load() -> &'static str;
fn save() -> &'static str;
fn add() -> &'static str;
fn name() -> &'static str;
fn from_sql(row: &Row) -> Result<T, Error>;
fn insert_params(entity: &T) -> impl Params;
fn update_params(entity: &T) -> impl Params;
}
pub struct SqliteDbLayer<T, Q>
where
Q: TableAccessQuerySet<T>,
{
_phantom: PhantomData<(T, Q)>,
}
impl<T: EntityData, Q: TableAccessQuerySet<T>> DbLayer<T> for SqliteDbLayer<T, Q> {
fn write_entity(&self, entity: &T, connection: &Connection) -> MaybeError {
let time = Instant::now();
connection
.execute(Q::load(), Q::update_params(entity))
.map_err(|_| DbSaveError)?;
debug!("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());
debug!("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);
}
connection
.execute(
format!("DELETE FROM {} WHERE id = ?1", Q::name()).as_str(),
(&id,),
)
.map_err(|_| DbSaveError)?;
debug!("Deleted from {} for {:?}", Q::name(), time.elapsed());
Ok(())
}
fn load_all(&self, storage: &mut Vec<T>, connection: &Connection) -> MaybeError {
let time = Instant::now();
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));
debug!(
"Loaded {} from {} for {:?}",
storage.len(),
Q::name(),
time.elapsed()
);
Ok(())
}
}
impl<T: EntityData, Q: TableAccessQuerySet<T>> BatchedDbLayer<T> for SqliteDbLayer<T, Q> {
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()
.map(|i| i.to_string())
.collect::<Vec<_>>()
.join(",");
connection
.execute(
format!("DELETE FROM {} WHERE id in ({})", Q::name(), stringed).as_str(),
(),
)
.map_err(|_| DbSaveError)?;
}
debug!(
"Deleted {} from {} for {:?}",
ids.len(),
Q::name(),
time.elapsed()
);
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());
}
debug!(
"Added {} into {} for {:?}",
entities.len(),
Q::name(),
time.elapsed()
);
Ok(())
}
}
+29 -4
View File
@@ -20,6 +20,8 @@ fn create_tables(conn: &Connection) {
make_word_group(conn).expect("Failed to init table");
make_words(conn).expect("Failed to init table");
make_card_stats(conn).expect("Failed to init table");
make_history(conn).expect("Failed to init table");
make_custom_fields(conn).expect("Failed to init table");
}
fn make_settings(conn: &Connection) -> Result<(), rusqlite::Error> {
@@ -29,15 +31,15 @@ fn make_settings(conn: &Connection) -> Result<(), rusqlite::Error> {
}
fn make_card_set(conn: &Connection) -> Result<(), rusqlite::Error> {
let query = include_str!("../../sql/card_set.sql");
let query = include_str!("../../sql/decks.sql");
conn.execute(query, ())?;
Ok(())
}
fn make_word_group(conn: &Connection) -> Result<(), rusqlite::Error> {
let query = include_str!("../../sql/word_group.sql");
let query = include_str!("../../sql/word_groups.sql");
conn.execute(query, ())?;
conn.execute("insert into word_group (name) values (\"Слова\");", ())?;
conn.execute("insert into word_groups (name) values (\"Слова\");", ())?;
Ok(())
}
fn make_words(conn: &Connection) -> Result<(), rusqlite::Error> {
@@ -46,7 +48,19 @@ fn make_words(conn: &Connection) -> Result<(), rusqlite::Error> {
Ok(())
}
fn make_card_stats(conn: &Connection) -> Result<(), rusqlite::Error> {
let query = include_str!("../../sql/card_stats.sql");
let query = include_str!("../../sql/states.sql");
conn.execute(query, ())?;
Ok(())
}
fn make_history(conn: &Connection) -> Result<(), rusqlite::Error> {
let query = include_str!("../../sql/history.sql");
conn.execute(query, ())?;
Ok(())
}
fn make_custom_fields(conn: &Connection) -> Result<(), rusqlite::Error> {
let query = include_str!("../../sql/custom_fields.sql");
conn.execute(query, ())?;
Ok(())
}
@@ -58,4 +72,15 @@ pub fn default_connection() -> Connection {
.execute("PRAGMA foreign_keys = ON;", [])
.expect("Failed to set PRAGMA");
connection
.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");
connection
}
+37
View File
@@ -0,0 +1,37 @@
use crate::data::entities::WordGroup;
use crate::data_provider::TableAccessQuerySet;
use rusqlite::{Error, Params, Row};
pub struct WordGroupQuerySet;
impl TableAccessQuerySet<WordGroup> 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<WordGroup, Error> {
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)
}
}
+59 -7
View File
@@ -1,9 +1,62 @@
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 litemap::LiteMap;
use rusqlite::{Connection, Error, Params, Row, params};
use std::collections::HashMap;
pub struct WordQuerySet;
impl TableAccessQuerySet<WordData> 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<WordData, Error> {
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 fn add_word(word: &mut WordData, connection: &Connection) -> MaybeError {
let index : u32 = connection
.query_row(
@@ -99,8 +152,7 @@ pub fn load_words(connection: &Connection) -> Result<Vec<WordData>, Notification
key: row.get(1)?,
value: row.get(2)?,
tags: row.get(3)?,
additional: serde_json::from_str::<HashMap<String, String>>(&additional)
.unwrap_or_default(),
additional: serde_json::from_str(&additional).unwrap_or_default(),
group_id: row.get(5)?,
})
})
@@ -116,7 +168,7 @@ pub fn load_words(connection: &Connection) -> Result<Vec<WordData>, Notification
pub fn load_word_groups(connection: &Connection) -> Result<Vec<WordGroup>, NotificationData> {
let mut stmt = connection
.prepare("SELECT id, name FROM word_group")
.prepare("SELECT id, name FROM word_groups")
.expect("Failed to prepare statement");
let group_iter = stmt
.query_map([], |row| {
@@ -138,7 +190,7 @@ pub fn load_word_groups(connection: &Connection) -> Result<Vec<WordGroup>, Notif
pub fn add_group(group: &mut WordGroup, connection: &Connection) -> MaybeError {
let index: u32 = connection
.query_row(
"INSERT INTO word_group (name) VALUES (?1) RETURNING id",
"INSERT INTO word_groups (name) VALUES (?1) RETURNING id",
(&group.name,),
|row| row.get(0),
)
@@ -154,7 +206,7 @@ pub fn update_group(group: &mut WordGroup, connection: &Connection) -> MaybeErro
} else {
connection
.execute(
"UPDATE word_group SET name = ?1 WHERE id = ?2",
"UPDATE word_groups SET name = ?1 WHERE id = ?2",
(&group.name, &group.id),
)
.map_err(|_| DbSaveError)?;
@@ -167,7 +219,7 @@ pub fn delete_group(group: &WordGroup, connection: &Connection) -> MaybeError {
return Err(DbSaveError);
}
connection
.execute("DELETE FROM word_group WHERE id = ?1", (&group.id,))
.execute("DELETE FROM word_groups WHERE id = ?1", (&group.id,))
.map_err(|_| DbSaveError)?;
Ok(())
}
+3 -3
View File
@@ -37,7 +37,7 @@ use iced::keyboard::Event;
use iced::widget::stack;
use iced::{Element, Task};
use iced_core::Length::Fill;
use log::info;
use log::{debug, info};
use parking_lot::Mutex;
use rusqlite::Connection;
use std::sync::Arc;
@@ -241,7 +241,7 @@ impl ScreenState {
Import
);
}
info!("Update took {:?}", time.elapsed());
debug!("Update took {:?}", time.elapsed());
task.0
}
@@ -288,7 +288,7 @@ impl ScreenState {
let notifications = self.notification_manager.draw().map(Notification);
info!("View took {:?}", time.elapsed());
debug!("View took {:?}", time.elapsed());
stack![view, notifications].width(Fill).height(Fill).into()
}
+2 -2
View File
@@ -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")
+2 -2
View File
@@ -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<Option<MixerDeviceSink>>,
+2 -2
View File
@@ -1,7 +1,7 @@
use crate::app::AppState;
use crate::data::entities::DeckSettings;
use crate::data_provider::card_sets::{delete_set, update_deck};
use crate::data_provider::card_stats::{delete_stats, load_stats_of_deck};
use crate::data_provider::card_states::{delete_stats, load_stats_of_deck};
use crate::data_provider::decks::{delete_set, update_deck};
use crate::navigation::Page::PreviousPage;
use crate::navigation::{NavigatedPage, Page, RootMessage};
use crate::notification::MaybeError;
+7 -4
View File
@@ -1,9 +1,9 @@
use crate::app::AppState;
use crate::core::srs::{AppendMode, OrderMode};
use crate::data::Id;
use crate::data::entities::{CardStatistics, DeckSettings};
use crate::data_provider::card_sets::update_deck;
use crate::data_provider::card_stats::{add_stat_list, load_stats_of_deck};
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};
use crate::navigation::{NavigatedPage, Page, RootMessage};
use crate::notification::{MaybeError, NotificationRequest};
@@ -585,12 +585,15 @@ 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(),
score: 1,
fsrs_opened: Utc::now().date_naive(),
fsrs_memory: None,
set_id: deck.id,
fsrs_open_day: Utc::now().date_naive(),
}
})
.collect::<Vec<_>>();