Skip to content

Commit 8999fd5

Browse files
authored
[ISSUE #1160]🔥Optimize ValidateTopicResult String with CheetahString (#1161)
1 parent 347e7f8 commit 8999fd5

2 files changed

Lines changed: 98 additions & 13 deletions

File tree

rocketmq-broker/src/processor/admin_broker_processor/topic_request_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl TopicRequestHandler {
8787
return Some(
8888
response
8989
.set_code(ResponseCode::SystemError)
90-
.set_remark(result.remark()),
90+
.set_remark(result.remark().clone()),
9191
);
9292
}
9393
if self
@@ -213,7 +213,7 @@ impl TopicRequestHandler {
213213
return Some(
214214
response
215215
.set_code(ResponseCode::SystemError)
216-
.set_remark(result.remark()),
216+
.set_remark(result.remark().clone()),
217217
);
218218
}
219219
if self

rocketmq-common/src/common/topic.rs

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use std::collections::HashSet;
1919
use std::sync::Mutex;
2020

21+
use cheetah_string::CheetahString;
2122
use lazy_static::lazy_static;
2223

2324
pub const TOPIC_MAX_LENGTH: usize = 127;
@@ -97,35 +98,35 @@ impl TopicValidator {
9798

9899
pub fn validate_topic(topic: &str) -> ValidateTopicResult {
99100
if topic.trim().is_empty() {
101+
const REMARK: &str = "The specified topic is blank.";
100102
return ValidateTopicResult {
101103
valid: false,
102-
remark: String::from("The specified topic is blank."),
104+
remark: CheetahString::from_static_str(REMARK),
103105
};
104106
}
105107

106108
if Self::is_topic_or_group_illegal(topic) {
109+
const REMARK: &str =
110+
"The specified topic contains illegal characters, allowing only ^[%|a-zA-Z0-9_-]+$";
107111
return ValidateTopicResult {
108112
valid: false,
109-
remark: String::from(
110-
"The specified topic contains illegal characters, allowing only \
111-
^[%|a-zA-Z0-9_-]+$",
112-
),
113+
remark: CheetahString::from_static_str(REMARK),
113114
};
114115
}
115116

116117
if topic.len() > TOPIC_MAX_LENGTH {
117118
return ValidateTopicResult {
118119
valid: false,
119-
remark: format!(
120+
remark: CheetahString::from(format!(
120121
"The specified topic is longer than topic max length {}.",
121122
TOPIC_MAX_LENGTH
122-
),
123+
)),
123124
};
124125
}
125126

126127
ValidateTopicResult {
127128
valid: true,
128-
remark: String::new(),
129+
remark: CheetahString::empty(),
129130
}
130131
}
131132

@@ -155,21 +156,105 @@ impl TopicValidator {
155156

156157
pub struct ValidateTopicResult {
157158
valid: bool,
158-
remark: String,
159+
remark: CheetahString,
159160
}
160161

161162
impl ValidateTopicResult {
162163
pub fn valid(&self) -> bool {
163164
self.valid
164165
}
165-
pub fn remark(&self) -> &str {
166+
pub fn remark(&self) -> &CheetahString {
166167
&self.remark
167168
}
168169

169170
pub fn set_valid(&mut self, valid: bool) {
170171
self.valid = valid;
171172
}
172-
pub fn set_remark(&mut self, remark: String) {
173+
pub fn set_remark(&mut self, remark: CheetahString) {
173174
self.remark = remark;
174175
}
175176
}
177+
178+
#[cfg(test)]
179+
mod tests {
180+
use super::*;
181+
#[test]
182+
fn validate_topic_with_valid_topic() {
183+
let result = TopicValidator::validate_topic("valid_topic");
184+
assert!(result.valid());
185+
assert_eq!(result.remark(), "");
186+
}
187+
188+
#[test]
189+
fn validate_topic_with_empty_topic() {
190+
let result = TopicValidator::validate_topic("");
191+
assert!(!result.valid());
192+
assert_eq!(result.remark(), "The specified topic is blank.");
193+
}
194+
195+
#[test]
196+
fn validate_topic_with_illegal_characters() {
197+
let result = TopicValidator::validate_topic("invalid@topic");
198+
assert!(!result.valid());
199+
assert_eq!(
200+
result.remark(),
201+
"The specified topic contains illegal characters, allowing only ^[%|a-zA-Z0-9_-]+$"
202+
);
203+
}
204+
205+
#[test]
206+
fn validate_topic_with_exceeding_length() {
207+
let long_topic = "a".repeat(TOPIC_MAX_LENGTH + 1);
208+
let result = TopicValidator::validate_topic(&long_topic);
209+
assert!(!result.valid());
210+
assert_eq!(
211+
result.remark().as_str(),
212+
format!(
213+
"The specified topic is longer than topic max length {}.",
214+
TOPIC_MAX_LENGTH
215+
)
216+
);
217+
}
218+
219+
#[test]
220+
fn is_system_topic_with_system_topic() {
221+
assert!(TopicValidator::is_system_topic(
222+
TopicValidator::RMQ_SYS_SCHEDULE_TOPIC
223+
));
224+
}
225+
226+
#[test]
227+
fn is_system_topic_with_non_system_topic() {
228+
assert!(!TopicValidator::is_system_topic("non_system_topic"));
229+
}
230+
231+
#[test]
232+
fn is_not_allowed_send_topic_with_not_allowed_topic() {
233+
assert!(TopicValidator::is_not_allowed_send_topic(
234+
TopicValidator::RMQ_SYS_SCHEDULE_TOPIC
235+
));
236+
}
237+
238+
#[test]
239+
fn is_not_allowed_send_topic_with_allowed_topic() {
240+
assert!(!TopicValidator::is_not_allowed_send_topic("allowed_topic"));
241+
}
242+
243+
#[test]
244+
fn add_system_topic_adds_new_topic() {
245+
TopicValidator::add_system_topic("new_system_topic");
246+
assert!(TopicValidator::is_system_topic("new_system_topic"));
247+
}
248+
249+
#[test]
250+
fn get_system_topic_set_returns_all_system_topics() {
251+
let system_topics = TopicValidator::get_system_topic_set();
252+
assert!(system_topics.contains(TopicValidator::RMQ_SYS_SCHEDULE_TOPIC));
253+
}
254+
255+
#[test]
256+
fn get_not_allowed_send_topic_set_returns_all_not_allowed_topics() {
257+
let not_allowed_topics = TopicValidator::get_not_allowed_send_topic_set();
258+
assert!(not_allowed_topics.contains(TopicValidator::RMQ_SYS_SCHEDULE_TOPIC));
259+
}
260+
}

0 commit comments

Comments
 (0)