Skip to content

Commit

Permalink
refactor: engine init with whole project_dirs struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliot00 committed May 16, 2023
1 parent 2da6490 commit 007f4ff
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion benches/my_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use liushu_core::{
};

pub fn engine_benchmark(c: &mut Criterion) {
let engine = Engine::init(&PROJECT_DIRS.data_dir).unwrap();
let engine = Engine::init(&PROJECT_DIRS).unwrap();
let test_inputs = ["a", "aac", "bo", "cfl", "df", "fojq", "qiq", "hir", "zzz"];

let mut group = c.benchmark_group("Engine bench");
Expand Down
26 changes: 14 additions & 12 deletions liushu-core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ mod candidates;
mod segmentor;
pub mod state;

use std::{
fs::File,
path::{Path, PathBuf},
};
use std::{fs::File, path::PathBuf};

use itertools::Itertools;
use patricia_tree::PatriciaMap;
use redb::{Database, ReadableTable};

use crate::{dict::DICTIONARY, error::LiushuError, hmm::pinyin_to_sentence};
use crate::{dict::DICTIONARY, dirs::MyProjectDirs, error::LiushuError, hmm::pinyin_to_sentence};

use self::{
candidates::{Candidate, CandidateSource},
Expand All @@ -31,19 +28,24 @@ pub struct Engine {
}

impl Engine {
pub fn init(data_dir: impl AsRef<Path>) -> Result<Self, LiushuError> {
let data_dir = data_dir.as_ref();
let target_dir = data_dir.join("target");
let state: State = bincode::deserialize_from(File::open(data_dir.join(".state"))?)?;
pub fn init(proj_dirs: &MyProjectDirs) -> Result<Self, LiushuError> {
let state: State =
bincode::deserialize_from(File::open(proj_dirs.data_dir.join(".state"))?)?;

let active_formula = state.get_active_formula();
let db = Database::open(target_dir.join(format!("{}.redb", active_formula.id)))?;
let db = Database::open(
proj_dirs
.target_dir
.join(format!("{}.redb", active_formula.id)),
)?;
let trie: PatriciaMap<Vec<String>> = bincode::deserialize_from(File::open(
target_dir.join(format!("{}.trie", active_formula.id)),
proj_dirs
.target_dir
.join(format!("{}.trie", active_formula.id)),
)?)?;

Ok(Self {
target_dir,
target_dir: proj_dirs.target_dir.clone(),
state,
db,
trie,
Expand Down
2 changes: 1 addition & 1 deletion liushu-ls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Backend {

impl Backend {
pub fn new(client: Client) -> Self {
let engine = Engine::init(&PROJECT_DIRS.data_dir).unwrap();
let engine = Engine::init(&PROJECT_DIRS).unwrap();
Self {
client,
input: RwLock::new(String::new()),
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {
deploy(&PROJECT_DIRS).unwrap();
}
Commands::Repl => {
let mut engine = Engine::init(&PROJECT_DIRS.data_dir).unwrap();
let mut engine = Engine::init(&PROJECT_DIRS).unwrap();

loop {
print!("liushu> ");
Expand Down

0 comments on commit 007f4ff

Please sign in to comment.