Skip to main content

rocketmq_protocol/protocol/static_topic/
topic_remapping_detail_wrapper.rs

1// Copyright 2023 The RocketMQ Rust Authors
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;
16use std::collections::HashSet;
17
18use cheetah_string::CheetahString;
19use serde::Deserialize;
20use serde::Serialize;
21
22use crate::protocol::static_topic::topic_config_and_queue_mapping::TopicConfigAndQueueMapping;
23
24pub const TYPE_CREATE_OR_UPDATE: &str = "CREATE_OR_UPDATE";
25pub const TYPE_REMAPPING: &str = "REMAPPING";
26
27pub const SUFFIX_BEFORE: &str = ".before";
28pub const SUFFIX_AFTER: &str = ".after";
29
30#[derive(Debug, Serialize, Deserialize)]
31#[serde(rename_all = "camelCase")]
32pub struct TopicRemappingDetailWrapper {
33    topic: CheetahString,
34    method: CheetahString,
35    epoch: u64,
36    broker_config_map: HashMap<CheetahString, TopicConfigAndQueueMapping>,
37    broker_to_map_in: HashSet<CheetahString>,
38    broker_to_map_out: HashSet<CheetahString>,
39}
40
41impl TopicRemappingDetailWrapper {
42    pub fn empty() -> Self {
43        Self {
44            topic: CheetahString::new(),
45            method: CheetahString::new(),
46            epoch: 0,
47            broker_config_map: HashMap::new(),
48            broker_to_map_in: HashSet::new(),
49            broker_to_map_out: HashSet::new(),
50        }
51    }
52
53    pub fn new(
54        topic: CheetahString,
55        method: CheetahString,
56        epoch: u64,
57        broker_config_map: HashMap<CheetahString, TopicConfigAndQueueMapping>,
58        broker_to_map_in: HashSet<CheetahString>,
59        broker_to_map_out: HashSet<CheetahString>,
60    ) -> Self {
61        Self {
62            topic,
63            method,
64            epoch,
65            broker_config_map,
66            broker_to_map_in,
67            broker_to_map_out,
68        }
69    }
70
71    pub fn topic(&self) -> &str {
72        &self.topic
73    }
74
75    pub fn method(&self) -> &str {
76        &self.method
77    }
78
79    pub fn get_epoch(&self) -> u64 {
80        self.epoch
81    }
82
83    pub fn broker_config_map(&self) -> &HashMap<CheetahString, TopicConfigAndQueueMapping> {
84        &self.broker_config_map
85    }
86
87    pub fn broker_config_map_mut(&mut self) -> &mut HashMap<CheetahString, TopicConfigAndQueueMapping> {
88        &mut self.broker_config_map
89    }
90
91    pub fn broker_to_map_in(&self) -> &HashSet<CheetahString> {
92        &self.broker_to_map_in
93    }
94
95    pub fn broker_to_map_out(&self) -> &HashSet<CheetahString> {
96        &self.broker_to_map_out
97    }
98
99    pub fn set_broker_config_map(&mut self, broker_config_map: HashMap<CheetahString, TopicConfigAndQueueMapping>) {
100        self.broker_config_map = broker_config_map;
101    }
102
103    pub fn set_broker_to_map_in(&mut self, broker_to_map_in: HashSet<CheetahString>) {
104        self.broker_to_map_in = broker_to_map_in;
105    }
106
107    pub fn set_broker_to_map_out(&mut self, broker_to_map_out: HashSet<CheetahString>) {
108        self.broker_to_map_out = broker_to_map_out;
109    }
110
111    pub fn set_topic(&mut self, topic: CheetahString) {
112        self.topic = topic;
113    }
114
115    pub fn set_method(&mut self, method: CheetahString) {
116        self.method = method;
117    }
118
119    pub fn set_epoch(&mut self, epoch: u64) {
120        self.epoch = epoch;
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127
128    #[test]
129    fn topic_remapping_detail_wrapper_empty() {
130        let wrapper = TopicRemappingDetailWrapper::empty();
131        assert!(wrapper.topic().is_empty());
132        assert!(wrapper.method().is_empty());
133        assert_eq!(wrapper.get_epoch(), 0);
134        assert!(wrapper.broker_config_map().is_empty());
135        assert!(wrapper.broker_to_map_in().is_empty());
136        assert!(wrapper.broker_to_map_out().is_empty());
137    }
138
139    #[test]
140    fn topic_remapping_detail_wrapper_new() {
141        let topic = CheetahString::from("test_topic");
142        let method = CheetahString::from(TYPE_REMAPPING);
143        let epoch = 100u64;
144        let mut broker_config_map = HashMap::new();
145        broker_config_map.insert(CheetahString::from("broker_a"), TopicConfigAndQueueMapping::default());
146        let mut broker_to_map_in = HashSet::new();
147        broker_to_map_in.insert(CheetahString::from("broker_b"));
148        let mut broker_to_map_out = HashSet::new();
149        broker_to_map_out.insert(CheetahString::from("broker_c"));
150
151        let wrapper = TopicRemappingDetailWrapper::new(
152            topic.clone(),
153            method.clone(),
154            epoch,
155            broker_config_map.clone(),
156            broker_to_map_in.clone(),
157            broker_to_map_out.clone(),
158        );
159
160        assert_eq!(wrapper.topic(), topic.as_str());
161        assert_eq!(wrapper.method(), method.as_str());
162        assert_eq!(wrapper.get_epoch(), epoch);
163        assert_eq!(wrapper.broker_config_map(), &broker_config_map);
164        assert_eq!(wrapper.broker_to_map_in(), &broker_to_map_in);
165        assert_eq!(wrapper.broker_to_map_out(), &broker_to_map_out);
166    }
167
168    #[test]
169    fn topic_remapping_detail_wrapper_setters_and_getters() {
170        let mut wrapper = TopicRemappingDetailWrapper::empty();
171
172        wrapper.set_topic(CheetahString::from("new_topic"));
173        assert_eq!(wrapper.topic(), "new_topic");
174
175        wrapper.set_method(CheetahString::from(TYPE_CREATE_OR_UPDATE));
176        assert_eq!(wrapper.method(), TYPE_CREATE_OR_UPDATE);
177
178        wrapper.set_epoch(500);
179        assert_eq!(wrapper.get_epoch(), 500);
180
181        let mut config_map = HashMap::new();
182        config_map.insert(CheetahString::from("b1"), TopicConfigAndQueueMapping::default());
183        wrapper.set_broker_config_map(config_map.clone());
184        assert_eq!(wrapper.broker_config_map(), &config_map);
185
186        let mut map_in = HashSet::new();
187        map_in.insert(CheetahString::from("b2"));
188        wrapper.set_broker_to_map_in(map_in.clone());
189        assert_eq!(wrapper.broker_to_map_in(), &map_in);
190
191        let mut map_out = HashSet::new();
192        map_out.insert(CheetahString::from("b3"));
193        wrapper.set_broker_to_map_out(map_out.clone());
194        assert_eq!(wrapper.broker_to_map_out(), &map_out);
195
196        wrapper
197            .broker_config_map_mut()
198            .insert(CheetahString::from("b4"), TopicConfigAndQueueMapping::default());
199        assert!(wrapper.broker_config_map().contains_key(&CheetahString::from("b4")));
200    }
201}