Compare commits
23
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa58bb2918 | ||
|
|
6f010428d5 | ||
|
|
d9bbf9e705 | ||
|
|
9a5b64a4fe | ||
|
|
c9ccacc2d1 | ||
|
|
068b9c7197 | ||
|
|
45830a7985 | ||
|
|
8ec7c8f05d | ||
|
|
18aa748451 | ||
|
|
3ac7cd068b | ||
|
|
b006ece0c1 | ||
|
|
ea214d8ac0 | ||
|
|
4b17a5a4a7 | ||
|
|
2c1163fc3b | ||
|
|
d8e3a5b6db | ||
|
|
a01a29c492 | ||
|
|
70aff6ed18 | ||
|
|
89aa1abc13 | ||
|
|
0f2090af77 | ||
|
|
0326b56ded | ||
|
|
3eba0c93d4 | ||
|
|
d99fd337da | ||
|
|
807f398d81 |
No files matched your search
Generated
+19
-14
@@ -1066,7 +1066,7 @@ dependencies = [
|
||||
"cosmic-text",
|
||||
"etagere",
|
||||
"lru",
|
||||
"rustc-hash 2.1.1",
|
||||
"rustc-hash 2.1.3",
|
||||
"wgpu",
|
||||
]
|
||||
|
||||
@@ -2091,7 +2091,7 @@ dependencies = [
|
||||
"lilt",
|
||||
"log",
|
||||
"num-traits",
|
||||
"rustc-hash 2.1.1",
|
||||
"rustc-hash 2.1.3",
|
||||
"smol_str",
|
||||
"thiserror 2.0.18",
|
||||
"web-time",
|
||||
@@ -2117,7 +2117,7 @@ dependencies = [
|
||||
"futures",
|
||||
"iced_core",
|
||||
"log",
|
||||
"rustc-hash 2.1.1",
|
||||
"rustc-hash 2.1.3",
|
||||
"tokio",
|
||||
"wasm-bindgen-futures",
|
||||
"wasmtimer",
|
||||
@@ -2137,7 +2137,7 @@ dependencies = [
|
||||
"iced_futures",
|
||||
"log",
|
||||
"raw-window-handle",
|
||||
"rustc-hash 2.1.1",
|
||||
"rustc-hash 2.1.3",
|
||||
"thiserror 2.0.18",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
@@ -2191,7 +2191,7 @@ dependencies = [
|
||||
"kurbo 0.10.4",
|
||||
"log",
|
||||
"resvg",
|
||||
"rustc-hash 2.1.1",
|
||||
"rustc-hash 2.1.3",
|
||||
"softbuffer",
|
||||
"tiny-skia",
|
||||
]
|
||||
@@ -2212,7 +2212,7 @@ dependencies = [
|
||||
"iced_graphics",
|
||||
"log",
|
||||
"resvg",
|
||||
"rustc-hash 2.1.1",
|
||||
"rustc-hash 2.1.3",
|
||||
"thiserror 2.0.18",
|
||||
"wgpu",
|
||||
]
|
||||
@@ -2227,7 +2227,7 @@ dependencies = [
|
||||
"log",
|
||||
"num-traits",
|
||||
"ouroboros",
|
||||
"rustc-hash 2.1.1",
|
||||
"rustc-hash 2.1.3",
|
||||
"thiserror 2.0.18",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
@@ -2242,7 +2242,7 @@ dependencies = [
|
||||
"iced_program",
|
||||
"log",
|
||||
"mundy",
|
||||
"rustc-hash 2.1.1",
|
||||
"rustc-hash 2.1.3",
|
||||
"thiserror 2.0.18",
|
||||
"tracing",
|
||||
"wasm-bindgen-futures",
|
||||
@@ -2456,6 +2456,7 @@ dependencies = [
|
||||
"hex",
|
||||
"iced",
|
||||
"iced_core",
|
||||
"litemap",
|
||||
"log",
|
||||
"mimalloc",
|
||||
"parking_lot",
|
||||
@@ -2466,6 +2467,7 @@ dependencies = [
|
||||
"rhai",
|
||||
"rodio",
|
||||
"rusqlite",
|
||||
"rustc-hash 2.1.3",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"tokio",
|
||||
@@ -2738,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"
|
||||
@@ -3907,7 +3912,7 @@ dependencies = [
|
||||
"pin-project-lite",
|
||||
"quinn-proto",
|
||||
"quinn-udp",
|
||||
"rustc-hash 2.1.1",
|
||||
"rustc-hash 2.1.3",
|
||||
"rustls",
|
||||
"socket2",
|
||||
"thiserror 2.0.18",
|
||||
@@ -3929,7 +3934,7 @@ dependencies = [
|
||||
"rand",
|
||||
"rand_pcg",
|
||||
"ring",
|
||||
"rustc-hash 2.1.1",
|
||||
"rustc-hash 2.1.3",
|
||||
"rustls",
|
||||
"rustls-pki-types",
|
||||
"slab",
|
||||
@@ -4334,9 +4339,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "2.1.1"
|
||||
version = "2.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
|
||||
checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d"
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
|
||||
+5
-1
@@ -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"
|
||||
@@ -38,9 +39,12 @@ panic = "abort"
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = { version = "0.8.2", features = ["html_reports"] }
|
||||
rustc-hash = "2.1.3"
|
||||
|
||||
[[bench]]
|
||||
name = "map_bench"
|
||||
harness = false
|
||||
|
||||
|
||||
[[bench]]
|
||||
name = "repo_cache_bench"
|
||||
harness = false
|
||||
@@ -0,0 +1,325 @@
|
||||
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
|
||||
use hashbrown::HashMap as HashbrownMap;
|
||||
use rustc_hash::FxHashMap;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::hint::black_box;
|
||||
|
||||
// Тестовая структура (аналог вашей сущности)
|
||||
#[derive(Debug, Clone)]
|
||||
struct Entity {
|
||||
id: i64,
|
||||
data: [u8; 64], // 64 байта данных для реалистичности
|
||||
}
|
||||
|
||||
impl Entity {
|
||||
fn new(id: i64) -> Self {
|
||||
Self {
|
||||
id,
|
||||
data: [id as u8; 64],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ========== ТЕСТОВЫЕ ФУНКЦИИ ==========
|
||||
|
||||
fn bench_vec_insert_sorted(vec: &mut Vec<Entity>, entities: &[Entity]) {
|
||||
vec.clear();
|
||||
for entity in entities {
|
||||
vec.push(entity.clone());
|
||||
}
|
||||
// Vec уже отсортирован, так как ID монотонно возрастают
|
||||
}
|
||||
|
||||
fn bench_vec_search_separated<'a>(ids: &[i64], vec: &'a [Entity], id: i64) -> Option<&'a Entity> {
|
||||
match ids.binary_search(&id) {
|
||||
Ok(index) => Some(&vec[index]),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn bench_vec_search(vec: &Vec<Entity>, id: i64) -> Option<&Entity> {
|
||||
match vec.binary_search_by(|probe| probe.id.cmp(&id)) {
|
||||
Ok(index) => Some(&vec[index]),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn bench_btreemap_insert(map: &mut BTreeMap<i64, Entity>, entities: &[Entity]) {
|
||||
map.clear();
|
||||
for entity in entities {
|
||||
map.insert(entity.id, entity.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn bench_btreemap_search(map: &BTreeMap<i64, Entity>, id: i64) -> Option<&Entity> {
|
||||
map.get(&id)
|
||||
}
|
||||
|
||||
fn bench_hashmap_insert(map: &mut HashMap<i64, Entity>, entities: &[Entity]) {
|
||||
map.clear();
|
||||
for entity in entities {
|
||||
map.insert(entity.id, entity.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn bench_hashmap_search(map: &HashMap<i64, Entity>, id: i64) -> Option<&Entity> {
|
||||
map.get(&id)
|
||||
}
|
||||
|
||||
fn bench_hashbrown_insert(map: &mut HashbrownMap<i64, Entity>, entities: &[Entity]) {
|
||||
map.clear();
|
||||
for entity in entities {
|
||||
map.insert(entity.id, entity.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn bench_hashbrown_search(map: &HashbrownMap<i64, Entity>, id: i64) -> Option<&Entity> {
|
||||
map.get(&id)
|
||||
}
|
||||
|
||||
fn bench_fxhashmap_insert(map: &mut FxHashMap<i64, Entity>, entities: &[Entity]) {
|
||||
map.clear();
|
||||
for entity in entities {
|
||||
map.insert(entity.id, entity.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn bench_fxhashmap_search(map: &FxHashMap<i64, Entity>, id: i64) -> Option<&Entity> {
|
||||
map.get(&id)
|
||||
}
|
||||
|
||||
// ========== ГЕНЕРАЦИЯ ДАННЫХ ==========
|
||||
|
||||
fn generate_entities(count: usize) -> Vec<Entity> {
|
||||
(0..count).map(|i| Entity::new(i as i64)).collect()
|
||||
}
|
||||
|
||||
fn generate_search_ids(count: usize, max_id: i64) -> Vec<i64> {
|
||||
// 70% попаданий, 30% промахов (реалистичный сценарий)
|
||||
let mut ids = Vec::with_capacity(count);
|
||||
for i in 0..count {
|
||||
if i % 10 < 7 {
|
||||
// Попадание
|
||||
ids.push((i as i64 * 13) % max_id);
|
||||
} else {
|
||||
// Промах (несуществующий ID)
|
||||
ids.push(max_id + i as i64);
|
||||
}
|
||||
}
|
||||
ids
|
||||
}
|
||||
|
||||
// ========== БЕНЧМАРКИ ==========
|
||||
|
||||
fn bench_insert(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("insert");
|
||||
|
||||
for size in [10_000, 100_000, 500_000] {
|
||||
let entities = generate_entities(size);
|
||||
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("Vec (sorted)", size),
|
||||
&entities,
|
||||
|b, entities| {
|
||||
let mut vec = Vec::with_capacity(size);
|
||||
b.iter(|| {
|
||||
bench_vec_insert_sorted(&mut vec, black_box(entities));
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("BTreeMap", size),
|
||||
&entities,
|
||||
|b, entities| {
|
||||
let mut map = BTreeMap::new();
|
||||
b.iter(|| {
|
||||
bench_btreemap_insert(&mut map, black_box(entities));
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("HashMap (std)", size),
|
||||
&entities,
|
||||
|b, entities| {
|
||||
let mut map = HashMap::new();
|
||||
b.iter(|| {
|
||||
bench_hashmap_insert(&mut map, black_box(entities));
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("HashMap (hashbrown)", size),
|
||||
&entities,
|
||||
|b, entities| {
|
||||
let mut map = HashbrownMap::new();
|
||||
b.iter(|| {
|
||||
bench_hashbrown_insert(&mut map, black_box(entities));
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("FxHashMap", size),
|
||||
&entities,
|
||||
|b, entities| {
|
||||
let mut map = FxHashMap::default();
|
||||
b.iter(|| {
|
||||
bench_fxhashmap_insert(&mut map, black_box(entities));
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_search(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("search");
|
||||
|
||||
for size in [10_000, 100_000, 500_000] {
|
||||
let entities = generate_entities(size);
|
||||
let entity_ids = entities.iter().map(|entity| entity.id).collect::<Vec<_>>();
|
||||
let search_ids = generate_search_ids(1000, size as i64);
|
||||
|
||||
// Подготовка коллекций
|
||||
let mut vec = Vec::with_capacity(size);
|
||||
bench_vec_insert_sorted(&mut vec, &entities);
|
||||
|
||||
let mut btreemap = BTreeMap::new();
|
||||
bench_btreemap_insert(&mut btreemap, &entities);
|
||||
|
||||
let mut hashmap = HashMap::new();
|
||||
bench_hashmap_insert(&mut hashmap, &entities);
|
||||
|
||||
let mut hashbrown = HashbrownMap::new();
|
||||
bench_hashbrown_insert(&mut hashbrown, &entities);
|
||||
|
||||
let mut fxhashmap = FxHashMap::default();
|
||||
bench_fxhashmap_insert(&mut fxhashmap, &entities);
|
||||
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("Vec (binary search)", size),
|
||||
&search_ids,
|
||||
|b, ids| {
|
||||
b.iter(|| {
|
||||
for &id in ids {
|
||||
black_box(bench_vec_search_separated(&entity_ids, &vec, black_box(id)));
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("Vec (binary search) separated", size),
|
||||
&search_ids,
|
||||
|b, ids| {
|
||||
b.iter(|| {
|
||||
for &id in ids {
|
||||
black_box(bench_vec_search(&vec, black_box(id)));
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group.bench_with_input(BenchmarkId::new("BTreeMap", size), &search_ids, |b, ids| {
|
||||
b.iter(|| {
|
||||
for &id in ids {
|
||||
black_box(bench_btreemap_search(&btreemap, black_box(id)));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("HashMap (std)", size),
|
||||
&search_ids,
|
||||
|b, ids| {
|
||||
b.iter(|| {
|
||||
for &id in ids {
|
||||
black_box(bench_hashmap_search(&hashmap, black_box(id)));
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("HashMap (hashbrown)", size),
|
||||
&search_ids,
|
||||
|b, ids| {
|
||||
b.iter(|| {
|
||||
for &id in ids {
|
||||
black_box(bench_hashbrown_search(&hashbrown, black_box(id)));
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("FxHashMap", size),
|
||||
&search_ids,
|
||||
|b, ids| {
|
||||
b.iter(|| {
|
||||
for &id in ids {
|
||||
black_box(bench_fxhashmap_search(&fxhashmap, black_box(id)));
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_memory_efficiency(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("memory_efficiency");
|
||||
|
||||
let size = 100_000;
|
||||
let entities = generate_entities(size);
|
||||
|
||||
group.bench_function("Vec memory", |b| {
|
||||
b.iter(|| {
|
||||
let mut vec = Vec::with_capacity(size);
|
||||
bench_vec_insert_sorted(&mut vec, black_box(&entities));
|
||||
black_box(vec);
|
||||
});
|
||||
});
|
||||
|
||||
group.bench_function("BTreeMap memory", |b| {
|
||||
b.iter(|| {
|
||||
let mut map = BTreeMap::new();
|
||||
bench_btreemap_insert(&mut map, black_box(&entities));
|
||||
black_box(map);
|
||||
});
|
||||
});
|
||||
|
||||
group.bench_function("HashMap (std) memory", |b| {
|
||||
b.iter(|| {
|
||||
let mut map = HashMap::new();
|
||||
bench_hashmap_insert(&mut map, black_box(&entities));
|
||||
black_box(map);
|
||||
});
|
||||
});
|
||||
|
||||
group.bench_function("HashMap (hashbrown) memory", |b| {
|
||||
b.iter(|| {
|
||||
let mut map = HashbrownMap::new();
|
||||
bench_hashbrown_insert(&mut map, black_box(&entities));
|
||||
black_box(map);
|
||||
});
|
||||
});
|
||||
|
||||
group.bench_function("FxHashMap memory", |b| {
|
||||
b.iter(|| {
|
||||
let mut map = FxHashMap::default();
|
||||
bench_fxhashmap_insert(&mut map, black_box(&entities));
|
||||
black_box(map);
|
||||
});
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_insert, bench_search, bench_memory_efficiency);
|
||||
criterion_main!(benches);
|
||||
+3
-3
@@ -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
@@ -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
|
||||
|
||||
+25
-17
@@ -1,62 +1,70 @@
|
||||
pub mod entities;
|
||||
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;
|
||||
|
||||
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);
|
||||
+111
-5
@@ -6,24 +6,47 @@ use fsrs::MemoryState;
|
||||
use log::info;
|
||||
use rayon::prelude::*;
|
||||
use rhai::{Engine, Scope};
|
||||
use std::collections::HashMap;
|
||||
use std::cmp::Ordering;
|
||||
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 {
|
||||
fn id(&self) -> Id {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
self.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WordGroup {
|
||||
pub id: Id,
|
||||
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 {
|
||||
@@ -132,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,
|
||||
@@ -142,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 => {
|
||||
@@ -168,3 +202,75 @@ impl CardStatistics {
|
||||
(self.score as f32 * multiplier).max(1.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy)]
|
||||
pub struct EntityHandle<E> {
|
||||
pub(crate) id: Id,
|
||||
index: u32,
|
||||
pub(crate) _phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<T> Clone for EntityHandle<T> {
|
||||
fn clone(&self) -> Self {
|
||||
EntityHandle {
|
||||
id: self.id,
|
||||
index: self.index,
|
||||
_phantom: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T> EntityHandle<T> {
|
||||
pub fn new(id: Id, index: usize, real: bool) -> Self {
|
||||
Self {
|
||||
id,
|
||||
index: ((index as u32) << 1 >> 1) | (!real as u32) << 31,
|
||||
_phantom: Default::default(),
|
||||
}
|
||||
}
|
||||
pub fn index(&self) -> usize {
|
||||
(self.index << 1 >> 1) as usize
|
||||
}
|
||||
|
||||
pub fn set_index(&mut self, index: usize) {
|
||||
self.index = (self.index >> 31 << 31) | ((index as u32) << 1 >> 1);
|
||||
}
|
||||
|
||||
pub fn real(&self) -> bool {
|
||||
(self.index >> 31) == 0
|
||||
}
|
||||
|
||||
pub fn set_real(&mut self, value: bool) {
|
||||
self.index = self.index << 1 >> 1 | (!value as u32) << 31;
|
||||
}
|
||||
}
|
||||
impl<T> Eq for EntityHandle<T> {}
|
||||
|
||||
impl<T> PartialEq<Self> for EntityHandle<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.real() == other.real() && self.id == other.id
|
||||
}
|
||||
}
|
||||
impl<T> PartialOrd<Self> for EntityHandle<T> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Ord for EntityHandle<T> {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
if self.real() && !other.real() {
|
||||
return Ordering::Less;
|
||||
}
|
||||
|
||||
if !self.real() && other.real() {
|
||||
return Ordering::Greater;
|
||||
}
|
||||
|
||||
self.id.cmp(&other.id)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait EntityData {
|
||||
fn id(&self) -> Id;
|
||||
fn set_id(&mut self, id: Id);
|
||||
}
|
||||
@@ -0,0 +1,550 @@
|
||||
use crate::data::Id;
|
||||
use crate::data::entities::{EntityData, EntityHandle};
|
||||
use crate::helpers::into_result;
|
||||
use crate::notification::{MaybeError, NotificationData};
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use rayon::iter::IndexedParallelIterator;
|
||||
use rayon::iter::IntoParallelRefIterator;
|
||||
use rayon::iter::ParallelIterator;
|
||||
use rusqlite::Connection;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub trait Repository<T, D>
|
||||
where
|
||||
D: DbLayer<T>,
|
||||
{
|
||||
fn get(&self, handle: &mut EntityHandle<T>) -> Option<T>;
|
||||
fn get_ref(&self, handle: &mut EntityHandle<T>) -> Option<&T>;
|
||||
fn get_mut(&mut self, handle: &mut EntityHandle<T>) -> Option<&mut T>;
|
||||
fn 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;
|
||||
}
|
||||
|
||||
struct QueryManger<T, R, D>
|
||||
where
|
||||
R: Repository<T, D>,
|
||||
D: DbLayer<T>,
|
||||
{
|
||||
queries: Vec<Arc<Mutex<Query<T, R, D>>>>,
|
||||
}
|
||||
|
||||
impl<T, R, D> QueryManger<T, R, D>
|
||||
where
|
||||
R: Repository<T, D>,
|
||||
D: DbLayer<T>,
|
||||
T: EntityData,
|
||||
{
|
||||
pub(crate) fn handle_save(&self, old: EntityHandle<T>, new: &mut EntityHandle<T>, data: &T) {
|
||||
return;
|
||||
if old.real() && new.real() {
|
||||
return;
|
||||
}
|
||||
|
||||
let affected = self.get_affected_queries(data);
|
||||
for q in affected {
|
||||
let mut query = self.queries[q].lock();
|
||||
let index = query.cache.binary_search(&old).unwrap();
|
||||
query.cache.remove(index);
|
||||
let real_end = query.cache.iter().rposition(|r| r.real()).unwrap() + 1;
|
||||
query.cache.insert(real_end, new.clone());
|
||||
}
|
||||
}
|
||||
pub(crate) fn handle_delete(&self, handle: &EntityHandle<T>, data: &T) {
|
||||
return;
|
||||
let affected = self.get_affected_queries(data);
|
||||
for q in affected {
|
||||
let mut query = self.queries[q].lock();
|
||||
let slice = query.cache.as_slice();
|
||||
if let Ok(index) = slice.binary_search(&handle) {
|
||||
query.cache.remove(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_affected_queries(&self, data: &T) -> Vec<usize> {
|
||||
self.queries
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, q)| {
|
||||
let query = q.lock();
|
||||
let rule = &query.rule;
|
||||
return rule(data);
|
||||
|
||||
false
|
||||
})
|
||||
.map(|(i, _)| i)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn reload_queries(&mut self, data_repository: impl Repository<T, D>) {
|
||||
for q in &self.queries {
|
||||
let mut query = q.lock();
|
||||
query.cache.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct Query<T, R, D>
|
||||
where
|
||||
R: Repository<T, D>,
|
||||
D: DbLayer<T>,
|
||||
{
|
||||
cache: Vec<EntityHandle<T>>,
|
||||
rule: Box<dyn Fn(&T) -> bool + Send + Sync>,
|
||||
parallel: bool,
|
||||
_marker: PhantomData<(R, D)>,
|
||||
shift_stamp: u16,
|
||||
}
|
||||
|
||||
impl<T: EntityData + Clone, R: Repository<T, D>, D: DbLayer<T>> Query<T, R, D> {
|
||||
pub fn iter<'a>(&mut self, data_repository_inner: &'a R) -> impl Iterator<Item = &'a T>
|
||||
where
|
||||
T: 'a,
|
||||
{
|
||||
self.cache
|
||||
.iter_mut()
|
||||
.filter_map(move |e| data_repository_inner.get_ref(e))
|
||||
}
|
||||
|
||||
pub fn for_each_mut<F>(&mut self, repo: &mut R, mut f: F)
|
||||
where
|
||||
F: FnMut(&mut T),
|
||||
{
|
||||
for handle in &mut self.cache {
|
||||
if let Some(entity) = repo.get_mut(handle) {
|
||||
f(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(&mut self, data_repository: &DataRepositoryInner<T, D>)
|
||||
where
|
||||
T: Send + Sync,
|
||||
{
|
||||
self.cache.clear();
|
||||
if self.parallel {
|
||||
self.cache = data_repository
|
||||
.cache
|
||||
.par_iter()
|
||||
.enumerate()
|
||||
.filter(|(_, entity)| (self.rule)(entity))
|
||||
.map(|(i, entity)| EntityHandle::new(entity.id(), i, true))
|
||||
.collect();
|
||||
} else {
|
||||
self.cache = data_repository
|
||||
.cache
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, entity)| (self.rule)(entity))
|
||||
.map(|(i, entity)| EntityHandle::new(entity.id(), i, true))
|
||||
.collect();
|
||||
}
|
||||
|
||||
self.cache.extend(
|
||||
data_repository
|
||||
.temporal
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, entity)| (self.rule)(entity))
|
||||
.map(|(i, entity)| EntityHandle::new(entity.id(), i, false))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub trait BatchedRepository<T, D>
|
||||
where
|
||||
D: BatchedDbLayer<T>,
|
||||
T: EntityData,
|
||||
{
|
||||
fn save_many(&mut self, handles: &mut [EntityHandle<T>]) -> MaybeError;
|
||||
fn delete_many(&mut self, handles: &mut [EntityHandle<T>]) -> MaybeError;
|
||||
}
|
||||
|
||||
pub trait DbLayer<T> {
|
||||
fn write_entity(&self, entity: &T, connection: &Connection) -> MaybeError;
|
||||
fn add_entity(&mut self, entity: &mut T, connection: &Connection) -> MaybeError;
|
||||
fn delete_entity(&mut self, id: Id, connection: &Connection) -> MaybeError;
|
||||
fn load_all(&self, storage: &mut Vec<T>, connection: &Connection) -> MaybeError;
|
||||
}
|
||||
|
||||
pub trait BatchedDbLayer<T>
|
||||
where
|
||||
T: EntityData,
|
||||
{
|
||||
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)]
|
||||
pub struct DataRepository<T: std::clone::Clone + EntityData, D: DbLayer<T>> {
|
||||
inner: Arc<RwLock<DataRepositoryInner<T, D>>>,
|
||||
}
|
||||
|
||||
pub struct DataRepositoryInner<T: std::clone::Clone + EntityData, D: DbLayer<T>> {
|
||||
cache: Vec<T>,
|
||||
temporal: Vec<T>,
|
||||
next_temporal: u32,
|
||||
connection: Arc<RwLock<Connection>>,
|
||||
data_layer: D,
|
||||
query_manger: QueryManger<T, Self, D>,
|
||||
version: u16,
|
||||
shifts: u16,
|
||||
}
|
||||
|
||||
impl<T: std::clone::Clone + EntityData, D: DbLayer<T>> Deref for DataRepository<T, D> {
|
||||
type Target = Arc<RwLock<DataRepositoryInner<T, D>>>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: EntityData + Clone, D: DbLayer<T>> DataRepositoryInner<T, D> {
|
||||
pub fn query_par(
|
||||
&self,
|
||||
rule: Box<dyn Fn(&T) -> bool + Send + Sync>,
|
||||
) -> Query<T, DataRepositoryInner<T, D>, D>
|
||||
where
|
||||
T: Send + Sync,
|
||||
{
|
||||
let mut cache: Vec<EntityHandle<T>> = self
|
||||
.cache
|
||||
.par_iter()
|
||||
.enumerate()
|
||||
.filter(|(_, entity)| rule(entity))
|
||||
.map(|(i, entity)| EntityHandle::new(entity.id(), i, true))
|
||||
.collect();
|
||||
|
||||
cache.extend(
|
||||
self.temporal
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, entity)| rule(entity))
|
||||
.map(|(i, entity)| EntityHandle::new(entity.id(), i, false))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
Query {
|
||||
cache,
|
||||
rule,
|
||||
parallel: true,
|
||||
_marker: PhantomData,
|
||||
shift_stamp: self.shifts,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn query(
|
||||
&self,
|
||||
rule: Box<dyn Fn(&T) -> bool + Send + Sync>,
|
||||
) -> Query<T, DataRepositoryInner<T, D>, D>
|
||||
where
|
||||
T: Send + Sync,
|
||||
{
|
||||
let mut cache: Vec<EntityHandle<T>> = self
|
||||
.cache
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, entity)| rule(entity))
|
||||
.map(|(i, entity)| EntityHandle::new(entity.id(), i, true))
|
||||
.collect();
|
||||
|
||||
cache.extend(
|
||||
self.temporal
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, entity)| rule(entity))
|
||||
.map(|(i, entity)| EntityHandle::new(entity.id(), i, false))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
Query {
|
||||
cache,
|
||||
rule,
|
||||
parallel: false,
|
||||
_marker: PhantomData,
|
||||
shift_stamp: self.shifts,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryInner<T, D> {
|
||||
fn get(&self, handle: &mut EntityHandle<T>) -> Option<T> {
|
||||
let slice = if handle.real() {
|
||||
self.cache.as_slice()
|
||||
} else {
|
||||
self.temporal.as_slice()
|
||||
};
|
||||
let index = fast_position_of(slice, handle)?;
|
||||
Some(slice[index].clone())
|
||||
}
|
||||
|
||||
fn get_ref(&self, handle: &mut EntityHandle<T>) -> Option<&T> {
|
||||
let slice = if handle.real() {
|
||||
self.cache.as_slice()
|
||||
} else {
|
||||
self.temporal.as_slice()
|
||||
};
|
||||
let index = fast_position_of(slice, handle)?;
|
||||
Some(&slice[index])
|
||||
}
|
||||
|
||||
fn get_mut(&mut self, handle: &mut EntityHandle<T>) -> Option<&mut T> {
|
||||
let slice = if handle.real() {
|
||||
self.cache.as_mut_slice()
|
||||
} else {
|
||||
self.temporal.as_mut_slice()
|
||||
};
|
||||
let index = fast_position_of(slice, handle)?;
|
||||
Some(&mut slice[index])
|
||||
}
|
||||
|
||||
fn handle_by_id(&self, id: Id) -> EntityHandle<T> {
|
||||
EntityHandle::new(id, id.0 as usize, true)
|
||||
}
|
||||
|
||||
fn create(&mut self, mut new: T) -> EntityHandle<T> {
|
||||
let next_temporal = self.next_temporal;
|
||||
self.next_temporal += 1;
|
||||
let entity = EntityHandle::new(next_temporal.into(), self.temporal.len(), false);
|
||||
new.set_id(entity.id);
|
||||
self.temporal.push(new);
|
||||
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() {
|
||||
let data = self.get_ref(entity).unwrap();
|
||||
self.data_layer
|
||||
.write_entity(data, &self.connection.read())?;
|
||||
} else {
|
||||
let index = fast_position_of(self.temporal.as_slice(), entity).unwrap();
|
||||
let mut data = self.temporal.remove(index);
|
||||
self.data_layer
|
||||
.add_entity(&mut data, &self.connection.read())?;
|
||||
entity.set_real(true);
|
||||
entity.id = data.id();
|
||||
entity.set_index(self.cache.len());
|
||||
self.query_manger.handle_save(original_state, entity, &data);
|
||||
|
||||
self.cache.push(data);
|
||||
self.shifts += 1;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete(&mut self, mut handle: EntityHandle<T>) -> MaybeError {
|
||||
let vec = if handle.real() {
|
||||
self.data_layer
|
||||
.delete_entity(handle.id, &self.connection.read())?;
|
||||
&mut self.cache
|
||||
} else {
|
||||
&mut self.temporal
|
||||
};
|
||||
|
||||
let index = into_result(
|
||||
fast_position_of(vec.as_slice(), &mut handle),
|
||||
NotificationData::DbSaveError,
|
||||
)?;
|
||||
let value = vec.remove(index);
|
||||
self.query_manger.handle_delete(&handle, &value);
|
||||
self.shifts += 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init(&mut self) -> MaybeError {
|
||||
self.data_layer
|
||||
.load_all(&mut self.cache, &self.connection.read())?;
|
||||
self.version += 1;
|
||||
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();
|
||||
self.init()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository<T, D>
|
||||
for DataRepositoryInner<T, D>
|
||||
{
|
||||
fn save_many(&mut self, handles: &mut [EntityHandle<T>]) -> MaybeError {
|
||||
let mut temp = Vec::with_capacity(handles.len() / 2);
|
||||
let mut cached = Vec::with_capacity(handles.len() / 2);
|
||||
handles.sort_by_key(|a| a.id);
|
||||
handles.iter_mut().for_each(|handle| {
|
||||
if handle.real() {
|
||||
cached.push(handle)
|
||||
} else {
|
||||
temp.push(handle)
|
||||
}
|
||||
});
|
||||
|
||||
let mut temp_values = Vec::with_capacity(temp.len());
|
||||
let mut cell = 0;
|
||||
let len = temp.len();
|
||||
self.temporal.retain(|x| {
|
||||
if cell == len {
|
||||
return true;
|
||||
}
|
||||
let find = temp[cell].id;
|
||||
let remove = x.id() == find;
|
||||
if remove {
|
||||
cell += 1;
|
||||
temp_values.push(x.clone());
|
||||
}
|
||||
!remove
|
||||
});
|
||||
|
||||
self.data_layer
|
||||
.add_many_entities(&mut temp_values, &mut self.connection.write())?;
|
||||
|
||||
let start = self.cache.len();
|
||||
|
||||
for (i, (handle, data)) in temp.into_iter().zip(&mut temp_values).enumerate() {
|
||||
let old = handle.clone();
|
||||
handle.id = data.id();
|
||||
handle.set_index(start + i);
|
||||
handle.set_real(true);
|
||||
self.query_manger.handle_save(old, handle, data);
|
||||
}
|
||||
|
||||
self.cache.extend(temp_values);
|
||||
|
||||
for handle in cached.into_iter() {
|
||||
let h = handle;
|
||||
let idx = fast_position_of(self.cache.as_slice(), h).unwrap();
|
||||
self.data_layer
|
||||
.write_entity(&self.cache[idx], &self.connection.read())?;
|
||||
}
|
||||
self.shifts += 1;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete_many(&mut self, handles: &mut [EntityHandle<T>]) -> MaybeError {
|
||||
let (cached, temp) = split_temp_real(handles, |e| e.id);
|
||||
|
||||
let mut cell = 0;
|
||||
let len = temp.len();
|
||||
self.temporal.retain(|x| {
|
||||
if cell == len {
|
||||
return true;
|
||||
}
|
||||
let find = temp[cell];
|
||||
let remove = x.id() == find;
|
||||
if remove {
|
||||
cell += 1;
|
||||
}
|
||||
!remove
|
||||
});
|
||||
self.data_layer
|
||||
.delete_many_entities(cached.as_slice(), &mut self.connection.write())?;
|
||||
let len = cached.len();
|
||||
cell = 0;
|
||||
self.cache.retain(|x| {
|
||||
if cell == len {
|
||||
return true;
|
||||
}
|
||||
let find = cached[cell];
|
||||
let remove = x.id() == find;
|
||||
if remove {
|
||||
cell += 1;
|
||||
}
|
||||
|
||||
!remove
|
||||
});
|
||||
self.shifts += 1;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn split_temp_real<T, F, V>(handles: &mut [EntityHandle<T>], getter: F) -> (Vec<V>, Vec<V>)
|
||||
where
|
||||
F: Fn(&EntityHandle<T>) -> V,
|
||||
{
|
||||
let mut temp = Vec::with_capacity(handles.len() / 2);
|
||||
let mut cached = Vec::with_capacity(handles.len() / 2);
|
||||
handles.sort_by(|a, b| a.id.cmp(&b.id));
|
||||
handles.iter().for_each(|handle| {
|
||||
if handle.real() {
|
||||
cached.push(getter(handle))
|
||||
} else {
|
||||
temp.push(getter(handle))
|
||||
}
|
||||
});
|
||||
(cached, temp)
|
||||
}
|
||||
|
||||
pub fn fast_position_of<E: EntityData>(
|
||||
storage: &[E],
|
||||
handle: &mut EntityHandle<E>,
|
||||
) -> Option<usize> {
|
||||
if storage.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let max_idx = storage.len() - 1;
|
||||
let upper = (handle.index()).min(max_idx);
|
||||
let peek = storage[upper].id();
|
||||
|
||||
if peek == handle.id {
|
||||
handle.set_index(upper);
|
||||
return Some(upper);
|
||||
}
|
||||
|
||||
if peek.0 < handle.id.0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let target_id_val = handle.id.0 as i64;
|
||||
let peek_val = peek.0 as i64;
|
||||
let delta = peek_val - target_id_val;
|
||||
|
||||
let new_index = (upper as i64 - delta).max(0) as usize;
|
||||
|
||||
if storage[new_index].id() == handle.id {
|
||||
handle.set_index(new_index);
|
||||
return Some(new_index);
|
||||
}
|
||||
|
||||
if new_index == upper {
|
||||
return None;
|
||||
}
|
||||
|
||||
let index = storage[new_index..upper]
|
||||
.binary_search_by(|e| e.id().cmp(&handle.id))
|
||||
.ok()?
|
||||
+ new_index;
|
||||
|
||||
handle.set_index(index);
|
||||
Some(index)
|
||||
}
|
||||
@@ -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(());
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)?,
|
||||
})
|
||||
|
||||
@@ -9,6 +9,7 @@ mod navigation;
|
||||
pub mod notification;
|
||||
mod pages;
|
||||
mod styling;
|
||||
pub mod tests;
|
||||
|
||||
use crate::app::start;
|
||||
use mimalloc::MiMalloc;
|
||||
|
||||
+2
-2
@@ -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,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>>,
|
||||
|
||||
@@ -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(),
|
||||
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
#[cfg(test)]
|
||||
mod mod_tests {
|
||||
use crate::data::Id;
|
||||
use crate::data::entities::{EntityData, EntityHandle};
|
||||
use crate::data::repositories::fast_position_of;
|
||||
|
||||
struct ProtoEntity {
|
||||
id: Id,
|
||||
_data: i128,
|
||||
}
|
||||
impl EntityData for ProtoEntity {
|
||||
fn id(&self) -> Id {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
self.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
impl ProtoEntity {
|
||||
fn new(n: i128) -> Self {
|
||||
Self {
|
||||
id: (n as u32).into(),
|
||||
_data: n,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_direct() {
|
||||
let mut entity = EntityHandle::new(8.into(), 3, true);
|
||||
assert_eq!(
|
||||
fast_position_of(&[1, 2, 3, 8, 9, 12].map(ProtoEntity::new), &mut entity),
|
||||
Some(3)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_0() {
|
||||
let mut entity = EntityHandle::new(8.into(), 4, true);
|
||||
assert_eq!(
|
||||
fast_position_of(&[1, 2, 3, 8, 9, 12].map(ProtoEntity::new), &mut entity),
|
||||
Some(3)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_1() {
|
||||
let mut entity = EntityHandle::new(9.into(), 5, true);
|
||||
assert_eq!(
|
||||
fast_position_of(&[1, 2, 3, 8, 9, 12].map(ProtoEntity::new), &mut entity),
|
||||
Some(4)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_2() {
|
||||
let mut entity = EntityHandle::new(106.into(), 25, true);
|
||||
assert_eq!(
|
||||
fast_position_of(
|
||||
&[
|
||||
1, 2, 3, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21, 22, 23, 105, 106, 107,
|
||||
108, 109, 110
|
||||
]
|
||||
.map(ProtoEntity::new),
|
||||
&mut entity
|
||||
),
|
||||
Some(18)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_0() {
|
||||
let entity: EntityHandle<ProtoEntity> = EntityHandle::new(8.into(), 25, true);
|
||||
assert_eq!(entity.real(), true)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_1() {
|
||||
let entity: EntityHandle<ProtoEntity> = EntityHandle::new(8.into(), 25, false);
|
||||
assert_eq!(entity.real(), false)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_3() {
|
||||
let entity: EntityHandle<ProtoEntity> = EntityHandle::new(8.into(), 25, true);
|
||||
assert_eq!(entity.index(), 25)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_4() {
|
||||
let entity: EntityHandle<ProtoEntity> = EntityHandle::new(8.into(), 25, false);
|
||||
assert_eq!(entity.index(), 25)
|
||||
}
|
||||
#[test]
|
||||
fn id_5() {
|
||||
let mut entity: EntityHandle<ProtoEntity> = EntityHandle::new(8.into(), 25, true);
|
||||
entity.set_real(true);
|
||||
assert_eq!(entity.real(), true)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_6() {
|
||||
let mut entity: EntityHandle<ProtoEntity> = EntityHandle::new(8.into(), 25, false);
|
||||
entity.set_real(true);
|
||||
assert_eq!(entity.real(), true)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_7() {
|
||||
let mut entity: EntityHandle<ProtoEntity> = EntityHandle::new(8.into(), 25, true);
|
||||
entity.set_index(0);
|
||||
assert_eq!(entity.index(), 0)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_8() {
|
||||
let mut entity: EntityHandle<ProtoEntity> = EntityHandle::new(8.into(), 25, false);
|
||||
entity.set_index(0);
|
||||
assert_eq!(entity.index(), 0)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user