pub struct Continuum { /* private fields */ }Expand description
Sorted continuum, ready for dispatch.
§Examples
use dynomite::hashkit::ketama::{Continuum, ServerSpec};
use dynomite::hashkit::DynToken;
let c = Continuum::build(&[
ServerSpec { name: "a".into(), weight: 1 },
ServerSpec { name: "b".into(), weight: 1 },
]).unwrap();
assert!(!c.is_empty());
let _ = c.dispatch(&DynToken::from_u32(123)).unwrap();Implementations§
Source§impl Continuum
impl Continuum
Sourcepub fn build(servers: &[ServerSpec]) -> Result<Self, DynError>
pub fn build(servers: &[ServerSpec]) -> Result<Self, DynError>
Build the continuum for the supplied servers.
§Errors
Returns DynError::Generic when a server’s name + index would
overflow the MAX_HOSTLEN buffer.
§Examples
use dynomite::hashkit::ketama::{Continuum, ServerSpec, POINTS_PER_SERVER};
let c = Continuum::build(&[ServerSpec { name: "s0".into(), weight: 1 }]).unwrap();
assert_eq!(c.len(), POINTS_PER_SERVER as usize);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of continuum points.
§Examples
use dynomite::hashkit::ketama::Continuum;
assert_eq!(Continuum::default().len(), 0);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the continuum is empty.
§Examples
use dynomite::hashkit::ketama::Continuum;
assert!(Continuum::default().is_empty());Sourcepub fn points(&self) -> &[ContinuumPoint]
pub fn points(&self) -> &[ContinuumPoint]
Read-only view of the continuum points, in sorted order.
§Examples
use dynomite::hashkit::ketama::{Continuum, ServerSpec};
let c = Continuum::build(&[ServerSpec { name: "a".into(), weight: 1 }]).unwrap();
let pts = c.points();
assert!(pts.windows(2).all(|w| w[0].token <= w[1].token));Sourcepub fn dispatch(&self, hash: &DynToken) -> Result<usize, DynError>
pub fn dispatch(&self, hash: &DynToken) -> Result<usize, DynError>
Map a hash value to the owning server index.
Walks the continuum with a binary search and wraps around when the requested token sorts after the last point.
§Errors
Returns an error if the continuum is empty.
§Examples
use dynomite::hashkit::ketama::{Continuum, ServerSpec};
use dynomite::hashkit::DynToken;
let c = Continuum::build(&[
ServerSpec { name: "a".into(), weight: 1 },
ServerSpec { name: "b".into(), weight: 1 },
]).unwrap();
let s = c.dispatch(&DynToken::from_u32(0xabcd)).unwrap();
assert!(s < 2);
assert!(Continuum::default().dispatch(&DynToken::from_u32(0)).is_err());Trait Implementations§
Auto Trait Implementations§
impl Freeze for Continuum
impl RefUnwindSafe for Continuum
impl Send for Continuum
impl Sync for Continuum
impl Unpin for Continuum
impl UnsafeUnpin for Continuum
impl UnwindSafe for Continuum
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more