Skip to main content

Memtable

Struct Memtable 

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

A concurrent in-memory write buffer for time-series data.

Points are stored in a crossbeam_skiplist::SkipMap keyed by MemtableKey (series_key_hash, timestamp). The skip list provides lock-free concurrent inserts and ordered iteration.

§Deduplication

If a point with the same (series_key_hash, timestamp) already exists, the new value overwrites the old one (last-write-wins).

Implementations§

Source§

impl Memtable

Source

pub fn new() -> Self

Create a new empty memtable.

Source

pub fn insert(&self, point: &Point) -> Result<()>

Insert a point into the memtable.

The point is stored keyed by (series_key_hash, timestamp). If a point with the same key already exists, it is overwritten (last-write-wins).

§Errors

Returns MemtableError::Frozen if the memtable has been frozen.

Source

pub fn insert_with_wal_seq(&self, point: &Point, wal_seq: u64) -> Result<()>

Insert a point with an associated WAL sequence number.

Updates the min/max WAL sequence tracking in addition to inserting the point.

§Errors

Returns MemtableError::Frozen if the memtable has been frozen.

Source

pub fn insert_batch(&self, points: &[Point]) -> Result<usize>

Insert a batch of points in one call.

More efficient than calling insert in a loop because: The frozen check is performed once. Strings are pre-interned and the measurement index is updated in a single write-lock acquisition. Size adjustments are accumulated locally and flushed with a single fetch_add.

Returns the number of points successfully inserted.

§Errors

Returns MemtableError::Frozen if the memtable has been frozen.

Source

pub fn insert_batch_with_wal_seq( &self, points: &[Point], wal_seq: u64, ) -> Result<usize>

Insert a batch of points with an associated WAL sequence number.

Uses the batch to set the WAL sequence range, then delegates to insert_batch.

§Errors

Returns MemtableError::Frozen if the memtable has been frozen.

Source

pub fn scan( &self, series_key: &SeriesKey, min_ts: Timestamp, max_ts: Timestamp, ) -> Vec<Point>

Scan points for a specific series within a time range.

Returns points sorted by timestamp for the series identified by series_key. Only points within [min_ts, max_ts] are returned. Uses canonical form comparison (not hash) to avoid false positives from hash collisions.

Source

pub fn scan_range(&self, min_ts: Timestamp, max_ts: Timestamp) -> Vec<Point>

Scan all points across all series within a time range.

Returns all points sorted by (series_key_hash, timestamp).

Note: this is intentionally O(N) — it scans all series. For measurement-scoped queries (the common case), use scan_measurement which leverages the measurement index for O(S × log N) per-series range scans.

Source

pub fn scan_all(&self) -> Vec<Point>

Scan all points in the memtable, sorted by (series_key_hash, timestamp).

Source

pub fn scan_measurement( &self, measurement: &str, min_ts: Timestamp, max_ts: Timestamp, ) -> Vec<Point>

Scan all points belonging to a specific measurement within a time range.

Uses a secondary measurement index to perform targeted range scans per series hash, achieving O(S × log N) complexity instead of O(N) full-table iteration, where S is the number of distinct series for the measurement.

Source

pub fn series_canonical_forms(&self) -> Vec<Arc<str>>

The canonical form of every series this memtable holds.

The cardinality budget is derived from what the database holds, and a memtable holds series no segment does yet — so a repair that consulted only the segments on disk would release a series written a second ago and then count it again on the next write.

series_tags is already keyed by canonical form, so this walks the map rather than scanning the points.

Source

pub fn visit_tags(&self, f: impl FnMut(&str, &str))

Hand every (tag key, tag value) pair this memtable holds to f.

Label discovery has to answer from the memtable as well as from the segments: the tag inverted index is built at flush, so a series written a second ago is in no segment and therefore in no index, and an answer drawn from the index alone omits exactly the labels a fresh database has. A visitor rather than a Vec, because the caller is building a set and the pairs repeat once per series.

Source

pub fn len(&self) -> usize

Returns the number of entries in the memtable.

Source

pub fn is_empty(&self) -> bool

Returns true if the memtable contains no entries.

Source

pub fn interner_bytes(&self) -> usize

Returns the estimated memory usage in bytes.

Heap bytes held by this memtable’s string interner.

Reported separately from estimated_size, which counts rows and index entries: the interner holds the tag keys, tag values and measurement names, so it grows with cardinality rather than with row count and is the term a high-cardinality workload sees first.

Source

pub fn estimated_size(&self) -> usize

Includes the measurement_index DashMap overhead.

Source

pub fn min_wal_seq(&self) -> Option<u64>

Returns the minimum WAL sequence number tracked.

Returns None if no WAL sequences have been recorded.

Source

pub fn max_wal_seq(&self) -> Option<u64>

Returns the maximum WAL sequence number tracked.

Returns None if no WAL sequences have been recorded.

Source

pub fn is_frozen(&self) -> bool

Returns whether this memtable has been frozen.

Source

pub fn freeze(&self)

Freeze this memtable, preventing any further inserts.

After freezing, all insert calls will return MemtableError::Frozen.

Source

pub fn to_points(&self) -> Vec<Point>

Collect all points as a Vec<Point> for flushing to a segment.

Source§

impl Memtable

Source

pub fn to_record_batches(&self) -> Vec<MeasurementBatch>

Every row, as one Arrow batch per measurement, built straight from the skip-list entries.

This is what a flush hands the segment writer. It replaced to_points() on that path: materialising a full memtable as Points — two BTreeMaps each — cost a kilobyte a row and made the flush of an 8 MB memtable peak at ten times that. A batch is the columns themselves, and the writer encodes it without another copy.

Trait Implementations§

Source§

impl Debug for Memtable

Source§

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

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

impl Default for Memtable

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more