Skip to main content

rocketmq_protocol/protocol/static_topic/
topic_queue_mapping_one.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 serde::Deserialize;
16use serde::Serialize;
17
18use crate::protocol::static_topic::logic_queue_mapping_item::LogicQueueMappingItem;
19use crate::protocol::static_topic::topic_queue_mapping_detail::TopicQueueMappingDetail;
20
21#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
22pub struct TopicQueueMappingOne {
23    topic: String, // redundant field
24    bname: String, // identify the hosted broker name
25    global_id: i32,
26    items: Vec<LogicQueueMappingItem>,
27    mapping_detail: TopicQueueMappingDetail,
28}
29
30impl TopicQueueMappingOne {
31    pub fn new(
32        mapping_detail: TopicQueueMappingDetail,
33        topic: String,
34        bname: String,
35        global_id: i32,
36        items: Vec<LogicQueueMappingItem>,
37    ) -> Self {
38        Self {
39            topic,
40            bname,
41            global_id,
42            items,
43            mapping_detail,
44        }
45    }
46
47    pub fn topic(&self) -> &str {
48        &self.topic
49    }
50
51    pub fn bname(&self) -> &str {
52        &self.bname
53    }
54
55    pub fn global_id(&self) -> i32 {
56        self.global_id
57    }
58
59    pub fn items(&self) -> &Vec<LogicQueueMappingItem> {
60        &self.items
61    }
62
63    pub fn mapping_detail(&self) -> &TopicQueueMappingDetail {
64        &self.mapping_detail
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn topic_queue_mapping_one_new_and_getters() {
74        let mapping_detail = TopicQueueMappingDetail::default();
75        let topic = "test_topic".to_string();
76        let bname = "broker_a".to_string();
77        let global_id = 1;
78        let items = vec![LogicQueueMappingItem::default()];
79
80        let mapping_one = TopicQueueMappingOne::new(
81            mapping_detail.clone(),
82            topic.clone(),
83            bname.clone(),
84            global_id,
85            items.clone(),
86        );
87
88        assert_eq!(mapping_one.topic(), topic);
89        assert_eq!(mapping_one.bname(), bname);
90        assert_eq!(mapping_one.global_id(), global_id);
91        assert_eq!(mapping_one.items(), &items);
92        assert_eq!(mapping_one.mapping_detail(), &mapping_detail);
93    }
94
95    #[test]
96    fn topic_queue_mapping_one_serde() {
97        let mapping_detail = TopicQueueMappingDetail::default();
98        let mapping_one = TopicQueueMappingOne::new(
99            mapping_detail,
100            "topic".to_string(),
101            "broker".to_string(),
102            1,
103            vec![LogicQueueMappingItem::default()],
104        );
105
106        let json = serde_json::to_string(&mapping_one).unwrap();
107        let expected = r#"{"topic":"topic","bname":"broker","global_id":1,"items":[{"gen":0,"queueId":0,"bname":null,"logicOffset":0,"startOffset":0,"endOffset":-1,"timeOfStart":-1,"timeOfEnd":-1}],"mapping_detail":{"topic":null,"scope":"__global__","totalQueues":0,"bname":null,"epoch":0,"dirty":false,"currIdMap":null,"hostedQueues":{}}}"#;
108        assert_eq!(json, expected);
109
110        let deserialized = serde_json::from_str::<TopicQueueMappingOne>(&json).unwrap();
111        assert_eq!(deserialized, mapping_one);
112    }
113}