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:
Yureka
2025-04-20 20:33:34 +02:00
parent e79b485aa8
commit c8e9c45889
4 changed files with 22 additions and 15 deletions
+8 -6
View File
@@ -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> {