rocketmq_remoting/protocol/body/
epoch_entry_cache.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 */
17
18use cheetah_string::CheetahString;
19use serde::Deserialize;
20use serde::Serialize;
21#[derive(Deserialize, Serialize, Default)]
22pub struct EpochEntry;
23
24#[derive(Deserialize, Serialize, Default)]
25#[serde(rename_all = "camelCase")]
26pub struct EpochEntryCache {
27    cluster_name: CheetahString,
28    broker_name: CheetahString,
29    broker_id: u64,
30    epoch_list: Vec<EpochEntry>,
31    max_offset: u64,
32}
33
34impl EpochEntryCache {
35    pub fn new(
36        cluster_name: impl Into<CheetahString>,
37        broker_name: impl Into<CheetahString>,
38        broker_id: u64,
39        epoch_list: Vec<EpochEntry>,
40        max_offset: u64,
41    ) -> Self {
42        Self {
43            cluster_name: cluster_name.into(),
44            broker_name: broker_name.into(),
45            broker_id,
46            epoch_list,
47            max_offset,
48        }
49    }
50    pub fn get_cluster_name(&self) -> &CheetahString {
51        &self.cluster_name
52    }
53    pub fn set_cluster_name(&mut self, cluster_name: impl Into<CheetahString>) {
54        self.cluster_name = cluster_name.into()
55    }
56    pub fn get_broker_name(&self) -> &CheetahString {
57        &self.broker_name
58    }
59    pub fn set_broker_name(&mut self, broker_name: impl Into<CheetahString>) {
60        self.broker_name = broker_name.into()
61    }
62    pub fn get_broker_id(&self) -> u64 {
63        self.broker_id
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use cheetah_string::CheetahString;
70
71    use super::*;
72
73    #[test]
74    fn new_creates_instance_of_epoch_entry_cache() {
75        let epoch_entry_cache = EpochEntryCache::new("cluster1", "broker1", 1, vec![EpochEntry], 1);
76        assert_eq!(
77            epoch_entry_cache.get_cluster_name(),
78            &CheetahString::from("cluster1")
79        );
80        assert_eq!(epoch_entry_cache.get_broker_id(), 1);
81        assert_eq!(
82            epoch_entry_cache.get_broker_name(),
83            &CheetahString::from("broker1")
84        );
85    }
86    #[test]
87    fn set_broker_name_updates_broker_name() {
88        let mut epoch_entry_cache =
89            EpochEntryCache::new("cluster1", "broker1", 1, vec![EpochEntry], 1);
90        epoch_entry_cache.set_broker_name("broker2");
91        assert_eq!(
92            epoch_entry_cache.get_broker_name(),
93            &CheetahString::from("broker2")
94        );
95    }
96
97    #[test]
98    fn set_cluster_name_updates_cluster_name() {
99        let mut epoch_entry_cache =
100            EpochEntryCache::new("cluster1", "broker1", 1, vec![EpochEntry], 1);
101        epoch_entry_cache.set_cluster_name("cluster2");
102        assert_eq!(
103            epoch_entry_cache.get_cluster_name(),
104            &CheetahString::from("cluster2")
105        );
106    }
107}