Optimized again dictionary page and working import

This commit is contained in:
2026-08-14 23:03:15 +03:00
parent 17e06256c1
commit eb6c9c5084
9 changed files with 605 additions and 95 deletions
+44 -10
View File
@@ -1,23 +1,26 @@
use rusqlite::Connection;
use crate::lang::WordData;
use rusqlite::fallible_iterator::FallibleIterator;
use rusqlite::{Connection, MappedRows};
use serde_json::Value;
use std::collections::HashMap;
#[derive(Clone)]
pub struct ImportData(Vec<ImportGroup>);
pub struct ImportData(pub(crate) Vec<ImportGroup>);
#[derive(Clone, Debug)]
pub struct ImportGroup {
id: u64,
name: String,
fields: Vec<String>,
mapping: HashMap<String, String>,
pub id: u64,
pub name: String,
pub fields: Vec<String>,
pub length: u64,
pub mapping: HashMap<String, String>,
pub imported: bool
}
#[derive(Clone)]
pub struct ImportNote {
name: String,
tags: String,
fields: Vec<String>,
pub tags: String,
pub fields: Vec<String>,
}
pub fn load_groups(connection: &Connection) -> ImportData {
@@ -28,6 +31,16 @@ pub fn load_groups(connection: &Connection) -> ImportData {
let sets = raw.as_object().unwrap();
let mut total_data = ImportData(vec![]);
let mut count_stmt = connection
.prepare("select mid, count(id) from notes group by mid")
.unwrap();
let counts = count_stmt
.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
.unwrap()
.map(|x| x.unwrap())
.collect::<Vec<(i64, i64)>>();
for (_, collection) in sets {
let fields = collection["flds"]
.as_array()
@@ -35,14 +48,35 @@ pub fn load_groups(connection: &Connection) -> ImportData {
.iter()
.map(|x| x["name"].as_str().unwrap().to_string())
.collect();
let group = ImportGroup {
let mut group = ImportGroup {
id: collection["id"].as_u64().unwrap(),
name: collection["name"].as_str().unwrap().to_string(),
fields,
mapping: Default::default(),
length: 0,
imported: false,
};
if let Some((_, count)) = counts.iter().find(|(id, _)| *id == group.id as i64) {
group.length = *count as u64
}
println!("{:?}", group);
total_data.0.push(group);
}
total_data
}
pub fn get_words_of_group(connection: &Connection, group_id: u64) -> Vec<ImportNote> {
let mut count_stmt = connection
.prepare("select tags, flds from notes where mid == ?1;")
.unwrap();
count_stmt.query_map((group_id as i64, ), |row| {
Ok(ImportNote {
tags: row.get(0)?,
fields: row.get::<usize, String>(1)?
.split('')
.map(|x| x.to_string())
.collect()
})
}).unwrap().map(|x| x.unwrap()).collect::<Vec<ImportNote>>()
}
+37 -4
View File
@@ -1,12 +1,11 @@
use crate::lang::{WordData, WordGroup};
use rusqlite::Connection;
use rusqlite::{params, Connection};
use std::collections::HashMap;
pub fn add_word(word: &mut WordData, connection: &Connection) {
let index = connection
.query_row(
"INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5\
) RETURNING id",
"INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5) RETURNING id",
(
&word.key,
&word.value,
@@ -24,6 +23,40 @@ pub fn add_word(word: &mut WordData, connection: &Connection) {
word.id = index;
}
pub fn add_words(words: &mut[WordData], connection: &mut Connection) {
let tx = connection.transaction().unwrap();
let count = words.len();
{
let mut stmt = tx.prepare(
"INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5)",
).unwrap();
for word in words.iter() {
stmt.execute(params![word.key, word.value, word.tags, serde_json::to_string(&word.additional).unwrap(), word.group_id]).unwrap();
}
}
tx.commit().unwrap();
let last_index: u32 = connection
.query_one(
"SELECT seq from sqlite_sequence WHERE name == ?1",
("words".to_string(),),
|row| row.get(0),
)
.unwrap();
let start_index = last_index - (count as u32) + 1;
let mut index = 0;
for id in start_index..=last_index {
words[index].id = id;
index += 1;
}
}
pub fn update_word(word: &mut WordData, connection: &Connection) {
if word.id == 0 {
add_word(word, &connection);
@@ -126,7 +159,7 @@ pub fn update_group(group: &mut WordGroup, connection: &Connection) {
} else {
connection
.execute(
"UPDATE word_group SET name = ?1 WHERE id = ?5",
"UPDATE word_group SET name = ?1 WHERE id = ?2",
(&group.name, &group.id),
)
.unwrap_or_else(|e| {