DongyuanPan commented on code in PR #131:
URL: https://github.com/apache/rocketmq-mqtt/pull/131#discussion_r941302869


##########
meta/src/main/java/org/apache/rocketmq/mqtt/meta/raft/processor/RetainedMsgStateProcess.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.
+ */
+
+package org.apache.rocketmq.mqtt.meta.raft.processor;
+
+import com.alibaba.fastjson.JSON;
+import com.alipay.sofa.jraft.storage.snapshot.SnapshotReader;
+import com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter;
+import com.google.protobuf.ByteString;
+import org.apache.rocketmq.mqtt.common.model.Trie;
+import org.apache.rocketmq.mqtt.common.model.consistency.ReadRequest;
+import org.apache.rocketmq.mqtt.common.model.consistency.Response;
+import org.apache.rocketmq.mqtt.common.model.consistency.WriteRequest;
+import org.apache.rocketmq.mqtt.common.util.TopicUtils;
+import org.apache.rocketmq.mqtt.meta.raft.snapshot.SnapshotOperation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.util.ArrayList;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.BiConsumer;
+
+
+public class RetainedMsgStateProcess extends StateProcessor {
+
+    private static Logger logger = 
LoggerFactory.getLogger(RetainedMsgStateProcess.class);
+    private final ConcurrentHashMap<String, byte[]> retainedMsgMap = new 
ConcurrentHashMap<>();  //key:topic value:retained msg
+    private final ConcurrentHashMap<String, Trie<String, String>> 
retainedMsgTopicTrie = new ConcurrentHashMap<>();  //key:firstTopic 
value:retained topic Trie
+
+    protected final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+    private SnapshotOperation snapshotOperation;
+    private int maxRetainedMessageNum ;
+
+    public RetainedMsgStateProcess(int maxRetainedMessageNum) {
+        setMaxRetainedMessageNum(maxRetainedMessageNum);
+    }
+    @Override
+    public Response onReadRequest(ReadRequest request) {
+        try {
+            String topic = request.getExtDataMap().get("topic");
+            String firstTopic = request.getExtDataMap().get("firstTopic");
+            String operation = request.getOperation();
+
+            logger.info("FirstTopic:{} Topic:{} 
Operation:{}",firstTopic,topic,operation);
+
+            if (operation.equals("topic")) {    //return retained msg
+                byte[] msgBytes = retainedMsgMap.get(topic);
+                return Response.newBuilder()
+                    .setSuccess(true)
+                    .setData(ByteString.copyFrom(msgBytes))
+                    .build();
+            } else { //return retain msgs of matched Topic
+                if (!retainedMsgTopicTrie.containsKey(firstTopic)) {
+                    Trie<String, String> newTrie = new Trie<>();
+                    retainedMsgTopicTrie.put(firstTopic, newTrie);

Review Comment:
   Judging that there is no such topic, after initializing the trie tree, you 
can return directly



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to