Skip to main content

rocketmq_protocol/protocol/header/
get_earliest_msg_storetime_request_header.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 cheetah_string::CheetahString;
16use rocketmq_macros::RequestHeaderCodec;
17use serde::Deserialize;
18use serde::Serialize;
19
20use crate::protocol::header::message_operation_header::TopicRequestHeaderTrait;
21use crate::rpc::topic_request_header::TopicRequestHeader;
22
23#[derive(Debug, Clone, Default, Serialize, Deserialize, RequestHeaderCodec)]
24#[header(
25    type_id = "rocketmq_protocol::protocol::header::get_earliest_msg_storetime_request_header::GetEarliestMsgStoretimeRequestHeader",
26    java_class = "org.apache.rocketmq.remoting.protocol.header.GetEarliestMsgStoretimeRequestHeader"
27)]
28#[serde(rename_all = "camelCase")]
29pub struct GetEarliestMsgStoretimeRequestHeader {
30    #[header(required)]
31    pub topic: CheetahString,
32
33    #[header(required)]
34    pub queue_id: i32,
35
36    #[serde(flatten)]
37    #[header(flatten, presence = "always")]
38    pub topic_request_header: Option<TopicRequestHeader>,
39}
40
41impl TopicRequestHeaderTrait for GetEarliestMsgStoretimeRequestHeader {
42    fn set_lo(&mut self, lo: Option<bool>) {
43        if let Some(header) = self.topic_request_header.as_mut() {
44            header.lo = lo;
45        }
46    }
47
48    fn lo(&self) -> Option<bool> {
49        self.topic_request_header.as_ref().and_then(|header| header.lo)
50    }
51
52    fn set_topic(&mut self, topic: CheetahString) {
53        self.topic = topic;
54    }
55
56    fn topic(&self) -> &CheetahString {
57        &self.topic
58    }
59
60    fn broker_name(&self) -> Option<&CheetahString> {
61        self.topic_request_header
62            .as_ref()
63            .and_then(|header| header.rpc_request_header.as_ref())
64            .and_then(|header| header.broker_name.as_ref())
65    }
66
67    fn set_broker_name(&mut self, broker_name: CheetahString) {
68        if let Some(header) = self.topic_request_header.as_mut() {
69            if let Some(rpc_header) = header.rpc_request_header.as_mut() {
70                rpc_header.broker_name = Some(broker_name);
71            }
72        }
73    }
74
75    fn namespace(&self) -> Option<&str> {
76        self.topic_request_header
77            .as_ref()
78            .and_then(|header| header.rpc_request_header.as_ref())
79            .and_then(|header| header.namespace.as_deref())
80    }
81
82    fn set_namespace(&mut self, namespace: CheetahString) {
83        if let Some(header) = self.topic_request_header.as_mut() {
84            if let Some(rpc_header) = header.rpc_request_header.as_mut() {
85                rpc_header.namespace = Some(namespace);
86            }
87        }
88    }
89
90    fn namespaced(&self) -> Option<bool> {
91        self.topic_request_header
92            .as_ref()
93            .and_then(|header| header.rpc_request_header.as_ref())
94            .and_then(|header| header.namespaced)
95    }
96
97    fn set_namespaced(&mut self, namespaced: bool) {
98        if let Some(header) = self.topic_request_header.as_mut() {
99            if let Some(rpc_header) = header.rpc_request_header.as_mut() {
100                rpc_header.namespaced = Some(namespaced);
101            }
102        }
103    }
104
105    fn oneway(&self) -> Option<bool> {
106        self.topic_request_header
107            .as_ref()
108            .and_then(|header| header.rpc_request_header.as_ref())
109            .and_then(|header| header.oneway)
110    }
111
112    fn set_oneway(&mut self, oneway: bool) {
113        if let Some(header) = self.topic_request_header.as_mut() {
114            if let Some(rpc_header) = header.rpc_request_header.as_mut() {
115                rpc_header.oneway = Some(oneway);
116            }
117        }
118    }
119
120    fn queue_id(&self) -> i32 {
121        self.queue_id
122    }
123
124    fn set_queue_id(&mut self, queue_id: i32) {
125        self.queue_id = queue_id;
126    }
127}
128
129#[cfg(test)]
130mod tests {
131    use super::*;
132
133    #[test]
134    fn get_earliest_msg_storetime_request_header_serializes() {
135        let header = GetEarliestMsgStoretimeRequestHeader {
136            topic: CheetahString::from_static_str("topic-a"),
137            queue_id: 3,
138            topic_request_header: None,
139        };
140
141        let serialized = serde_json::to_string(&header).expect("serialize earliest msg storetime request header");
142        assert_eq!(serialized, r#"{"topic":"topic-a","queueId":3}"#);
143    }
144}