rocketmq_remoting/protocol/static_topic/
topic_queue_mapping_info.rs1use std::collections::HashMap;
16
17use cheetah_string::CheetahString;
18use rocketmq_common::common::mix_all::METADATA_SCOPE_GLOBAL;
19use serde::Deserialize;
20use serde::Serialize;
21
22#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
23pub struct TopicQueueMappingInfo {
24 pub topic: Option<CheetahString>,
25 pub scope: Option<CheetahString>,
26 #[serde(rename = "totalQueues")]
27 pub total_queues: i32,
28 pub bname: Option<CheetahString>,
29 pub epoch: i64,
30 pub dirty: bool,
31 #[serde(rename = "currIdMap")]
32 pub curr_id_map: Option<HashMap<i32, i32>>,
33}
34
35impl Default for TopicQueueMappingInfo {
36 fn default() -> Self {
37 Self {
38 topic: None,
39 scope: Some(CheetahString::from_static_str(METADATA_SCOPE_GLOBAL)),
40 total_queues: 0,
41 bname: None,
42 epoch: 0,
43 dirty: false,
44 curr_id_map: None,
45 }
46 }
47}
48
49impl TopicQueueMappingInfo {
50 pub fn new(topic: CheetahString, total_queues: i32, bname: CheetahString, epoch: i64) -> Self {
51 Self {
52 topic: Some(topic),
53 scope: Some(CheetahString::from_static_str(METADATA_SCOPE_GLOBAL)),
54 total_queues,
55 bname: Some(bname),
56 epoch,
57 dirty: false,
58 curr_id_map: None,
59 }
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn topic_queue_mapping_info_default() {
69 let info = TopicQueueMappingInfo::default();
70 assert!(info.topic.is_none());
71 assert_eq!(
72 info.scope.unwrap(),
73 CheetahString::from_static_str(METADATA_SCOPE_GLOBAL)
74 );
75 assert_eq!(info.total_queues, 0);
76 assert!(info.bname.is_none());
77 assert_eq!(info.epoch, 0);
78 assert!(!info.dirty);
79 assert!(info.curr_id_map.is_none());
80 }
81
82 #[test]
83 fn topic_queue_mapping_info_new() {
84 let topic = CheetahString::from("test_topic");
85 let bname = CheetahString::from("broker_a");
86 let info = TopicQueueMappingInfo::new(topic.clone(), 8, bname.clone(), 12345);
87 assert_eq!(info.topic.unwrap(), topic);
88 assert_eq!(
89 info.scope.unwrap(),
90 CheetahString::from_static_str(METADATA_SCOPE_GLOBAL)
91 );
92 assert_eq!(info.total_queues, 8);
93 assert_eq!(info.bname.unwrap(), bname);
94 assert_eq!(info.epoch, 12345);
95 assert!(!info.dirty);
96 assert!(info.curr_id_map.is_none());
97 }
98
99 #[test]
100 fn topic_queue_mapping_info_serde() {
101 let mut curr_id_map = HashMap::new();
102 curr_id_map.insert(1, 100);
103
104 let info = TopicQueueMappingInfo {
105 topic: Some(CheetahString::from("test_topic")),
106 scope: Some(CheetahString::from("custom_scope")),
107 total_queues: 16,
108 bname: Some(CheetahString::from("broker_b")),
109 epoch: 67890,
110 dirty: true,
111 curr_id_map: Some(curr_id_map),
112 };
113
114 let json = serde_json::to_string(&info).unwrap();
115 let expected = r#"{"topic":"test_topic","scope":"custom_scope","totalQueues":16,"bname":"broker_b","epoch":67890,"dirty":true,"currIdMap":{"1":100}}"#;
116 assert_eq!(json, expected);
117
118 let deserialized: TopicQueueMappingInfo = serde_json::from_str(&json).unwrap();
119 assert_eq!(info, deserialized);
120 }
121}