mirror of
https://github.com/deuxfleurs-org/garage.git
synced 2026-09-02 18:28:12 +00:00
refactor: Use ReplicationFactor type in more places
- Remove the replication_factor.replication_factor() in favor of usize::from(replication_factor) to make the conversion more explicit. - Implement Display on ReplicationFactor so that it can be formatted without converting to usize - Use ReplicationFactor in the constructor of LayoutVersion and add a method to get a ReplicationFactor from a LayoutVersion, despite LayoutVersion still storing it as usize internally.
This commit is contained in:
@@ -38,14 +38,10 @@ impl ReplicationFactor {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn replication_factor(&self) -> usize {
|
||||
self.0
|
||||
}
|
||||
|
||||
pub fn read_quorum(&self, consistency_mode: ConsistencyMode) -> usize {
|
||||
match consistency_mode {
|
||||
ConsistencyMode::Dangerous | ConsistencyMode::Degraded => 1,
|
||||
ConsistencyMode::Consistent => self.replication_factor().div_ceil(2),
|
||||
ConsistencyMode::Consistent => usize::from(*self).div_ceil(2),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +49,7 @@ impl ReplicationFactor {
|
||||
match consistency_mode {
|
||||
ConsistencyMode::Dangerous => 1,
|
||||
ConsistencyMode::Degraded | ConsistencyMode::Consistent => {
|
||||
(self.replication_factor() + 1) - self.read_quorum(ConsistencyMode::Consistent)
|
||||
(usize::from(*self) + 1) - self.read_quorum(ConsistencyMode::Consistent)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,6 +61,12 @@ impl std::convert::From<ReplicationFactor> for usize {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ReplicationFactor {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_replication_mode(
|
||||
config: &Config,
|
||||
) -> Result<(ReplicationFactor, ConsistencyMode), Error> {
|
||||
|
||||
Reference in New Issue
Block a user