db update schema

This commit is contained in:
polv committed 2026-02-22 17:58:39 +07:00
1 parent 707c4274d1
commit 442e309a81
5 files changed
+43 -21

No files matched your search

+2 -5
View File
@@ -23,11 +23,8 @@ fn main() -> Result<(), anyhow::Error> {
let start = Instant::now(); let start = Instant::now();
println!("zip_file: {:?}", p); println!("zip_file: {:?}", p);
println!( println!("{:?}", import_bundled_zip_file(conn, p, "ja")?,);
"{:?}\n[{:.2?}]", println!("[{:.2?}]", start.elapsed(),);
import_bundled_zip_file(conn, p)?,
start.elapsed(),
);
} else { } else {
println!("not zip_file: {:?}", p); println!("not zip_file: {:?}", p);
} }
+4 -2
View File
@@ -8,8 +8,10 @@ fn main() -> Result<(), anyhow::Error> {
let conn = &mut Connection::open("test.db")?; let conn = &mut Connection::open("test.db")?;
let start = Instant::now(); let start = Instant::now();
let rs = search_yomitan(conn, "食べる", "食べる", 5, 0)?; let rs = search_yomitan(conn, "擦る", "する", 10, 0)?;
println!("{:#?}\n[{:.2?}]", rs, start.elapsed()); println!("{:#?}", rs);
println!("n={}", rs.len());
println!("[{:.2?}]", start.elapsed());
Ok(()) Ok(())
} }
+15
View File
@@ -0,0 +1,15 @@
use std::time::Instant;
use anyhow::Ok;
use cjdic2_core::db::create_schema;
use rusqlite::Connection;
fn main() -> Result<(), anyhow::Error> {
let conn = &mut Connection::open("test.db")?;
let start = Instant::now();
create_schema(conn)?;
println!("[{:.2?}]", start.elapsed());
Ok(())
}
+1 -1
View File
@@ -5,7 +5,7 @@ use rusqlite::{Connection, Result};
use crate::models::Entry; use crate::models::Entry;
mod yomitan; mod yomitan;
pub use yomitan::{YomitanRow, import_bundled_zip_file, search_yomitan}; pub use yomitan::{YomitanRow, create_schema, import_bundled_zip_file, search_yomitan};
pub struct Database { pub struct Database {
conn: Mutex<Connection>, conn: Mutex<Connection>,
+21 -13
View File
@@ -13,9 +13,10 @@ fn blake3_hex(s: &str) -> String {
format!("{}", hash(s.as_bytes())) format!("{}", hash(s.as_bytes()))
} }
fn create_schema(conn: &Connection) -> rusqlite::Result<()> { pub fn create_schema(conn: &Connection) -> rusqlite::Result<()> {
conn.execute_batch( conn.execute_batch(
"PRAGMA journal_mode = WAL; "
PRAGMA journal_mode = WAL;
PRAGMA foreign_keys = ON; PRAGMA foreign_keys = ON;
PRAGMA synchronous = NORMAL; PRAGMA synchronous = NORMAL;
PRAGMA cache_size = -65536; PRAGMA cache_size = -65536;
@@ -32,9 +33,9 @@ fn create_schema(conn: &Connection) -> rusqlite::Result<()> {
author TEXT, author TEXT,
url TEXT, url TEXT,
description TEXT, description TEXT,
is_bundled INTEGER NOT NULL DEFAULT 1,
sort_order INTEGER NOT NULL DEFAULT 0, sort_order INTEGER NOT NULL DEFAULT 0,
installed_at TEXT NOT NULL DEFAULT (datetime('now')), installed_at TEXT NOT NULL DEFAULT (datetime('now')),
lang TEXT NOT NULL,
UNIQUE (title, revision) UNIQUE (title, revision)
); );
CREATE TABLE IF NOT EXISTS glossaries ( CREATE TABLE IF NOT EXISTS glossaries (
@@ -57,10 +58,6 @@ fn create_schema(conn: &Connection) -> rusqlite::Result<()> {
sequence INTEGER, sequence INTEGER,
term_tags_id INTEGER term_tags_id INTEGER
); );
CREATE INDEX IF NOT EXISTS terms_term_reading ON terms (term, reading);
CREATE INDEX IF NOT EXISTS terms_term ON terms (term);
CREATE INDEX IF NOT EXISTS terms_reading ON terms (reading);
CREATE TABLE IF NOT EXISTS term_meta ( CREATE TABLE IF NOT EXISTS term_meta (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
dict_id INTEGER NOT NULL REFERENCES dictionaries(id) ON DELETE CASCADE, dict_id INTEGER NOT NULL REFERENCES dictionaries(id) ON DELETE CASCADE,
@@ -92,6 +89,15 @@ fn create_schema(conn: &Connection) -> rusqlite::Result<()> {
", ",
)?; )?;
conn.execute_batch(
"
CREATE INDEX IF NOT EXISTS dictionaries_lang ON dictionaries (lang);
CREATE INDEX IF NOT EXISTS terms_term ON terms (term);
CREATE INDEX IF NOT EXISTS terms_reading ON terms (reading);
",
)?;
let mut stmt = conn.prepare("SELECT 1 FROM schema_meta WHERE key = 'schema_version'")?; let mut stmt = conn.prepare("SELECT 1 FROM schema_meta WHERE key = 'schema_version'")?;
if stmt.exists([])? == false { if stmt.exists([])? == false {
conn.prepare("INSERT INTO schema_meta (key, value) VALUES ('schema_version', '1')")? conn.prepare("INSERT INTO schema_meta (key, value) VALUES ('schema_version', '1')")?
@@ -124,13 +130,14 @@ fn intern(
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct YomitanZipImportResult { pub struct YomitanZipImportResult {
exists: bool, exists: bool,
done: bool, load: bool,
error: Option<String>, error: Option<String>,
} }
pub fn import_bundled_zip_file( pub fn import_bundled_zip_file(
conn: &mut Connection, conn: &mut Connection,
zip_path: PathBuf, zip_path: PathBuf,
lang: &str,
) -> anyhow::Result<YomitanZipImportResult, CJDicError> { ) -> anyhow::Result<YomitanZipImportResult, CJDicError> {
create_schema(&conn)?; create_schema(&conn)?;
@@ -148,7 +155,7 @@ pub fn import_bundled_zip_file(
eprintln!("{}", e); eprintln!("{}", e);
return Ok(YomitanZipImportResult { return Ok(YomitanZipImportResult {
exists: false, exists: false,
done: false, load: false,
error: Some(format!("{}", e)), error: Some(format!("{}", e)),
}); });
} }
@@ -172,20 +179,21 @@ pub fn import_bundled_zip_file(
if exists { if exists {
return Ok(YomitanZipImportResult { return Ok(YomitanZipImportResult {
exists: true, exists: true,
done: false, load: false,
error: None, error: None,
}); });
} }
let tx = conn.transaction()?; let tx = conn.transaction()?;
tx.execute( tx.execute(
"INSERT INTO dictionaries (title, revision, author, url, description, is_bundled) VALUES (?1, ?2, ?3, ?4, ?5, 1)", "INSERT INTO dictionaries (title, revision, author, url, description, lang) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
[ params![
index_file.get("title").and_then(Value::as_str), index_file.get("title").and_then(Value::as_str),
index_file.get("revision").and_then(Value::as_str), index_file.get("revision").and_then(Value::as_str),
index_file.get("author").and_then(Value::as_str), index_file.get("author").and_then(Value::as_str),
index_file.get("url").and_then(Value::as_str), index_file.get("url").and_then(Value::as_str),
index_file.get("description").and_then(Value::as_str), index_file.get("description").and_then(Value::as_str),
lang,
], ],
)?; )?;
@@ -373,7 +381,7 @@ pub fn import_bundled_zip_file(
Ok(YomitanZipImportResult { Ok(YomitanZipImportResult {
exists: true, exists: true,
done: true, load: true,
error: None, error: None,
}) })
} }