block manager: move blocks in write_block if necessary

This commit is contained in:
Alex Auvolat 2023-09-05 14:37:10 +02:00
parent 887b3233f4
commit a09f86729c

View file

@ -707,25 +707,39 @@ impl BlockManagerLocked {
let compressed = data.is_compressed(); let compressed = data.is_compressed();
let data = data.inner_buffer(); let data = data.inner_buffer();
let mut path = mgr.data_layout.primary_block_dir(hash); let mut tgt_path = mgr.data_layout.primary_block_dir(hash);
let directory = path.clone(); let directory = tgt_path.clone();
path.push(hex::encode(hash)); tgt_path.push(hex::encode(hash));
if compressed {
tgt_path.set_extension("zst");
}
fs::create_dir_all(&directory).await?; fs::create_dir_all(&directory).await?;
let to_delete = match (mgr.find_block(hash).await, compressed) { let to_delete = match (mgr.find_block(hash).await, compressed) {
(Some(DataBlockPath::Compressed(_)), _) => return Ok(()), // If the block is stored in the wrong directory,
(Some(DataBlockPath::Plain(_)), false) => return Ok(()), // write it again at the correct path and delete the old path
(Some(DataBlockPath::Plain(p)), false) if p != tgt_path => Some(p),
(Some(DataBlockPath::Compressed(p)), true) if p != tgt_path => Some(p),
// If the block is already stored not compressed but we have a compressed
// copy, write the compressed copy and delete the uncompressed one
(Some(DataBlockPath::Plain(plain_path)), true) => Some(plain_path), (Some(DataBlockPath::Plain(plain_path)), true) => Some(plain_path),
(None, compressed) => {
if compressed { // If the block is already stored compressed,
path.set_extension("zst"); // keep the stored copy, we have nothing to do
} (Some(DataBlockPath::Compressed(_)), _) => return Ok(()),
None
} // If the block is already stored not compressed,
// and we don't have a compressed copy either,
// keep the stored copy, we have nothing to do
(Some(DataBlockPath::Plain(_)), false) => return Ok(()),
// If the block isn't stored already, just store what is given to us
(None, _) => None,
}; };
let mut path_tmp = path.clone(); let mut path_tmp = tgt_path.clone();
let tmp_extension = format!("tmp{}", hex::encode(thread_rng().gen::<[u8; 4]>())); let tmp_extension = format!("tmp{}", hex::encode(thread_rng().gen::<[u8; 4]>()));
path_tmp.set_extension(tmp_extension); path_tmp.set_extension(tmp_extension);
@ -740,7 +754,7 @@ impl BlockManagerLocked {
drop(f); drop(f);
fs::rename(path_tmp, path).await?; fs::rename(path_tmp, tgt_path).await?;
delete_on_drop.cancel(); delete_on_drop.cancel();