Skip to main content

rocketmq_model/
message.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/// Identifies a queue within a broker for a topic.
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase")]
27pub struct MessageQueue {
28    topic: CheetahString,
29    broker_name: CheetahString,
30    queue_id: i32,
31}
32
33impl MessageQueue {
34    /// Creates an empty queue identifier.
35    pub fn new() -> Self {
36        Self {
37            topic: CheetahString::new(),
38            broker_name: CheetahString::new(),
39            queue_id: 0,
40        }
41    }
42
43    /// Copies an existing queue identifier.
44    pub fn from_other(other: &Self) -> Self {
45        other.clone()
46    }
47
48    /// Creates a queue identifier from its wire-level parts.
49    pub fn from_parts(topic: impl Into<CheetahString>, broker_name: impl Into<CheetahString>, queue_id: i32) -> Self {
50        Self {
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 Default for MessageQueue {
94    fn default() -> Self {
95        Self::new()
96    }
97}
98
99impl PartialEq for MessageQueue {
100    fn eq(&self, other: &Self) -> bool {
101        self.topic == other.topic && self.broker_name == other.broker_name && self.queue_id == other.queue_id
102    }
103}
104
105impl Eq for MessageQueue {}
106
107impl Hash for MessageQueue {
108    fn hash<H: Hasher>(&self, state: &mut H) {
109        self.topic.hash(state);
110        self.broker_name.hash(state);
111        self.queue_id.hash(state);
112    }
113}
114
115impl Ord for MessageQueue {
116    fn cmp(&self, other: &Self) -> Ordering {
117        self.topic
118            .cmp(&other.topic)
119            .then_with(|| self.broker_name.cmp(&other.broker_name))
120            .then_with(|| self.queue_id.cmp(&other.queue_id))
121    }
122}
123
124impl PartialOrd for MessageQueue {
125    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
126        Some(self.cmp(other))
127    }
128}
129
130impl fmt::Display for MessageQueue {
131    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132        write!(
133            f,
134            "MessageQueue [topic={}, brokerName={}, queueId={}]",
135            self.topic, self.broker_name, self.queue_id
136        )
137    }
138}