mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-08 17:35:18 +00:00
make starting from zero work on Android + Windows
This commit is contained in:
1 parent
653eacf8d4
commit
a00efbf505
10 files changed
+166
-44
No files matched your search
@@ -51,10 +51,11 @@ impl<'a> SearchDatabase<'a> {
|
||||
self.conn.execute_batch(&format!(
|
||||
"
|
||||
DELETE FROM {0}.terms;
|
||||
DELETE FROM {0}.terms_ft;
|
||||
DROP TABLE {0}.terms_ft;
|
||||
",
|
||||
DBSCHEMA[DbChild::Search]
|
||||
))?;
|
||||
self.create_schema()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use blake3::hash;
|
||||
use enum_map::EnumMap;
|
||||
use rusqlite::{Connection, OptionalExtension, Result, Transaction, params};
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
@@ -61,6 +62,7 @@ pub struct YomitanZipImportResult {
|
||||
pub struct YomitanWriter {
|
||||
dir: PathBuf,
|
||||
conn: Connection,
|
||||
is_db_attached: EnumMap<DbChild, bool>,
|
||||
}
|
||||
|
||||
impl YomitanWriter {
|
||||
@@ -78,7 +80,15 @@ impl YomitanWriter {
|
||||
",
|
||||
)?;
|
||||
|
||||
Ok(Self { dir, conn })
|
||||
Ok(Self {
|
||||
dir,
|
||||
conn,
|
||||
is_db_attached: enum_map! {
|
||||
DbChild::YomitanGlossary => false,
|
||||
DbChild::Search => false,
|
||||
_ => true
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub fn create_schema(
|
||||
@@ -172,8 +182,8 @@ impl YomitanWriter {
|
||||
)?;
|
||||
|
||||
self.create_materialized_views()?;
|
||||
self.attach_db(DbChild::YomitanGlossary)?;
|
||||
self.attach_db(DbChild::Search)?;
|
||||
self.ensure_db_attached(DbChild::YomitanGlossary)?;
|
||||
self.ensure_db_attached(DbChild::Search)?;
|
||||
|
||||
let schema_version = "2026-03-08";
|
||||
let current_schema_version = {
|
||||
@@ -334,7 +344,11 @@ impl YomitanWriter {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn attach_db(&mut self, db_child: DbChild) -> Result<(), CJDicError> {
|
||||
fn ensure_db_attached(&mut self, db_child: DbChild) -> Result<(), CJDicError> {
|
||||
if self.is_db_attached[db_child] {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let dir = self.dir.clone();
|
||||
let path = if self.dir.is_absolute() {
|
||||
dir
|
||||
@@ -371,6 +385,8 @@ impl YomitanWriter {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
self.is_db_attached[db_child] = true;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -506,7 +522,7 @@ impl YomitanWriter {
|
||||
}
|
||||
}
|
||||
|
||||
self.attach_db(DbChild::YomitanGlossary)?;
|
||||
self.ensure_db_attached(DbChild::YomitanGlossary)?;
|
||||
|
||||
{
|
||||
let tx = self.conn.transaction()?;
|
||||
@@ -793,12 +809,30 @@ impl YomitanWriter {
|
||||
tokenizer: Tokenizer,
|
||||
progress_callback: impl Fn(YomitanProgress),
|
||||
) -> Result<(), CJDicError> {
|
||||
self.ensure_db_attached(DbChild::Search)?;
|
||||
|
||||
let mut search_db = SearchDatabase::new(&mut self.conn);
|
||||
search_db.create_schema()?;
|
||||
search_db.reset_db()?;
|
||||
search_db.regenerate_yomitan("main", tokenizer.clone(), &progress_callback)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn check_search_db(&mut self) -> Result<bool, CJDicError> {
|
||||
self.ensure_db_attached(DbChild::Search)?;
|
||||
|
||||
let is_filled = self.conn.query_row(
|
||||
"
|
||||
SELECT
|
||||
(SELECT COALESCE(MAX(id), 0) FROM search.terms) >=
|
||||
(SELECT COALESCE(MAX(id), 0) FROM terms)
|
||||
",
|
||||
[],
|
||||
|r| r.get::<_, bool>(0),
|
||||
)?;
|
||||
Ok(is_filled)
|
||||
}
|
||||
}
|
||||
|
||||
fn intern(
|
||||
|
||||
+19
-9
@@ -1,4 +1,5 @@
|
||||
use std::{
|
||||
backtrace::Backtrace,
|
||||
string::FromUtf8Error,
|
||||
sync::{MutexGuard, PoisonError},
|
||||
};
|
||||
@@ -51,63 +52,72 @@ pub enum CJDicError {
|
||||
|
||||
impl From<rusqlite::Error> for CJDicError {
|
||||
fn from(e: rusqlite::Error) -> Self {
|
||||
eprintln!("{e:#}");
|
||||
let bt = Backtrace::capture();
|
||||
eprintln!("{e:#}\n{bt}");
|
||||
CJDicError::Database(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for CJDicError {
|
||||
fn from(e: serde_json::Error) -> Self {
|
||||
eprintln!("{e:#}");
|
||||
let bt = Backtrace::capture();
|
||||
eprintln!("{e:#}\n{bt}");
|
||||
CJDicError::Serialization(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<anyhow::Error> for CJDicError {
|
||||
fn from(e: anyhow::Error) -> Self {
|
||||
eprintln!("{e:#}");
|
||||
let bt = Backtrace::capture();
|
||||
eprintln!("{e:#}\n{bt}");
|
||||
CJDicError::AnyhowError(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for CJDicError {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
eprintln!("{e:#}");
|
||||
let bt = Backtrace::capture();
|
||||
eprintln!("{e:#}\n{bt}");
|
||||
CJDicError::IOError(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ZipError> for CJDicError {
|
||||
fn from(e: ZipError) -> Self {
|
||||
eprintln!("{e:#}");
|
||||
let bt = Backtrace::capture();
|
||||
eprintln!("{e:#}\n{bt}");
|
||||
CJDicError::ZipError(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<VibratoError> for CJDicError {
|
||||
fn from(e: VibratoError) -> Self {
|
||||
eprintln!("{e:#}");
|
||||
let bt = Backtrace::capture();
|
||||
eprintln!("{e:#}\n{bt}");
|
||||
CJDicError::VibratoError(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FromUtf8Error> for CJDicError {
|
||||
fn from(e: FromUtf8Error) -> Self {
|
||||
eprintln!("{e:#}");
|
||||
let bt = Backtrace::capture();
|
||||
eprintln!("{e:#}\n{bt}");
|
||||
CJDicError::FromUtf8Error(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PoisonError<MutexGuard<'_, Connection>>> for CJDicError {
|
||||
fn from(e: PoisonError<MutexGuard<'_, Connection>>) -> Self {
|
||||
eprintln!("{e:#}");
|
||||
let bt = Backtrace::capture();
|
||||
eprintln!("{e:#}\n{bt}");
|
||||
CJDicError::ConnectionMutexGuardError(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PoisonError<MutexGuard<'_, Tokenizer>>> for CJDicError {
|
||||
fn from(e: PoisonError<MutexGuard<'_, Tokenizer>>) -> Self {
|
||||
eprintln!("{e:#}");
|
||||
let bt = Backtrace::capture();
|
||||
eprintln!("{e:#}\n{bt}");
|
||||
CJDicError::TokenizerMutexGuardError(e.to_string())
|
||||
}
|
||||
}
|
||||
+13
-4
@@ -40,10 +40,10 @@ pub struct AppService {
|
||||
impl AppService {
|
||||
pub fn new(
|
||||
db_dir: impl AsRef<Path>,
|
||||
vibrato_dict: impl AsRef<Path>,
|
||||
vibrato_dic: impl AsRef<Path>,
|
||||
) -> Result<Self, CJDicError> {
|
||||
let db = Database::new(db_dir)?;
|
||||
let tokenizer = Tokenizer::new(vibrato_dict)?;
|
||||
let tokenizer = Tokenizer::new(vibrato_dic)?;
|
||||
Ok(Self { db, tokenizer })
|
||||
}
|
||||
|
||||
@@ -174,6 +174,10 @@ impl AppService {
|
||||
to_be_removed_dicts.push(z.to_string());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for z in dir_zip_map.keys() {
|
||||
new_dicts.push(z.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
// Import ordering
|
||||
@@ -202,14 +206,19 @@ impl AppService {
|
||||
|
||||
{
|
||||
let mut writer = YomitanWriter::new(self.db.dir.clone())?;
|
||||
|
||||
for z in new_dicts.clone().iter() {
|
||||
if let Some(zip_file) = dir_zip_map.get(z) {
|
||||
Self::import_yomitan_zip_file(&mut writer, *zip_file, lang, &import_callback)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writer.make_search_db(self.tokenizer.clone(), &import_callback)?;
|
||||
{
|
||||
let mut writer = YomitanWriter::new(self.db.dir.clone())?;
|
||||
|
||||
if new_dicts.len() + to_be_removed_dicts.len() > 0 || !writer.check_search_db()? {
|
||||
writer.make_search_db(self.tokenizer.clone(), &import_callback)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(LoadYomitanZipDirResult {
|
||||
|
||||
@@ -26,9 +26,8 @@ pub struct Tokenizer {
|
||||
}
|
||||
|
||||
impl Tokenizer {
|
||||
pub fn new(vibrato_dict: impl AsRef<Path>) -> Result<Self, CJDicError> {
|
||||
let file = BufReader::new(File::open(vibrato_dict)?);
|
||||
let reader = zstd::Decoder::new(file)?;
|
||||
pub fn new(vibrato_dic: impl AsRef<Path>) -> Result<Self, CJDicError> {
|
||||
let reader = BufReader::new(File::open(vibrato_dic)?);
|
||||
let dictionary = vibrato_rkyv::Dictionary::read(reader)?;
|
||||
Ok(Self {
|
||||
ja_tokenizer: vibrato_rkyv::Tokenizer::new(dictionary),
|
||||
|
||||
Reference in new issue
Block a user