Skip to main content

rocketmq_protocol/protocol/body/
consumer_offset_serialize_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 std::collections::HashMap;
16
17use cheetah_string::CheetahString;
18use serde::Deserialize;
19use serde::Serialize;
20
21use crate::protocol::DataVersion;
22
23#[derive(Serialize, Deserialize, Debug, Default)]
24#[serde(rename_all = "camelCase")]
25pub struct ConsumerOffsetSerializeWrapper {
26    data_version: DataVersion,
27    // Pop mode offset table
28    offset_table: HashMap<CheetahString /* topic@group */, HashMap<i32 /* queue id */, i64>>,
29}
30
31impl ConsumerOffsetSerializeWrapper {
32    /// Returns a reference to the data_version field.
33    pub fn data_version(&self) -> &DataVersion {
34        &self.data_version
35    }
36    /// Sets the data_version field.
37    pub fn set_data_version(&mut self, data_version: DataVersion) {
38        self.data_version = data_version;
39    }
40    /// Returns a reference to the offset_table field.
41    pub fn offset_table_ref(&self) -> &HashMap<CheetahString, HashMap<i32, i64>> {
42        &self.offset_table
43    }
44    pub fn offset_table(self) -> HashMap<CheetahString, HashMap<i32, i64>> {
45        self.offset_table
46    }
47    /// Returns a mutable reference to the offset_table field.
48    pub fn offset_table_mut(&mut self) -> &mut HashMap<CheetahString, HashMap<i32, i64>> {
49        &mut self.offset_table
50    }
51    /// Sets the offset_table field.
52    pub fn set_offset_table(&mut self, offset_table: HashMap<CheetahString, HashMap<i32, i64>>) {
53        self.offset_table = offset_table;
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use std::collections::HashMap;
60
61    use super::*;
62
63    #[test]
64    fn test_consumer_offset_serialize_wrapper_insert_and_retrieve() {
65        let mut wrapper = ConsumerOffsetSerializeWrapper::default();
66
67        let topic_group = CheetahString::from("test_topic@test_group");
68        let mut queue_offsets = HashMap::new();
69        queue_offsets.insert(0, 100i64);
70        queue_offsets.insert(1, 200i64);
71
72        wrapper.offset_table.insert(topic_group.clone(), queue_offsets);
73
74        assert_eq!(wrapper.offset_table.len(), 1);
75        assert!(wrapper.offset_table.contains_key(&topic_group));
76
77        let retrieved_offsets = wrapper.offset_table.get(&topic_group).unwrap();
78        assert_eq!(retrieved_offsets.get(&0), Some(&100i64));
79        assert_eq!(retrieved_offsets.get(&1), Some(&200i64));
80    }
81}