rocketmq_remoting/protocol/body/topic_info_wrapper/
topic_config_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 std::collections::HashMap;
18
19use cheetah_string::CheetahString;
20use dashmap::DashMap;
21use rocketmq_common::common::config::TopicConfig;
22use rocketmq_rust::ArcMut;
23use serde::Deserialize;
24use serde::Serialize;
25
26use crate::protocol::static_topic::topic_queue_info::TopicQueueMappingInfo;
27use crate::protocol::static_topic::topic_queue_mapping_detail::TopicQueueMappingDetail;
28use crate::protocol::DataVersion;
29
30#[derive(Debug, Clone, Serialize, Deserialize, Default)]
31pub struct TopicConfigAndMappingSerializeWrapper {
32    #[serde(rename = "topicQueueMappingInfoMap")]
33    pub topic_queue_mapping_info_map:
34        DashMap<CheetahString /* topic */, ArcMut<TopicQueueMappingInfo>>,
35
36    #[serde(rename = "topicQueueMappingDetailMap")]
37    pub topic_queue_mapping_detail_map:
38        DashMap<CheetahString /* topic */, ArcMut<TopicQueueMappingDetail>>,
39
40    #[serde(rename = "mappingDataVersion")]
41    pub mapping_data_version: DataVersion,
42
43    #[serde(flatten)]
44    pub topic_config_serialize_wrapper: TopicConfigSerializeWrapper,
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
48pub struct TopicConfigSerializeWrapper {
49    #[serde(rename = "topicConfigTable")]
50    pub topic_config_table: HashMap<CheetahString, TopicConfig>,
51    #[serde(rename = "dataVersion")]
52    pub data_version: DataVersion,
53}
54
55impl TopicConfigAndMappingSerializeWrapper {
56    pub fn topic_queue_mapping_info_map(
57        &self,
58    ) -> &DashMap<CheetahString, ArcMut<TopicQueueMappingInfo>> {
59        &self.topic_queue_mapping_info_map
60    }
61
62    pub fn topic_queue_mapping_detail_map(
63        &self,
64    ) -> &DashMap<CheetahString /* topic */, ArcMut<TopicQueueMappingDetail>> {
65        &self.topic_queue_mapping_detail_map
66    }
67
68    pub fn mapping_data_version(&self) -> &DataVersion {
69        &self.mapping_data_version
70    }
71    pub fn topic_config_serialize_wrapper(&self) -> &TopicConfigSerializeWrapper {
72        &self.topic_config_serialize_wrapper
73    }
74}
75
76impl TopicConfigSerializeWrapper {
77    pub fn topic_config_table(&self) -> &HashMap<CheetahString, TopicConfig> {
78        &self.topic_config_table
79    }
80
81    pub fn data_version(&self) -> &DataVersion {
82        &self.data_version
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn topic_config_and_mapping_serialize_wrapper_default() {
92        let wrapper = TopicConfigAndMappingSerializeWrapper::default();
93        assert!(wrapper.topic_queue_mapping_info_map.is_empty());
94        assert!(wrapper.topic_queue_mapping_detail_map.is_empty());
95        //assert_eq!(wrapper.mapping_data_version, DataVersion::new());
96        assert!(wrapper
97            .topic_config_serialize_wrapper()
98            .topic_config_table()
99            .is_empty());
100        // assert_eq!(
101        //     wrapper.topic_config_serialize_wrapper().data_version(),
102        //     &DataVersion::new()
103        // );
104    }
105
106    #[test]
107    fn topic_config_and_mapping_serialize_wrapper_getters() {
108        let mut wrapper = TopicConfigAndMappingSerializeWrapper::default();
109        let _topic_config = TopicConfig::default();
110        let topic_queue_mapping_info = ArcMut::new(TopicQueueMappingInfo::default());
111        let topic_queue_mapping_detail = ArcMut::<TopicQueueMappingDetail>::default();
112        //let data_version = DataVersion::default();
113
114        let topic_config_serialize_wrapper = TopicConfigSerializeWrapper::default();
115        wrapper.topic_config_serialize_wrapper = topic_config_serialize_wrapper.clone();
116        wrapper
117            .topic_queue_mapping_info_map
118            .insert("test".into(), topic_queue_mapping_info.clone());
119        wrapper
120            .topic_queue_mapping_detail_map
121            .insert("test".into(), topic_queue_mapping_detail.clone());
122
123        //assert_eq!(wrapper.mapping_data_version(), &data_version);
124        assert_eq!(
125            wrapper.topic_config_serialize_wrapper(),
126            &topic_config_serialize_wrapper
127        );
128    }
129}