rocketmq_common/common/message/message_accessor.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;
20
21use crate::common::message::message_single::Message;
22use crate::common::message::MessageConst;
23use crate::common::message::MessageTrait;
24
25pub struct MessageAccessor;
26
27impl MessageAccessor {
28 /// Sets the properties of a message.
29 ///
30 /// # Arguments
31 ///
32 /// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
33 /// * `properties` - A `HashMap` containing the properties to set.
34 #[inline]
35 pub fn set_properties<T: MessageTrait>(
36 msg: &mut T,
37 properties: HashMap<CheetahString, CheetahString>,
38 ) {
39 msg.set_properties(properties);
40 }
41
42 /// Puts a property into a message.
43 ///
44 /// # Arguments
45 ///
46 /// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
47 /// * `name` - The name of the property.
48 /// * `value` - The value of the property.
49 #[inline]
50 pub fn put_property<T: MessageTrait>(msg: &mut T, name: CheetahString, value: CheetahString) {
51 msg.put_property(name, value);
52 }
53
54 /// Clears a property from a message.
55 ///
56 /// # Arguments
57 ///
58 /// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
59 /// * `name` - The name of the property to clear.
60 #[inline]
61 pub fn clear_property<T: MessageTrait>(msg: &mut T, name: &str) {
62 msg.clear_property(name);
63 }
64
65 /// Sets the transfer flag of a message.
66 ///
67 /// # Arguments
68 ///
69 /// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
70 /// * `unit` - The transfer flag value.
71 #[inline]
72 pub fn set_transfer_flag<T: MessageTrait>(msg: &mut T, unit: CheetahString) {
73 msg.put_property(
74 CheetahString::from_static_str(MessageConst::PROPERTY_TRANSFER_FLAG),
75 unit,
76 );
77 }
78
79 /// Gets the transfer flag of a message.
80 ///
81 /// # Arguments
82 ///
83 /// * `msg` - A reference to a message implementing the `MessageTrait`.
84 ///
85 /// # Returns
86 ///
87 /// * `Option<String>` - The transfer flag value if it exists.
88 #[inline]
89 pub fn get_transfer_flag<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
90 msg.get_property(&CheetahString::from_static_str(
91 MessageConst::PROPERTY_TRANSFER_FLAG,
92 ))
93 }
94
95 /// Sets the correction flag of a message.
96 ///
97 /// # Arguments
98 ///
99 /// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
100 /// * `unit` - The correction flag value.
101 #[inline]
102 pub fn set_correction_flag<T: MessageTrait>(msg: &mut T, unit: CheetahString) {
103 msg.put_property(
104 CheetahString::from_static_str(MessageConst::PROPERTY_CORRECTION_FLAG),
105 unit,
106 );
107 }
108
109 /// Gets the correction flag of a message.
110 ///
111 /// # Arguments
112 ///
113 /// * `msg` - A reference to a message implementing the `MessageTrait`.
114 ///
115 /// # Returns
116 ///
117 /// * `Option<String>` - The correction flag value if it exists.
118 #[inline]
119 pub fn get_correction_flag<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
120 msg.get_property(&CheetahString::from_static_str(
121 MessageConst::PROPERTY_CORRECTION_FLAG,
122 ))
123 }
124
125 /// Sets the origin message ID of a message.
126 ///
127 /// # Arguments
128 ///
129 /// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
130 /// * `origin_message_id` - The origin message ID value.
131 #[inline]
132 pub fn set_origin_message_id<T: MessageTrait>(msg: &mut T, origin_message_id: CheetahString) {
133 msg.put_property(
134 CheetahString::from_static_str(MessageConst::PROPERTY_ORIGIN_MESSAGE_ID),
135 origin_message_id,
136 );
137 }
138
139 /// Gets the origin message ID of a message.
140 ///
141 /// # Arguments
142 ///
143 /// * `msg` - A reference to a message implementing the `MessageTrait`.
144 ///
145 /// # Returns
146 ///
147 /// * `Option<String>` - The origin message ID value if it exists.
148 #[inline]
149 pub fn get_origin_message_id<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
150 msg.get_property(&CheetahString::from_static_str(
151 MessageConst::PROPERTY_ORIGIN_MESSAGE_ID,
152 ))
153 }
154
155 /// Sets the MQ2 flag of a message.
156 ///
157 /// # Arguments
158 ///
159 /// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
160 /// * `flag` - The MQ2 flag value.
161 #[inline]
162 pub fn set_mq2_flag<T: MessageTrait>(msg: &mut T, flag: CheetahString) {
163 msg.put_property(
164 CheetahString::from_static_str(MessageConst::PROPERTY_MQ2_FLAG),
165 flag,
166 );
167 }
168
169 /// Gets the MQ2 flag of a message.
170 ///
171 /// # Arguments
172 ///
173 /// * `msg` - A reference to a message implementing the `MessageTrait`.
174 ///
175 /// # Returns
176 ///
177 /// * `Option<String>` - The MQ2 flag value if it exists.
178 #[inline]
179 pub fn get_mq2_flag<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
180 msg.get_property(&CheetahString::from_static_str(
181 MessageConst::PROPERTY_MQ2_FLAG,
182 ))
183 }
184
185 /// Sets the reconsume time of a message.
186 ///
187 /// # Arguments
188 ///
189 /// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
190 /// * `reconsume_times` - The reconsume time value.
191 #[inline]
192 pub fn set_reconsume_time<T: MessageTrait>(msg: &mut T, reconsume_times: CheetahString) {
193 msg.put_property(
194 CheetahString::from_static_str(MessageConst::PROPERTY_RECONSUME_TIME),
195 reconsume_times,
196 );
197 }
198
199 /// Gets the reconsume time of a message.
200 ///
201 /// # Arguments
202 ///
203 /// * `msg` - A reference to a message implementing the `MessageTrait`.
204 ///
205 /// # Returns
206 ///
207 /// * `Option<String>` - The reconsume time value if it exists.
208 #[inline]
209 pub fn get_reconsume_time<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
210 msg.get_property(&CheetahString::from_static_str(
211 MessageConst::PROPERTY_RECONSUME_TIME,
212 ))
213 }
214
215 /// Sets the maximum reconsume times of a message.
216 ///
217 /// # Arguments
218 ///
219 /// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
220 /// * `max_reconsume_times` - The maximum reconsume times value.
221 #[inline]
222 pub fn set_max_reconsume_times<T: MessageTrait>(
223 msg: &mut T,
224 max_reconsume_times: CheetahString,
225 ) {
226 msg.put_property(
227 CheetahString::from_static_str(MessageConst::PROPERTY_MAX_RECONSUME_TIMES),
228 max_reconsume_times,
229 );
230 }
231
232 /// Gets the maximum reconsume times of a message.
233 ///
234 /// # Arguments
235 ///
236 /// * `msg` - A reference to a message implementing the `MessageTrait`.
237 ///
238 /// # Returns
239 ///
240 /// * `Option<String>` - The maximum reconsume times value if it exists.
241 #[inline]
242 pub fn get_max_reconsume_times<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
243 msg.get_property(&CheetahString::from_static_str(
244 MessageConst::PROPERTY_MAX_RECONSUME_TIMES,
245 ))
246 }
247
248 /// Sets the consume start timestamp of a message.
249 ///
250 /// # Arguments
251 ///
252 /// * `msg` - A mutable reference to a message implementing the `MessageTrait`.
253 /// * `property_consume_start_time_stamp` - The consume start timestamp value.
254 #[inline]
255 pub fn set_consume_start_time_stamp<T: MessageTrait>(
256 msg: &mut T,
257 property_consume_start_time_stamp: CheetahString,
258 ) {
259 msg.put_property(
260 CheetahString::from_static_str(MessageConst::PROPERTY_CONSUME_START_TIMESTAMP),
261 property_consume_start_time_stamp,
262 );
263 }
264
265 /// Gets the consume start timestamp of a message.
266 ///
267 /// # Arguments
268 ///
269 /// * `msg` - A reference to a message implementing the `MessageTrait`.
270 ///
271 /// # Returns
272 ///
273 /// * `Option<String>` - The consume start timestamp value if it exists.
274 #[inline]
275 pub fn get_consume_start_time_stamp<T: MessageTrait>(msg: &T) -> Option<CheetahString> {
276 msg.get_property(&CheetahString::from_static_str(
277 MessageConst::PROPERTY_CONSUME_START_TIMESTAMP,
278 ))
279 }
280}