Skip to content

Commit

Permalink
docs(fs-index): add README and example for the crate
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Aug 8, 2024
1 parent 4c2178f commit 8f8bb70
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
35 changes: 35 additions & 0 deletions fs-index/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# fs-index

`fs-index` is a Rust crate for managing and indexing file system resources. It provides a flexible and efficient way to track changes, query resources, and keep files in sync across multiple devices or locations.

Originally developed for the ARK framework to support local-first applications, `fs-index` can also be used in various scenarios including backup systems, content management, and more.

## Features

The most important struct in this crate is `ResourceIndex` which comes with:

- **Reactive API**
- `update_all`: Method to update the index by rescanning files and returning changes (additions/deletions).
- **Snapshot API**
- `get_resources_by_id`: Query resources from the index by ID.
- `get_resource_by_path`: Query a resource from the index by its path.

## Custom Serialization

The `ResourceIndex` struct includes a custom serialization implementation to avoid writing a large repetitive index file with double maps.

## Tests and Benchmarks

- Unit tests are located in `src/tests.rs`.
- The benchmarking suite is in `benches/resource_index_benchmark.rs`, benchmarking all methods of `ResourceIndex`.
- Run benchmarks with `cargo bench`.

## Examples

To get started, take a look at the examples in the `examples/` directory.

To run a specific example:

```shell
cargo run --example resource_index
```
46 changes: 46 additions & 0 deletions fs-index/examples/resource_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::path::Path;

use anyhow::Result;

use dev_hash::Blake3;
use fs_index::ResourceIndex;

/// A simple example of how to use [`ResourceIndex`] to index a directory.
fn main() -> Result<()> {
// Create a new `ResourceIndex` from the directory "test-assets" using
// blake3 as the hashing algorithm.
let mut index: ResourceIndex<Blake3> =
ResourceIndex::build(Path::new("test-assets"))?;

// Print the indexed resources.
for resource in index.resources() {
println!("{:?}", resource);
}

// Save the index to a file.
index.store()?;

// Get resources by their id.
let id = Blake3(
"172b4bf148e858b13dde0fc6613413bcb7552e5c4e5c45195ac6c80f20eb5ff5"
.to_string(),
);
let resources = index.get_resources_by_id(&id).ok_or_else(|| {
anyhow::anyhow!("Resource with id {:?} not found", id)
})?;
for resource in resources {
println!("{:?}", resource);
}

// Get resources by their path.
let path = Path::new("lena.jpg");
let resource = index.get_resource_by_path(path).ok_or_else(|| {
anyhow::anyhow!("Resource with path {:?} not found", path)
})?;
println!("{:?}", resource);

// Update the index.
index.update_all()?;

Ok(())
}

0 comments on commit 8f8bb70

Please sign in to comment.