2020-04-09 21:45:07 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
use futures_util::future::*;
|
2020-04-09 21:45:07 +00:00
|
|
|
use tokio::fs;
|
|
|
|
use tokio::prelude::*;
|
2020-04-11 21:00:26 +00:00
|
|
|
use tokio::sync::Mutex;
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
use crate::background::*;
|
2020-04-10 20:01:48 +00:00
|
|
|
use crate::data::*;
|
2020-04-09 21:45:07 +00:00
|
|
|
use crate::error::Error;
|
|
|
|
use crate::proto::*;
|
2020-04-11 21:00:26 +00:00
|
|
|
|
|
|
|
pub struct BlockManager {
|
|
|
|
pub data_dir: PathBuf,
|
|
|
|
pub rc: sled::Tree,
|
|
|
|
pub lock: Mutex<()>,
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:00:26 +00:00
|
|
|
impl BlockManager {
|
|
|
|
pub fn new(db: &sled::Db, data_dir: PathBuf) -> Self {
|
2020-04-11 21:53:32 +00:00
|
|
|
let rc = db
|
|
|
|
.open_tree("block_local_rc")
|
2020-04-11 21:00:26 +00:00
|
|
|
.expect("Unable to open block_local_rc tree");
|
|
|
|
rc.set_merge_operator(rc_merge);
|
2020-04-11 21:53:32 +00:00
|
|
|
Self {
|
2020-04-11 21:00:26 +00:00
|
|
|
rc,
|
|
|
|
data_dir,
|
|
|
|
lock: Mutex::new(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn write_block(&self, hash: &Hash, data: &[u8]) -> Result<Message, Error> {
|
|
|
|
let _lock = self.lock.lock().await;
|
|
|
|
|
|
|
|
let mut path = self.block_dir(hash);
|
|
|
|
fs::create_dir_all(&path).await?;
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2020-04-11 21:00:26 +00:00
|
|
|
path.push(hex::encode(hash));
|
|
|
|
if fs::metadata(&path).await.is_ok() {
|
|
|
|
return Ok(Message::Ok);
|
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2020-04-11 21:00:26 +00:00
|
|
|
let mut f = fs::File::create(path).await?;
|
|
|
|
f.write_all(data).await?;
|
|
|
|
drop(f);
|
|
|
|
|
|
|
|
Ok(Message::Ok)
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:00:26 +00:00
|
|
|
pub async fn read_block(&self, hash: &Hash) -> Result<Message, Error> {
|
|
|
|
let mut path = self.block_dir(hash);
|
|
|
|
path.push(hex::encode(hash));
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2020-04-11 21:00:26 +00:00
|
|
|
let mut f = fs::File::open(path).await?;
|
|
|
|
let mut data = vec![];
|
|
|
|
f.read_to_end(&mut data).await?;
|
|
|
|
|
|
|
|
Ok(Message::PutBlock(PutBlockMessage {
|
|
|
|
hash: hash.clone(),
|
|
|
|
data,
|
|
|
|
}))
|
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2020-04-11 21:00:26 +00:00
|
|
|
fn block_dir(&self, hash: &Hash) -> PathBuf {
|
|
|
|
let mut path = self.data_dir.clone();
|
|
|
|
path.push(hex::encode(&hash.as_slice()[0..1]));
|
|
|
|
path.push(hex::encode(&hash.as_slice()[1..2]));
|
|
|
|
path
|
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2020-04-11 21:00:26 +00:00
|
|
|
pub fn block_incref(&self, hash: &Hash) -> Result<(), Error> {
|
|
|
|
self.rc.merge(&hash, vec![1])?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2020-04-11 21:00:26 +00:00
|
|
|
pub fn block_decref(&self, hash: &Hash, background: &BackgroundRunner) -> Result<(), Error> {
|
|
|
|
match self.rc.merge(&hash, vec![0])? {
|
|
|
|
None => {
|
|
|
|
let mut path = self.block_dir(hash);
|
|
|
|
path.push(hex::encode(hash));
|
|
|
|
background.spawn(tokio::fs::remove_file(path).map_err(Into::into));
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-11 21:53:32 +00:00
|
|
|
Some(_) => Ok(()),
|
2020-04-11 21:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rc_merge(_key: &[u8], old: Option<&[u8]>, new: &[u8]) -> Option<Vec<u8>> {
|
2020-04-11 21:53:32 +00:00
|
|
|
let old = old
|
|
|
|
.map(|x| {
|
|
|
|
assert!(x.len() == 8);
|
|
|
|
let mut x8 = [0u8; 8];
|
|
|
|
x8.copy_from_slice(x);
|
|
|
|
u64::from_be_bytes(x8)
|
|
|
|
})
|
|
|
|
.unwrap_or(0);
|
2020-04-11 21:00:26 +00:00
|
|
|
assert!(new.len() == 1);
|
|
|
|
let new = match new[0] {
|
|
|
|
0 => {
|
|
|
|
if old > 0 {
|
|
|
|
old - 1
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
1 => old + 1,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
if new == 0 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(u64::to_be_bytes(new).to_vec())
|
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
}
|