-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from luleyleo/core-next
Rewrite search engine
- Loading branch information
Showing
21 changed files
with
1,108 additions
and
1,399 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::{ | ||
search::{self, SearchId, SearchParameters, SharedSearchId}, | ||
SearchMessage, | ||
}; | ||
use flume::{Receiver, Sender}; | ||
use std::{ | ||
path::PathBuf, | ||
sync::{ | ||
atomic::{AtomicUsize, Ordering}, | ||
Arc, | ||
}, | ||
thread, | ||
}; | ||
|
||
pub struct SearchEngine { | ||
pub(crate) sender: Sender<SearchMessage>, | ||
pub(crate) receiver: Receiver<SearchMessage>, | ||
pub(crate) current_search_id: SharedSearchId, | ||
} | ||
|
||
impl Default for SearchEngine { | ||
fn default() -> Self { | ||
let (sender, receiver) = flume::unbounded(); | ||
|
||
SearchEngine { | ||
sender, | ||
receiver, | ||
current_search_id: Arc::new(AtomicUsize::new(0)), | ||
} | ||
} | ||
} | ||
|
||
impl SearchEngine { | ||
pub fn receiver(&self) -> Receiver<SearchMessage> { | ||
self.receiver.clone() | ||
} | ||
|
||
pub fn search(&self, params: SearchParameters) { | ||
self.current_search_id.fetch_add(1, Ordering::Release); | ||
|
||
let engine = self.clone(); | ||
thread::spawn(move || search::run(engine, params)); | ||
} | ||
|
||
pub fn cancel(&self) { | ||
self.current_search_id.fetch_add(1, Ordering::Release); | ||
} | ||
|
||
pub fn is_current(&self, message: &SearchMessage) -> bool { | ||
let current = self.current_search_id.load(Ordering::Acquire); | ||
|
||
message.search() == current | ||
} | ||
|
||
pub(crate) fn clone(&self) -> Self { | ||
SearchEngine { | ||
sender: self.sender.clone(), | ||
receiver: self.receiver.clone(), | ||
current_search_id: self.current_search_id.clone(), | ||
} | ||
} | ||
|
||
pub(crate) fn send_error( | ||
&self, | ||
search: SearchId, | ||
path: PathBuf, | ||
message: String, | ||
) -> Result<(), flume::SendError<SearchMessage>> { | ||
self.sender | ||
.send(SearchMessage::Error(crate::result::SearchError { | ||
search, | ||
path, | ||
message, | ||
})) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use crate::search::SearchSink; | ||
use grep::{regex::RegexMatcher, searcher::Searcher}; | ||
use std::{error::Error, path::Path}; | ||
|
||
pub mod office; | ||
pub mod pdf; | ||
|
||
pub type ExtraFn = | ||
fn(&mut Searcher, &RegexMatcher, &Path, &mut SearchSink) -> Result<(), Box<dyn Error>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::search::SearchSink; | ||
use dotext::{doc::OpenOfficeDoc, *}; | ||
use grep::{regex::RegexMatcher, searcher::Searcher}; | ||
use std::{error::Error, io::Read, path::Path}; | ||
|
||
pub static EXTENSIONS: &[&str] = &["docx", "pptx", "xlsx", "odt", "odp", "ods"]; | ||
|
||
pub fn process( | ||
searcher: &mut Searcher, | ||
matcher: &RegexMatcher, | ||
path: &Path, | ||
sink: &mut SearchSink, | ||
) -> Result<(), Box<dyn Error>> { | ||
let text = extract(path)?; | ||
searcher.search_slice(matcher, text.as_bytes(), sink)?; | ||
Ok(()) | ||
} | ||
|
||
fn extract(path: &Path) -> Result<String, Box<dyn Error>> { | ||
let ext = path | ||
.extension() | ||
.unwrap_or_default() | ||
.to_string_lossy() | ||
.to_string(); | ||
|
||
let mut string = String::new(); | ||
match ext.as_str() { | ||
"docx" => { | ||
let mut docx = Docx::open(path)?; | ||
docx.read_to_string(&mut string)?; | ||
} | ||
"xlsx" => { | ||
let mut xlsx = Xlsx::open(path)?; | ||
xlsx.read_to_string(&mut string)?; | ||
} | ||
"pptx" => { | ||
let mut pptx = Pptx::open(path)?; | ||
pptx.read_to_string(&mut string)?; | ||
} | ||
"odt" => { | ||
let mut odt = Odt::open(path)?; | ||
odt.read_to_string(&mut string)?; | ||
} | ||
// "ods" => { | ||
// let ods = Ods::open(&path)?; | ||
// ods.read_to_string(&mut string)?; | ||
// } | ||
"odp" => { | ||
let mut odp = Odp::open(path)?; | ||
odp.read_to_string(&mut string)?; | ||
} | ||
_ => return Err("unknown extension".into()), | ||
} | ||
Ok(string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
pub mod extended; | ||
pub mod fileinfo; | ||
pub mod manager; | ||
pub mod options; | ||
pub mod rgtools; | ||
pub mod search; | ||
mod engine; | ||
mod result; | ||
mod search; | ||
mod utils; | ||
|
||
pub mod extra; | ||
|
||
pub use engine::SearchEngine; | ||
pub use result::{Location, ResultEntry, SearchMessage, SearchResult}; | ||
pub use search::{SearchFlags, SearchParameters}; | ||
|
||
pub use grep::matcher::Match; |
Oops, something went wrong.