rocketmq-model 1.0.0

Runtime-neutral RocketMQ domain models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashMap;
use std::fmt;
use std::fmt::Display;
use std::net::SocketAddr;

use bytes::Bytes;
use cheetah_string::CheetahString;

use crate::common::message::message_client_id_setter::MessageClientIDSetter;
use crate::common::message::message_single::Message;
use crate::common::message::routing_context::RoutingContext;
use crate::common::message::storage_metadata::StorageMetadata;
use crate::common::message::MessageTrait;

/// Complete message envelope (stored message)
///
/// Represents a message that has been received and stored by the Broker, containing complete
/// lifecycle information.
///
/// # Use Cases
///
/// - Consumer receiving messages
/// - Message query and tracking
/// - Message passing between Broker internal modules
#[derive(Clone, Debug)]
pub struct MessageEnvelope {
    /// Message content
    message: Message,

    /// Routing context (network layer)
    routing: RoutingContext,

    /// Storage metadata (persistence layer)
    storage: StorageMetadata,

    /// Message ID (calculated from store_host + commit_log_offset)
    msg_id: CheetahString,

    /// Message body CRC checksum
    body_crc: u32,

    /// Reconsume times
    reconsume_times: i32,

    /// Prepared transaction offset (only valid for transaction messages)
    prepared_transaction_offset: i64,
}

impl MessageEnvelope {
    /// Creates a new message envelope
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        message: Message,
        routing: RoutingContext,
        storage: StorageMetadata,
        msg_id: CheetahString,
        body_crc: u32,
        reconsume_times: i32,
        prepared_transaction_offset: i64,
    ) -> Self {
        Self {
            message,
            routing,
            storage,
            msg_id,
            body_crc,
            reconsume_times,
            prepared_transaction_offset,
        }
    }

    // ===== Message Access =====

    /// Gets the message reference
    #[inline]
    pub fn message(&self) -> &Message {
        &self.message
    }

    /// Gets the mutable message reference
    #[inline]
    pub fn message_mut(&mut self) -> &mut Message {
        &mut self.message
    }

    /// Gets the message topic
    #[inline]
    pub fn topic(&self) -> &CheetahString {
        self.message.topic()
    }

    /// Gets the message body
    #[inline]
    pub fn body(&self) -> Option<Bytes> {
        self.message.body()
    }

    /// Gets the message properties
    #[inline]
    pub fn properties(&self) -> &HashMap<CheetahString, CheetahString> {
        self.message.properties().as_map()
    }

    /// Gets the message tags
    #[inline]
    pub fn tags(&self) -> Option<CheetahString> {
        self.message.get_tags()
    }

    /// Gets the message keys
    #[inline]
    pub fn keys(&self) -> Option<Vec<String>> {
        self.message.keys()
    }

    /// Gets the message flag
    #[inline]
    pub fn flag(&self) -> i32 {
        self.message.flag()
    }

    // ===== Routing Access =====

    /// Gets the routing context
    #[inline]
    pub fn routing(&self) -> &RoutingContext {
        &self.routing
    }

    /// Gets the message sender's client address
    #[inline]
    pub fn born_host(&self) -> SocketAddr {
        self.routing.born_host()
    }

    /// Gets the message creation timestamp
    #[inline]
    pub fn born_timestamp(&self) -> i64 {
        self.routing.born_timestamp()
    }

    /// Gets the system flag bits
    #[inline]
    pub fn sys_flag(&self) -> i32 {
        self.routing.sys_flag()
    }

    /// Gets the born host bytes
    #[inline]
    pub fn born_host_bytes(&self) -> Bytes {
        self.routing.born_host_bytes()
    }

    // ===== Storage Access =====

    /// Gets the storage metadata
    #[inline]
    pub fn storage(&self) -> &StorageMetadata {
        &self.storage
    }

    /// Gets the broker name
    #[inline]
    pub fn broker_name(&self) -> &str {
        self.storage.broker_name()
    }

    /// Gets the queue ID
    #[inline]
    pub fn queue_id(&self) -> i32 {
        self.storage.queue_id()
    }

    /// Gets the queue logical offset
    #[inline]
    pub fn queue_offset(&self) -> i64 {
        self.storage.queue_offset()
    }

    /// Gets the CommitLog physical offset
    #[inline]
    pub fn commit_log_offset(&self) -> i64 {
        self.storage.commit_log_offset()
    }

    /// Gets the storage timestamp
    #[inline]
    pub fn store_timestamp(&self) -> i64 {
        self.storage.store_timestamp()
    }

    /// Gets the storage host address
    #[inline]
    pub fn store_host(&self) -> SocketAddr {
        self.storage.store_host()
    }

    /// Gets the storage size in bytes
    #[inline]
    pub fn store_size(&self) -> i32 {
        self.storage.store_size()
    }

    /// Gets the store host bytes
    #[inline]
    pub fn store_host_bytes(&self) -> Bytes {
        self.storage.store_host_bytes()
    }

    // ===== Other Metadata Access =====

    /// Gets the message ID (offset-based)
    #[inline]
    pub fn msg_id(&self) -> &CheetahString {
        &self.msg_id
    }

    /// Gets the client message ID (UNIQ_ID preferred, otherwise returns offset msg_id)
    ///
    /// This is the client-side message ID, preferring UNIQ_ID (globally unique)
    pub fn client_msg_id(&self) -> CheetahString {
        MessageClientIDSetter::get_uniq_id(self).unwrap_or_else(|| self.msg_id.clone())
    }

    /// Gets the message body CRC
    #[inline]
    pub fn body_crc(&self) -> u32 {
        self.body_crc
    }

    /// Gets the reconsume times
    #[inline]
    pub fn reconsume_times(&self) -> i32 {
        self.reconsume_times
    }

    /// Gets the prepared transaction offset
    #[inline]
    pub fn prepared_transaction_offset(&self) -> i64 {
        self.prepared_transaction_offset
    }
}

impl Default for MessageEnvelope {
    fn default() -> Self {
        Self {
            message: Message::default(),
            routing: RoutingContext::default(),
            storage: StorageMetadata::default(),
            msg_id: CheetahString::new(),
            body_crc: 0,
            reconsume_times: 0,
            prepared_transaction_offset: 0,
        }
    }
}

impl Display for MessageEnvelope {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "MessageEnvelope {{ topic: {}, queue_id: {}, queue_offset: {}, commit_log_offset: {}, msg_id: {}, \
             reconsume_times: {} }}",
            self.topic(),
            self.queue_id(),
            self.queue_offset(),
            self.commit_log_offset(),
            self.msg_id,
            self.reconsume_times
        )
    }
}

// Implement MessageTrait for MessageEnvelope
impl crate::common::message::MessageTrait for MessageEnvelope {
    fn put_property(&mut self, key: CheetahString, value: CheetahString) {
        self.message.put_property(key, value);
    }

    fn clear_property(&mut self, name: &str) {
        self.message.clear_property(name);
    }

    fn property(&self, name: &CheetahString) -> Option<CheetahString> {
        self.message.get_property(name)
    }

    fn property_ref(&self, name: &CheetahString) -> Option<&CheetahString> {
        self.message.property_ref(name)
    }

    fn topic(&self) -> &CheetahString {
        self.topic()
    }

    fn set_topic(&mut self, topic: CheetahString) {
        self.message.set_topic(topic);
    }

    fn get_flag(&self) -> i32 {
        self.flag()
    }

    fn set_flag(&mut self, flag: i32) {
        self.message.set_flag(flag);
    }

    fn get_body(&self) -> Option<&Bytes> {
        self.message.get_body()
    }

    fn set_body(&mut self, body: Bytes) {
        self.message.set_body(Some(body));
    }

    fn get_properties(&self) -> &HashMap<CheetahString, CheetahString> {
        self.properties()
    }

    fn set_properties(&mut self, properties: HashMap<CheetahString, CheetahString>) {
        self.message.set_properties(properties);
    }

    fn transaction_id(&self) -> Option<&CheetahString> {
        MessageTrait::transaction_id(&self.message)
    }

    fn set_transaction_id(&mut self, transaction_id: CheetahString) {
        self.message.set_transaction_id(transaction_id);
    }

    fn get_compressed_body_mut(&mut self) -> Option<&mut Bytes> {
        self.message.get_compressed_body_mut()
    }

    fn get_compressed_body(&self) -> Option<&Bytes> {
        self.message.get_compressed_body()
    }

    fn set_compressed_body_mut(&mut self, compressed_body: Bytes) {
        self.message.set_compressed_body_mut(compressed_body);
    }

    fn take_body(&mut self) -> Option<Bytes> {
        self.message.take_body()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_envelope() -> MessageEnvelope {
        let message = Message::default();
        let routing = RoutingContext::default();
        let storage = StorageMetadata::default();

        MessageEnvelope::new(
            message,
            routing,
            storage,
            CheetahString::from_static_str("test-msg-id"),
            12345,
            0,
            0,
        )
    }

    #[test]
    fn test_envelope_creation() {
        let envelope = create_test_envelope();
        assert_eq!(envelope.msg_id().as_str(), "test-msg-id");
        assert_eq!(envelope.body_crc(), 12345);
        assert_eq!(envelope.reconsume_times(), 0);
    }

    #[test]
    fn test_message_access() {
        let envelope = create_test_envelope();
        let _message = envelope.message();
        let _topic = envelope.topic();
        let _body = envelope.body();
        let _properties = envelope.properties();
    }

    #[test]
    fn test_routing_access() {
        let envelope = create_test_envelope();
        let _born_host = envelope.born_host();
        let _born_timestamp = envelope.born_timestamp();
        let _sys_flag = envelope.sys_flag();
    }

    #[test]
    fn test_storage_access() {
        let envelope = create_test_envelope();
        let _broker_name = envelope.broker_name();
        let _queue_id = envelope.queue_id();
        let _queue_offset = envelope.queue_offset();
        let _commit_log_offset = envelope.commit_log_offset();
    }

    #[test]
    fn test_display() {
        let envelope = create_test_envelope();
        let display_str = format!("{}", envelope);
        assert!(display_str.contains("MessageEnvelope"));
        assert!(display_str.contains("test-msg-id"));
    }
}