Skip to main content

ShardingRouter

Struct ShardingRouter 

Source
pub struct ShardingRouter { /* private fields */ }
Expand description

Sharding router.

Routes a key to the corresponding shard according to ShardingStrategy. From v0.3.0 supports new strategies Enum/List/Directory/Composite.

Implementations§

Source§

impl ShardingRouter

Source

pub fn new(strategy: ShardingStrategy, shards: Vec<&str>) -> Self

Create a router (legacy API: takes a strategy and a shard list).

Source

pub fn new_enum( mapping: HashMap<String, String>, default: Option<String>, ) -> Self

Construct an enum-sharding router.

Source

pub fn new_list( keys: HashSet<String>, target: String, default: Option<String>, ) -> Self

Construct a list-sharding router.

Source

pub fn new_directory(table: HashMap<String, String>) -> Self

Construct a directory-sharding router.

Source

pub fn new_composite( primary: ShardingStrategy, primary_shards: Vec<String>, secondary: ShardingStrategy, secondary_shards: Vec<String>, ) -> Self

Construct a composite-sharding router.

Source

pub fn route(&self, key: &str) -> Result<&str, ShardingError>

Route a key to the corresponding shard.

§Errors
Source

pub fn route_by_data( &self, data: &dyn Any, extractor: &dyn ShardKeyExtractor, ) -> Result<&str, ShardingError>

Route via a data object and an extractor: first extract a key from data, then call route(key).

§Errors

Returns the corresponding ShardingError when extraction or routing fails.

Source

pub fn query_all(&self) -> &[String]

Return all shards (for broadcast queries; only valid for Hash/Range/Date).

Source

pub fn strategy(&self) -> ShardingStrategy

Return the current strategy (cloned).

Source

pub fn shard_count(&self) -> usize

Return the number of shards.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.