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
impl ShardingRouter
Sourcepub fn new(strategy: ShardingStrategy, shards: Vec<&str>) -> Self
pub fn new(strategy: ShardingStrategy, shards: Vec<&str>) -> Self
Create a router (legacy API: takes a strategy and a shard list).
Sourcepub fn new_enum(
mapping: HashMap<String, String>,
default: Option<String>,
) -> Self
pub fn new_enum( mapping: HashMap<String, String>, default: Option<String>, ) -> Self
Construct an enum-sharding router.
Sourcepub fn new_list(
keys: HashSet<String>,
target: String,
default: Option<String>,
) -> Self
pub fn new_list( keys: HashSet<String>, target: String, default: Option<String>, ) -> Self
Construct a list-sharding router.
Sourcepub fn new_directory(table: HashMap<String, String>) -> Self
pub fn new_directory(table: HashMap<String, String>) -> Self
Construct a directory-sharding router.
Sourcepub fn new_composite(
primary: ShardingStrategy,
primary_shards: Vec<String>,
secondary: ShardingStrategy,
secondary_shards: Vec<String>,
) -> Self
pub fn new_composite( primary: ShardingStrategy, primary_shards: Vec<String>, secondary: ShardingStrategy, secondary_shards: Vec<String>, ) -> Self
Construct a composite-sharding router.
Sourcepub fn route(&self, key: &str) -> Result<&str, ShardingError>
pub fn route(&self, key: &str) -> Result<&str, ShardingError>
Route a key to the corresponding shard.
§Errors
- Returns
ShardingError::NoShardsConfiguredwhenshardsis empty under Hash/Range/Date strategies. - Returns
ShardingError::NoMappingForKeywhen Enum/List/Directory strategies miss and no default is set.
Sourcepub fn route_by_data(
&self,
data: &dyn Any,
extractor: &dyn ShardKeyExtractor,
) -> Result<&str, ShardingError>
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.
Sourcepub fn query_all(&self) -> &[String]
pub fn query_all(&self) -> &[String]
Return all shards (for broadcast queries; only valid for Hash/Range/Date).
Sourcepub fn strategy(&self) -> ShardingStrategy
pub fn strategy(&self) -> ShardingStrategy
Return the current strategy (cloned).
Sourcepub fn shard_count(&self) -> usize
pub fn shard_count(&self) -> usize
Return the number of shards.
Auto Trait Implementations§
impl Freeze for ShardingRouter
impl RefUnwindSafe for ShardingRouter
impl Send for ShardingRouter
impl Sync for ShardingRouter
impl Unpin for ShardingRouter
impl UnsafeUnpin for ShardingRouter
impl UnwindSafe for ShardingRouter
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