def test_to_replication(self) -> None: # Empty dicts return empty replication properties self.assertEqual(ReplicationProperty(), convert.to_replication({})) # None is returned as none self.assertEqual(None, convert.to_replication(None)) # None is returned as none invalid_input1 = {"invalid": 3, "Rack": 4} with self.assertRaises(KeyError): convert.to_replication(invalid_input1) valid_input = {"node": 3, "Rack": 4} expected = {} expected[LocationScope.RACK] = 4 expected[LocationScope.NODE] = 3 expected = ReplicationProperty(expected) self.assertEqual(expected, convert.to_replication(valid_input))
def normalize_replication( replication: ReplicationProperty) -> ReplicationProperty: """ If we got replication {cluster: 3}, it actually means {cluster: 3, node: 3} """ repl = dict(replication) replication_factor = max(v for v in repl.values()) repl[LocationScope.NODE] = replication_factor return ReplicationProperty(repl)
def get_biggest_scope(replication: ReplicationProperty) -> LocationScope: """ Returns the biggest scope at given replication property """ biggest_scope = LocationScope.NODE for scope in replication.keys(): if scope.value >= biggest_scope.value: biggest_scope = scope return biggest_scope
def to_replication( raw_repl: Optional[Mapping[str, int]]) -> Optional[ReplicationProperty]: """ Converts a dictionary like {'rack': 3, 'node': 2} to the corresponding ReplicationProperty object. {LocationScope.RACK: 3, LocationScope.NODE: 2} """ if raw_repl is None: return None res: Dict[LocationScope, int] = {} for scope, value in raw_repl.items(): normalized_scope = scope.upper() res[LocationScope[normalized_scope]] = value return ReplicationProperty(res)
def to_replication( raw_repl: Optional[Mapping[str, int]]) -> Optional[ReplicationProperty]: """ Converts a dictionary like {'rack': 3, 'node': 2} to the corresponding ReplicationProperty object. {LocationScope.RACK: 3, LocationScope.NODE: 2} """ if raw_repl is None: return None res: Dict[LocationScope, int] = {} for scope, value in raw_repl.items(): normalized_scope = scope.upper() replication_scope = LocationScope.__members__.get(normalized_scope) if replication_scope is None: raise ValueError(f"Unknown location scope '{scope}'") res[replication_scope] = value return ReplicationProperty(res)