Skip to main content

risingwave_hummock_sdk/
frontend_version.rs

1// Copyright 2024 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
15use std::collections::{HashMap, HashSet};
16
17use risingwave_common::catalog::TableId;
18use risingwave_common::util::epoch::INVALID_EPOCH;
19use risingwave_pb::hummock::{PbHummockVersion, PbHummockVersionDelta, StateTableInfoDelta};
20
21use crate::version::{HummockVersion, HummockVersionDelta, HummockVersionStateTableInfo};
22use crate::{HummockVersionId, INVALID_VERSION_ID};
23
24#[derive(Clone, Debug)]
25pub struct FrontendHummockVersion {
26    pub id: HummockVersionId,
27    pub state_table_info: HummockVersionStateTableInfo,
28}
29
30impl FrontendHummockVersion {
31    pub fn from_version(version: &HummockVersion) -> Self {
32        Self {
33            id: version.id,
34            state_table_info: version.state_table_info.clone(),
35        }
36    }
37
38    pub fn to_protobuf(&self) -> PbHummockVersion {
39        #[expect(deprecated)]
40        PbHummockVersion {
41            id: self.id,
42            levels: Default::default(),
43            max_committed_epoch: INVALID_EPOCH,
44            table_watermarks: Default::default(),
45            table_change_logs: Default::default(),
46            state_table_info: self.state_table_info.info().clone(),
47            vector_indexes: Default::default(),
48        }
49    }
50
51    pub fn from_protobuf(value: PbHummockVersion) -> Self {
52        Self {
53            id: value.id,
54            state_table_info: HummockVersionStateTableInfo::from_protobuf(&value.state_table_info),
55        }
56    }
57
58    pub fn apply_delta(&mut self, delta: FrontendHummockVersionDelta) {
59        if self.id != INVALID_VERSION_ID {
60            assert_eq!(self.id, delta.prev_id);
61        }
62        self.id = delta.id;
63        self.state_table_info
64            .apply_delta(&delta.state_table_info_delta, &delta.removed_table_id);
65    }
66}
67
68pub struct FrontendHummockVersionDelta {
69    pub prev_id: HummockVersionId,
70    pub id: HummockVersionId,
71    pub removed_table_id: HashSet<TableId>,
72    pub state_table_info_delta: HashMap<TableId, StateTableInfoDelta>,
73}
74
75impl FrontendHummockVersionDelta {
76    pub fn from_delta(delta: &HummockVersionDelta) -> Self {
77        Self {
78            prev_id: delta.prev_id,
79            id: delta.id,
80            removed_table_id: delta.removed_table_ids.clone(),
81            state_table_info_delta: delta.state_table_info_delta.clone(),
82        }
83    }
84
85    pub fn to_protobuf(&self) -> PbHummockVersionDelta {
86        #[expect(deprecated)]
87        PbHummockVersionDelta {
88            id: self.id,
89            prev_id: self.prev_id,
90            group_deltas: Default::default(),
91            max_committed_epoch: INVALID_EPOCH,
92            trivial_move: false,
93            new_table_watermarks: Default::default(),
94            removed_table_ids: self.removed_table_id.iter().copied().collect(),
95            change_log_delta: Default::default(),
96            state_table_info_delta: self.state_table_info_delta.clone(),
97            vector_index_delta: Default::default(),
98        }
99    }
100
101    pub fn from_protobuf(delta: PbHummockVersionDelta) -> Self {
102        Self {
103            prev_id: delta.prev_id,
104            id: delta.id,
105            removed_table_id: delta.removed_table_ids.into_iter().collect(),
106            state_table_info_delta: delta
107                .state_table_info_delta
108                .iter()
109                .map(|(table_id, delta)| ((*table_id), *delta))
110                .collect(),
111        }
112    }
113}