Skip to content

Commit

Permalink
Any place where u can use Stores, you can also use any Store
Browse files Browse the repository at this point in the history
  • Loading branch information
muhamadazmy committed May 21, 2024
1 parent d5af1a2 commit 3253e8a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod test {
use crate::{
cache::Cache,
fungi::meta,
store::{dir::DirStore, Router, Stores},
store::{dir::DirStore, Router},
};
use std::path::PathBuf;
use tokio::{fs, io::AsyncReadExt};
Expand Down Expand Up @@ -61,8 +61,8 @@ mod test {
let store1 = DirStore::new(root.join("store1")).await.unwrap();
let mut store = Router::new();

store.add(0x00, 0x7f, Stores::Dir(store0));
store.add(0x80, 0xff, Stores::Dir(store1));
store.add(0x00, 0x7f, store0);
store.add(0x80, 0xff, store1);

pack(writer, store, &source, false).await.unwrap();

Expand All @@ -72,8 +72,8 @@ mod test {
let store1 = DirStore::new(root.join("store1")).await.unwrap();
let mut store = Router::new();

store.add(0x00, 0x7f, Stores::Dir(store0));
store.add(0x80, 0xff, Stores::Dir(store1));
store.add(0x00, 0x7f, store0);
store.add(0x80, 0xff, store1);

let cache = Cache::new(root.join("cache"), store);

Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use clap::{ArgAction, Args, Parser, Subcommand};

use rfs::cache;
use rfs::fungi;
use rfs::store::{self, Router};
use rfs::store::{self, Router, Stores};

use regex::Regex;

Expand Down Expand Up @@ -230,7 +230,7 @@ async fn fuse(opts: MountOptions) -> Result<()> {
filesystem.mount(opts.target).await
}

async fn get_router(meta: &fungi::Reader) -> Result<Router> {
async fn get_router(meta: &fungi::Reader) -> Result<Router<Stores>> {
let mut router = store::Router::new();

for route in meta.routes().await.context("failed to get store routes")? {
Expand All @@ -243,7 +243,7 @@ async fn get_router(meta: &fungi::Reader) -> Result<Router> {
Ok(router)
}

async fn parse_router(urls: &[String]) -> Result<Router> {
async fn parse_router(urls: &[String]) -> Result<Router<Stores>> {
let mut router = Router::new();
let pattern = r"^(?P<range>[0-9a-f]{2}-[0-9a-f]{2})=(?P<url>.+)$";
let re = Regex::new(pattern)?;
Expand Down
19 changes: 7 additions & 12 deletions src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use rand::seq::SliceRandom;

pub use bs::BlockStore;

pub use self::router::Router;

pub async fn make<U: AsRef<str>>(u: U) -> Result<Stores> {
let parsed = url::Url::parse(u.as_ref())?;

Expand Down Expand Up @@ -100,18 +102,11 @@ pub trait Store: Send + Sync + 'static {
fn routes(&self) -> Vec<Route>;
}

/// Router holds a set of shards (stores) where each store can be configured to serve
/// a range of hashes.
///
/// On get, all possible stores that is configured to serve this key are tried until the first
/// one succeed
///
/// On set, the router set the object on all matching stores, and fails if at least
/// one store fails, or if no store matches the key
pub type Router = router::Router<Stores>;

#[async_trait::async_trait]
impl Store for Router {
impl<S> Store for Router<S>
where
S: Store,
{
async fn get(&self, key: &[u8]) -> Result<Vec<u8>> {
if key.is_empty() {
return Err(Error::InvalidKey);
Expand All @@ -120,7 +115,7 @@ impl Store for Router {

// to make it fare we shuffle the list of matching routers randomly everytime
// before we do a get
let mut routers: Vec<&Stores> = self.route(key[0]).collect();
let mut routers: Vec<&S> = self.route(key[0]).collect();
routers.shuffle(&mut rand::thread_rng());
for store in routers {
match store.get(key).await {
Expand Down

0 comments on commit 3253e8a

Please sign in to comment.