Skip to main content

mito2/
series_index.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//! Series index construction, search, and maintenance.
16//!
17//! Narthex index artifacts are owned and published by the region manifest.
18
19pub(crate) mod artifact;
20mod artifact_writer;
21pub(crate) mod columnar;
22pub(crate) mod format;
23pub(crate) mod identity;
24pub(crate) mod samples;
25mod task;
26#[cfg(test)]
27mod tests;
28mod writer;
29
30#[cfg(test)]
31pub(crate) use tests::prepare_region_with_metadata;
32
33use datatypes::prelude::ConcreteDataType;
34use store_api::codec::PrimaryKeyEncoding;
35use store_api::metric_engine_consts::{
36    DATA_SCHEMA_TABLE_ID_COLUMN_NAME as TABLE_ID_COLUMN,
37    DATA_SCHEMA_TSID_COLUMN_NAME as TSID_COLUMN,
38};
39use store_api::storage::consts::ReservedColumnId;
40
41pub(crate) use crate::series_index::task::{SeriesIndexTaskState, spawn_series_index_tasks};
42
43pub(crate) const MIN_TS_COLUMN: &str = "__series_min_ts";
44pub(crate) const MAX_TS_COLUMN: &str = "__series_max_ts";
45pub(crate) const ROW_COUNT_COLUMN: &str = "__series_row_count";
46
47/// Identifies one series in a physical metric region.
48#[derive(
49    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
50)]
51pub struct MetricSeriesId {
52    /// Logical table ID inside the physical metric region.
53    pub table_id: u32,
54    /// Time-series ID inside the logical table.
55    pub tsid: u64,
56}
57
58pub(crate) fn is_sparse_metric_metadata(metadata: &store_api::metadata::RegionMetadataRef) -> bool {
59    let valid_prefix = metadata
60        .primary_key
61        .starts_with(&[ReservedColumnId::table_id(), ReservedColumnId::tsid()]);
62    let valid_types = metadata
63        .column_by_id(ReservedColumnId::table_id())
64        .zip(metadata.column_by_id(ReservedColumnId::tsid()))
65        .is_some_and(|(table_id, tsid)| {
66            table_id.column_schema.data_type == ConcreteDataType::uint32_datatype()
67                && tsid.column_schema.data_type == ConcreteDataType::uint64_datatype()
68        });
69
70    metadata.primary_key_encoding == PrimaryKeyEncoding::Sparse && valid_prefix && valid_types
71}