pub struct RowIdGenerator {
base: SystemTime,
last_timestamp_ms: i64,
vnode_bit: u32,
vnodes: Vec<VirtualNode>,
vnodes_index: u16,
sequence: u16,
}
Expand description
RowIdGenerator
generates unique row ids using snowflake algorithm as following format:
timestamp | vnode & sequence |
---|---|
41 bits | 22 bits |
The vnode part can occupy 10..=15 bits, which is determined by the vnode count. Thus,
the sequence part will occupy 7..=12 bits. See bit_for_vnode
for more details.
Fields§
§base: SystemTime
Specific base timestamp using for generating row ids.
last_timestamp_ms: i64
Last timestamp part of row id, based on base
.
vnode_bit: u32
The number of bits used for vnode.
vnodes: Vec<VirtualNode>
Virtual nodes used by this generator.
vnodes_index: u16
Current index of vnodes
.
sequence: u16
Last sequence part of row id.
Implementations§
source§impl RowIdGenerator
impl RowIdGenerator
sourcepub fn new(
vnodes: impl IntoIterator<Item = VirtualNode>,
vnode_count: usize,
) -> Self
pub fn new( vnodes: impl IntoIterator<Item = VirtualNode>, vnode_count: usize, ) -> Self
Create a new RowIdGenerator
with given virtual nodes and vnode count.
sourcefn sequence_upper_bound(&self) -> u16
fn sequence_upper_bound(&self) -> u16
The upper bound of the sequence part, exclusive.
sourcefn try_update_timestamp(&mut self)
fn try_update_timestamp(&mut self)
Update the timestamp, so that the millisecond part of row id is always increased.
This method will immediately return if the timestamp is increased or there’s remaining sequence for the current millisecond. Otherwise, it will spin loop until the timestamp is increased.
sourcefn next_row_id_in_current_timestamp(&mut self) -> Option<RowId>
fn next_row_id_in_current_timestamp(&mut self) -> Option<RowId>
Generate a new RowId
. Returns None
if the sequence reaches the upper bound of current
timestamp, and try_update_timestamp
should be called to update the timestamp and reset the
sequence. After that, the next call of this method always returns Some
.
sourcefn gen_iter(&mut self) -> impl Iterator<Item = RowId> + '_
fn gen_iter(&mut self) -> impl Iterator<Item = RowId> + '_
Returns an infinite iterator that generates RowId
s.
sourcepub fn next_batch(&mut self, length: usize) -> Vec<RowId>
pub fn next_batch(&mut self, length: usize) -> Vec<RowId>
Generate a sequence of RowId
s. Compared to next
, this method is more efficient as it
only checks the timestamp once before generating the first RowId
, instead of doing that
every RowId
.
This may block for a while if too many IDs are generated in one millisecond.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RowIdGenerator
impl RefUnwindSafe for RowIdGenerator
impl Send for RowIdGenerator
impl Sync for RowIdGenerator
impl Unpin for RowIdGenerator
impl UnwindSafe for RowIdGenerator
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
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
source§impl<M> MetricVecRelabelExt for M
impl<M> MetricVecRelabelExt for M
source§fn relabel(
self,
metric_level: MetricLevel,
relabel_threshold: MetricLevel,
) -> RelabeledMetricVec<M>
fn relabel( self, metric_level: MetricLevel, relabel_threshold: MetricLevel, ) -> RelabeledMetricVec<M>
RelabeledMetricVec::with_metric_level
.source§fn relabel_n(
self,
metric_level: MetricLevel,
relabel_threshold: MetricLevel,
relabel_num: usize,
) -> RelabeledMetricVec<M>
fn relabel_n( self, metric_level: MetricLevel, relabel_threshold: MetricLevel, relabel_num: usize, ) -> RelabeledMetricVec<M>
RelabeledMetricVec::with_metric_level_relabel_n
.source§fn relabel_debug_1(
self,
relabel_threshold: MetricLevel,
) -> RelabeledMetricVec<M>
fn relabel_debug_1( self, relabel_threshold: MetricLevel, ) -> RelabeledMetricVec<M>
RelabeledMetricVec::with_metric_level_relabel_n
with metric_level
set to
MetricLevel::Debug
and relabel_num
set to 1.