pipeline/etl/transform/
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
15use crate::error::{Error, Result, UnsupportedIndexTypeSnafu};
16
17const INDEX_TIMESTAMP: &str = "timestamp";
18const INDEX_TIMEINDEX: &str = "time";
19const INDEX_TAG: &str = "tag";
20const INDEX_FULLTEXT: &str = "fulltext";
21const INDEX_SKIPPING: &str = "skipping";
22const INDEX_INVERTED: &str = "inverted";
23
24#[derive(Debug, PartialEq, Eq, Clone, Copy)]
25#[allow(clippy::enum_variant_names)]
26pub enum Index {
27    Time,
28    // deprecated, use Inverted instead
29    Tag,
30    Fulltext,
31    Skipping,
32    Inverted,
33}
34
35impl std::fmt::Display for Index {
36    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
37        let index = match self {
38            Index::Time => INDEX_TIMEINDEX,
39            Index::Tag => INDEX_TAG,
40            Index::Fulltext => INDEX_FULLTEXT,
41            Index::Skipping => INDEX_SKIPPING,
42            Index::Inverted => INDEX_INVERTED,
43        };
44
45        write!(f, "{}", index)
46    }
47}
48
49impl TryFrom<String> for Index {
50    type Error = Error;
51
52    fn try_from(value: String) -> Result<Self> {
53        Index::try_from(value.as_str())
54    }
55}
56
57impl TryFrom<&str> for Index {
58    type Error = Error;
59
60    fn try_from(value: &str) -> Result<Self> {
61        match value {
62            INDEX_TIMESTAMP | INDEX_TIMEINDEX => Ok(Index::Time),
63            INDEX_TAG => Ok(Index::Tag),
64            INDEX_FULLTEXT => Ok(Index::Fulltext),
65            INDEX_SKIPPING => Ok(Index::Skipping),
66            INDEX_INVERTED => Ok(Index::Inverted),
67            _ => UnsupportedIndexTypeSnafu { value }.fail(),
68        }
69    }
70}