Skip to content

Commit 4718cd1

Browse files
authored
[ISSUE #1612]⚡️Optimize ConsumeMessageDirectlyResult struct🔥 (#1613)
1 parent 174052a commit 4718cd1

File tree

1 file changed

+199
-87
lines changed

1 file changed

+199
-87
lines changed
Lines changed: 199 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,199 @@
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-
use serde::Deserialize;
18-
use serde::Serialize;
19-
20-
use crate::protocol::body::cm_result::CMResult;
21-
22-
#[derive(Debug, Serialize, Deserialize)]
23-
pub struct ConsumeMessageDirectlyResult {
24-
order: bool,
25-
auto_commit: bool,
26-
consume_result: CMResult,
27-
remark: String,
28-
spent_time_mills: u64,
29-
}
30-
31-
impl ConsumeMessageDirectlyResult {
32-
pub fn new(
33-
order: bool,
34-
auto_commit: bool,
35-
consume_result: CMResult,
36-
remark: String,
37-
spent_time_mills: u64,
38-
) -> Self {
39-
Self {
40-
order,
41-
auto_commit,
42-
consume_result,
43-
remark,
44-
spent_time_mills,
45-
}
46-
}
47-
48-
pub fn order(&self) -> bool {
49-
self.order
50-
}
51-
52-
pub fn set_order(&mut self, order: bool) {
53-
self.order = order;
54-
}
55-
56-
pub fn auto_commit(&self) -> bool {
57-
self.auto_commit
58-
}
59-
60-
pub fn set_auto_commit(&mut self, auto_commit: bool) {
61-
self.auto_commit = auto_commit;
62-
}
63-
64-
pub fn consume_result(&self) -> &CMResult {
65-
&self.consume_result
66-
}
67-
68-
pub fn set_consume_result(&mut self, consume_result: CMResult) {
69-
self.consume_result = consume_result;
70-
}
71-
72-
pub fn remark(&self) -> &str {
73-
&self.remark
74-
}
75-
76-
pub fn set_remark(&mut self, remark: String) {
77-
self.remark = remark;
78-
}
79-
80-
pub fn spent_time_mills(&self) -> u64 {
81-
self.spent_time_mills
82-
}
83-
84-
pub fn set_spent_time_mills(&mut self, spent_time_mills: u64) {
85-
self.spent_time_mills = spent_time_mills;
86-
}
87-
}
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+
use std::fmt::Display;
18+
19+
use cheetah_string::CheetahString;
20+
use serde::Deserialize;
21+
use serde::Serialize;
22+
23+
use crate::protocol::body::cm_result::CMResult;
24+
25+
#[derive(Debug, Serialize, Deserialize)]
26+
pub struct ConsumeMessageDirectlyResult {
27+
order: bool,
28+
auto_commit: bool,
29+
consume_result: Option<CMResult>,
30+
remark: Option<CheetahString>,
31+
spent_time_mills: u64,
32+
}
33+
34+
impl Default for ConsumeMessageDirectlyResult {
35+
#[inline]
36+
fn default() -> Self {
37+
Self {
38+
order: false,
39+
auto_commit: true,
40+
consume_result: None,
41+
remark: None,
42+
spent_time_mills: 0,
43+
}
44+
}
45+
}
46+
47+
impl Display for ConsumeMessageDirectlyResult {
48+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49+
write!(
50+
f,
51+
"ConsumeMessageDirectlyResult [order={}, auto_commit={}, consume_result={:?}, \
52+
remark={:?}, spent_time_mills={}]",
53+
self.order, self.auto_commit, self.consume_result, self.remark, self.spent_time_mills
54+
)
55+
}
56+
}
57+
58+
impl ConsumeMessageDirectlyResult {
59+
pub fn new(
60+
order: bool,
61+
auto_commit: bool,
62+
consume_result: CMResult,
63+
remark: CheetahString,
64+
spent_time_mills: u64,
65+
) -> Self {
66+
Self {
67+
order,
68+
auto_commit,
69+
consume_result: Some(consume_result),
70+
remark: Some(remark),
71+
spent_time_mills,
72+
}
73+
}
74+
75+
#[inline]
76+
pub fn order(&self) -> bool {
77+
self.order
78+
}
79+
80+
#[inline]
81+
pub fn set_order(&mut self, order: bool) {
82+
self.order = order;
83+
}
84+
85+
#[inline]
86+
pub fn auto_commit(&self) -> bool {
87+
self.auto_commit
88+
}
89+
90+
#[inline]
91+
pub fn set_auto_commit(&mut self, auto_commit: bool) {
92+
self.auto_commit = auto_commit;
93+
}
94+
95+
#[inline]
96+
pub fn consume_result(&self) -> &Option<CMResult> {
97+
&self.consume_result
98+
}
99+
100+
#[inline]
101+
pub fn set_consume_result(&mut self, consume_result: CMResult) {
102+
self.consume_result = Some(consume_result);
103+
}
104+
105+
#[inline]
106+
pub fn remark(&self) -> &Option<CheetahString> {
107+
&self.remark
108+
}
109+
110+
#[inline]
111+
pub fn set_remark(&mut self, remark: CheetahString) {
112+
self.remark = Some(remark);
113+
}
114+
115+
#[inline]
116+
pub fn spent_time_mills(&self) -> u64 {
117+
self.spent_time_mills
118+
}
119+
120+
#[inline]
121+
pub fn set_spent_time_mills(&mut self, spent_time_mills: u64) {
122+
self.spent_time_mills = spent_time_mills;
123+
}
124+
}
125+
126+
#[cfg(test)]
127+
mod tests {
128+
use cheetah_string::CheetahString;
129+
130+
use super::*;
131+
use crate::protocol::body::cm_result::CMResult;
132+
133+
#[test]
134+
fn consume_message_directly_result_default_initializes_correctly() {
135+
let result = ConsumeMessageDirectlyResult::default();
136+
assert!(!result.order());
137+
assert!(result.auto_commit());
138+
assert!(result.consume_result().is_none());
139+
assert!(result.remark().is_none());
140+
assert_eq!(result.spent_time_mills(), 0);
141+
}
142+
143+
#[test]
144+
fn consume_message_directly_result_new_initializes_correctly() {
145+
let consume_result = CMResult::default();
146+
let remark = CheetahString::from_static_str("test remark");
147+
let result = ConsumeMessageDirectlyResult::new(
148+
true,
149+
false,
150+
consume_result.clone(),
151+
remark.clone(),
152+
12345,
153+
);
154+
assert!(result.order());
155+
assert!(!result.auto_commit());
156+
assert_eq!(result.consume_result().as_ref().unwrap(), &consume_result);
157+
assert_eq!(result.remark().as_ref().unwrap(), &remark);
158+
assert_eq!(result.spent_time_mills(), 12345);
159+
}
160+
161+
#[test]
162+
fn consume_message_directly_result_setters_work_correctly() {
163+
let mut result = ConsumeMessageDirectlyResult::default();
164+
result.set_order(true);
165+
result.set_auto_commit(false);
166+
let consume_result = CMResult::default();
167+
result.set_consume_result(consume_result.clone());
168+
let remark = CheetahString::from_static_str("updated remark");
169+
result.set_remark(remark.clone());
170+
result.set_spent_time_mills(67890);
171+
172+
assert!(result.order());
173+
assert!(!result.auto_commit());
174+
assert_eq!(result.consume_result().as_ref().unwrap(), &consume_result);
175+
assert_eq!(result.remark().as_ref().unwrap(), &remark);
176+
assert_eq!(result.spent_time_mills(), 67890);
177+
}
178+
179+
#[test]
180+
fn consume_message_directly_result_display_formats_correctly() {
181+
let consume_result = CMResult::default();
182+
let remark = CheetahString::from_static_str("test remark");
183+
let result = ConsumeMessageDirectlyResult::new(
184+
true,
185+
false,
186+
consume_result.clone(),
187+
remark.clone(),
188+
12345,
189+
);
190+
let display = format!("{}", result);
191+
let expected = format!(
192+
"ConsumeMessageDirectlyResult [order=true, auto_commit=false, consume_result={:?}, \
193+
remark={:?}, spent_time_mills=12345]",
194+
Some(consume_result),
195+
Some(remark)
196+
);
197+
assert_eq!(display, expected);
198+
}
199+
}

0 commit comments

Comments
 (0)