Skip to content

Commit

Permalink
WIP: refactor for separate stores
Browse files Browse the repository at this point in the history
  • Loading branch information
muhamadazmy committed Sep 18, 2023
1 parent 3f39b68 commit bb44194
Show file tree
Hide file tree
Showing 15 changed files with 702 additions and 1,300 deletions.
6 changes: 0 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,4 @@ fn main() {
"cargo:rustc-env=GIT_VERSION={}",
git_version::git_version!(args = ["--tags", "--always", "--dirty=-modified"])
);

capnpc::CompilerCommand::new()
.src_prefix("schema")
.file("schema/schema.capnp")
.run()
.expect("schema compiler command");
}
97 changes: 0 additions & 97 deletions schema/schema.capnp

This file was deleted.

14 changes: 7 additions & 7 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::meta::types::FileBlock;
use crate::fungi::meta::Block;
use anyhow::{Context, Result};
use bb8_redis::redis::aio::Connection;

Expand Down Expand Up @@ -127,21 +127,21 @@ impl Cache {
//con.
let result: Vec<u8> = con.get(id).await?;
if result.is_empty() {
bail!("invalid chunk length downloaded");
anyhow::bail!("invalid chunk length downloaded");
}

let key = unsafe { std::str::from_utf8_unchecked(key) };
let mut decoder = snap::raw::Decoder::new();
let result = match decoder.decompress_vec(&xxtea::decrypt(&result, key)) {
Ok(data) => data,
Err(_) => bail!("invalid chunk"),
Err(_) => anyhow::bail!("invalid chunk"),
};

Ok(result)
}

// download given an open file, writes the content of the chunk to the file
async fn download(&self, file: &mut File, block: &FileBlock) -> Result<u64> {
async fn download(&self, file: &mut File, block: &Block) -> Result<u64> {
let data = self.get_data(&block.hash, &block.key).await?;
file.write_all(&data).await?;

Expand All @@ -151,7 +151,7 @@ impl Cache {
async fn prepare(&self, id: &[u8]) -> Result<File> {
let name = id.hex();
if name.len() < 4 {
bail!("invalid chunk hash");
anyhow::bail!("invalid chunk hash");
}
let path = self.root.join(&name[0..2]).join(&name[2..4]);
fs::create_dir_all(&path).await?;
Expand All @@ -170,7 +170,7 @@ impl Cache {

/// get a file block either from cache or from remote if it's already
/// not cached
pub async fn get(&self, block: &FileBlock) -> Result<(u64, File)> {
pub async fn get(&self, block: &Block) -> Result<(u64, File)> {
let mut file = self.prepare(&block.hash).await?;
// TODO: locking must happen here so no
// other processes start downloading the same chunk
Expand Down Expand Up @@ -198,7 +198,7 @@ impl Cache {

/// direct downloads all the file blocks from remote and write it to output
#[allow(dead_code)]
pub async fn direct(&self, blocks: &[FileBlock], out: &mut File) -> Result<()> {
pub async fn direct(&self, blocks: &[Block], out: &mut File) -> Result<()> {
use tokio::io::copy;
for (index, block) in blocks.iter().enumerate() {
let (_, mut chunk) = self.get(block).await?;
Expand Down
Loading

0 comments on commit bb44194

Please sign in to comment.