Skip to main content

ShardingRouter

Struct ShardingRouter 

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

分片路由器

根据 ShardingStrategy 将 key 路由到对应的 shard。 v0.3.0 起支持 Enum/List/Directory/Composite 等新策略。

Implementations§

Source§

impl ShardingRouter

Source

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

创建路由器(兼容旧 API:传入策略与 shard 列表)

Source

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

构造枚举分片路由器

Source

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

构造列表分片路由器

Source

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

构造目录分片路由器

Source

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

构造复合分片路由器

Source

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

根据 key 路由到对应的 shard

§Errors
Source

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

通过数据对象 + 提取器路由:先从 data 提取 key,再 route(key)

§Errors

提取失败或路由失败时返回对应 ShardingError

Source

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

返回所有 shard(用于广播查询;仅 Hash/Range/Date 有效)

Source

pub fn strategy(&self) -> ShardingStrategy

返回当前策略(克隆)

Source

pub fn shard_count(&self) -> usize

返回 shard 数量

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.