diff --git a/src-core/examples/add-to-yomitan.rs b/src-core/examples/add-to-yomitan.rs index 6d4ca6f..d053991 100644 --- a/src-core/examples/add-to-yomitan.rs +++ b/src-core/examples/add-to-yomitan.rs @@ -23,11 +23,8 @@ fn main() -> Result<(), anyhow::Error> { let start = Instant::now(); println!("zip_file: {:?}", p); - println!( - "{:?}\n[{:.2?}]", - import_bundled_zip_file(conn, p)?, - start.elapsed(), - ); + println!("{:?}", import_bundled_zip_file(conn, p, "ja")?,); + println!("[{:.2?}]", start.elapsed(),); } else { println!("not zip_file: {:?}", p); } diff --git a/src-core/examples/search-yomitan.rs b/src-core/examples/search-yomitan.rs index 6dfddbb..df04713 100644 --- a/src-core/examples/search-yomitan.rs +++ b/src-core/examples/search-yomitan.rs @@ -8,8 +8,10 @@ fn main() -> Result<(), anyhow::Error> { let conn = &mut Connection::open("test.db")?; let start = Instant::now(); - let rs = search_yomitan(conn, "食べる", "食べる", 5, 0)?; - println!("{:#?}\n[{:.2?}]", rs, start.elapsed()); + let rs = search_yomitan(conn, "擦る", "する", 10, 0)?; + println!("{:#?}", rs); + println!("n={}", rs.len()); + println!("[{:.2?}]", start.elapsed()); Ok(()) } diff --git a/src-core/examples/yomitan-mod-schema.rs b/src-core/examples/yomitan-mod-schema.rs new file mode 100644 index 0000000..1ddcc24 --- /dev/null +++ b/src-core/examples/yomitan-mod-schema.rs @@ -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(()) +} diff --git a/src-core/src/db.rs b/src-core/src/db.rs index 6d3b674..a3d5756 100644 --- a/src-core/src/db.rs +++ b/src-core/src/db.rs @@ -5,7 +5,7 @@ use rusqlite::{Connection, Result}; use crate::models::Entry; 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 { conn: Mutex, diff --git a/src-core/src/db/yomitan.rs b/src-core/src/db/yomitan.rs index b8011be..3a2be6e 100644 --- a/src-core/src/db/yomitan.rs +++ b/src-core/src/db/yomitan.rs @@ -13,9 +13,10 @@ fn blake3_hex(s: &str) -> String { format!("{}", hash(s.as_bytes())) } -fn create_schema(conn: &Connection) -> rusqlite::Result<()> { +pub fn create_schema(conn: &Connection) -> rusqlite::Result<()> { conn.execute_batch( - "PRAGMA journal_mode = WAL; + " + PRAGMA journal_mode = WAL; PRAGMA foreign_keys = ON; PRAGMA synchronous = NORMAL; PRAGMA cache_size = -65536; @@ -32,9 +33,9 @@ fn create_schema(conn: &Connection) -> rusqlite::Result<()> { author TEXT, url TEXT, description TEXT, - is_bundled INTEGER NOT NULL DEFAULT 1, sort_order INTEGER NOT NULL DEFAULT 0, installed_at TEXT NOT NULL DEFAULT (datetime('now')), + lang TEXT NOT NULL, UNIQUE (title, revision) ); CREATE TABLE IF NOT EXISTS glossaries ( @@ -57,10 +58,6 @@ fn create_schema(conn: &Connection) -> rusqlite::Result<()> { sequence 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 ( id INTEGER PRIMARY KEY, 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'")?; if stmt.exists([])? == false { conn.prepare("INSERT INTO schema_meta (key, value) VALUES ('schema_version', '1')")? @@ -124,13 +130,14 @@ fn intern( #[derive(Serialize, Debug)] pub struct YomitanZipImportResult { exists: bool, - done: bool, + load: bool, error: Option, } pub fn import_bundled_zip_file( conn: &mut Connection, zip_path: PathBuf, + lang: &str, ) -> anyhow::Result { create_schema(&conn)?; @@ -148,7 +155,7 @@ pub fn import_bundled_zip_file( eprintln!("{}", e); return Ok(YomitanZipImportResult { exists: false, - done: false, + load: false, error: Some(format!("{}", e)), }); } @@ -172,20 +179,21 @@ pub fn import_bundled_zip_file( if exists { return Ok(YomitanZipImportResult { exists: true, - done: false, + load: false, error: None, }); } let tx = conn.transaction()?; 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("revision").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("description").and_then(Value::as_str), + lang, ], )?; @@ -373,7 +381,7 @@ pub fn import_bundled_zip_file( Ok(YomitanZipImportResult { exists: true, - done: true, + load: true, error: None, }) }