rocketmq_remoting/protocol/header/message_operation_header/
send_message_response_header.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;
20use rocketmq_macros::RequestHeaderCodecV2;
21use serde::Deserialize;
22use serde::Serialize;
23
24use crate::protocol::FastCodesHeader;
25
26#[derive(Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
27#[serde(rename_all = "camelCase")]
28pub struct SendMessageResponseHeader {
29    msg_id: CheetahString,
30    queue_id: i32,
31    queue_offset: i64,
32    transaction_id: Option<CheetahString>,
33    batch_uniq_id: Option<CheetahString>,
34}
35
36impl SendMessageResponseHeader {
37    pub fn new(
38        msg_id: CheetahString,
39        queue_id: i32,
40        queue_offset: i64,
41        transaction_id: Option<CheetahString>,
42        batch_uniq_id: Option<CheetahString>,
43    ) -> Self {
44        SendMessageResponseHeader {
45            msg_id,
46            queue_id,
47            queue_offset,
48            transaction_id,
49            batch_uniq_id,
50        }
51    }
52
53    pub fn msg_id(&self) -> &CheetahString {
54        &self.msg_id
55    }
56
57    pub fn queue_id(&self) -> i32 {
58        self.queue_id
59    }
60
61    pub fn queue_offset(&self) -> i64 {
62        self.queue_offset
63    }
64
65    pub fn transaction_id(&self) -> Option<&str> {
66        self.transaction_id.as_deref()
67    }
68
69    pub fn batch_uniq_id(&self) -> Option<&str> {
70        self.batch_uniq_id.as_deref()
71    }
72
73    pub fn set_msg_id(&mut self, msg_id: impl Into<CheetahString>) {
74        self.msg_id = msg_id.into();
75    }
76
77    pub fn set_queue_id(&mut self, queue_id: i32) {
78        self.queue_id = queue_id;
79    }
80
81    pub fn set_queue_offset(&mut self, queue_offset: i64) {
82        self.queue_offset = queue_offset;
83    }
84
85    pub fn set_transaction_id(&mut self, transaction_id: Option<CheetahString>) {
86        self.transaction_id = transaction_id;
87    }
88
89    pub fn set_batch_uniq_id(&mut self, batch_uniq_id: Option<CheetahString>) {
90        self.batch_uniq_id = batch_uniq_id;
91    }
92}
93
94impl FastCodesHeader for SendMessageResponseHeader {
95    fn encode_fast(&mut self, out: &mut bytes::BytesMut) {
96        Self::write_if_not_null(out, "msgId", self.msg_id.to_string().as_str());
97        Self::write_if_not_null(out, "queueId", self.queue_id.to_string().as_str());
98        Self::write_if_not_null(out, "queueOffset", self.queue_offset.to_string().as_str());
99        Self::write_if_not_null(
100            out,
101            "transactionId",
102            self.transaction_id.clone().as_deref().unwrap(),
103        );
104        Self::write_if_not_null(
105            out,
106            "batchUniqId",
107            self.batch_uniq_id.clone().as_deref().unwrap(),
108        );
109    }
110
111    fn decode_fast(&mut self, fields: &HashMap<CheetahString, CheetahString>) {
112        if let Some(str) = fields.get(&CheetahString::from_slice("msgId")) {
113            self.msg_id = str.clone();
114        }
115
116        if let Some(str) = fields.get(&CheetahString::from_slice("queueId")) {
117            self.queue_id = str.parse::<i32>().unwrap_or_default();
118        }
119
120        if let Some(str) = fields.get(&CheetahString::from_slice("queueOffset")) {
121            self.queue_offset = str.parse::<i64>().unwrap_or_default();
122        }
123
124        if let Some(str) = fields.get(&CheetahString::from_slice("transactionId")) {
125            self.transaction_id = Some(str.clone());
126        }
127
128        if let Some(str) = fields.get(&CheetahString::from_slice("batchUniqId")) {
129            self.batch_uniq_id = Some(str.clone());
130        }
131    }
132}