Make BlockManagerLocked fully private again
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Alex 2022-09-02 16:52:22 +02:00
parent 47be652a1f
commit 5e8baa433d
Signed by: lx
GPG key ID: 0E496D15096376BE
2 changed files with 24 additions and 25 deletions

View file

@ -68,7 +68,7 @@ pub struct BlockManager {
compression_level: Option<i32>, compression_level: Option<i32>,
pub(crate) mutation_lock: Mutex<BlockManagerLocked>, mutation_lock: Mutex<BlockManagerLocked>,
pub(crate) rc: BlockRc, pub(crate) rc: BlockRc,
pub resync: BlockResyncManager, pub resync: BlockResyncManager,
@ -84,7 +84,7 @@ pub struct BlockManager {
// This custom struct contains functions that must only be ran // This custom struct contains functions that must only be ran
// when the lock is held. We ensure that it is the case by storing // when the lock is held. We ensure that it is the case by storing
// it INSIDE a Mutex. // it INSIDE a Mutex.
pub(crate) struct BlockManagerLocked(); struct BlockManagerLocked();
impl BlockManager { impl BlockManager {
pub fn new( pub fn new(
@ -331,17 +331,30 @@ impl BlockManager {
Ok(data) Ok(data)
} }
/// Check if this node should have a block, but don't actually have it /// Check if this node has a block and whether it needs it
async fn need_block(&self, hash: &Hash) -> Result<bool, Error> { pub(crate) async fn check_block_status(&self, hash: &Hash) -> Result<BlockStatus, Error> {
let BlockStatus { exists, needed } = self self.mutation_lock
.mutation_lock
.lock() .lock()
.await .await
.check_block_status(hash, self) .check_block_status(hash, self)
.await?; .await
}
/// Check if this node should have a block, but don't actually have it
async fn need_block(&self, hash: &Hash) -> Result<bool, Error> {
let BlockStatus { exists, needed } = self.check_block_status(hash).await?;
Ok(needed.is_nonzero() && !exists) Ok(needed.is_nonzero() && !exists)
} }
/// Delete block if it is not needed anymore
pub(crate) async fn delete_if_unneeded(&self, hash: &Hash) -> Result<(), Error> {
self.mutation_lock
.lock()
.await
.delete_if_unneeded(hash, self)
.await
}
/// Utility: gives the path of the directory in which a block should be found /// Utility: gives the path of the directory in which a block should be found
fn block_dir(&self, hash: &Hash) -> PathBuf { fn block_dir(&self, hash: &Hash) -> PathBuf {
let mut path = self.data_dir.clone(); let mut path = self.data_dir.clone();
@ -392,7 +405,7 @@ pub(crate) struct BlockStatus {
} }
impl BlockManagerLocked { impl BlockManagerLocked {
pub(crate) async fn check_block_status( async fn check_block_status(
&self, &self,
hash: &Hash, hash: &Hash,
mgr: &BlockManager, mgr: &BlockManager,
@ -479,11 +492,7 @@ impl BlockManagerLocked {
Ok(()) Ok(())
} }
pub(crate) async fn delete_if_unneeded( async fn delete_if_unneeded(&self, hash: &Hash, mgr: &BlockManager) -> Result<(), Error> {
&self,
hash: &Hash,
mgr: &BlockManager,
) -> Result<(), Error> {
let BlockStatus { exists, needed } = self.check_block_status(hash, mgr).await?; let BlockStatus { exists, needed } = self.check_block_status(hash, mgr).await?;
if exists && needed.is_deletable() { if exists && needed.is_deletable() {

View file

@ -282,12 +282,7 @@ impl BlockResyncManager {
} }
async fn resync_block(&self, manager: &BlockManager, hash: &Hash) -> Result<(), Error> { async fn resync_block(&self, manager: &BlockManager, hash: &Hash) -> Result<(), Error> {
let BlockStatus { exists, needed } = manager let BlockStatus { exists, needed } = manager.check_block_status(hash).await?;
.mutation_lock
.lock()
.await
.check_block_status(hash, manager)
.await?;
if exists != needed.is_needed() || exists != needed.is_nonzero() { if exists != needed.is_needed() || exists != needed.is_nonzero() {
debug!( debug!(
@ -370,12 +365,7 @@ impl BlockResyncManager {
who.len() who.len()
); );
manager manager.delete_if_unneeded(hash).await?;
.mutation_lock
.lock()
.await
.delete_if_unneeded(hash, manager)
.await?;
manager.rc.clear_deleted_block_rc(hash)?; manager.rc.clear_deleted_block_rc(hash)?;
} }