Skip to main content

mito2/sst/index/
kind.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//! Index identities shared by planning, execution and publication.
16
17use crate::sst::file::IndexType;
18
19/// An index implementation known to the storage engine or supplied by a plugin.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
21pub enum IndexKind {
22    /// Inverted index.
23    Inverted,
24    /// Full-text index.
25    Fulltext,
26    /// Bloom filter index.
27    Bloom,
28    /// An index whose implementation is supplied by the embedding application.
29    Extension(ExtensionIndexKind),
30}
31
32/// Extension algorithm identities; implementations remain outside mito2.
33#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
34pub enum ExtensionIndexKind {
35    /// HyperLogLog distinct-value estimate.
36    Hll,
37    /// KLL rank estimate.
38    Kll,
39    /// Misra–Gries frequency estimate.
40    Mg,
41}
42
43impl ExtensionIndexKind {
44    /// Known extension algorithms checked by operation-level provider validation.
45    pub const ALL: [Self; 3] = [Self::Hll, Self::Kll, Self::Mg];
46
47    /// Column metadata key that requires this algorithm's provider.
48    pub const fn required_metadata_key(self) -> &'static str {
49        match self {
50            Self::Hll => datatypes::schema::HLL_KEY,
51            Self::Kll => datatypes::schema::KLL_KEY,
52            Self::Mg => datatypes::schema::MG_KEY,
53        }
54    }
55
56    /// Stable label used by index diagnostics and metrics.
57    pub const fn as_str(self) -> &'static str {
58        match self {
59            Self::Hll => "hll_index",
60            Self::Kll => "kll_index",
61            Self::Mg => "mg_index",
62        }
63    }
64}
65
66impl IndexKind {
67    /// Stable label used by the existing index metrics.
68    pub const fn as_str(self) -> &'static str {
69        match self {
70            Self::Inverted => "inverted_index",
71            Self::Fulltext => "fulltext_index",
72            Self::Bloom => "bloom_filter_index",
73            Self::Extension(kind) => kind.as_str(),
74        }
75    }
76
77    /// Returns the identity recorded in file and column index metadata.
78    pub fn index_type(self) -> IndexType {
79        match self {
80            Self::Inverted => IndexType::InvertedIndex,
81            Self::Fulltext => IndexType::FulltextIndex,
82            Self::Bloom => IndexType::BloomFilterIndex,
83            Self::Extension(ExtensionIndexKind::Hll) => IndexType::HllIndex,
84            Self::Extension(ExtensionIndexKind::Kll) => IndexType::KllIndex,
85            Self::Extension(ExtensionIndexKind::Mg) => IndexType::MgIndex,
86        }
87    }
88
89    /// Whether this kind is implemented by the storage engine.
90    pub fn is_builtin(self) -> bool {
91        !matches!(self, Self::Extension(_))
92    }
93}