Skip to main content

rocketmq_protocol/protocol/admin/
offset_wrapper.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 serde::Deserialize;
16use serde::Serialize;
17
18#[derive(Debug, Clone, Serialize, Deserialize, Default)]
19#[serde(rename_all = "camelCase")]
20pub struct OffsetWrapper {
21    broker_offset: i64,
22    consumer_offset: i64,
23    pull_offset: i64,
24    last_timestamp: i64,
25}
26
27impl OffsetWrapper {
28    pub fn new() -> Self {
29        Self {
30            broker_offset: 0,
31            consumer_offset: 0,
32            pull_offset: 0,
33            last_timestamp: 0,
34        }
35    }
36
37    pub fn get_broker_offset(&self) -> i64 {
38        self.broker_offset
39    }
40
41    pub fn set_broker_offset(&mut self, broker_offset: i64) {
42        self.broker_offset = broker_offset;
43    }
44
45    pub fn get_consumer_offset(&self) -> i64 {
46        self.consumer_offset
47    }
48
49    pub fn set_consumer_offset(&mut self, consumer_offset: i64) {
50        self.consumer_offset = consumer_offset;
51    }
52
53    pub fn get_pull_offset(&self) -> i64 {
54        self.pull_offset
55    }
56
57    pub fn set_pull_offset(&mut self, pull_offset: i64) {
58        self.pull_offset = pull_offset;
59    }
60
61    pub fn get_last_timestamp(&self) -> i64 {
62        self.last_timestamp
63    }
64
65    pub fn set_last_timestamp(&mut self, last_timestamp: i64) {
66        self.last_timestamp = last_timestamp;
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn methods_and_serde_preserve_offsets() {
76        let mut wrapper = OffsetWrapper::new();
77        assert_eq!(
78            (
79                wrapper.get_broker_offset(),
80                wrapper.get_consumer_offset(),
81                wrapper.get_pull_offset(),
82                wrapper.get_last_timestamp(),
83            ),
84            (0, 0, 0, 0)
85        );
86        wrapper.set_broker_offset(-100);
87        wrapper.set_consumer_offset(-200);
88        wrapper.set_pull_offset(-300);
89        wrapper.set_last_timestamp(-400);
90
91        let serialized = serde_json::to_string(&wrapper).unwrap();
92        let expected = r#"{"brokerOffset":-100,"consumerOffset":-200,"pullOffset":-300,"lastTimestamp":-400}"#;
93        assert_eq!(serialized, expected);
94
95        let deserialized: OffsetWrapper = serde_json::from_str(&serialized).unwrap();
96        assert_eq!(
97            (
98                deserialized.get_broker_offset(),
99                deserialized.get_consumer_offset(),
100                deserialized.get_pull_offset(),
101                deserialized.get_last_timestamp(),
102            ),
103            (-100, -200, -300, -400)
104        );
105    }
106}