rocketmq_remoting/protocol/body/
subscription_group_wrapper.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use cheetah_string::CheetahString;
18use dashmap::DashMap;
19use serde::Deserialize;
20use serde::Serialize;
21
22use crate::protocol::subscription::subscription_group_config::SubscriptionGroupConfig;
23use crate::protocol::DataVersion;
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase")]
27pub struct SubscriptionGroupWrapper {
28    pub subscription_group_table: DashMap<CheetahString, SubscriptionGroupConfig>,
29
30    pub data_version: DataVersion,
31}
32
33impl Default for SubscriptionGroupWrapper {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39impl SubscriptionGroupWrapper {
40    pub fn new() -> Self {
41        SubscriptionGroupWrapper {
42            subscription_group_table: DashMap::with_capacity(1024),
43            data_version: DataVersion::default(),
44        }
45    }
46
47    pub fn get_subscription_group_table(&self) -> &DashMap<CheetahString, SubscriptionGroupConfig> {
48        &self.subscription_group_table
49    }
50
51    pub fn set_subscription_group_table(
52        &mut self,
53        table: DashMap<CheetahString, SubscriptionGroupConfig>,
54    ) {
55        self.subscription_group_table = table;
56    }
57
58    pub fn data_version(&self) -> &DataVersion {
59        &self.data_version
60    }
61
62    pub fn set_data_version(&mut self, version: DataVersion) {
63        self.data_version = version;
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70    use crate::protocol::subscription::subscription_group_config::SubscriptionGroupConfig;
71
72    #[test]
73    fn new_creates_wrapper_with_default_values() {
74        let wrapper = SubscriptionGroupWrapper::new();
75
76        assert_eq!(wrapper.subscription_group_table.len(), 0);
77        assert!(wrapper.data_version.timestamp <= DataVersion::default().timestamp);
78    }
79
80    #[test]
81    fn get_subscription_group_table_returns_reference() {
82        let wrapper = SubscriptionGroupWrapper::new();
83        wrapper
84            .subscription_group_table
85            .insert("test_group".into(), SubscriptionGroupConfig::default());
86
87        let table = wrapper.get_subscription_group_table();
88        assert_eq!(table.len(), 1);
89        assert!(table.contains_key("test_group"));
90    }
91}