Skip to content

Commit

Permalink
feat(dict): override reopen and flush in AnyDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Jan 2, 2024
1 parent 02688cf commit bb05466
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,22 @@ impl Dictionary for AnyDictionary {
}
}

fn reopen(&mut self) -> Result<(), DictionaryUpdateError> {
match self {
AnyDictionary::SqliteDictionary(dict) => dict.reopen(),
AnyDictionary::TrieDictionary(dict) => dict.reopen(),
AnyDictionary::HashMapDictionary(dict) => dict.reopen(),

Check warning on line 525 in src/dictionary/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/dictionary/mod.rs#L524-L525

Added lines #L524 - L525 were not covered by tests
}
}

fn flush(&mut self) -> Result<(), DictionaryUpdateError> {
match self {
AnyDictionary::SqliteDictionary(dict) => dict.flush(),
AnyDictionary::TrieDictionary(dict) => dict.flush(),
AnyDictionary::HashMapDictionary(dict) => dict.flush(),

Check warning on line 533 in src/dictionary/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/dictionary/mod.rs#L532-L533

Added lines #L532 - L533 were not covered by tests
}
}

fn insert<Syl: AsRef<Syllable>>(
&mut self,
syllables: &[Syl],
Expand Down
10 changes: 5 additions & 5 deletions src/dictionary/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl From<RusqliteError> for DictionaryUpdateError {

impl Dictionary for SqliteDictionary {
fn lookup_phrase<Syl: AsRef<Syllable>>(&self, syllables: &[Syl]) -> Phrases<'static> {
let syllables_bytes = syllables.into_syllables_bytes();
let syllables_bytes = syllables.into_bytes();
let mut stmt = self
.conn
.prepare_cached(
Expand Down Expand Up @@ -353,7 +353,7 @@ impl Dictionary for SqliteDictionary {
source: Some(Box::new(SqliteDictionaryError::ReadOnly)),
});
}
let syllables_bytes = syllables.into_syllables_bytes();
let syllables_bytes = syllables.into_bytes();
let mut stmt = self.conn.prepare_cached(
"INSERT OR REPLACE INTO dictionary_v1 (
syllables,
Expand All @@ -377,7 +377,7 @@ impl Dictionary for SqliteDictionary {
source: Some(Box::new(SqliteDictionaryError::ReadOnly)),
});
}
let syllables_bytes = syllables.into_syllables_bytes();
let syllables_bytes = syllables.into_bytes();
let tx = self.conn.transaction()?;
{
let mut stmt = tx.prepare_cached(
Expand Down Expand Up @@ -424,7 +424,7 @@ impl Dictionary for SqliteDictionary {
syllables: &[Syl],
phrase_str: &str,
) -> Result<(), DictionaryUpdateError> {
let syllables_bytes = syllables.into_syllables_bytes();
let syllables_bytes = syllables.into_bytes();
let mut stmt = self
.conn
.prepare_cached("DELETE FROM dictionary_v1 WHERE syllables = ? AND phrase = ?")?;
Expand Down Expand Up @@ -507,7 +507,7 @@ impl DictionaryBuilder for SqliteDictionaryBuilder {
} else {
0
};
let syllables_bytes = syllables.into_syllables_bytes();
let syllables_bytes = syllables.into_bytes();

Check warning on line 510 in src/dictionary/sqlite.rs

View check run for this annotation

Codecov / codecov/patch

src/dictionary/sqlite.rs#L510

Added line #L510 was not covered by tests
let mut stmt = self.dict.conn.prepare_cached(
"INSERT OR REPLACE INTO dictionary_v1 (
syllables,
Expand Down
4 changes: 2 additions & 2 deletions src/zhuyin/syllable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ impl AsRef<Syllable> for Syllable {
/// TODO: docs
pub trait IntoSyllablesBytes {
/// TODO: docs
fn into_syllables_bytes(&self) -> Vec<u8>;
fn into_bytes(&self) -> Vec<u8>;
}

impl<Syl: AsRef<Syllable>> IntoSyllablesBytes for &[Syl] {
fn into_syllables_bytes(&self) -> Vec<u8> {
fn into_bytes(&self) -> Vec<u8> {
let mut syllables_bytes = vec![];
self.iter()
.for_each(|syl| syllables_bytes.extend_from_slice(&syl.as_ref().to_le_bytes()));
Expand Down

0 comments on commit bb05466

Please sign in to comment.