-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use draco_interfaces::{ | ||
Blake3Hash, Blake3Tree, BlockStoreInterface, CompressionAlgoSet, CompressionAlgorithm, | ||
ConfigConsumer, ContentChunk, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use tempdir::TempDir; | ||
use tokio::{ | ||
fs::{self, File}, | ||
io::AsyncWriteExt, | ||
}; | ||
|
||
use crate::{put::IncrementalPut, store::Store, Block, BlockContent, Key}; | ||
|
||
const TMP_DIR_PREFIX: &str = "tmp-store"; | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
pub struct FsStoreConfig { | ||
store_dir_path: String, | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct FsStore { | ||
store_dir_path: String, | ||
tmp_dir: Arc<TempDir>, | ||
} | ||
|
||
impl ConfigConsumer for FsStore { | ||
const KEY: &'static str = "fsstore"; | ||
type Config = FsStoreConfig; | ||
} | ||
|
||
#[async_trait] | ||
impl BlockStoreInterface for FsStore { | ||
type SharedPointer<T: ?Sized + Send + Sync> = Arc<T>; | ||
type Put = IncrementalPut<Self>; | ||
|
||
async fn init(config: Self::Config) -> anyhow::Result<Self> { | ||
Ok(Self { | ||
store_dir_path: config.store_dir_path, | ||
tmp_dir: TempDir::new(TMP_DIR_PREFIX).map(Arc::new)?, | ||
}) | ||
} | ||
|
||
async fn get_tree(&self, cid: &Blake3Hash) -> Option<Self::SharedPointer<Blake3Tree>> { | ||
match bincode::deserialize::<BlockContent>( | ||
self.fetch(&Key::tree_key(*cid)).await?.as_slice(), | ||
) | ||
.expect("Stored content to be serialized properly") | ||
{ | ||
BlockContent::Tree(tree) => Some(Arc::new(Blake3Tree(tree))), | ||
_ => None, | ||
} | ||
} | ||
|
||
async fn get( | ||
&self, | ||
block_counter: u32, | ||
block_hash: &Blake3Hash, | ||
_compression: CompressionAlgoSet, | ||
) -> Option<Self::SharedPointer<ContentChunk>> { | ||
match bincode::deserialize::<BlockContent>( | ||
self.fetch(&Key::chunk_key(*block_hash, block_counter)) | ||
.await? | ||
.as_slice(), | ||
) | ||
.expect("Stored content to be serialized properly") | ||
{ | ||
BlockContent::Chunk(content) => Some(Arc::new(ContentChunk { | ||
compression: CompressionAlgorithm::Uncompressed, | ||
content, | ||
})), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn put(&self, root: Option<Blake3Hash>) -> Self::Put { | ||
match root { | ||
Some(root) => IncrementalPut::verifier(self.clone(), root), | ||
None => IncrementalPut::trust(self.clone()), | ||
} | ||
} | ||
} | ||
|
||
// TODO: Add logging. | ||
#[async_trait] | ||
impl Store for FsStore { | ||
async fn fetch(&self, key: &Key) -> Option<Block> { | ||
let path = format!("{}/{:?}", self.store_dir_path, key.0); | ||
fs::read(path).await.ok() | ||
} | ||
|
||
// TODO: This should perhaps return an error. | ||
async fn insert(&mut self, key: Key, block: Block) { | ||
let filename = format!("{:?}", key.0); | ||
let path = self.tmp_dir.path().join(filename); | ||
if let Ok(mut tmp_file) = File::create(&path).await { | ||
if tmp_file.write_all(block.as_ref()).await.is_err() { | ||
return; | ||
} | ||
// TODO: Is this needed before calling rename? | ||
if tmp_file.sync_all().await.is_err() { | ||
return; | ||
} | ||
let store_path = format!("{}/{:?}", self.store_dir_path, key.0); | ||
if fs::rename(path, store_path).await.is_err() { | ||
return; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod config; | ||
mod fs; | ||
pub mod memory; | ||
pub mod put; | ||
mod store; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
use async_trait::async_trait; | ||
|
||
use crate::{Block, Key}; | ||
|
||
/// Basic get and put store trait. | ||
/// Simple block store interface. | ||
#[async_trait] | ||
pub trait Store { | ||
fn store_get(&self, key: &Key) -> Option<Block>; | ||
fn store_put(&mut self, key: Key, block: Block); | ||
async fn fetch(&self, key: &Key) -> Option<Block>; | ||
async fn insert(&mut self, key: Key, block: Block); | ||
} |