rocketmq_remoting/protocol/body/
consumer_offset_serialize_wrapper.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use std::collections::HashMap;
18
19use cheetah_string::CheetahString;
20use serde::Deserialize;
21use serde::Serialize;
22
23use crate::protocol::DataVersion;
24
25#[derive(Serialize, Deserialize, Debug, Default)]
26#[serde(rename_all = "camelCase")]
27pub struct ConsumerOffsetSerializeWrapper {
28    data_version: DataVersion,
29    // Pop mode offset table
30    offset_table: HashMap<CheetahString /* topic@group */, HashMap<i32 /* queue id */, i64>>,
31}
32
33impl ConsumerOffsetSerializeWrapper {
34    /// Returns a reference to the data_version field.
35    pub fn data_version(&self) -> &DataVersion {
36        &self.data_version
37    }
38    /// Sets the data_version field.
39    pub fn set_data_version(&mut self, data_version: DataVersion) {
40        self.data_version = data_version;
41    }
42    /// Returns a reference to the offset_table field.
43    pub fn offset_table_ref(&self) -> &HashMap<CheetahString, HashMap<i32, i64>> {
44        &self.offset_table
45    }
46    pub fn offset_table(self) -> HashMap<CheetahString, HashMap<i32, i64>> {
47        self.offset_table
48    }
49    /// Returns a mutable reference to the offset_table field.
50    pub fn offset_table_mut(&mut self) -> &mut HashMap<CheetahString, HashMap<i32, i64>> {
51        &mut self.offset_table
52    }
53    /// Sets the offset_table field.
54    pub fn set_offset_table(&mut self, offset_table: HashMap<CheetahString, HashMap<i32, i64>>) {
55        self.offset_table = offset_table;
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use std::collections::HashMap;
62
63    use super::*;
64
65    #[test]
66    fn test_consumer_offset_serialize_wrapper_insert_and_retrieve() {
67        let mut wrapper = ConsumerOffsetSerializeWrapper::default();
68
69        let topic_group = CheetahString::from("test_topic@test_group");
70        let mut queue_offsets = HashMap::new();
71        queue_offsets.insert(0, 100i64);
72        queue_offsets.insert(1, 200i64);
73
74        wrapper
75            .offset_table
76            .insert(topic_group.clone(), queue_offsets);
77
78        assert_eq!(wrapper.offset_table.len(), 1);
79        assert!(wrapper.offset_table.contains_key(&topic_group));
80
81        let retrieved_offsets = wrapper.offset_table.get(&topic_group).unwrap();
82        assert_eq!(retrieved_offsets.get(&0), Some(&100i64));
83        assert_eq!(retrieved_offsets.get(&1), Some(&200i64));
84    }
85}