Refactor a bit

This commit is contained in:
Alex Auvolat
2020-04-12 13:03:55 +02:00
parent 419c70e506
commit 5967c5a5af
4 changed files with 61 additions and 58 deletions
+7 -8
View File
@@ -1,11 +1,11 @@
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::data::*;
use crate::server::Garage;
use crate::table::*;
use crate::background::*;
use crate::block::*;
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct BlockRef {
@@ -35,7 +35,8 @@ impl Entry<Hash, UUID> for BlockRef {
}
pub struct BlockRefTable {
pub garage: RwLock<Option<Arc<Garage>>>,
pub background: Arc<BackgroundRunner>,
pub block_manager: Arc<BlockManager>,
}
#[async_trait]
@@ -45,19 +46,17 @@ impl TableFormat for BlockRefTable {
type E = BlockRef;
async fn updated(&self, old: Option<Self::E>, new: Self::E) {
let garage = self.garage.read().await.as_ref().cloned().unwrap();
let was_before = old.map(|x| !x.deleted).unwrap_or(false);
let is_after = !new.deleted;
if is_after && !was_before {
if let Err(e) = garage.block_manager.block_incref(&new.block) {
if let Err(e) = self.block_manager.block_incref(&new.block) {
eprintln!("Failed to incref block {:?}: {}", &new.block, e);
}
}
if was_before && !is_after {
if let Err(e) = garage
if let Err(e) = self
.block_manager
.block_decref(&new.block, &garage.background)
.block_decref(&new.block, &self.background)
{
eprintln!("Failed to decref block {:?}: {}", &new.block, e);
}