Skip to main content

rocketmq_model/common/lite/
lite_subscription.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::HashSet;
16use std::fmt;
17
18use cheetah_string::CheetahString;
19
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct LiteSubscription {
22    pub group: CheetahString,
23    pub topic: CheetahString,
24    pub lite_topic_set: HashSet<CheetahString>,
25    pub update_time: i64,
26    pub version: i64,
27}
28
29impl LiteSubscription {
30    #[must_use]
31    #[inline]
32    pub fn new(group: CheetahString, topic: CheetahString, update_time: i64) -> Self {
33        Self {
34            group,
35            topic,
36            lite_topic_set: HashSet::new(),
37            update_time,
38            version: 0,
39        }
40    }
41
42    #[must_use]
43    #[inline]
44    pub fn with_lite_topic_set(mut self, lite_topic_set: HashSet<CheetahString>, update_time: i64) -> Self {
45        self.lite_topic_set = lite_topic_set;
46        self.update_time = update_time;
47        self
48    }
49
50    #[must_use]
51    #[inline]
52    pub fn with_update_time(mut self, update_time: i64) -> Self {
53        self.update_time = update_time;
54        self
55    }
56
57    #[must_use]
58    #[inline]
59    pub fn with_version(mut self, version: i64) -> Self {
60        self.version = version;
61        self
62    }
63
64    #[inline]
65    pub fn add_lite_topic(&mut self, lite_topic: CheetahString, update_time: i64) -> bool {
66        self.update_time = update_time;
67        self.lite_topic_set.insert(lite_topic)
68    }
69
70    #[inline]
71    pub fn add_lite_topic_set(&mut self, set: &HashSet<CheetahString>, update_time: i64) {
72        self.update_time = update_time;
73        self.lite_topic_set.extend(set.iter().cloned());
74    }
75
76    #[inline]
77    pub fn remove_lite_topic(&mut self, lite_topic: &CheetahString, update_time: i64) -> bool {
78        self.update_time = update_time;
79        self.lite_topic_set.remove(lite_topic)
80    }
81
82    #[inline]
83    pub fn remove_lite_topic_set(&mut self, set: &HashSet<CheetahString>, update_time: i64) {
84        self.update_time = update_time;
85        for topic in set {
86            self.lite_topic_set.remove(topic);
87        }
88    }
89
90    #[must_use]
91    #[inline]
92    pub const fn group(&self) -> &CheetahString {
93        &self.group
94    }
95
96    #[inline]
97    pub fn set_group(&mut self, group: CheetahString) {
98        self.group = group;
99    }
100
101    #[must_use]
102    #[inline]
103    pub const fn topic(&self) -> &CheetahString {
104        &self.topic
105    }
106
107    #[inline]
108    pub fn set_topic(&mut self, topic: CheetahString) {
109        self.topic = topic;
110    }
111
112    #[must_use]
113    #[inline]
114    pub fn lite_topic_set(&self) -> &HashSet<CheetahString> {
115        &self.lite_topic_set
116    }
117
118    #[inline]
119    pub fn set_lite_topic_set(&mut self, lite_topic_set: HashSet<CheetahString>, update_time: i64) {
120        self.lite_topic_set = lite_topic_set;
121        self.update_time = update_time;
122    }
123
124    #[must_use]
125    #[inline]
126    pub const fn update_time(&self) -> i64 {
127        self.update_time
128    }
129
130    #[inline]
131    pub fn set_update_time(&mut self, update_time: i64) {
132        self.update_time = update_time;
133    }
134
135    #[must_use]
136    #[inline]
137    pub const fn version(&self) -> i64 {
138        self.version
139    }
140
141    #[inline]
142    pub fn set_version(&mut self, version: i64) {
143        self.version = version;
144    }
145}
146
147impl fmt::Display for LiteSubscription {
148    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149        write!(
150            f,
151            "LiteSubscription {{ group: {}, topic: {}, lite_topic_set: {:?}, update_time: {}, version: {} }}",
152            self.group, self.topic, self.lite_topic_set, self.update_time, self.version
153        )
154    }
155}
156
157#[cfg(test)]
158mod tests {
159    use super::*;
160
161    #[test]
162    fn lite_subscription_new_with_methods() {
163        let mut set = HashSet::new();
164        set.insert("lite_topic".into());
165        let subscription = LiteSubscription::new("group".into(), "topic".into(), 1)
166            .with_lite_topic_set(set.clone(), 2)
167            .with_update_time(100)
168            .with_version(2);
169
170        assert_eq!(subscription.group(), "group");
171        assert_eq!(subscription.topic(), "topic");
172        assert_eq!(subscription.lite_topic_set(), &set);
173        assert_eq!(subscription.update_time(), 100);
174        assert_eq!(subscription.version(), 2);
175    }
176
177    #[test]
178    fn lite_subscription_topic_operations() {
179        let mut subscription = LiteSubscription::new("group".into(), "topic".into(), 1);
180
181        subscription.add_lite_topic("topic1".into(), 2);
182        assert!(subscription.lite_topic_set().contains(&CheetahString::from("topic1")));
183
184        let mut set = HashSet::new();
185        set.insert(CheetahString::from("topic2"));
186        subscription.add_lite_topic_set(&set, 3);
187        assert!(subscription.lite_topic_set().contains(&CheetahString::from("topic2")));
188
189        subscription.remove_lite_topic(&CheetahString::from("topic1"), 4);
190        assert!(!subscription.lite_topic_set().contains(&CheetahString::from("topic1")));
191
192        subscription.remove_lite_topic_set(&set, 5);
193        assert!(!subscription.lite_topic_set().contains(&CheetahString::from("topic2")));
194    }
195
196    #[test]
197    fn lite_subscription_setters() {
198        let mut subscription = LiteSubscription::new("group".into(), "topic".into(), 1);
199        subscription.set_group("new_group".into());
200        subscription.set_topic("new_topic".into());
201        let mut set = HashSet::new();
202        set.insert("topic1".into());
203        subscription.set_lite_topic_set(set.clone(), 2);
204        subscription.set_update_time(200);
205        subscription.set_version(3);
206
207        assert_eq!(subscription.group(), "new_group");
208        assert_eq!(subscription.topic(), "new_topic");
209        assert_eq!(subscription.lite_topic_set(), &set);
210        assert_eq!(subscription.update_time(), 200);
211        assert_eq!(subscription.version(), 3);
212    }
213
214    #[test]
215    fn lite_subscription_display() {
216        let subscription = LiteSubscription::new("group".into(), "topic".into(), 1);
217        let display = format!("{}", subscription);
218        assert!(display.contains("group: group"));
219        assert!(display.contains("topic: topic"));
220        assert!(display.contains("version: 0"));
221    }
222}