Skip to content

Commit

Permalink
Store needs to return their url and range
Browse files Browse the repository at this point in the history
  • Loading branch information
muhamadazmy committed Sep 20, 2023
1 parent 46d5880 commit 0d959b9
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
23 changes: 21 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ pub async fn unpack<P: AsRef<Path>, S: Store>(
pub async fn pack<P: AsRef<Path>, S: Store>(meta: Writer, store: S, root: P) -> Result<()> {
use tokio::fs;

// building routing table from store information
for route in store.routes() {
meta.route(
route.start.unwrap_or(u8::MIN),
route.end.unwrap_or(u8::MAX),
route.url,
)
.await?;
}

let store: BlockStore<S> = store.into();

let m = fs::metadata(&root)
Expand Down Expand Up @@ -207,7 +217,10 @@ async fn scan<S: Store>(

#[cfg(test)]
mod test {
use crate::{fungi::meta, store::dir::DirStore};
use crate::{
fungi::meta,
store::{dir::DirStore, Router},
};

use super::*;

Expand All @@ -218,7 +231,13 @@ mod test {
async fn create_meta() {
let writer = meta::Writer::new("/tmp/build.fl").await.unwrap();

let store = DirStore::new("/tmp/store").await.unwrap();
let store0 = DirStore::new("/tmp/store0").await.unwrap();
let store1 = DirStore::new("/tmp/store1").await.unwrap();
let mut store = Router::new();

store.add(0x00, 0x7f, Box::new(store0));
store.add(0x80, 0xff, Box::new(store1));

pack(writer, store, "/home/azmy/Documents/Visa Application")
.await
.unwrap();
Expand Down
6 changes: 6 additions & 0 deletions src/store/bs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ where

#[cfg(test)]
mod test {
use super::super::Route;

use super::*;
use std::collections::HashMap;
use std::sync::Arc;
Expand All @@ -105,6 +107,10 @@ mod test {

Ok(())
}

fn routes(&self) -> Vec<Route> {
vec![Route::url("mem://")]
}
}

#[tokio::test]
Expand Down
12 changes: 11 additions & 1 deletion src/store/dir.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{Error, Result, Store};
use super::{Error, Result, Route, Store};
use std::io::ErrorKind;
use std::os::unix::prelude::OsStrExt;
use std::path::PathBuf;
use tokio::fs;

Expand Down Expand Up @@ -41,4 +42,13 @@ impl Store for DirStore {
fs::write(path, blob).await?;
Ok(())
}

fn routes(&self) -> Vec<Route> {
let r = Route::url(format!(
"dir://{}",
String::from_utf8_lossy(self.root.as_os_str().as_bytes())
));

vec![r]
}
}
32 changes: 32 additions & 0 deletions src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,29 @@ pub enum Error {

pub type Result<T> = std::result::Result<T, Error>;

pub struct Route {
pub start: Option<u8>,
pub end: Option<u8>,
pub url: String,
}

impl Route {
pub fn url<S: Into<String>>(s: S) -> Self {
Self {
start: None,
end: None,
url: s.into(),
}
}
}
/// The store trait defines a simple (low level) key/value store interface to set/get blobs
/// the concern of the store is to only store given data with given key and implement
/// the means to retrieve it again once a get is called.
#[async_trait::async_trait]
pub trait Store: Send + Sync + 'static {
async fn get(&self, key: &[u8]) -> Result<Vec<u8>>;
async fn set(&self, key: &[u8], blob: &[u8]) -> Result<()>;
fn routes(&self) -> Vec<Route>;
}

/// The store factory trait works as a factory for a specific store
Expand Down Expand Up @@ -107,4 +123,20 @@ impl Store for Router {

Ok(())
}

fn routes(&self) -> Vec<Route> {
let mut routes = Vec::default();
for (key, value) in self.routes.iter() {
for sub in value.routes() {
let r = Route {
start: Some(sub.start.unwrap_or(key.start)),
end: Some(sub.end.unwrap_or(key.end)),
url: sub.url,
};
routes.push(r);
}
}

routes
}
}
2 changes: 1 addition & 1 deletion src/store/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::Range;
/// available routers and find that ones that matches this given prefix
#[derive(Default)]
pub struct Router<T> {
routes: Vec<(Range<u8>, T)>,
pub(crate) routes: Vec<(Range<u8>, T)>,
}

impl<T> Router<T> {
Expand Down
10 changes: 8 additions & 2 deletions src/store/zdb.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Error, Result, Store, StoreFactory};
use super::{Error, Result, Route, Store, StoreFactory};
use anyhow::Context;

use bb8_redis::{
Expand Down Expand Up @@ -79,6 +79,7 @@ impl StoreFactory for ZdbStoreFactory {
type Store = ZdbStore;

async fn new<U: AsRef<str> + Send>(&self, u: U) -> anyhow::Result<Self::Store> {
let url = u.as_ref().to_owned();
let (mut info, namespace) = self.get_connection_info(u)?;

let namespace = WithNamespace {
Expand All @@ -95,12 +96,13 @@ impl StoreFactory for ZdbStoreFactory {
.build(mgr)
.await?;

Ok(ZdbStore { pool })
Ok(ZdbStore { url, pool })
}
}

#[derive(Clone)]
pub struct ZdbStore {
url: String,
pool: Pool<RedisConnectionManager>,
}

Expand Down Expand Up @@ -128,6 +130,10 @@ impl Store for ZdbStore {

Ok(())
}

fn routes(&self) -> Vec<Route> {
vec![Route::url(self.url.clone())]
}
}

#[cfg(test)]
Expand Down

0 comments on commit 0d959b9

Please sign in to comment.