Coder Social home page Coder Social logo

parthjadhav / rust_search Goto Github PK

View Code? Open in Web Editor NEW
127.0 4.0 11.0 34 KB

Blazingly fast file search library built in Rust

License: MIT License

Rust 100.00%
rust crate fast filesearch filesystem hacktoberfest library recursive-algorithm rust-lang search-algorithm

rust_search's Introduction

Group 1

Blazingly fast file search crate built in Rust ๐Ÿ”ฅ

Version info Documentation License

๐Ÿ“ฆ Usage

Please report any problems you encounter when using rust search here: Issues

Add rust_search = "2.0.0" in Cargo.toml.

[dependencies]
rust_search = "2.0.0"

Examples

  • General use
use rust_search::SearchBuilder;

fn main(){
    let search: Vec<String> = SearchBuilder::default()
        .location("~/path/to/directory")
        .search_input("what to search")
        .more_locations(vec!["/anotherPath/to/search", "/keepAddingIfYouWant/"])
        .limit(1000) // results to return
        .ext("extension")
        .strict()
        .depth(1)
        .ignore_case()
        .hidden()
        .build()
        .collect();

    for path in search {
        println!("{}", path);
    }
}
  • Sort the output by similarity with the input
 use rust_search::{SearchBuilder, similarity_sort};
 fn main() {
     let search_input = "fly";
     let mut search: Vec<String> = SearchBuilder::default()
         .location("~/Desktop/")
         .search_input(search_input)
         .depth(1)
         .ignore_case()
         .build()
        .collect();

     similarity_sort(&mut search, &search_input);
     for path in search {
        println!("{:?}", path);
     }
 }
 

search without similarity sort ["afly.txt", "bfly.txt", "flyer.txt", "fly.txt"]

search with similarity sort ["fly.txt", "flyer.txt", "afly.txt", "bfly.txt",]

  • To get all the files with a specific extension in a directory, use:
use rust_search::SearchBuilder;

let files: Vec<String> = SearchBuilder::default()
    .location("/path/to/directory")
    .ext("file_extension")
    .build()
    .collect();
  • To get all the files in a directory, use:
use rust_search::SearchBuilder;

let files: Vec<String> = SearchBuilder::default()
    .location("/path/to/directory")
    .depth(1)
    .build()
    .collect();

To filter files by date_created, date_modified, file_size and/or custom_filter, use:

use rust_search::{FileSize, FilterExt, SearchBuilder};
use std::time::{Duration, SystemTime};

let search: Vec<String> = SearchBuilder::default()
		.location("~/path/to/directory")
		.file_size_greater(FileSize::Kilobyte(200.0))
		.file_size_smaller(FileSize::Megabyte(10.0))
		.created_after(SystemTime::now() - Duration::from_secs(3600 * 24 * 10))
		.created_before(SystemTime::now())
		.modified_after(SystemTime::now() - Duration::from_secs(3600 * 24 * 5))
		.custom_filter(|dir| dir.metadata().unwrap().is_file())
		.custom_filter(|dir| !dir.metadata().unwrap().permissions().readonly())
		.build()
		.collect();

๐Ÿ‘‰ For more examples, please refer to the Documentation

โš™๏ธ Benchmarks

The difference in sample size is due to the fact that fd and glob are different tools and have different use cases. fd is a command line tool that searches for files and directories. glob is a library that can be used to search for files and directories. The benchmark is done on a MacBook Air M2, 16 GB Unified memory.

Benchmarks are done using hyperfine, Benchmarks files are available in the benchmarks drive folder.

- Rust Search vs Glob

The benchmark was done on a directories containing 300K files.

Command / Library Mean [s] Min [s] Max [s] Relative
rust_search 1.317 ยฑ 0.002 1.314 1.320 1.00
glob 22.728 ยฑ 0.023 22.690 22.746 17.25 ยฑ 0.03

- Rust Search vs FD

The benchmark was done on a directories containing 45K files.

Command / Library Mean [ms] Min [ms] Max [ms] Relative
rust_search 680.5 ยฑ 2.1 678.3 683.6 1.00
fd -e .js 738.7 ยฑ 10.2 720.8 746.7 1.09 ยฑ 0.02

Results:-

+ rust_search is 17.25 times faster than Glob.

+ rust_search** is 1.09 times faster than FD.

๐Ÿ‘จโ€๐Ÿ’ป Contributors

Any contributions would be greatly valued as this library is still in its early stages.

  • Documentation
  • Benchmarks
  • Implementation guidelines
  • Code Improvement

If you want to contribute to this project, please follow the steps below:

  1. Fork the project
  2. Clone the forked repository
  3. Create a feature branch
  4. Make changes to the code
  5. Commit the changes
  6. Push the changes to the forked repository
  7. Create a pull request
  8. Wait for the pull request to be reviewed and merged (if approved)

License

This project is licensed under the terms of the MIT license.

Discord server & Linkedin

Click the button below to join the discord server or Linkedin

Join Discord Server Connect on Linkedin

rust_search's People

Contributors

jewlexx avatar mucks avatar parthjadhav avatar simonmonecke avatar theawiteb avatar weykon avatar xithrius avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rust_search's Issues

Low speed on x86_64-unknown-linux-musl

Why so different speed for some targets?
For x86_64-unknown-linux-gnu

./target/release/walk    
Time elapsed: 1.022172369s
Found 11 files

and x7 slow for x86_64-unknown-linux-musl

./target/x86_64-unknown-linux-musl/release/walk
Time elapsed: 7.560947213s
Found 11 files

Why?

[request] Add example of returning results

I'm a total newb to Rust. Trying to figure out how to use this library in the context of https://github.com/tauri-apps/tauri but am having trouble getting the return value right. Trying to have it return the file paths in a simple array (to be used in JS).

Based on the example code:

use rust_search::SearchBuilder;

fn main(){
    let search: Vec<String> = SearchBuilder::default()
        .location("~/path/to/directory")
        .search_input("what to search")
        .more_locations(vec!["/anotherPath/to/search", "/keepAddingIfYouWant/"])
        .limit(1000) // results to return
        .ext("extension")
        .strict()
        .depth(1)
        .ignore_case()
        .hidden()
        .build()
        .collect();

    for path in search {
        println!("{}", path);
    }

   // How to return `search` here as a simple array?
}

I've tried simply adding:

return search;

But that returns "expected (), found struct Vec".

I know that for Rust developers this is likely embarrassingly simple. Please forgive my ignorance! I've done quite a bit of searching and tested different things but nothing has worked.

[Bug]: Search input includes `location` to search for

let search: Vec<String> = SearchBuilder::default()
        .location("/System/")
        .search_input("Syste")
        .depth(1)
        .ignore_case()
        .build()
        .collect();

This will return everything contained in /System/ directory because the Regex will be matched with the path input i.e /System/ and return everything.

This could be solved by adding: .split("/").last().unwrap() to path

reg_exp.is_match(&path.split("/").last().unwrap())

@TheAwiteb , Is there a better way to do this? Or are there any implications using this approach ?

[Feature]: Sort the paths by file names

Currently the results are scattered & random.

Eg:

Current output:
Search input: "a"
Search output: ["man", "cat", "ant", "ban", "tan"]

Expected result with .sort(true) :
Search input: "a"
Search output: ["ant", "ban", "cat", "man", "tan"]

Is it possible to exclude directories?

This would help performance when using a high depth value and knowing that desired results are not in directories {...}. For example, node_modules (which often have deeply nested structures with lots of files).

Return &Path or OsString object from iter

I am very impressed with the simplicity and speed of your solution!

Are you planning to abandon the use of String as a return value in the future? On Linux, this can create some problems, as far as I can tell.

Is there any example using this library in c++?

This is a cool library. I want to using this library in my c++ project, but I cannot find any example. I know I can use rust cxx to embed rust library to c++, but that's not easy to return string vector from rust to c++.
So hope any cxx/c++ exmaple using this library. Have you ever tired cxx? It will be awsome to have an example of that.

Ability to use outside variables in custom_filter() for better filtering

I want to use variables outside the scope of the closure inside custom_function(|d: DirEntry| { //code })
I am new to rust so please forgive me if I make a mistake.

Currently, this yeilds a type error because the closure is capturing outside variables.
image
image

Even using move or inner functions does not solve this problem.
This functionality would be particularly useful when the value of outside_var can change or is not determined from the start.

[Feature Request]: Make `Search` struct easier

Breaking changes

I think add search instance function to Search struct will make it easier. This will make it easier to do something like this

use rust_search::Search;

fn main() {
    let mains = Search::default().input("main").search();
    let readmes = Search::default().input("readme")
    .ignore_case() // will ignore ascii case
    .extension("md")
    .search();
}

[Bug]: Search work without file extension, but with it's not

^ Title

For example i have file it's name is some_random_word_yes_it_is.md in /home/User/foo/bar/

use rust_search::Search;

fn main(){
    let search = Search::new(
        "/home/User/foo/bar/",
        Some("yes"),
        None,
        None);

    for path in search {
        println!("{}", path);
    }
}

This will work, but when i add the file extinction it's not, for example

use rust_search::Search;

fn main(){
    let search = Search::new(
        "/home/User/foo/bar/",
        Some("yes"),
        Some(".md"), // also "md"
        None);

    for path in search {
        println!("{}", path);
    }
}

rust_search::Filesize cannot be imported

error[E0432]: unresolved import rust_search::FileType
--> src/main.rs:3:5
|
3 | use rust_search::FileType;
| ^^^^^^^^^^^^^^^^^^^^^ no FileType in the root
|
help: consider importing this struct instead
|
3 | use std::fs::FileType;
| ~~~~~~~~~~~~~~~~~~

error[E0433]: failed to resolve: use of undeclared type FileSize

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.