Skip to content

Commit

Permalink
feat(dict): add explicit flush support and use in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Jan 2, 2024
1 parent 7fcc5aa commit 02688cf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ pub trait Dictionary: Debug {
/// Returns information about the dictionary instance.
fn about(&self) -> DictionaryInfo;

/// Reopens the dictionary if it was changed by a different process
///
/// It should not fail if the dictionary is read-only or able to sync across
/// processes automatically.
fn reopen(&mut self) -> Result<(), DictionaryUpdateError> {
Ok(())
}
/// Flushes all the changes back to the filesystem
///
/// The change made to the dictionary might not be persisted without
/// calling this method.
fn flush(&mut self) -> Result<(), DictionaryUpdateError> {
Ok(())
}
/// An method for updating dictionaries.
///
/// For more about the concept of dictionaries generally, please see the
Expand Down
5 changes: 5 additions & 0 deletions src/dictionary/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ impl Dictionary for SqliteDictionary {
self.info.clone()
}

fn flush(&mut self) -> Result<(), DictionaryUpdateError> {
self.conn.pragma_update(None, "wal_checkpoint", "PASSIVE")?;
Ok(())
}

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

View check run for this annotation

Codecov / codecov/patch

src/dictionary/sqlite.rs#L341-L344

Added lines #L341 - L344 were not covered by tests

fn insert<Syl: AsRef<Syllable>>(
&mut self,
syllables: &[Syl],
Expand Down
18 changes: 16 additions & 2 deletions src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ where
options: EditorOptions,
state: Transition,

dirty_dict: bool,
nth_conversion: usize,
commit_buffer: String,
notice_buffer: String,
Expand All @@ -143,6 +144,7 @@ where
estimate,
options: EditorOptions::default(),
state: Transition::Entering(EditorKeyBehavior::Ignore, Entering),
dirty_dict: false,
nth_conversion: 0,
commit_buffer: String::new(),
notice_buffer: String::new(),
Expand Down Expand Up @@ -303,10 +305,15 @@ where
{
return Err(format!("已有:{phrase}"));
}
self.dict
let result = self
.dict
.insert(&syllables, (&phrase, 100).into())
.map(|_| phrase)
.map_err(|_| "加詞失敗:字數不符或夾雜符號".to_owned())
.map_err(|_| "加詞失敗:字數不符或夾雜符號".to_owned());
if result.is_ok() {
self.dirty_dict = true;
}
result
}
pub fn learn_phrase<Syl: AsRef<Syllable>>(&mut self, syllables: &[Syl], phrase: &str) {
let phrases = Vec::from_iter(self.dict.lookup_phrase(syllables));
Expand All @@ -326,9 +333,11 @@ where
let time = self.estimate.now().unwrap();

let _ = self.dict.update(&syllables, phrase, user_freq, time);
self.dirty_dict = true;
}
pub fn unlearn_phrase(&mut self, syllables: &[Syllable], phrase: &str) {
let _ = self.dict.remove(&syllables, phrase);
self.dirty_dict = true;
}
pub fn switch_character_form(&mut self) {
self.options = EditorOptions {
Expand Down Expand Up @@ -548,6 +557,11 @@ where
if self.last_key_behavior() == EditorKeyBehavior::Absorb {
self.try_auto_commit();
}
if self.dirty_dict {
let _ = self.user_dict().reopen();
let _ = self.user_dict().flush();
self.dirty_dict = false;
}
self.last_key_behavior()
}
}
Expand Down

0 comments on commit 02688cf

Please sign in to comment.