rocketmq_common/utils/
cleanup_policy_utils.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 std::str::FromStr;
19
20use crate::common::attribute::cleanup_policy::CleanupPolicy;
21use crate::common::attribute::Attribute;
22use crate::common::config::TopicConfig;
23use crate::TopicAttributes::TopicAttributes;
24
25pub fn is_compaction(topic_config: &Option<TopicConfig>) -> bool {
26    match topic_config {
27        Some(config) => CleanupPolicy::COMPACTION == get_delete_policy(Some(config)),
28        None => false,
29    }
30}
31
32pub fn get_delete_policy(topic_config: Option<&TopicConfig>) -> CleanupPolicy {
33    match topic_config {
34        Some(config) => {
35            let attribute_name = TopicAttributes::cleanup_policy_attribute().name();
36            match config.attributes.get(attribute_name) {
37                Some(value) => CleanupPolicy::from_str(value.as_str()).unwrap(),
38                None => CleanupPolicy::from_str(
39                    TopicAttributes::cleanup_policy_attribute().default_value(),
40                )
41                .unwrap(),
42            }
43        }
44        None => {
45            CleanupPolicy::from_str(TopicAttributes::cleanup_policy_attribute().default_value())
46                .unwrap()
47        }
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54    use crate::common::attribute::cleanup_policy::CleanupPolicy;
55    use crate::common::config::TopicConfig;
56
57    #[test]
58    fn is_compaction_returns_true_when_cleanup_policy_is_compaction() {
59        let mut topic_config = TopicConfig::default();
60        topic_config.attributes.insert(
61            TopicAttributes::cleanup_policy_attribute().name().into(),
62            CleanupPolicy::COMPACTION.to_string().into(),
63        );
64        assert_eq!(is_compaction(&Some(topic_config)), true);
65    }
66
67    #[test]
68    fn is_compaction_returns_false_when_cleanup_policy_is_not_compaction() {
69        let mut topic_config = TopicConfig::default();
70        topic_config.attributes.insert(
71            TopicAttributes::cleanup_policy_attribute()
72                .name()
73                .to_string()
74                .into(),
75            CleanupPolicy::DELETE.to_string().into(),
76        );
77        assert_eq!(is_compaction(&Some(topic_config)), false);
78    }
79
80    #[test]
81    fn is_compaction_returns_false_when_topic_config_is_none() {
82        assert_eq!(is_compaction(&None), false);
83    }
84
85    #[test]
86    fn get_delete_policy_returns_cleanup_policy_from_topic_config() {
87        let mut topic_config = TopicConfig::default();
88        topic_config.attributes.insert(
89            TopicAttributes::cleanup_policy_attribute()
90                .name()
91                .to_string()
92                .into(),
93            CleanupPolicy::DELETE.to_string().into(),
94        );
95        assert_eq!(
96            get_delete_policy(Some(&topic_config)),
97            CleanupPolicy::DELETE
98        );
99    }
100
101    #[test]
102    fn get_delete_policy_returns_default_cleanup_policy_when_not_set_in_topic_config() {
103        let topic_config = TopicConfig::default();
104        assert_eq!(
105            get_delete_policy(Some(&topic_config)),
106            CleanupPolicy::from_str(TopicAttributes::cleanup_policy_attribute().default_value())
107                .unwrap()
108        );
109    }
110
111    #[test]
112    fn get_delete_policy_returns_default_cleanup_policy_when_topic_config_is_none() {
113        assert_eq!(
114            get_delete_policy(None),
115            CleanupPolicy::from_str(TopicAttributes::cleanup_policy_attribute().default_value())
116                .unwrap()
117        );
118    }
119}