Merge pull request 'Db layers' (#14) from db_layers into master

Reviewed-on: #14
This commit was merged in pull request #14.
This commit is contained in:
micialware committed 2026-08-30 23:38:33 +03:00
commit 97e301b0d6
15 files changed
+492 -88

No files matched your search

Generated
+6 -2
View File
@@ -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"
+1
View File
@@ -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"
+3 -3
View File
@@ -15,7 +15,7 @@ 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,
@@ -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;
+18 -18
View File
@@ -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::<Vec<usize>>();
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::<Vec<_>>();
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
+33 -26
View File
@@ -3,61 +3,68 @@ 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;
#[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 {
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<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> for u32 {
fn from(value: Id) -> Self {
impl From<$id_type> for $name {
fn from(value: $id_type) -> Self {
$name(value)
}
}
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);
+30 -9
View File
@@ -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<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,7 +155,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 +165,18 @@ pub struct CardStatistics {
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 => {
@@ -185,7 +206,7 @@ impl CardStatistics {
#[derive(Copy)]
pub struct EntityHandle<E> {
pub(crate) id: Id,
index: i32,
index: u32,
pub(crate) _phantom: PhantomData<E>,
}
@@ -202,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(),
}
}
@@ -211,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 {
@@ -219,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> {}
+14 -4
View File
@@ -24,6 +24,7 @@ where
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;
}
@@ -178,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)]
@@ -375,6 +376,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();
@@ -414,7 +424,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();
@@ -456,7 +466,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| {
+70 -16
View File
@@ -1,29 +1,79 @@
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<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<CardStatistics>, NotificationData> {
) -> 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(CardStatistics {
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: 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::<usize, i32>(6)?)
.expect("Incorrect date encoding"),
@@ -37,18 +87,25 @@ pub fn load_stats_of_deck(
Ok(buffer)
}
fn get_memory(value: i64) -> MemoryState {
MemoryState {
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: &MemoryState) -> i64 {
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 [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()
@@ -69,10 +126,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)?;
@@ -92,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
@@ -105,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(());
}
+48 -2
View File
@@ -1,8 +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<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
+162
View File
@@ -1,3 +1,13 @@
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::info;
use rusqlite::{Connection, Error, Params, Row};
use std::marker::PhantomData;
use std::time::Instant;
pub(crate) mod card_states;
pub(crate) mod decks;
pub(crate) mod history;
@@ -7,3 +17,155 @@ pub(crate) mod sqlite;
pub(crate) mod voice;
pub(crate) mod web_api;
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)?;
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);
}
connection
.execute(
format!("DELETE FROM {} WHERE id = ?1", Q::name()).as_str(),
(&id,),
)
.map_err(|_| DbSaveError)?;
info!(
"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));
info!(
"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)?;
}
info!(
"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());
}
info!(
"Added {} into {} for {:?}",
entities.len(),
Q::name(),
time.elapsed()
);
Ok(())
}
}
+11
View File
@@ -65,4 +65,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
}
+90 -2
View File
@@ -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<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 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)
}
}
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<Vec<WordData>, Notification
key: row.get(1)?,
value: row.get(2)?,
tags: row.get(3)?,
additional: serde_json::from_str::<HashMap<String, String>>(&additional)
additional: serde_json::from_str(&additional)
.unwrap_or_default(),
group_id: row.get(5)?,
})
+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::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(),