mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-08 17:35:18 +00:00
minimal working on Android with multiple zips' bundle
This commit is contained in:
1 parent
7d447f8cf1
commit
bd281a3b2e
13 files changed
+165
-115
No files matched your search
+3
-1
@@ -14,7 +14,9 @@ mod yomitan;
|
||||
pub use yomitan::YomitanRow;
|
||||
|
||||
mod yomitan_writer;
|
||||
pub use yomitan_writer::{YomitanWriter, YomitanZipImportProgress, YomitanZipImportResult};
|
||||
pub use yomitan_writer::{
|
||||
YomitanWriter, YomitanZipImportProgress, YomitanZipImportResult, ZipSource,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Database {
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
use anyhow::Context;
|
||||
use blake3::hash;
|
||||
use rusqlite::{Connection, Result, Transaction, params};
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use std::{collections::HashMap, fs::File, io::Read, path::PathBuf, time::Instant};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::HashMap,
|
||||
io::{Cursor, Read},
|
||||
path::{Path, PathBuf},
|
||||
time::Instant,
|
||||
};
|
||||
use zip::ZipArchive;
|
||||
|
||||
use crate::CJDicError;
|
||||
@@ -19,6 +24,21 @@ pub(super) fn normalize_term(s: &str) -> String {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub trait ZipSource {
|
||||
fn file_name(&self) -> &str;
|
||||
fn bytes(&self) -> std::io::Result<Cow<'_, [u8]>>;
|
||||
}
|
||||
|
||||
impl ZipSource for PathBuf {
|
||||
fn file_name(&self) -> &str {
|
||||
Path::file_name(self).and_then(|n| n.to_str()).unwrap_or("")
|
||||
}
|
||||
|
||||
fn bytes(&self) -> std::io::Result<Cow<'_, [u8]>> {
|
||||
std::fs::read(self).map(Cow::Owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
pub struct YomitanZipImportProgress {
|
||||
pub message: String,
|
||||
@@ -238,29 +258,23 @@ impl YomitanWriter {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn import_dictionary_zip_file<Callback>(
|
||||
pub fn import_dictionary_zip_file<Z, Callback>(
|
||||
&mut self,
|
||||
zip_file: PathBuf,
|
||||
zip_file: &Z,
|
||||
lang: &str,
|
||||
progress_callback: Callback,
|
||||
) -> anyhow::Result<YomitanZipImportResult, CJDicError>
|
||||
where
|
||||
Z: ZipSource,
|
||||
Callback: Fn(YomitanZipImportProgress),
|
||||
{
|
||||
let bundle_name = if let Some(fo) = zip_file.file_name()
|
||||
&& let Some(f) = fo.to_str()
|
||||
{
|
||||
f
|
||||
} else {
|
||||
return Err(CJDicError::FileNameNotFound);
|
||||
};
|
||||
let bundle_name = zip_file.file_name();
|
||||
|
||||
let start_time = Instant::now();
|
||||
self.conn.execute_batch("PRAGMA foreign_keys = off")?;
|
||||
|
||||
let f =
|
||||
File::open(&zip_file).with_context(|| format!("opening zip {}", zip_file.display()))?;
|
||||
let mut archive = ZipArchive::new(f).with_context(|| "reading zip archive")?;
|
||||
let cursor = Cursor::new(zip_file.bytes()?);
|
||||
let mut archive = ZipArchive::new(cursor)?;
|
||||
|
||||
let index_file = match archive.by_name("index.json") {
|
||||
Ok(mut f) => {
|
||||
@@ -575,8 +589,8 @@ impl YomitanWriter {
|
||||
tx.commit()?;
|
||||
|
||||
println!(
|
||||
"Importing {:?} takes {:.2?}",
|
||||
zip_file,
|
||||
"Importing {} takes {:.2?}",
|
||||
zip_file.file_name(),
|
||||
start_time.elapsed()
|
||||
);
|
||||
|
||||
|
||||
+43
-6
@@ -1,26 +1,63 @@
|
||||
use serde::Serialize;
|
||||
use thiserror::Error;
|
||||
use zip::result::ZipError;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
#[derive(Debug, Error, Serialize)]
|
||||
#[serde(tag = "kind", content = "message")]
|
||||
pub enum CJDicError {
|
||||
#[error("Database error: {0}")]
|
||||
Database(#[from] rusqlite::Error),
|
||||
Database(String),
|
||||
|
||||
#[error("Serialization error: {0}")]
|
||||
Serialization(#[from] serde_json::Error),
|
||||
Serialization(String),
|
||||
|
||||
#[error("Anyhow error: {0}")]
|
||||
AnyhowError(#[from] anyhow::Error),
|
||||
AnyhowError(String),
|
||||
|
||||
#[error("IO error: {0}")]
|
||||
IOError(#[from] std::io::Error),
|
||||
IOError(String),
|
||||
|
||||
#[error("File name not found")]
|
||||
FileNameNotFound,
|
||||
|
||||
#[error("Zip error: {0}")]
|
||||
ZipError(#[from] ZipError),
|
||||
ZipError(String),
|
||||
|
||||
#[error("Not found")]
|
||||
NotFound,
|
||||
}
|
||||
|
||||
impl From<rusqlite::Error> for CJDicError {
|
||||
fn from(e: rusqlite::Error) -> Self {
|
||||
log::error!("{e:#}");
|
||||
CJDicError::Database(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for CJDicError {
|
||||
fn from(e: serde_json::Error) -> Self {
|
||||
log::error!("{e:#}");
|
||||
CJDicError::Serialization(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<anyhow::Error> for CJDicError {
|
||||
fn from(e: anyhow::Error) -> Self {
|
||||
log::error!("{e:#}");
|
||||
CJDicError::AnyhowError(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for CJDicError {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
log::error!("{e:#}");
|
||||
CJDicError::IOError(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ZipError> for CJDicError {
|
||||
fn from(e: ZipError) -> Self {
|
||||
log::error!("{e:#}");
|
||||
CJDicError::ZipError(e.to_string())
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -2,7 +2,7 @@ mod db;
|
||||
mod error;
|
||||
mod service;
|
||||
|
||||
pub use db::{YomitanRow, YomitanZipImportProgress, YomitanZipImportResult};
|
||||
pub use db::{YomitanRow, YomitanZipImportProgress, YomitanZipImportResult, ZipSource};
|
||||
pub use error::CJDicError;
|
||||
pub use service::AppService;
|
||||
|
||||
|
||||
+18
-26
@@ -1,13 +1,13 @@
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fs::read_dir,
|
||||
path::{Path, PathBuf},
|
||||
collections::{HashMap, HashSet},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use rusqlite::Connection;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
ZipSource,
|
||||
db::{
|
||||
Database, YOMITAN_DBFILE, YomitanRow, YomitanWriter, YomitanZipImportProgress,
|
||||
YomitanZipImportResult,
|
||||
@@ -51,28 +51,22 @@ impl AppService {
|
||||
Ok(writer)
|
||||
}
|
||||
|
||||
pub fn load_yomitan_zip_dir<LoadCallback, ImportCallback>(
|
||||
pub fn load_yomitan_zip_dir<Z, LoadCallback, ImportCallback>(
|
||||
&self,
|
||||
zip_dir: PathBuf,
|
||||
zip_dir: Vec<Z>,
|
||||
lang: &str,
|
||||
load_callback: LoadCallback,
|
||||
import_callback: ImportCallback,
|
||||
) -> Result<LoadYomitanZipDirResult, CJDicError>
|
||||
where
|
||||
Z: ZipSource,
|
||||
LoadCallback: Fn(LoadYomitanZipDirResult),
|
||||
ImportCallback: Fn(YomitanZipImportProgress),
|
||||
{
|
||||
let mut dir_zip_list: HashSet<String> = HashSet::new();
|
||||
let mut dir_zip_map: HashMap<String, Z> = HashMap::new();
|
||||
|
||||
for entry in read_dir(&zip_dir)? {
|
||||
let e = entry?;
|
||||
let p = e.path();
|
||||
|
||||
if p.extension().and_then(|s| s.to_str()) == Some("zip")
|
||||
&& let Some(f) = e.file_name().to_str()
|
||||
{
|
||||
dir_zip_list.insert(f.to_string());
|
||||
}
|
||||
for entry in zip_dir {
|
||||
dir_zip_map.insert(entry.file_name().to_string(), entry);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -96,14 +90,14 @@ impl AppService {
|
||||
db_zip_list.insert(b_name);
|
||||
}
|
||||
|
||||
for z in dir_zip_list.iter() {
|
||||
if !db_zip_list.contains(z) {
|
||||
for z in dir_zip_map.keys() {
|
||||
if !db_zip_list.contains(&z.to_string()) {
|
||||
new_dicts.push(z.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
for z in db_zip_list.iter() {
|
||||
if !dir_zip_list.contains(z) {
|
||||
if !dir_zip_map.contains_key(z) {
|
||||
to_be_removed_dicts.push(z.to_string());
|
||||
}
|
||||
}
|
||||
@@ -138,12 +132,9 @@ impl AppService {
|
||||
let mut writer = YomitanWriter::new(conn)?;
|
||||
|
||||
for z in new_dicts.clone().iter() {
|
||||
Self::import_yomitan_zip_file(
|
||||
&mut writer,
|
||||
zip_dir.join(z),
|
||||
lang,
|
||||
&import_callback,
|
||||
)?;
|
||||
if let Some(zip_file) = dir_zip_map.get(z) {
|
||||
Self::import_yomitan_zip_file(&mut writer, zip_file, lang, &import_callback)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,13 +144,14 @@ impl AppService {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn import_yomitan_zip_file<Callback>(
|
||||
pub fn import_yomitan_zip_file<Z, Callback>(
|
||||
writer: &mut YomitanWriter,
|
||||
zip_file: PathBuf,
|
||||
zip_file: &Z,
|
||||
lang: &str,
|
||||
progress_callback: Callback,
|
||||
) -> Result<YomitanZipImportResult, CJDicError>
|
||||
where
|
||||
Z: ZipSource,
|
||||
Callback: Fn(YomitanZipImportProgress),
|
||||
{
|
||||
Ok(writer.import_dictionary_zip_file(zip_file, lang, progress_callback)?)
|
||||
|
||||
Reference in new issue
Block a user