Skip to main content

rocketmq_protocol/protocol/header/
get_min_offset_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, Serialize, Deserialize, Default, RequestHeaderCodec)]
24#[header(
25    type_id = "rocketmq_protocol::protocol::header::get_min_offset_request_header::GetMinOffsetRequestHeader",
26    java_class = "org.apache.rocketmq.remoting.protocol.header.GetMinOffsetRequestHeader"
27)]
28#[serde(rename_all = "camelCase")]
29pub struct GetMinOffsetRequestHeader {
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 GetMinOffsetRequestHeader {
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(|h| h.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(|h| h.rpc_request_header.as_ref())
64            .and_then(|h| h.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(|h| h.rpc_request_header.as_ref())
79            .and_then(|r| r.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(|h| h.rpc_request_header.as_ref())
94            .and_then(|r| r.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(|h| h.rpc_request_header.as_ref())
109            .and_then(|r| r.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 std::collections::HashMap;
132
133    use super::*;
134    use crate::protocol::command_custom_header::FromMap;
135
136    #[test]
137    fn get_min_offset_request_header_serialization() {
138        let header = GetMinOffsetRequestHeader {
139            topic: CheetahString::from("test"),
140            queue_id: 1,
141            topic_request_header: Some(TopicRequestHeader {
142                lo: Some(true),
143                ..Default::default()
144            }),
145        };
146        let json = serde_json::to_string(&header).unwrap();
147        assert!(json.contains("\"topic\":\"test\""));
148        assert!(json.contains("\"queueId\":1"));
149        assert!(json.contains("\"lo\":true"));
150    }
151
152    #[test]
153    fn get_min_offset_request_header_deserialization() {
154        let json = r#"{"topic":"test","queueId":1,"lo":true}"#;
155        let header: GetMinOffsetRequestHeader = serde_json::from_str(json).unwrap();
156        assert_eq!(header.topic, "test");
157        assert_eq!(header.queue_id, 1);
158        assert_eq!(header.topic_request_header.unwrap().lo, Some(true));
159    }
160
161    #[test]
162    fn get_min_offset_request_header_from_map() {
163        let mut map = HashMap::new();
164        map.insert(CheetahString::from("topic"), CheetahString::from("test_topic"));
165        map.insert(CheetahString::from("queueId"), CheetahString::from("2"));
166        map.insert(CheetahString::from("lo"), CheetahString::from("true"));
167
168        let header = <GetMinOffsetRequestHeader as FromMap>::from(&map).unwrap();
169        assert_eq!(header.topic, "test_topic");
170        assert_eq!(header.queue_id, 2);
171        assert_eq!(header.topic_request_header.unwrap().lo, Some(true));
172    }
173}