rocketmq_common/common/message/
message_queue.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 */
17
18use std::cmp::Ordering;
19use std::fmt;
20use std::hash::Hash;
21use std::hash::Hasher;
22
23use cheetah_string::CheetahString;
24use serde::Deserialize;
25use serde::Serialize;
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(rename_all = "camelCase")]
29pub struct MessageQueue {
30    topic: CheetahString,
31    broker_name: CheetahString,
32    queue_id: i32,
33}
34
35impl MessageQueue {
36    pub fn new() -> Self {
37        MessageQueue {
38            topic: CheetahString::new(),
39            broker_name: CheetahString::new(),
40            queue_id: 0,
41        }
42    }
43
44    pub fn from_other(other: &MessageQueue) -> Self {
45        MessageQueue {
46            topic: other.topic.clone(),
47            broker_name: other.broker_name.clone(),
48            queue_id: other.queue_id,
49        }
50    }
51
52    pub fn from_parts(
53        topic: impl Into<CheetahString>,
54        broker_name: impl Into<CheetahString>,
55        queue_id: i32,
56    ) -> Self {
57        MessageQueue {
58            topic: topic.into(),
59            broker_name: broker_name.into(),
60            queue_id,
61        }
62    }
63
64    #[inline]
65    pub fn get_topic(&self) -> &str {
66        &self.topic
67    }
68
69    #[inline]
70    pub fn get_topic_cs(&self) -> &CheetahString {
71        &self.topic
72    }
73
74    #[inline]
75    pub fn set_topic(&mut self, topic: CheetahString) {
76        self.topic = topic;
77    }
78
79    #[inline]
80    pub fn get_broker_name(&self) -> &CheetahString {
81        &self.broker_name
82    }
83
84    #[inline]
85    pub fn set_broker_name(&mut self, broker_name: CheetahString) {
86        self.broker_name = broker_name;
87    }
88
89    #[inline]
90    pub fn get_queue_id(&self) -> i32 {
91        self.queue_id
92    }
93
94    #[inline]
95    pub fn set_queue_id(&mut self, queue_id: i32) {
96        self.queue_id = queue_id;
97    }
98}
99
100impl PartialEq for MessageQueue {
101    fn eq(&self, other: &Self) -> bool {
102        self.topic == other.topic
103            && self.broker_name == other.broker_name
104            && self.queue_id == other.queue_id
105    }
106}
107
108impl Eq for MessageQueue {}
109
110impl Hash for MessageQueue {
111    fn hash<H: Hasher>(&self, state: &mut H) {
112        self.topic.hash(state);
113        self.broker_name.hash(state);
114        self.queue_id.hash(state);
115    }
116}
117
118impl Ord for MessageQueue {
119    fn cmp(&self, other: &Self) -> Ordering {
120        match self.topic.cmp(&other.topic) {
121            Ordering::Equal => match self.broker_name.cmp(&other.broker_name) {
122                Ordering::Equal => self.queue_id.cmp(&other.queue_id),
123                other => other,
124            },
125            other => other,
126        }
127    }
128}
129
130impl PartialOrd for MessageQueue {
131    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
132        Some(self.cmp(other))
133    }
134}
135
136impl fmt::Display for MessageQueue {
137    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138        write!(
139            f,
140            "MessageQueue [topic={}, broker_name={}, queue_id={}]",
141            self.topic, self.broker_name, self.queue_id
142        )
143    }
144}
145
146impl Default for MessageQueue {
147    fn default() -> Self {
148        MessageQueue::new()
149    }
150}
151
152#[cfg(test)]
153mod test {
154    use super::*;
155
156    #[test]
157    fn new_message_queue_has_default_values() {
158        let mq = MessageQueue::new();
159        assert_eq!(mq.get_topic(), "");
160        assert_eq!(mq.get_broker_name(), "");
161        assert_eq!(mq.get_queue_id(), 0);
162    }
163
164    #[test]
165    fn from_other_creates_identical_copy() {
166        let mq1 = MessageQueue::from_parts("topic1", "broker1", 1);
167        let mq2 = MessageQueue::from_other(&mq1);
168        assert_eq!(mq1, mq2);
169    }
170
171    #[test]
172    fn from_parts_creates_message_queue_with_given_values() {
173        let mq = MessageQueue::from_parts("topic1", "broker1", 1);
174        assert_eq!(mq.get_topic(), "topic1");
175        assert_eq!(mq.get_broker_name(), "broker1");
176        assert_eq!(mq.get_queue_id(), 1);
177    }
178
179    #[test]
180    fn set_topic_updates_topic() {
181        let mut mq = MessageQueue::new();
182        mq.set_topic(CheetahString::from("new_topic"));
183        assert_eq!(mq.get_topic(), "new_topic");
184    }
185
186    #[test]
187    fn set_broker_name_updates_broker_name() {
188        let mut mq = MessageQueue::new();
189        mq.set_broker_name(CheetahString::from("new_broker"));
190        assert_eq!(mq.get_broker_name(), "new_broker");
191    }
192
193    #[test]
194    fn set_queue_id_updates_queue_id() {
195        let mut mq = MessageQueue::new();
196        mq.set_queue_id(10);
197        assert_eq!(mq.get_queue_id(), 10);
198    }
199
200    #[test]
201    fn message_queue_equality() {
202        let mq1 = MessageQueue::from_parts("topic1", "broker1", 1);
203        let mq2 = MessageQueue::from_parts("topic1", "broker1", 1);
204        assert_eq!(mq1, mq2);
205    }
206
207    #[test]
208    fn message_queue_inequality() {
209        let mq1 = MessageQueue::from_parts("topic1", "broker1", 1);
210        let mq2 = MessageQueue::from_parts("topic2", "broker2", 2);
211        assert_ne!(mq1, mq2);
212    }
213
214    #[test]
215    fn message_queue_ordering() {
216        let mq1 = MessageQueue::from_parts("topic1", "broker1", 1);
217        let mq2 = MessageQueue::from_parts("topic1", "broker1", 2);
218        assert!(mq1 < mq2);
219    }
220
221    #[test]
222    fn message_queue_display_format() {
223        let mq = MessageQueue::from_parts("topic1", "broker1", 1);
224        assert_eq!(
225            format!("{}", mq),
226            "MessageQueue [topic=topic1, broker_name=broker1, queue_id=1]"
227        );
228    }
229}