use {
super::{
DefaultHasher,
DefaultReplicationStrategy,
Keyspace,
KeyspaceNode,
KeyspaceResult,
ReplicationStrategy,
},
std::hash::{BuildHasher, BuildHasherDefault},
};
pub struct KeyspaceBuilder<N: KeyspaceNode, H: BuildHasher = BuildHasherDefault<DefaultHasher>>(
Vec<N>,
H,
);
impl<N: KeyspaceNode> KeyspaceBuilder<N> {
pub fn new<I: IntoIterator<Item = N>>(init_nodes: I) -> Self {
Self::with_build_hasher(init_nodes, BuildHasherDefault::default())
}
}
impl<N: KeyspaceNode, H: BuildHasher> KeyspaceBuilder<N, H> {
pub fn with_build_hasher<I>(init_nodes: I, build_hasher: H) -> Self
where
I: IntoIterator<Item = N>,
{
Self(init_nodes.into_iter().collect(), build_hasher)
}
pub fn with_replication_factor<const RF: usize>(
self,
) -> KeyspaceBuilderWithReplicationFactor<N, DefaultReplicationStrategy, RF, H> {
KeyspaceBuilderWithReplicationFactor(self.0, DefaultReplicationStrategy::new(), self.1)
}
pub fn with_replication_strategy<R: ReplicationStrategy<N>>(
self,
replication_strategy: R,
) -> KeyspaceBuilderWithReplicationStrategy<N, R, 3, H> {
KeyspaceBuilderWithReplicationStrategy(self.0, replication_strategy, self.1)
}
pub fn build(self) -> KeyspaceResult<Keyspace<N, DefaultReplicationStrategy, 3, H>> {
Keyspace::with_build_hasher(self.1, self.0, DefaultReplicationStrategy::new())
}
}
pub struct KeyspaceBuilderWithReplicationStrategy<N, R, const RF: usize, H>(Vec<N>, R, H);
impl<N, R, const RF: usize, H> KeyspaceBuilderWithReplicationStrategy<N, R, RF, H>
where
N: KeyspaceNode,
R: ReplicationStrategy<N>,
H: BuildHasher,
{
pub fn with_replication_factor<const CUSTOM_RF: usize>(
self,
) -> KeyspaceBuilderWithReplicationFactor<N, R, CUSTOM_RF, H> {
KeyspaceBuilderWithReplicationFactor(self.0, self.1, self.2)
}
pub fn build(self) -> KeyspaceResult<Keyspace<N, R, RF, H>> {
Keyspace::with_build_hasher(self.2, self.0, self.1)
}
}
pub struct KeyspaceBuilderWithReplicationFactor<N, R, const RF: usize, H>(Vec<N>, R, H);
impl<N, R, const RF: usize, H> KeyspaceBuilderWithReplicationFactor<N, R, RF, H>
where
N: KeyspaceNode,
H: BuildHasher,
{
pub fn with_replication_strategy<CustomR: ReplicationStrategy<N>>(
self,
replication_strategy: CustomR,
) -> KeyspaceBuilderWithReplicationStrategy<N, CustomR, RF, H> {
KeyspaceBuilderWithReplicationStrategy(self.0, replication_strategy, self.2)
}
pub fn build(self) -> KeyspaceResult<Keyspace<N, DefaultReplicationStrategy, RF, H>> {
Keyspace::with_build_hasher(self.2, self.0, DefaultReplicationStrategy::new())
}
}