Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation for text_grep - Issue (Searcher (rustcore) #1715) #1986

Merged
merged 7 commits into from
Apr 16, 2024

Conversation

itsmesamster
Copy link
Collaborator

No description provided.

process_file(&file_path, &patterns, chunk_size, &cancel_token, &sender)
{
if error_sender_clone.send(err.to_string()).is_err() {
eprintln!("Error sending error message through channel");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crate should not post any to console, it should return error in case of error

let error_sender_clone = error_sender.clone();
let cancel_token = cancel_token_clone.clone();
let file_path = Arc::clone(file_path);
thread::spawn(move || {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we need to spawn system thread with async method? In general it's very expensive. If we really need threads here tokio's task should be used. But I don't think we need it at all.

Comment on lines 90 to 93
file_path: &Arc<str>,
patterns: &[Arc<str>],
chunk_size: usize,
cancel_token: &CancellationToken,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Arc potentially will give more headaches than cloning.

chunk_size: usize,
cancel_token: &CancellationToken,
sender: &mpsc::Sender<Result<SearchResult, String>>,
) -> Result<(), String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crate should have own Error enum. Please take a look, for example, to crate thiserror, which helps to create it. See examples in other places of chipmunk's solution.

if !is_text_file(&file_path) {
let error_msg = format!("File '{}' is not a text file", file_path.display());
if sender.send(Err(error_msg.clone())).is_err() {
eprintln!("Error sending search result through channel");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no output into terminal from crate

.collect();

for handle in thread_handles {
handle.join().unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap() is acceptable only in tests and very-very specific use-cases. Please, check for error here and return some error if it is. For ex: handle.join().map_err(|e| e.into())?;

@DmitryAstafyev
Copy link
Collaborator

DmitryAstafyev commented Feb 19, 2024

@itsmesamster thanks for PR. I have a couple of comments in general:

  • do not mix async and stuff from std. I'm about threads, channels etc from std... If you need to spawn something it should be really motivated and we should use tokio to do it (because it's major framework for chipmunk)
  • try to keep away from Arc, Mutex etc... many use-cases can be resolved avoiding it.
  • make sure you do not post any into terminal... It's crate/lib - it should return status. We can post something only for debugging
  • please define Error enum for errors of your crate. Take a look to thiserror crate, which can be useful for string converting
  • please add some information about measurements...

@itsmesamster
Copy link
Collaborator Author

image

Some random measurements and proof that the count is correct as per ripgrep, depending on the case sensitivity flag.

@itsmesamster itsmesamster marked this pull request as ready for review April 15, 2024 08:43
@DmitryAstafyev DmitryAstafyev merged commit 016cd81 into esrlabs:master Apr 16, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants