Skip to content

Commit

Permalink
Merge pull request #20 from naaive/user_setting
Browse files Browse the repository at this point in the history
i18n、Highlighting、Exclude path 4 index
  • Loading branch information
naaive authored Apr 2, 2022
2 parents 8906f7a + 6a9758c commit 302e45c
Show file tree
Hide file tree
Showing 25 changed files with 1,478 additions and 623 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
"@testing-library/user-event": "^13.2.1",
"af-virtual-scroll": "2.7.9",
"copy-to-clipboard": "^3.3.1",
"i18next": "^21.6.14",
"lodash": "^4.17.21",
"mark.js": "^8.11.1",
"moment": "^2.29.1",
"office-ui-fabric-react": "^7.183.1",
"ramda": "^0.27.1",
"react": "^17.0.2",
"react-custom-scrollbars": "^4.2.1",
"react-dom": "^17.0.2",
"react-file-icon": "1.1.0",
"react-i18next": "^11.16.2",
"react-mark.js": "^3.1.16",
"react-modern-drawer": "^1.0.2",
"react-scripts": "5.0.0",
"styled-components": "^5.3.3",
Expand Down
43 changes: 43 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pinyin = "0.9"
directories = "4.0"
jwalk = "0.6.0"
convert_case = "0.1.0"

lazy_static = "1.4.0"
webbrowser = "0.6.0"

[features]
# by default Tauri runs in production mode
Expand All @@ -53,5 +54,5 @@ default = [ "custom-protocol" ]
# DO NOT remove this
custom-protocol = [ "tauri/custom-protocol" ]

[profile.test]
opt-level=3
#[profile.test]
#opt-level=3
2 changes: 0 additions & 2 deletions src-tauri/src/file_doc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::utils;
use jwalk::DirEntry;
use std::fs::Metadata;
use std::path::PathBuf;

#[derive(Debug)]
pub struct FileDoc {
Expand Down
56 changes: 21 additions & 35 deletions src-tauri/src/fs_watcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate notify;

use crate::idx_store::IdxStore;
use crate::idx_store::IDX_STORE;
use crate::utils;
use crate::utils::subs;
use log::{error, info};
Expand All @@ -12,18 +12,16 @@ use std::os::unix::fs::MetadataExt;
#[cfg(target_os = "windows")]
use std::os::windows::fs::MetadataExt;
use std::path;
use std::path::PathBuf;

use std::sync::mpsc::channel;
use std::sync::Arc;

pub struct FsWatcher {
index_store: Arc<IdxStore>,
path: String,
}

impl FsWatcher {
pub fn new(index_store: Arc<IdxStore>, path: String) -> FsWatcher {
FsWatcher { index_store, path }
pub fn new(path: String) -> FsWatcher {
FsWatcher { path }
}

pub fn start(&mut self) {
Expand All @@ -49,14 +47,13 @@ impl FsWatcher {
continue;
}
if Op::REMOVE & op == Op::REMOVE {
self.index_store._del(path_str.to_string())
IDX_STORE._del(path_str.to_string())
};
let result = path.metadata();
match result {
Ok(meta) => {
let abs_path = path.to_str().unwrap().to_string();

let name = Self::get_filename(&path);
let abs_path = path.to_str().unwrap_or("").to_string();
let name = utils::path2name(abs_path.clone());

#[cfg(windows)]
let _size = meta.file_size();
Expand All @@ -76,9 +73,7 @@ impl FsWatcher {
}
let name0 = name.clone();
let ext = utils::file_ext(&name0);
self
.index_store
.add(name, abs_path, meta.is_dir(), ext.to_string())
IDX_STORE.add(name, abs_path, meta.is_dir(), ext.to_string())
}
Err(_) => {}
}
Expand All @@ -102,27 +97,18 @@ impl FsWatcher {
if let Ok(meta) = sub_path.metadata() {
let name0 = name.clone();
let ext = utils::file_ext(&name0);
self
.index_store
.add(name, sub.clone(), meta.is_dir(), ext.to_string());
IDX_STORE.add(name, sub.clone(), meta.is_dir(), ext.to_string());
}
}
}

fn get_filename(path: &PathBuf) -> String {
let name = path
.file_name()
.map(|x| x.to_str().unwrap())
.unwrap_or_default()
.to_string();
name
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::idx_store::IdxStore;
use notify::watcher;
use std::sync::Arc;
use std::time::Duration;

#[test]
Expand Down Expand Up @@ -176,8 +162,8 @@ mod tests {
match rx.recv() {
Ok(RawEvent {
path: Some(path),
op: Ok(op),
cookie,
op: Ok(_op),
cookie: _,
}) => {
let x = path.to_str().unwrap();
if x.contains("orangecachedata") {
Expand All @@ -191,14 +177,14 @@ mod tests {
}
}
}
}

#[test]
fn t4() {
let conf_path = format!("{}{}", utils::data_dir(), "/orangecachedata/conf");
let idx_path = format!("{}{}", utils::data_dir(), "/orangecachedata/idx");
#[test]
fn t4() {
let _conf_path = format!("{}{}", utils::data_dir(), "/orangecachedata/conf");
let idx_path = format!("{}{}", utils::data_dir(), "/orangecachedata/idx");

let idx_store = Arc::new(IdxStore::new(&idx_path));
let mut watcher = FsWatcher::new(idx_store, "/".to_string());
watcher.start();
let _idx_store = Arc::new(IdxStore::new(&idx_path));
let mut watcher = FsWatcher::new("/".to_string());
watcher.start();
}
}
47 changes: 25 additions & 22 deletions src-tauri/src/idx_store.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
use std::collections::HashSet;

use std::fs;

#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
#[cfg(windows)]
use std::os::windows::fs::MetadataExt;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use convert_case::{Case, Casing};
use jieba_rs::Jieba;
use pinyin::ToPinyin;
use tantivy::collector::TopDocs;
use tantivy::query::{BooleanQuery, FuzzyTermQuery, Occur, Query, QueryParser, TermQuery};
use tantivy::merge_policy::NoMergePolicy;
use tantivy::query::{BooleanQuery, FuzzyTermQuery, Occur, QueryParser, TermQuery};
use tantivy::schema::*;

use tantivy::{doc, Index, IndexReader, IndexWriter, ReloadPolicy};

use crate::file_doc::FileDoc;
use crate::file_view::FileView;
use crate::utils;
use crate::utils::is_ascii_alphanumeric;
use convert_case::{Case, Casing};
use jieba_rs::Jieba;
use pinyin::ToPinyin;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tantivy::merge_policy::NoMergePolicy;

lazy_static! {
pub static ref IDX_STORE: IdxStore = {
let idx_path = format!("{}{}", utils::data_dir(), "/orangecachedata/idx");
IdxStore::new(&idx_path)
};
}

pub struct IdxStore {
pub writer: Arc<Mutex<IndexWriter>>,
Expand All @@ -45,7 +48,7 @@ impl IdxStore {
}
let space = " ";
let hans = hans.replace("-", space).replace("_", space);
let mut words = self.tokenizer.cut(&hans, false);
let words = self.tokenizer.cut(&hans, false);

let mut token_text: HashSet<String> = vec![].into_iter().collect();

Expand All @@ -68,7 +71,7 @@ impl IdxStore {
}
let space = " ";
let hans = hans.replace("-", space).replace("_", space);
let mut words = self.tokenizer.cut(&hans, false);
let words = self.tokenizer.cut(&hans, false);

let mut token_text: HashSet<String> = vec![].into_iter().collect();

Expand Down Expand Up @@ -113,7 +116,7 @@ impl IdxStore {
}

pub fn search(&self, kw: String, limit: usize) -> Vec<FileView> {
let mut paths = self.search_paths(self.search_tokenize(kw.clone()), limit);
let paths = self.search_paths(self.search_tokenize(kw.clone()), limit);
// if paths.is_empty() {
// paths = self.suggest_path(kw, limit);
// }
Expand All @@ -129,7 +132,6 @@ impl IdxStore {
limit: usize,
is_dir_opt: Option<bool>,
ext_opt: Option<String>,
parent_dirs_opt: Option<String>,
) -> Vec<FileView> {
let searcher = self.reader.searcher();

Expand Down Expand Up @@ -201,7 +203,7 @@ impl IdxStore {
.map(|x| {
return FileView {
abs_path: "".to_string(),
name: utils::path2name(utils::norm(&x)).unwrap_or("".to_string()),
name: utils::path2name(x),
created_at: 0,
mod_at: 0,
size: 0,
Expand Down Expand Up @@ -287,7 +289,7 @@ impl IdxStore {

file_views.push(FileView {
abs_path: utils::norm(&path),
name: utils::path2name(utils::norm(&path)).unwrap_or("".to_string()),
name: utils::path2name(path),
created_at: meta
.created()
.unwrap_or(SystemTime::now())
Expand Down Expand Up @@ -362,7 +364,7 @@ impl IdxStore {
.unwrap();

let mut query_parser = QueryParser::for_index(&index, vec![name_field]);
let mut ext_query_parser = QueryParser::for_index(&index, vec![ext_field]);
let ext_query_parser = QueryParser::for_index(&index, vec![ext_field]);
// let mut parent_dirs_query_parser = QueryParser::for_index(&index, vec![parent_dirs_field]);
query_parser.set_field_boost(name_field, 4.0f32);
let mut jieba = Jieba::new();
Expand Down Expand Up @@ -394,7 +396,7 @@ impl IdxStore {
ext = "".to_string();
}
let is_dir_bytes = IdxStore::is_dir_bytes(is_dir);
self.writer.lock().unwrap().add_document(doc!(
let _ = self.writer.lock().unwrap().add_document(doc!(
self.name_field => self.tokenize(name),
self.path_field=>path.as_bytes(),
self.is_dir_field=>is_dir_bytes,
Expand Down Expand Up @@ -423,14 +425,15 @@ impl IdxStore {

#[cfg(test)]
mod tests {
use super::*;
use std::thread::sleep;

use super::*;

#[test]
fn t1() {
let path = "./tmp";
fs::remove_dir_all(path);
let mut store = IdxStore::new(path);
let _ = fs::remove_dir_all(path);
let store = IdxStore::new(path);

let vec1 = vec![
"jack rose",
Expand Down
Loading

0 comments on commit 302e45c

Please sign in to comment.