store_api/storage/consts.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//! Constants.
16
17use crate::storage::descriptors::ColumnId;
18
19// ---------- Reserved column ids ----------------------------------------------
20
21// The reserved column id is too large to be defined as enum value (denied by the
22// `clippy::enum_clike_unportable_variant` lint). So we add this enum as offset
23// in ReservedColumnId to get the final column id.
24enum ReservedColumnType {
25 Version = 0,
26 Sequence,
27 OpType,
28 Tsid,
29 TableId,
30}
31
32/// Column id reserved by the engine.
33///
34/// All reserved column id has MSB (Most Significant Bit) set to 1.
35///
36/// Reserved column includes version column and other internal columns.
37pub struct ReservedColumnId;
38
39impl ReservedColumnId {
40 // Set MSB to 1.
41 const BASE: ColumnId = 1 << (ColumnId::BITS - 1);
42
43 /// Column id for version column.
44 /// Version column is a special reserved column that is enabled by user and
45 /// visible to user.
46 pub const fn version() -> ColumnId {
47 Self::BASE | ReservedColumnType::Version as ColumnId
48 }
49
50 /// Id for `__sequence` column.
51 pub const fn sequence() -> ColumnId {
52 Self::BASE | ReservedColumnType::Sequence as ColumnId
53 }
54
55 /// Id for `__op_type` column.
56 pub const fn op_type() -> ColumnId {
57 Self::BASE | ReservedColumnType::OpType as ColumnId
58 }
59
60 /// Id for storing TSID column.
61 ///
62 /// Used by: metric engine
63 pub const fn tsid() -> ColumnId {
64 Self::BASE | ReservedColumnType::Tsid as ColumnId
65 }
66
67 /// Id for storing logical table id column.
68 ///
69 /// Used by: metric engine
70 pub const fn table_id() -> ColumnId {
71 Self::BASE | ReservedColumnType::TableId as ColumnId
72 }
73
74 /// Test if the column id is reserved.
75 pub fn is_reserved(column_id: ColumnId) -> bool {
76 column_id & Self::BASE != 0
77 }
78}
79
80// -----------------------------------------------------------------------------
81
82// ---------- Names reserved for internal columns and engine -------------------
83
84/// Name for reserved column: sequence
85pub const SEQUENCE_COLUMN_NAME: &str = "__sequence";
86
87/// Name for reserved column: op_type
88pub const OP_TYPE_COLUMN_NAME: &str = "__op_type";
89
90/// Name for reserved column: primary_key
91pub const PRIMARY_KEY_COLUMN_NAME: &str = "__primary_key";
92
93/// Internal Column Name
94static INTERNAL_COLUMN_VEC: [&str; 3] = [
95 SEQUENCE_COLUMN_NAME,
96 OP_TYPE_COLUMN_NAME,
97 PRIMARY_KEY_COLUMN_NAME,
98];
99
100pub fn is_internal_column(name: &str) -> bool {
101 INTERNAL_COLUMN_VEC.contains(&name)
102}
103
104// -----------------------------------------------------------------------------
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 #[test]
111 fn test_reserved_id() {
112 assert_eq!(0x80000000, ReservedColumnId::version());
113 assert_eq!(0x80000001, ReservedColumnId::sequence());
114 assert_eq!(0x80000002, ReservedColumnId::op_type());
115 }
116
117 #[test]
118 fn test_is_internal_column() {
119 // contain internal column names
120 assert!(is_internal_column(SEQUENCE_COLUMN_NAME));
121 assert!(is_internal_column(OP_TYPE_COLUMN_NAME));
122 assert!(is_internal_column(PRIMARY_KEY_COLUMN_NAME));
123
124 // don't contain internal column names
125 assert!(!is_internal_column("my__column"));
126 assert!(!is_internal_column("my__sequence"));
127 assert!(!is_internal_column("my__op_type"));
128 assert!(!is_internal_column("my__primary_key"));
129 }
130}