Skip to main content

mito2/
memtable.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Memtables are write buffers for regions.
16
17use std::collections::{BTreeMap, HashMap};
18use std::fmt;
19use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
20use std::sync::{Arc, Mutex};
21use std::time::Duration;
22
23pub use bulk::part::EncodedBulkPart;
24use bytes::Bytes;
25use common_time::Timestamp;
26use datatypes::arrow::datatypes::SchemaRef;
27use datatypes::arrow::record_batch::RecordBatch;
28use mito_codec::key_values::KeyValue;
29pub use mito_codec::key_values::KeyValues;
30use mito_codec::row_converter::{PrimaryKeyCodec, build_primary_key_codec};
31use snafu::ensure;
32use store_api::codec::PrimaryKeyEncoding;
33use store_api::metadata::{RegionMetadata, RegionMetadataRef};
34use store_api::storage::{ColumnId, RegionId, SequenceNumber, SequenceRange};
35
36use crate::config::MitoConfig;
37use crate::error::{InvalidRegionOptionsSnafu, Result, UnsupportedOperationSnafu};
38use crate::flush::WriteBufferManagerRef;
39use crate::memtable::bulk::{BulkMemtableBuilder, BulkMemtableConfig, CompactDispatcher};
40use crate::memtable::time_series::TimeSeriesMemtableBuilder;
41use crate::metrics::WRITE_BUFFER_BYTES;
42use crate::read::Batch;
43use crate::read::batch_adapter::BatchToRecordBatchAdapter;
44use crate::read::prune::PruneTimeIterator;
45use crate::read::scan_region::PredicateGroup;
46use crate::region::options::{MemtableOptions, MergeMode, RegionOptions};
47use crate::sst::FormatType;
48use crate::sst::file::FileTimeRange;
49use crate::sst::parquet::SstInfo;
50use crate::sst::parquet::file_range::PreFilterMode;
51
52mod builder;
53pub mod bulk;
54pub mod simple_bulk_memtable;
55mod stats;
56pub mod time_partition;
57pub mod time_series;
58pub(crate) mod version;
59
60pub use bulk::part::{
61    BulkPart, BulkPartEncoder, BulkPartMeta, UnorderedPart, record_batch_estimated_size,
62    sort_primary_key_record_batch,
63};
64#[cfg(any(test, feature = "test"))]
65pub use time_partition::filter_record_batch;
66
67/// Groups sequence intervals into ordered, disjoint components. A component must
68/// not be split during a partial merge: an excluded interval could contain an
69/// intermediate row version. Bounds may conservatively include removed rows.
70pub(crate) fn group_by_sequence<T>(
71    mut parts: Vec<T>,
72    bounds: impl Fn(&T) -> (SequenceNumber, SequenceNumber),
73) -> Vec<Vec<T>> {
74    parts.sort_by_key(&bounds);
75    let mut groups: Vec<Vec<T>> = Vec::new();
76    let mut group_max = 0;
77    for part in parts {
78        let (min, max) = bounds(&part);
79        match groups.last_mut() {
80            Some(group) if min <= group_max => {
81                group.push(part);
82                group_max = group_max.max(max);
83            }
84            _ => {
85                groups.push(vec![part]);
86                group_max = max;
87            }
88        }
89    }
90    groups
91}
92
93/// Id for memtables.
94///
95/// Should be unique under the same region.
96pub type MemtableId = u32;
97
98/// Options for querying ranges from a memtable.
99#[derive(Clone)]
100pub struct RangesOptions {
101    /// Whether the ranges are being queried for flush.
102    pub for_flush: bool,
103    /// Mode to pre-filter columns in ranges.
104    pub pre_filter_mode: PreFilterMode,
105    /// Predicate to filter the data.
106    pub predicate: PredicateGroup,
107    /// Sequence range to filter the data.
108    pub sequence: Option<SequenceRange>,
109    /// Maximum number of rows readers should produce in one batch.
110    pub batch_size: usize,
111}
112
113impl Default for RangesOptions {
114    fn default() -> Self {
115        Self {
116            for_flush: false,
117            pre_filter_mode: PreFilterMode::All,
118            predicate: PredicateGroup::default(),
119            sequence: None,
120            batch_size: crate::sst::parquet::DEFAULT_READ_BATCH_SIZE,
121        }
122    }
123}
124
125impl RangesOptions {
126    /// Creates a new [RangesOptions] for flushing.
127    pub fn for_flush() -> Self {
128        Self {
129            for_flush: true,
130            pre_filter_mode: PreFilterMode::All,
131            predicate: PredicateGroup::default(),
132            sequence: None,
133            batch_size: crate::sst::parquet::DEFAULT_READ_BATCH_SIZE,
134        }
135    }
136
137    /// Sets the pre-filter mode.
138    #[must_use]
139    pub fn with_pre_filter_mode(mut self, pre_filter_mode: PreFilterMode) -> Self {
140        self.pre_filter_mode = pre_filter_mode;
141        self
142    }
143
144    /// Sets the predicate.
145    #[must_use]
146    pub fn with_predicate(mut self, predicate: PredicateGroup) -> Self {
147        self.predicate = predicate;
148        self
149    }
150
151    /// Sets the sequence range.
152    #[must_use]
153    pub fn with_sequence(mut self, sequence: Option<SequenceRange>) -> Self {
154        self.sequence = sequence;
155        self
156    }
157
158    /// Sets the maximum number of rows readers should produce in one batch.
159    #[must_use]
160    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
161        self.batch_size = batch_size.clamp(1, crate::sst::parquet::DEFAULT_READ_BATCH_SIZE);
162        self
163    }
164}
165
166#[derive(Debug, Default, Clone)]
167pub struct MemtableStats {
168    /// The estimated bytes allocated by this memtable from heap.
169    pub estimated_bytes: usize,
170    /// The inclusive time range that this memtable contains. It is None if
171    /// and only if the memtable is empty.
172    pub time_range: Option<(Timestamp, Timestamp)>,
173    /// Total rows in memtable
174    pub num_rows: usize,
175    /// Total number of ranges in the memtable.
176    pub num_ranges: usize,
177    /// The maximum sequence number in the memtable.
178    pub max_sequence: SequenceNumber,
179    /// Lower bound of the row sequences written to this memtable or range.
180    /// May remain below the actual minimum after deduplication. Zero is conservative
181    /// when a range does not track its minimum; the value is unused for empty memtables.
182    pub min_sequence: SequenceNumber,
183    /// Number of estimated timeseries in memtable.
184    pub series_count: usize,
185}
186
187impl MemtableStats {
188    /// Attaches the time range to the stats.
189    #[cfg(any(test, feature = "test"))]
190    pub fn with_time_range(mut self, time_range: Option<(Timestamp, Timestamp)>) -> Self {
191        self.time_range = time_range;
192        self
193    }
194
195    #[cfg(feature = "test")]
196    pub fn with_max_sequence(mut self, max_sequence: SequenceNumber) -> Self {
197        self.max_sequence = max_sequence;
198        self
199    }
200
201    /// Returns the estimated bytes allocated by this memtable.
202    pub fn bytes_allocated(&self) -> usize {
203        self.estimated_bytes
204    }
205
206    /// Returns the time range of the memtable.
207    pub fn time_range(&self) -> Option<(Timestamp, Timestamp)> {
208        self.time_range
209    }
210
211    /// Returns the num of total rows in memtable.
212    pub fn num_rows(&self) -> usize {
213        self.num_rows
214    }
215
216    /// Returns the number of ranges in the memtable.
217    pub fn num_ranges(&self) -> usize {
218        self.num_ranges
219    }
220
221    /// Returns the maximum sequence number in the memtable.
222    pub fn max_sequence(&self) -> SequenceNumber {
223        self.max_sequence
224    }
225
226    /// Series count in memtable.
227    pub fn series_count(&self) -> usize {
228        self.series_count
229    }
230}
231
232pub type BoxedBatchIterator = Box<dyn Iterator<Item = Result<Batch>> + Send>;
233
234pub type BoxedRecordBatchIterator = Box<dyn Iterator<Item = Result<RecordBatch>> + Send>;
235
236/// Ranges in a memtable.
237#[derive(Default)]
238pub struct MemtableRanges {
239    /// Range IDs and ranges.
240    pub ranges: BTreeMap<usize, MemtableRange>,
241}
242
243impl MemtableRanges {
244    /// Returns the total number of rows across all ranges.
245    pub fn num_rows(&self) -> usize {
246        self.ranges.values().map(|r| r.stats().num_rows()).sum()
247    }
248
249    /// Returns the total series count across all ranges.
250    pub fn series_count(&self) -> usize {
251        self.ranges.values().map(|r| r.stats().series_count()).sum()
252    }
253
254    /// Returns the maximum sequence number across all ranges.
255    pub fn max_sequence(&self) -> SequenceNumber {
256        self.ranges
257            .values()
258            .map(|r| r.stats().max_sequence())
259            .max()
260            .unwrap_or(0)
261    }
262}
263
264impl IterBuilder for MemtableRanges {
265    fn build(&self, _metrics: Option<MemScanMetrics>) -> Result<BoxedBatchIterator> {
266        ensure!(
267            self.ranges.len() == 1,
268            UnsupportedOperationSnafu {
269                err_msg: format!(
270                    "Building an iterator from MemtableRanges expects 1 range, but got {}",
271                    self.ranges.len()
272                ),
273            }
274        );
275
276        self.ranges.values().next().unwrap().build_iter()
277    }
278
279    fn is_record_batch(&self) -> bool {
280        self.ranges.values().all(|range| range.is_record_batch())
281    }
282}
283
284/// In memory write buffer.
285pub trait Memtable: Send + Sync + fmt::Debug {
286    /// Returns the id of this memtable.
287    fn id(&self) -> MemtableId;
288
289    /// Writes key values into the memtable.
290    fn write(&self, kvs: &KeyValues) -> Result<()>;
291
292    /// Writes one key value pair into the memtable.
293    fn write_one(&self, key_value: KeyValue) -> Result<()>;
294
295    /// Writes an encoded batch of into memtable.
296    fn write_bulk(&self, part: crate::memtable::bulk::part::BulkPart) -> Result<()>;
297
298    /// Returns the ranges in the memtable.
299    ///
300    /// The returned map contains the range id and the range after applying the predicate.
301    fn ranges(
302        &self,
303        projection: Option<&[ColumnId]>,
304        options: RangesOptions,
305    ) -> Result<MemtableRanges>;
306
307    /// Returns true if the memtable is empty.
308    fn is_empty(&self) -> bool;
309
310    /// Turns a mutable memtable into an immutable memtable.
311    fn freeze(&self) -> Result<()>;
312
313    /// Returns the [MemtableStats] info of Memtable.
314    fn stats(&self) -> MemtableStats;
315
316    /// Returns a conservative row-sequence lower bound without computing full statistics.
317    /// Returns zero when write statistics are unavailable. Callers must ignore
318    /// empty memtables when using this bound to constrain compaction.
319    fn min_sequence(&self) -> SequenceNumber;
320
321    /// Forks this (immutable) memtable and returns a new mutable memtable with specific memtable `id`.
322    ///
323    /// A region must freeze the memtable before invoking this method.
324    fn fork(&self, id: MemtableId, metadata: &RegionMetadataRef) -> MemtableRef;
325
326    /// Compacts the memtable.
327    ///
328    /// The `for_flush` is true when the flush job calls this method.
329    fn compact(&self, for_flush: bool) -> Result<()> {
330        let _ = for_flush;
331        Ok(())
332    }
333}
334
335pub type MemtableRef = Arc<dyn Memtable>;
336
337/// Builder to build a new [Memtable].
338pub trait MemtableBuilder: Send + Sync + fmt::Debug {
339    /// Builds a new memtable instance.
340    fn build(&self, id: MemtableId, metadata: &RegionMetadataRef) -> MemtableRef;
341
342    /// Returns true if the memtable supports bulk insert and benefits from it.
343    fn use_bulk_insert(&self, metadata: &RegionMetadataRef) -> bool {
344        let _metadata = metadata;
345        false
346    }
347}
348
349pub type MemtableBuilderRef = Arc<dyn MemtableBuilder>;
350
351/// Memtable memory allocation tracker.
352#[derive(Default)]
353pub struct AllocTracker {
354    write_buffer_manager: Option<WriteBufferManagerRef>,
355    /// Bytes allocated by the tracker.
356    bytes_allocated: AtomicUsize,
357    /// Whether allocating is done.
358    is_done_allocating: AtomicBool,
359}
360
361impl fmt::Debug for AllocTracker {
362    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
363        f.debug_struct("AllocTracker")
364            .field("bytes_allocated", &self.bytes_allocated)
365            .field("is_done_allocating", &self.is_done_allocating)
366            .finish()
367    }
368}
369
370impl AllocTracker {
371    /// Returns a new [AllocTracker].
372    pub fn new(write_buffer_manager: Option<WriteBufferManagerRef>) -> AllocTracker {
373        AllocTracker {
374            write_buffer_manager,
375            bytes_allocated: AtomicUsize::new(0),
376            is_done_allocating: AtomicBool::new(false),
377        }
378    }
379
380    /// Tracks `bytes` memory is allocated.
381    pub(crate) fn on_allocation(&self, bytes: usize) {
382        self.bytes_allocated.fetch_add(bytes, Ordering::Relaxed);
383        WRITE_BUFFER_BYTES.add(bytes as i64);
384        if let Some(write_buffer_manager) = &self.write_buffer_manager {
385            write_buffer_manager.reserve_mem(bytes);
386        }
387    }
388
389    /// Marks we have finished allocating memory so we can free it from
390    /// the write buffer's limit.
391    ///
392    /// The region MUST ensure that it calls this method inside the region writer's write lock.
393    pub(crate) fn done_allocating(&self) {
394        if let Some(write_buffer_manager) = &self.write_buffer_manager
395            && self
396                .is_done_allocating
397                .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
398                .is_ok()
399        {
400            write_buffer_manager.schedule_free_mem(self.bytes_allocated.load(Ordering::Relaxed));
401        }
402    }
403
404    /// Returns bytes allocated.
405    pub(crate) fn bytes_allocated(&self) -> usize {
406        self.bytes_allocated.load(Ordering::Relaxed)
407    }
408
409    /// Returns the write buffer manager.
410    pub(crate) fn write_buffer_manager(&self) -> Option<WriteBufferManagerRef> {
411        self.write_buffer_manager.clone()
412    }
413}
414
415impl Drop for AllocTracker {
416    fn drop(&mut self) {
417        if !self.is_done_allocating.load(Ordering::Relaxed) {
418            self.done_allocating();
419        }
420
421        let bytes_allocated = self.bytes_allocated.load(Ordering::Relaxed);
422        WRITE_BUFFER_BYTES.sub(bytes_allocated as i64);
423
424        // Memory tracked by this tracker is freed.
425        if let Some(write_buffer_manager) = &self.write_buffer_manager {
426            write_buffer_manager.free_mem(bytes_allocated);
427        }
428    }
429}
430
431/// Provider of memtable builders for regions.
432#[derive(Clone)]
433pub(crate) struct MemtableBuilderProvider {
434    write_buffer_manager: Option<WriteBufferManagerRef>,
435    config: Arc<MitoConfig>,
436    default_bulk_memtable_config: BulkMemtableConfig,
437    compact_dispatcher: Arc<CompactDispatcher>,
438}
439
440/// Ensures JSON2 columns are not used with [`TimeSeriesMemtable`].
441pub(crate) fn ensure_json2_not_use_time_series_memtable(
442    metadata: &RegionMetadata,
443    options: &RegionOptions,
444) -> Result<()> {
445    if metadata
446        .column_metadatas
447        .iter()
448        .any(|x| x.column_schema.data_type.is_json2())
449    {
450        ensure!(
451            !matches!(&options.memtable, Some(MemtableOptions::TimeSeries)),
452            InvalidRegionOptionsSnafu {
453                reason: "JSON2 columns only support BulkMemtable",
454            }
455        );
456    }
457    Ok(())
458}
459
460impl MemtableBuilderProvider {
461    pub(crate) fn new(
462        write_buffer_manager: Option<WriteBufferManagerRef>,
463        config: Arc<MitoConfig>,
464    ) -> Self {
465        let compact_dispatcher =
466            Arc::new(CompactDispatcher::new(config.max_background_compactions));
467        let default_bulk_memtable_config = BulkMemtableConfig::default_for_write_buffer_size(
468            config.global_write_buffer_size.as_bytes() as usize,
469        );
470
471        Self {
472            write_buffer_manager,
473            config,
474            default_bulk_memtable_config,
475            compact_dispatcher,
476        }
477    }
478
479    /// Parses region options with this provider's default bulk memtable config.
480    pub(crate) fn parse_options(
481        &self,
482        region_id: RegionId,
483        options: &HashMap<String, String>,
484    ) -> Result<RegionOptions> {
485        RegionOptions::try_from_options_with_bulk_config(
486            region_id,
487            options,
488            &self.default_bulk_memtable_config,
489        )
490    }
491
492    pub(crate) fn builder_for_options(&self, options: &RegionOptions) -> MemtableBuilderRef {
493        let dedup = options.need_dedup();
494        let merge_mode = options.merge_mode();
495        let primary_key_encoding = options.primary_key_encoding();
496        let flat_format = options
497            .sst_format
498            .map(|format| format == FormatType::Flat)
499            .unwrap_or(self.config.default_flat_format);
500        if flat_format {
501            if options.memtable.is_some()
502                && !matches!(&options.memtable, Some(MemtableOptions::Bulk(_)))
503            {
504                common_telemetry::info!(
505                    "Overriding memtable config, use BulkMemtable under flat format"
506                );
507            }
508
509            return Arc::new(self.bulk_memtable_builder(dedup, merge_mode, options));
510        }
511
512        if primary_key_encoding == PrimaryKeyEncoding::Sparse {
513            if options.memtable.is_some()
514                && !matches!(&options.memtable, Some(MemtableOptions::Bulk(_)))
515            {
516                common_telemetry::info!(
517                    "Overriding memtable config, use BulkMemtable for sparse primary key encoding"
518                );
519            }
520            return Arc::new(self.bulk_memtable_builder(dedup, merge_mode, options));
521        }
522
523        // The format is not flat.
524        match &options.memtable {
525            Some(MemtableOptions::Bulk(config)) => Arc::new(
526                BulkMemtableBuilder::new(self.write_buffer_manager.clone(), !dedup, merge_mode)
527                    .with_config(config.clone())
528                    .with_row_group_size(options.row_group_size())
529                    .with_float_field_encoding(options.float_field_encoding)
530                    .with_compact_dispatcher(self.compact_dispatcher.clone()),
531            ),
532            Some(MemtableOptions::TimeSeries) => Arc::new(TimeSeriesMemtableBuilder::new(
533                self.write_buffer_manager.clone(),
534                dedup,
535                merge_mode,
536            )),
537            None => self.default_primary_key_memtable_builder(dedup, merge_mode),
538        }
539    }
540
541    fn bulk_memtable_builder(
542        &self,
543        dedup: bool,
544        merge_mode: MergeMode,
545        options: &RegionOptions,
546    ) -> BulkMemtableBuilder {
547        let mut builder = BulkMemtableBuilder::new(
548            self.write_buffer_manager.clone(),
549            !dedup, // append_mode: true if not dedup, false if dedup
550            merge_mode,
551        )
552        .with_config(self.default_bulk_memtable_config.clone())
553        .with_row_group_size(options.row_group_size())
554        .with_float_field_encoding(options.float_field_encoding)
555        .with_compact_dispatcher(self.compact_dispatcher.clone());
556
557        if let Some(MemtableOptions::Bulk(config)) = &options.memtable {
558            builder = builder.with_config(config.clone());
559        }
560
561        builder
562    }
563
564    fn default_primary_key_memtable_builder(
565        &self,
566        dedup: bool,
567        merge_mode: MergeMode,
568    ) -> MemtableBuilderRef {
569        Arc::new(TimeSeriesMemtableBuilder::new(
570            self.write_buffer_manager.clone(),
571            dedup,
572            merge_mode,
573        ))
574    }
575}
576
577/// Metrics for scanning a memtable.
578#[derive(Clone, Default)]
579pub struct MemScanMetrics(Arc<Mutex<MemScanMetricsData>>);
580
581impl MemScanMetrics {
582    /// Merges the metrics.
583    pub(crate) fn merge_inner(&self, inner: &MemScanMetricsData) {
584        let mut metrics = self.0.lock().unwrap();
585        metrics.total_series += inner.total_series;
586        metrics.num_rows += inner.num_rows;
587        metrics.num_batches += inner.num_batches;
588        metrics.scan_cost += inner.scan_cost;
589        metrics.prefilter_cost += inner.prefilter_cost;
590        metrics.prefilter_rows_filtered += inner.prefilter_rows_filtered;
591    }
592
593    /// Gets the metrics data.
594    pub(crate) fn data(&self) -> MemScanMetricsData {
595        self.0.lock().unwrap().clone()
596    }
597}
598
599#[derive(Clone, Default)]
600pub(crate) struct MemScanMetricsData {
601    /// Total series in the memtable.
602    pub(crate) total_series: usize,
603    /// Number of rows read.
604    pub(crate) num_rows: usize,
605    /// Number of batch read.
606    pub(crate) num_batches: usize,
607    /// Duration to scan the memtable.
608    pub(crate) scan_cost: Duration,
609    /// Duration of prefilter in memtable scan.
610    pub(crate) prefilter_cost: Duration,
611    /// Number of rows filtered by prefilter in memtable scan.
612    pub(crate) prefilter_rows_filtered: usize,
613}
614
615/// Encoded range in the memtable.
616pub struct EncodedRange {
617    /// Encoded file data.
618    pub data: Bytes,
619    /// Metadata of the encoded range.
620    pub sst_info: SstInfo,
621}
622
623/// Builder to build an iterator to read the range.
624/// The builder should know the projection and the predicate to build the iterator.
625pub trait IterBuilder: Send + Sync {
626    /// Returns the iterator to read the range.
627    fn build(&self, metrics: Option<MemScanMetrics>) -> Result<BoxedBatchIterator>;
628
629    /// Returns whether the iterator is a record batch iterator.
630    fn is_record_batch(&self) -> bool {
631        false
632    }
633
634    /// Returns the record batch iterator to read the range.
635    /// ## Note
636    /// Implementations should ensure the iterator yields data within given time range.
637    fn build_record_batch(
638        &self,
639        time_range: Option<(Timestamp, Timestamp)>,
640        metrics: Option<MemScanMetrics>,
641    ) -> Result<BoxedRecordBatchIterator> {
642        let _metrics = metrics;
643        let _ = time_range;
644        UnsupportedOperationSnafu {
645            err_msg: "Record batch iterator is not supported by this memtable",
646        }
647        .fail()
648    }
649
650    /// Returns a cheap schema hint for record batches yielded by this builder.
651    fn record_batch_schema_hint(&self) -> Option<SchemaRef> {
652        None
653    }
654
655    /// Returns the [EncodedRange] if the range is already encoded into SST.
656    fn encoded_range(&self) -> Option<EncodedRange> {
657        None
658    }
659}
660
661pub type BoxedIterBuilder = Box<dyn IterBuilder>;
662
663/// Computes the column IDs to read based on the projection.
664///
665/// If `projection` is `Some`, returns those column IDs. If `None`, returns all column IDs
666/// from the metadata.
667pub fn read_column_ids_from_projection(
668    metadata: &RegionMetadataRef,
669    projection: Option<&[ColumnId]>,
670) -> Vec<ColumnId> {
671    if let Some(projection) = projection {
672        projection.to_vec()
673    } else {
674        metadata
675            .column_metadatas
676            .iter()
677            .map(|c| c.column_id)
678            .collect()
679    }
680}
681
682/// Context to adapt batch iterators to record batch iterators for flat scan.
683pub struct BatchToRecordBatchContext {
684    metadata: RegionMetadataRef,
685    codec: Arc<dyn PrimaryKeyCodec>,
686    read_column_ids: Vec<ColumnId>,
687}
688
689impl BatchToRecordBatchContext {
690    /// Creates a new context for adapting batch iterators.
691    pub fn new(metadata: RegionMetadataRef, mut read_column_ids: Vec<ColumnId>) -> Self {
692        if read_column_ids.is_empty() {
693            read_column_ids.push(metadata.time_index_column().column_id);
694        }
695
696        let codec = build_primary_key_codec(&metadata);
697        Self {
698            metadata,
699            codec,
700            read_column_ids,
701        }
702    }
703
704    fn adapt_iter(&self, iter: BoxedBatchIterator) -> BoxedRecordBatchIterator {
705        Box::new(BatchToRecordBatchAdapter::new(
706            iter,
707            self.metadata.clone(),
708            self.codec.clone(),
709            &self.read_column_ids,
710        ))
711    }
712}
713
714/// Context shared by ranges of the same memtable.
715pub struct MemtableRangeContext {
716    /// Id of the memtable.
717    id: MemtableId,
718    /// Iterator builder.
719    builder: BoxedIterBuilder,
720    /// All filters.
721    predicate: PredicateGroup,
722    /// Optional context to adapt batch iterators for flat scans.
723    batch_to_record_batch: Option<Arc<BatchToRecordBatchContext>>,
724}
725
726pub type MemtableRangeContextRef = Arc<MemtableRangeContext>;
727
728impl MemtableRangeContext {
729    /// Creates a new [MemtableRangeContext].
730    pub fn new(id: MemtableId, builder: BoxedIterBuilder, predicate: PredicateGroup) -> Self {
731        Self::new_with_batch_to_record_batch(id, builder, predicate, None)
732    }
733
734    /// Creates a new [MemtableRangeContext] with optional adapter context.
735    pub fn new_with_batch_to_record_batch(
736        id: MemtableId,
737        builder: BoxedIterBuilder,
738        predicate: PredicateGroup,
739        batch_to_record_batch: Option<Arc<BatchToRecordBatchContext>>,
740    ) -> Self {
741        Self {
742            id,
743            builder,
744            predicate,
745            batch_to_record_batch,
746        }
747    }
748}
749
750/// A range in the memtable.
751#[derive(Clone)]
752pub struct MemtableRange {
753    /// Shared context.
754    context: MemtableRangeContextRef,
755    /// Statistics for this memtable range.
756    stats: MemtableStats,
757}
758
759impl MemtableRange {
760    /// Creates a new range from context and stats.
761    pub fn new(context: MemtableRangeContextRef, stats: MemtableStats) -> Self {
762        Self { context, stats }
763    }
764
765    /// Returns the statistics for this range.
766    pub fn stats(&self) -> &MemtableStats {
767        &self.stats
768    }
769
770    /// Returns the id of the memtable to read.
771    pub fn id(&self) -> MemtableId {
772        self.context.id
773    }
774
775    /// Builds an iterator to read the range.
776    /// Filters the result by the specific time range, this ensures memtable won't return
777    /// rows out of the time range when new rows are inserted.
778    pub fn build_prune_iter(
779        &self,
780        time_range: FileTimeRange,
781        metrics: Option<MemScanMetrics>,
782    ) -> Result<BoxedBatchIterator> {
783        let iter = self.context.builder.build(metrics)?;
784        let time_filters = self.context.predicate.time_filters();
785        Ok(Box::new(PruneTimeIterator::new(
786            iter,
787            time_range,
788            time_filters,
789        )))
790    }
791
792    /// Builds an iterator to read all rows in range.
793    pub fn build_iter(&self) -> Result<BoxedBatchIterator> {
794        self.context.builder.build(None)
795    }
796
797    /// Builds a record batch iterator to read rows in range.
798    ///
799    /// For mutable memtables (adapter path), applies time-range pruning to ensure rows
800    /// outside the time range are filtered, matching the behavior of `build_prune_iter`.
801    pub fn build_record_batch_iter(
802        &self,
803        time_range: Option<FileTimeRange>,
804        metrics: Option<MemScanMetrics>,
805    ) -> Result<BoxedRecordBatchIterator> {
806        if self.context.builder.is_record_batch() {
807            return self.context.builder.build_record_batch(time_range, metrics);
808        }
809
810        if let Some(context) = self.context.batch_to_record_batch.as_ref() {
811            let iter = self.context.builder.build(metrics)?;
812            let iter: BoxedBatchIterator = if let Some(time_range) = time_range {
813                let time_filters = self.context.predicate.time_filters();
814                Box::new(PruneTimeIterator::new(iter, time_range, time_filters))
815            } else {
816                iter
817            };
818            return Ok(context.adapt_iter(iter));
819        }
820
821        UnsupportedOperationSnafu {
822            err_msg: "Record batch iterator is not supported by this memtable",
823        }
824        .fail()
825    }
826
827    /// Returns a cheap schema hint for record batches yielded by this range.
828    pub fn record_batch_schema_hint(&self) -> Option<SchemaRef> {
829        self.context.builder.record_batch_schema_hint()
830    }
831
832    /// Returns whether the iterator is a record batch iterator.
833    pub fn is_record_batch(&self) -> bool {
834        self.context.builder.is_record_batch()
835    }
836
837    pub fn num_rows(&self) -> usize {
838        self.stats.num_rows
839    }
840
841    /// Returns the encoded range if available.
842    pub fn encoded(&self) -> Option<EncodedRange> {
843        self.context.builder.encoded_range()
844    }
845}
846
847#[cfg(test)]
848mod tests {
849    use std::collections::HashMap;
850    use std::sync::Arc;
851
852    use common_base::readable_size::ReadableSize;
853    use common_error::ext::WhateverResult;
854    use datatypes::prelude::ConcreteDataType;
855    use datatypes::types::json_type::{JsonNativeType, JsonObjectType};
856    use store_api::metadata::RegionMetadataBuilder;
857    use store_api::storage::RegionId;
858
859    use super::*;
860    use crate::flush::{WriteBufferManager, WriteBufferManagerImpl};
861    use crate::memtable::bulk::BulkMemtableConfig;
862    use crate::test_util::sst_util::sst_region_metadata;
863
864    #[test]
865    fn test_alloc_tracker_without_manager() {
866        let tracker = AllocTracker::new(None);
867        assert_eq!(0, tracker.bytes_allocated());
868        tracker.on_allocation(100);
869        assert_eq!(100, tracker.bytes_allocated());
870        tracker.on_allocation(200);
871        assert_eq!(300, tracker.bytes_allocated());
872
873        tracker.done_allocating();
874        assert_eq!(300, tracker.bytes_allocated());
875    }
876
877    #[test]
878    fn test_alloc_tracker_with_manager() {
879        let manager = Arc::new(WriteBufferManagerImpl::new(1000));
880        {
881            let tracker = AllocTracker::new(Some(manager.clone() as WriteBufferManagerRef));
882
883            tracker.on_allocation(100);
884            assert_eq!(100, tracker.bytes_allocated());
885            assert_eq!(100, manager.memory_usage());
886            assert_eq!(100, manager.mutable_usage());
887
888            for _ in 0..2 {
889                // Done allocating won't free the same memory multiple times.
890                tracker.done_allocating();
891                assert_eq!(100, manager.memory_usage());
892                assert_eq!(0, manager.mutable_usage());
893            }
894        }
895
896        assert_eq!(0, manager.memory_usage());
897        assert_eq!(0, manager.mutable_usage());
898    }
899
900    #[test]
901    fn test_alloc_tracker_without_done_allocating() {
902        let manager = Arc::new(WriteBufferManagerImpl::new(1000));
903        {
904            let tracker = AllocTracker::new(Some(manager.clone() as WriteBufferManagerRef));
905
906            tracker.on_allocation(100);
907            assert_eq!(100, tracker.bytes_allocated());
908            assert_eq!(100, manager.memory_usage());
909            assert_eq!(100, manager.mutable_usage());
910        }
911
912        assert_eq!(0, manager.memory_usage());
913        assert_eq!(0, manager.mutable_usage());
914    }
915
916    #[test]
917    fn test_forced_bulk_memtable_preserves_bulk_config() {
918        let provider = MemtableBuilderProvider::new(None, Arc::new(MitoConfig::default()));
919        let config = BulkMemtableConfig {
920            merge_threshold: 7,
921            encode_row_threshold: 11,
922            encode_bytes_threshold: 13,
923            max_merge_groups: 17,
924        };
925        let options = RegionOptions {
926            memtable: Some(MemtableOptions::Bulk(config.clone())),
927            primary_key_encoding: Some(PrimaryKeyEncoding::Sparse),
928            ..Default::default()
929        };
930
931        let builder =
932            provider.bulk_memtable_builder(options.need_dedup(), options.merge_mode(), &options);
933
934        assert_eq!(&config, builder.config());
935    }
936
937    #[test]
938    fn test_provider_uses_adaptive_config_for_implicit_bulk_builder() {
939        let config = MitoConfig {
940            global_write_buffer_size: ReadableSize::gb(8),
941            ..Default::default()
942        };
943        let provider = MemtableBuilderProvider::new(None, Arc::new(config));
944        let options = RegionOptions::default();
945
946        let builder =
947            provider.bulk_memtable_builder(options.need_dedup(), options.merge_mode(), &options);
948
949        assert_eq!(256 * 1024 * 1024, builder.config().encode_bytes_threshold);
950    }
951
952    #[test]
953    fn test_provider_parses_bulk_memtable_with_adaptive_config() {
954        let config = MitoConfig {
955            global_write_buffer_size: ReadableSize::gb(8),
956            ..Default::default()
957        };
958        let provider = MemtableBuilderProvider::new(None, Arc::new(config));
959        let options = HashMap::from([("memtable.type".to_string(), "bulk".to_string())]);
960
961        let options = provider
962            .parse_options(RegionId::new(0, 0), &options)
963            .unwrap();
964
965        let Some(MemtableOptions::Bulk(config)) = options.memtable else {
966            panic!("expected bulk memtable options");
967        };
968        assert_eq!(256 * 1024 * 1024, config.encode_bytes_threshold);
969    }
970
971    #[test]
972    fn test_provider_preserves_explicit_bulk_encode_bytes_threshold() {
973        let config = MitoConfig {
974            global_write_buffer_size: ReadableSize::gb(8),
975            ..Default::default()
976        };
977        let provider = MemtableBuilderProvider::new(None, Arc::new(config));
978        let options = HashMap::from([
979            ("memtable.type".to_string(), "bulk".to_string()),
980            (
981                "memtable.bulk.encode_bytes_threshold".to_string(),
982                "13".to_string(),
983            ),
984        ]);
985
986        let options = provider
987            .parse_options(RegionId::new(0, 0), &options)
988            .unwrap();
989
990        let Some(MemtableOptions::Bulk(config)) = options.memtable else {
991            panic!("expected bulk memtable options");
992        };
993        assert_eq!(13, config.encode_bytes_threshold);
994    }
995
996    #[test]
997    fn test_json2_requires_bulk_memtable() -> WhateverResult<()> {
998        let mut metadata = sst_region_metadata();
999        metadata.column_metadatas[2].column_schema.data_type =
1000            ConcreteDataType::json2(JsonNativeType::Object(JsonObjectType::new()));
1001        let metadata = RegionMetadataBuilder::from_existing(metadata).build()?;
1002        let mut options = RegionOptions {
1003            sst_format: Some(FormatType::PrimaryKey),
1004            memtable: Some(MemtableOptions::TimeSeries),
1005            ..Default::default()
1006        };
1007
1008        let err = ensure_json2_not_use_time_series_memtable(&metadata, &options).unwrap_err();
1009        assert!(
1010            err.to_string()
1011                .contains("JSON2 columns only support BulkMemtable")
1012        );
1013
1014        options.memtable = Some(MemtableOptions::Bulk(BulkMemtableConfig::default()));
1015        ensure_json2_not_use_time_series_memtable(&metadata, &options)?;
1016        Ok(())
1017    }
1018}