rocketmq_remoting/protocol/static_topic/
topic_queue_mapping_context.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 */
17use cheetah_string::CheetahString;
18use rocketmq_rust::ArcMut;
19
20use crate::protocol::static_topic::logic_queue_mapping_item::LogicQueueMappingItem;
21use crate::protocol::static_topic::topic_queue_mapping_detail::TopicQueueMappingDetail;
22
23#[derive(Default)]
24pub struct TopicQueueMappingContext {
25    pub topic: CheetahString,
26    pub global_id: Option<i32>,
27    pub mapping_detail: Option<ArcMut<TopicQueueMappingDetail>>,
28    pub mapping_item_list: Vec<LogicQueueMappingItem>,
29    pub leader_item: Option<LogicQueueMappingItem>,
30    pub current_item: Option<LogicQueueMappingItem>,
31}
32
33impl TopicQueueMappingContext {
34    pub fn new(
35        topic: impl Into<CheetahString>,
36        global_id: Option<i32>,
37        mapping_detail: Option<ArcMut<TopicQueueMappingDetail>>,
38        mapping_item_list: Vec<LogicQueueMappingItem>,
39        leader_item: Option<LogicQueueMappingItem>,
40    ) -> Self {
41        Self {
42            topic: topic.into(),
43            global_id,
44            mapping_detail,
45            mapping_item_list,
46            leader_item,
47            current_item: None,
48        }
49    }
50}
51
52impl TopicQueueMappingContext {
53    pub fn is_leader(&self) -> bool {
54        if let Some(leader_item) = &self.leader_item {
55            match leader_item.bname {
56                None => {
57                    return false;
58                }
59                Some(ref broker_name) => match self.mapping_detail.as_ref() {
60                    None => return false,
61                    Some(inner) => match inner.topic_queue_mapping_info.bname {
62                        None => return false,
63                        Some(ref inner_bname) => {
64                            return inner_bname == broker_name;
65                        }
66                    },
67                },
68            }
69        }
70        false
71    }
72}