Skip to main content

CompositeRouter

Struct CompositeRouter 

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

复合分片路由器

两级分片:先按 group_id 路由到 ShardGroup,再在组内按二级 key 做一致性哈希。

§适用场景

  • 跨地域分片:先按地区(cn/us/eu),再按用户 ID 哈希
  • 大租户独享:先按 tenant_id 分组,组内再按业务 key 哈希

Implementations§

Source§

impl CompositeRouter

Source

pub fn new() -> Self

创建空复合路由器

Source

pub fn with_vnodes(self, vnodes: usize) -> Self

设置虚拟节点数(默认 100)

注意:必须在 add_group / with_default_group 之前调用, 否则会清除已构建的环缓存(强制下次 route 时重建)。

Source

pub fn add_group(self, group: ShardGroup) -> Self

添加分片组

Source

pub fn with_default_group(self, group: ShardGroup) -> Self

设置默认分片组

Source

pub fn route( &self, group_id: &str, secondary_key: &str, ) -> Result<String, EnhancedShardingError>

路由:先按 group_id 选组,再按 secondary_key 在组内做一致性哈希

§Errors

当 group_id 无匹配且无默认组时返回 EnhancedShardingError::NoGroupMatch

Source

pub fn group_count(&self) -> usize

返回组数量

Source

pub fn group_ids(&self) -> Vec<String>

列出所有组 ID

Source

pub fn has_default(&self) -> bool

是否有默认组

Trait Implementations§

Source§

impl Debug for CompositeRouter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CompositeRouter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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.