Faster new set initializing

This commit is contained in:
2026-07-29 13:30:49 +03:00
parent b8be482efd
commit 0b0b9f307d
5 changed files with 97 additions and 50 deletions
+36
View File
@@ -1,6 +1,7 @@
use crate::lang::CardStatistics;
use crate::repetitions::CardSetSettings;
use rusqlite::Connection;
use std::time::Instant;
pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<CardStatistics> {
let mut stmt = connection.prepare("SELECT id, word_id, score, last_opened FROM card_stats WHERE set_id = ?1").unwrap();
@@ -22,7 +23,37 @@ pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<
buffer
}
pub fn add_stat_list(stat: &mut Vec<CardStatistics>, connection: &Connection) {
let inserting = stat.iter().map(|stat| format!("({}, {}, {}, {})", stat.word_id.to_string(), stat.set_id.to_string(), stat.score.to_string(), stat.last_open.timestamp().to_string())).collect::<Vec<_>>().join(", ");
let query = format!("INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES {}", inserting);
let count = connection
.execute(
query.as_str(),
(
),
);
if count.is_err() {
println!("{}", count.unwrap_err());
return;
}
let last_index : u32 = connection.query_one("SELECT seq from sqlite_sequence WHERE name == ?1", ("card_stats".to_string(),), |row| row.get(0)).unwrap();
let start_index = last_index - (count.unwrap() as u32) + 1;
let mut index = 0;
for id in start_index..=last_index {
stat[index].id = id as u32;
index += 1;
}
}
pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
let time = Instant::now();
let index = connection
.query_row(
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4) RETURNING id",
@@ -37,9 +68,12 @@ pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
.unwrap_or_else(|e| {println!("{}", e); 0});
stat.id = index;
println!("Added stat: {}", time.elapsed().as_millis());
}
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection){
let time = Instant::now();
connection
.execute(
"UPDATE card_stats SET score = ?1, last_opened = ?2 WHERE id = ?3",
@@ -50,6 +84,8 @@ pub fn update_stat_score(stat: &CardStatistics, connection: &Connection){
),
)
.unwrap_or_else(|e| {println!("{}", e); 0});
println!("Updated stat: {}", time.elapsed().as_millis());
}
pub fn delete_stat(stat: &CardStatistics, connection: &Connection) {
+1
View File
@@ -326,6 +326,7 @@ impl DictionaryState {
if !self.search.is_empty() {
if word.key.contains(&self.search) == false
&& word.value.contains(&self.search) == false
&& word.tags.contains(&self.search) == false
{
continue;
}
+13 -10
View File
@@ -1,6 +1,5 @@
use crate::data_provider::card_stats::{
add_stat, delete_stat, load_stats_of_set, update_stat_score,
};
use crate::data_provider::card_stats::{add_stat_list, delete_stat, load_stats_of_set, update_stat_score};
use crate::data_provider::history::{push_note, HistoryItem};
use crate::repetitions::CardSetSettings;
use crate::AppState;
use chrono::{DateTime, Utc};
@@ -13,7 +12,7 @@ use serde::{Deserialize, Serialize};
use std::cmp::min;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use crate::data_provider::history::{push_note, HistoryItem};
use std::time::Instant;
const MAX_HISTORY_LEN: usize = 20;
const MAX_HISTORY_LEN_PART: f32 = 0.33;
@@ -329,7 +328,7 @@ impl CardSet {
let saved_ids = current_set.iter().map(|l| l.word_id).collect::<Vec<u32>>();
let word_ids = last_list.iter().map(|l| l.id).collect::<Vec<u32>>();
last_list
let new_stats: &mut Vec<CardStatistics> = &mut last_list
.iter()
.filter(|word| !saved_ids.contains(&word.id))
.map(|word| CardStatistics {
@@ -338,12 +337,16 @@ impl CardSet {
last_open: Utc::now(),
score: 1,
set_id: settings.id.clone(),
})
.for_each(|mut new_statistic| {
add_stat(&mut new_statistic, &state_locked.connection);
current_set.push(new_statistic);
});
}).collect();
let time = Instant::now();
if !new_stats.is_empty() {
add_stat_list(new_stats, &state_locked.connection);
current_set.append(new_stats);
}
println!("Added {} stats: {}", new_stats.len(), time.elapsed().as_millis());
let mut index = 0;
for stat in current_set.clone() {
if !word_ids.contains(&stat.word_id) {