Skip to main content

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