db sub schema (via Arc)

This commit is contained in:
polv committed 2026-02-22 21:34:44 +07:00
1 parent 442e309a81
commit 62cb1e5902
7 files changed
+93 -65

No files matched your search

+4 -4
View File
@@ -5,11 +5,11 @@ use std::{
};
use anyhow::{Context, Ok};
use cjdic2_core::db::import_bundled_zip_file;
use rusqlite::Connection;
use cjdic2_core::db::Database;
fn main() -> Result<(), anyhow::Error> {
let conn = &mut Connection::open("test.db")?;
let db = Database::new("test.db")?;
let zip_dir = Path::new("src-tauri/resources/yomitan");
println!("zip_dir: {:?}", absolute(zip_dir)); // Relative to workspace root
@@ -23,7 +23,7 @@ fn main() -> Result<(), anyhow::Error> {
let start = Instant::now();
println!("zip_file: {:?}", p);
println!("{:?}", import_bundled_zip_file(conn, p, "ja")?,);
println!("{:?}", db.yomitan().import_bundled_zip_file(p, "ja")?,);
println!("[{:.2?}]", start.elapsed(),);
} else {
println!("not zip_file: {:?}", p);
+3 -4
View File
@@ -1,14 +1,13 @@
use std::time::Instant;
use anyhow::Ok;
use cjdic2_core::db::search_yomitan;
use rusqlite::Connection;
use cjdic2_core::db::Database;
fn main() -> Result<(), anyhow::Error> {
let conn = &mut Connection::open("test.db")?;
let db = Database::new("test.db")?;
let start = Instant::now();
let rs = search_yomitan(conn, "擦る", "する", 10, 0)?;
let rs = db.yomitan().search_yomitan("擦る", "する", 10, 0)?;
println!("{:#?}", rs);
println!("n={}", rs.len());
println!("[{:.2?}]", start.elapsed());
+3 -4
View File
@@ -1,14 +1,13 @@
use std::time::Instant;
use anyhow::Ok;
use cjdic2_core::db::create_schema;
use rusqlite::Connection;
use cjdic2_core::db::Database;
fn main() -> Result<(), anyhow::Error> {
let conn = &mut Connection::open("test.db")?;
let db = Database::new("test.db")?;
let start = Instant::now();
create_schema(conn)?;
db.yomitan().create_schema()?;
println!("[{:.2?}]", start.elapsed());
Ok(())
+11 -4
View File
@@ -1,24 +1,31 @@
use std::{path::Path, sync::Mutex};
use std::{
path::Path,
sync::{Arc, Mutex},
};
use rusqlite::{Connection, Result};
use crate::models::Entry;
mod yomitan;
pub use yomitan::{YomitanRow, create_schema, import_bundled_zip_file, search_yomitan};
#[derive(Clone)]
pub struct Database {
conn: Mutex<Connection>,
pub(crate) conn: Arc<Mutex<Connection>>,
}
impl Database {
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
let conn = Connection::open(path)?;
Ok(Self {
conn: Mutex::new(conn),
conn: Arc::new(Mutex::new(conn)),
})
}
pub fn yomitan(&self) -> yomitan::YomitanDatabase {
yomitan::YomitanDatabase::new(self.clone())
}
pub fn init(&self) -> Result<()> {
let conn = self.conn.lock().unwrap();
+1
View File
@@ -0,0 +1 @@
+67 -49
View File
@@ -2,18 +2,29 @@ use std::{collections::HashMap, fs::File, io::Read, path::PathBuf};
use anyhow::Context;
use blake3::hash;
use rusqlite::{Connection, Transaction, params};
use rusqlite::{Result, Transaction, params};
use serde::Serialize;
use serde_json::Value;
use zip::ZipArchive;
use crate::CJDicError;
use crate::{CJDicError, db::Database};
fn blake3_hex(s: &str) -> String {
format!("{}", hash(s.as_bytes()))
}
pub fn create_schema(conn: &Connection) -> rusqlite::Result<()> {
pub struct YomitanDatabase {
db: Database,
}
impl YomitanDatabase {
pub(crate) fn new(db: Database) -> Self {
Self { db }
}
pub fn create_schema(&self) -> Result<()> {
let conn = self.db.conn.lock().unwrap();
conn.execute_batch(
"
PRAGMA journal_mode = WAL;
@@ -109,39 +120,17 @@ pub fn create_schema(conn: &Connection) -> rusqlite::Result<()> {
}
Ok(())
}
fn intern(
tx: &Transaction,
insert_sql: &str,
select_sql: &str,
cache: &mut HashMap<String, i64>,
value: &str,
) -> rusqlite::Result<i64> {
if let Some(&v) = cache.get(value) {
return Ok(v);
}
tx.execute(insert_sql, [value])?;
let id: i64 = tx.query_row(select_sql, [value], |r| r.get(0))?;
cache.insert(value.to_string(), id);
Ok(id)
}
#[derive(Serialize, Debug)]
pub struct YomitanZipImportResult {
exists: bool,
load: bool,
error: Option<String>,
}
pub fn import_bundled_zip_file(
conn: &mut Connection,
pub fn import_bundled_zip_file(
&mut self,
zip_path: PathBuf,
lang: &str,
) -> anyhow::Result<YomitanZipImportResult, CJDicError> {
create_schema(&conn)?;
) -> anyhow::Result<YomitanZipImportResult, CJDicError> {
self.create_schema()?;
let f = File::open(&zip_path).with_context(|| format!("opening zip {}", zip_path.display()))?;
let f =
File::open(&zip_path).with_context(|| format!("opening zip {}", zip_path.display()))?;
let mut archive = ZipArchive::new(f).with_context(|| "reading zip archive")?;
let index_file = match archive.by_name("index.json") {
@@ -172,6 +161,8 @@ pub fn import_bundled_zip_file(
.unwrap_or("")
.to_string();
let mut conn = self.db.conn.lock().unwrap();
// Skip if already installed
let exists: bool = conn
.prepare("SELECT 1 FROM dictionaries WHERE title = ?1 AND revision = ?2")?
@@ -258,7 +249,8 @@ pub fn import_bundled_zip_file(
id
} else {
tx.execute(insert_glossary, params![hash, glossary_json])?;
let id: i64 = tx.query_row(select_glossary, params![hash], |r| r.get(0))?;
let id: i64 =
tx.query_row(select_glossary, params![hash], |r| r.get(0))?;
glossary_cache.insert(hash.clone(), id);
id
};
@@ -384,28 +376,15 @@ pub fn import_bundled_zip_file(
load: true,
error: None,
})
}
}
#[derive(Serialize, Debug)]
pub struct YomitanRow {
term: String,
reading: String,
def_tags: String,
rules: String,
score: i64,
glossary_json: String,
sequence: Option<i64>,
term_tags: String,
dict_title: String,
}
pub fn search_yomitan(
conn: &Connection,
pub fn search_yomitan(
&self,
q_term: &str,
q_reading: &str,
limit: u32,
offset: u32,
) -> Result<Vec<YomitanRow>, CJDicError> {
) -> Result<Vec<YomitanRow>, CJDicError> {
let and_or = if q_term == q_reading { "OR" } else { "AND" };
// TODO: consider using = if not GLOB string
@@ -434,6 +413,8 @@ pub fn search_yomitan(
and_or
);
let conn = self.db.conn.lock().unwrap();
let mut stmt = conn.prepare(&sql)?;
let rows = stmt.query_map(params![q_term, q_reading, limit, offset], |r| {
Ok(YomitanRow {
@@ -454,4 +435,41 @@ pub fn search_yomitan(
out.push(r?);
}
Ok(out)
}
}
fn intern(
tx: &Transaction,
insert_sql: &str,
select_sql: &str,
cache: &mut HashMap<String, i64>,
value: &str,
) -> rusqlite::Result<i64> {
if let Some(&v) = cache.get(value) {
return Ok(v);
}
tx.execute(insert_sql, [value])?;
let id: i64 = tx.query_row(select_sql, [value], |r| r.get(0))?;
cache.insert(value.to_string(), id);
Ok(id)
}
#[derive(Serialize, Debug)]
pub struct YomitanZipImportResult {
exists: bool,
load: bool,
error: Option<String>,
}
#[derive(Serialize, Debug)]
pub struct YomitanRow {
term: String,
reading: String,
def_tags: String,
rules: String,
score: i64,
glossary_json: String,
sequence: Option<i64>,
term_tags: String,
dict_title: String,
}
+4
View File
@@ -21,4 +21,8 @@ impl AppService {
pub fn list_entries(&self) -> Result<Vec<Entry>, CJDicError> {
Ok(self.db.fetch_all_entries()?)
}
pub fn search_yomitan(&self, _term: &str, _reading: &str) -> Result<(), CJDicError> {
Ok(())
}
}