Skip to content

Commit

Permalink
[ISSUE #1612]⚡️Optimize ConsumeMessageDirectlyResult struct🔥 (#1613)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsm authored Dec 6, 2024
1 parent 174052a commit 4718cd1
Showing 1 changed file with 199 additions and 87 deletions.
286 changes: 199 additions & 87 deletions rocketmq-remoting/src/protocol/body/consume_message_directly_result.rs
Original file line number Diff line number Diff line change
@@ -1,87 +1,199 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use serde::Deserialize;
use serde::Serialize;

use crate::protocol::body::cm_result::CMResult;

#[derive(Debug, Serialize, Deserialize)]
pub struct ConsumeMessageDirectlyResult {
order: bool,
auto_commit: bool,
consume_result: CMResult,
remark: String,
spent_time_mills: u64,
}

impl ConsumeMessageDirectlyResult {
pub fn new(
order: bool,
auto_commit: bool,
consume_result: CMResult,
remark: String,
spent_time_mills: u64,
) -> Self {
Self {
order,
auto_commit,
consume_result,
remark,
spent_time_mills,
}
}

pub fn order(&self) -> bool {
self.order
}

pub fn set_order(&mut self, order: bool) {
self.order = order;
}

pub fn auto_commit(&self) -> bool {
self.auto_commit
}

pub fn set_auto_commit(&mut self, auto_commit: bool) {
self.auto_commit = auto_commit;
}

pub fn consume_result(&self) -> &CMResult {
&self.consume_result
}

pub fn set_consume_result(&mut self, consume_result: CMResult) {
self.consume_result = consume_result;
}

pub fn remark(&self) -> &str {
&self.remark
}

pub fn set_remark(&mut self, remark: String) {
self.remark = remark;
}

pub fn spent_time_mills(&self) -> u64 {
self.spent_time_mills
}

pub fn set_spent_time_mills(&mut self, spent_time_mills: u64) {
self.spent_time_mills = spent_time_mills;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::fmt::Display;

use cheetah_string::CheetahString;
use serde::Deserialize;
use serde::Serialize;

use crate::protocol::body::cm_result::CMResult;

#[derive(Debug, Serialize, Deserialize)]
pub struct ConsumeMessageDirectlyResult {
order: bool,
auto_commit: bool,
consume_result: Option<CMResult>,
remark: Option<CheetahString>,
spent_time_mills: u64,
}

impl Default for ConsumeMessageDirectlyResult {
#[inline]
fn default() -> Self {
Self {
order: false,
auto_commit: true,
consume_result: None,
remark: None,
spent_time_mills: 0,
}
}
}

impl Display for ConsumeMessageDirectlyResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"ConsumeMessageDirectlyResult [order={}, auto_commit={}, consume_result={:?}, \
remark={:?}, spent_time_mills={}]",
self.order, self.auto_commit, self.consume_result, self.remark, self.spent_time_mills
)
}
}

impl ConsumeMessageDirectlyResult {
pub fn new(
order: bool,
auto_commit: bool,
consume_result: CMResult,
remark: CheetahString,
spent_time_mills: u64,
) -> Self {
Self {
order,
auto_commit,
consume_result: Some(consume_result),
remark: Some(remark),
spent_time_mills,
}
}

#[inline]
pub fn order(&self) -> bool {
self.order
}

#[inline]
pub fn set_order(&mut self, order: bool) {
self.order = order;
}

#[inline]
pub fn auto_commit(&self) -> bool {
self.auto_commit
}

#[inline]
pub fn set_auto_commit(&mut self, auto_commit: bool) {
self.auto_commit = auto_commit;
}

#[inline]
pub fn consume_result(&self) -> &Option<CMResult> {
&self.consume_result
}

#[inline]
pub fn set_consume_result(&mut self, consume_result: CMResult) {
self.consume_result = Some(consume_result);
}

#[inline]
pub fn remark(&self) -> &Option<CheetahString> {
&self.remark
}

#[inline]
pub fn set_remark(&mut self, remark: CheetahString) {
self.remark = Some(remark);
}

#[inline]
pub fn spent_time_mills(&self) -> u64 {
self.spent_time_mills
}

#[inline]
pub fn set_spent_time_mills(&mut self, spent_time_mills: u64) {
self.spent_time_mills = spent_time_mills;
}
}

#[cfg(test)]
mod tests {
use cheetah_string::CheetahString;

use super::*;
use crate::protocol::body::cm_result::CMResult;

#[test]
fn consume_message_directly_result_default_initializes_correctly() {
let result = ConsumeMessageDirectlyResult::default();
assert!(!result.order());
assert!(result.auto_commit());
assert!(result.consume_result().is_none());
assert!(result.remark().is_none());
assert_eq!(result.spent_time_mills(), 0);
}

#[test]
fn consume_message_directly_result_new_initializes_correctly() {
let consume_result = CMResult::default();
let remark = CheetahString::from_static_str("test remark");
let result = ConsumeMessageDirectlyResult::new(
true,
false,
consume_result.clone(),
remark.clone(),
12345,
);
assert!(result.order());
assert!(!result.auto_commit());
assert_eq!(result.consume_result().as_ref().unwrap(), &consume_result);
assert_eq!(result.remark().as_ref().unwrap(), &remark);
assert_eq!(result.spent_time_mills(), 12345);
}

#[test]
fn consume_message_directly_result_setters_work_correctly() {
let mut result = ConsumeMessageDirectlyResult::default();
result.set_order(true);
result.set_auto_commit(false);
let consume_result = CMResult::default();
result.set_consume_result(consume_result.clone());
let remark = CheetahString::from_static_str("updated remark");
result.set_remark(remark.clone());
result.set_spent_time_mills(67890);

assert!(result.order());
assert!(!result.auto_commit());
assert_eq!(result.consume_result().as_ref().unwrap(), &consume_result);
assert_eq!(result.remark().as_ref().unwrap(), &remark);
assert_eq!(result.spent_time_mills(), 67890);
}

#[test]
fn consume_message_directly_result_display_formats_correctly() {
let consume_result = CMResult::default();
let remark = CheetahString::from_static_str("test remark");
let result = ConsumeMessageDirectlyResult::new(
true,
false,
consume_result.clone(),
remark.clone(),
12345,
);
let display = format!("{}", result);
let expected = format!(
"ConsumeMessageDirectlyResult [order=true, auto_commit=false, consume_result={:?}, \
remark={:?}, spent_time_mills=12345]",
Some(consume_result),
Some(remark)
);
assert_eq!(display, expected);
}
}

0 comments on commit 4718cd1

Please sign in to comment.