Skip to main content

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