Skip to content

Commit

Permalink
Merge pull request #64 from threefoldtech/development_optimize_dir_store
Browse files Browse the repository at this point in the history
Optimize dir store to have multi-layer structure
  • Loading branch information
rawdaGastan authored Sep 22, 2024
2 parents 725f1ed + c7c02f7 commit a6e52b0
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions rfs/src/store/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,24 @@ impl DirStore {
#[async_trait::async_trait]
impl Store for DirStore {
async fn get(&self, key: &[u8]) -> Result<Vec<u8>> {
let path = self.root.join(hex::encode(key));
let file_name = hex::encode(key);
let dir_path = self.root.join(&file_name[0..2]);

let mut path = dir_path.join(&file_name);
let data = match fs::read(&path).await {
Ok(data) => data,
Err(err) if err.kind() == ErrorKind::NotFound => {
return Err(Error::KeyNotFound);
path = self.root.join(file_name);
let data = match fs::read(&path).await {
Ok(data) => data,
Err(err) if err.kind() == ErrorKind::NotFound => {
return Err(Error::KeyNotFound);
}
Err(err) => {
return Err(Error::IO(err));
}
};
data
}
Err(err) => {
return Err(Error::IO(err));
Expand All @@ -49,9 +62,13 @@ impl Store for DirStore {
}

async fn set(&self, key: &[u8], blob: &[u8]) -> Result<()> {
let path = self.root.join(hex::encode(key));
let file_name = hex::encode(key);
let dir_path = self.root.join(&file_name[0..2]);

fs::create_dir_all(&dir_path).await?;

fs::write(path, blob).await?;
let file_path = dir_path.join(file_name);
fs::write(file_path, blob).await?;
Ok(())
}

Expand Down

0 comments on commit a6e52b0

Please sign in to comment.