Skip to main content

risingwave_common/
constants.rs

1// Copyright 2022 RisingWave Labs
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
15pub mod hummock {
16
17    use bitflags::bitflags;
18    bitflags! {
19
20        #[derive(Default)]
21        pub struct CompactionFilterFlag: u32 {
22            const NONE = 0b00000000;
23            const STATE_CLEAN = 0b00000010;
24            const TTL = 0b00000100;
25        }
26    }
27
28    impl From<CompactionFilterFlag> for u32 {
29        fn from(flag: CompactionFilterFlag) -> Self {
30            flag.bits()
31        }
32    }
33}
34
35pub mod log_store {
36    use crate::types::DataType;
37    use crate::util::sort_util::OrderType;
38
39    pub const EPOCH_COLUMN_NAME: &str = "kv_log_store_epoch";
40    pub const SEQ_ID_COLUMN_NAME: &str = "kv_log_store_seq_id";
41    pub const ROW_OP_COLUMN_NAME: &str = "kv_log_store_row_op";
42    pub const VNODE_COLUMN_NAME: &str = "kv_log_store_vnode";
43
44    pub const EPOCH_COLUMN_TYPE: DataType = DataType::Int64;
45    pub const SEQ_ID_COLUMN_TYPE: DataType = DataType::Int32;
46    pub const ROW_OP_COLUMN_TYPE: DataType = DataType::Int16;
47    pub const VNODE_COLUMN_TYPE: DataType = DataType::Int16;
48    pub const INSERT_OP_CODE: i16 = 1;
49
50    /// Encode an unsigned RisingWave epoch so that its signed integer ordering is preserved in
51    /// the log-store primary key.
52    pub const fn encode_epoch(epoch: u64) -> i64 {
53        epoch as i64 ^ (1i64 << 63)
54    }
55
56    /// Decode an epoch stored by [`encode_epoch`].
57    pub const fn decode_epoch(encoded_epoch: i64) -> u64 {
58        encoded_epoch as u64 ^ (1u64 << 63)
59    }
60
61    pub mod v1 {
62        use std::sync::LazyLock;
63
64        use super::*;
65
66        /// `epoch`, `seq_id`, `row_op`
67        pub const KV_LOG_STORE_PREDEFINED_COLUMNS: [(&str, DataType); 3] = [
68            (EPOCH_COLUMN_NAME, EPOCH_COLUMN_TYPE),
69            (SEQ_ID_COLUMN_NAME, SEQ_ID_COLUMN_TYPE),
70            (ROW_OP_COLUMN_NAME, ROW_OP_COLUMN_TYPE),
71        ];
72
73        pub const EPOCH_COLUMN_INDEX: usize = 0;
74        pub const SEQ_ID_COLUMN_INDEX: usize = 1;
75        pub const ROW_OP_COLUMN_INDEX: usize = 2;
76
77        /// `epoch`, `seq_id`
78        pub static PK_ORDERING: LazyLock<[OrderType; 2]> =
79            LazyLock::new(|| [OrderType::ascending(), OrderType::ascending_nulls_last()]);
80    }
81
82    pub mod v2 {
83        use std::sync::LazyLock;
84
85        use super::*;
86
87        /// `epoch`, `seq_id`, `vnode`, `row_op`
88        pub const KV_LOG_STORE_PREDEFINED_COLUMNS: [(&str, DataType); 4] = [
89            (EPOCH_COLUMN_NAME, EPOCH_COLUMN_TYPE),
90            (SEQ_ID_COLUMN_NAME, SEQ_ID_COLUMN_TYPE),
91            (VNODE_COLUMN_NAME, VNODE_COLUMN_TYPE),
92            (ROW_OP_COLUMN_NAME, ROW_OP_COLUMN_TYPE),
93        ];
94
95        pub const EPOCH_COLUMN_INDEX: usize = 0;
96        pub const SEQ_ID_COLUMN_INDEX: usize = 1;
97        pub const VNODE_COLUMN_INDEX: usize = 2;
98        pub const ROW_OP_COLUMN_INDEX: usize = 3;
99
100        pub static PK_ORDERING: LazyLock<[OrderType; 3]> = LazyLock::new(|| {
101            [
102                OrderType::ascending(),
103                OrderType::ascending_nulls_last(),
104                OrderType::ascending(),
105            ]
106        });
107    }
108}