caqhy commented on code in PR #20984:
URL: https://github.com/apache/shardingsphere/pull/20984#discussion_r976358743


##########
shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-nacos/src/main/java/org/apache/shardingsphere/mode/repository/cluster/nacos/listener/NamingEventListener.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.shardingsphere.mode.repository.cluster.nacos.listener;
+
+import com.alibaba.nacos.api.naming.listener.Event;
+import com.alibaba.nacos.api.naming.listener.EventListener;
+import com.alibaba.nacos.api.naming.listener.NamingEvent;
+import com.alibaba.nacos.api.naming.pojo.Instance;
+import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
+import 
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEventListener;
+import 
org.apache.shardingsphere.mode.repository.cluster.nacos.utils.MetadataUtil;
+
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/**
+ * Naming event listener.
+ */
+@RequiredArgsConstructor
+public class NamingEventListener implements EventListener {

Review Comment:
   OK



##########
shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-nacos/src/main/java/org/apache/shardingsphere/mode/repository/cluster/nacos/listener/NamingEventListener.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.shardingsphere.mode.repository.cluster.nacos.listener;
+
+import com.alibaba.nacos.api.naming.listener.Event;
+import com.alibaba.nacos.api.naming.listener.EventListener;
+import com.alibaba.nacos.api.naming.listener.NamingEvent;
+import com.alibaba.nacos.api.naming.pojo.Instance;
+import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
+import 
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEventListener;
+import 
org.apache.shardingsphere.mode.repository.cluster.nacos.utils.MetadataUtil;
+
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/**
+ * Naming event listener.
+ */
+@RequiredArgsConstructor
+public class NamingEventListener implements EventListener {
+    
+    private Map<String, Instance> preInstances = new HashMap<>();
+    
+    private final Map<String, DataChangedEventListener> prefixListenerMap;
+    
+    @Override
+    public void onEvent(final Event event) {
+        if (event instanceof NamingEvent) {
+            NamingEvent namingEvent = (NamingEvent) event;
+            List<Instance> instances = 
namingEvent.getInstances().stream().sorted(Comparator.comparing(MetadataUtil::getKey)).collect(Collectors.toList());
+            List<WatchData> watchDataList = new LinkedList<>();
+            instances.forEach(instance -> 
prefixListenerMap.forEach((prefixPath, listener) -> {
+                String key = MetadataUtil.getKey(instance);
+                if (key.startsWith(prefixPath)) {
+                    Instance preInstance = preInstances.remove(key);
+                    WatchData watchData = new WatchData(key, preInstance, 
instance, listener);
+                    watchDataList.add(watchData);
+                }
+            }));
+            
preInstances.values().stream().sorted(Comparator.comparing(MetadataUtil::getKey).reversed()).forEach(instance
 -> prefixListenerMap.forEach((prefixPath, listener) -> {
+                String key = MetadataUtil.getKey(instance);
+                if (key.startsWith(prefixPath)) {
+                    WatchData watchData = new WatchData(key, instance, null, 
listener);
+                    watchDataList.add(watchData);
+                }
+            }));
+            watchDataList.forEach(watchData -> {
+                String key = watchData.getKey();
+                Instance preInstance = watchData.getPreInstance();
+                Instance instance = watchData.getInstance();
+                DataChangedEventListener listener = watchData.getListener();
+                DataChangedEvent.Type changedType = 
getEventChangedType(preInstance, instance);
+                switch (changedType) {
+                    case ADDED:
+                    case UPDATED:
+                        listener.onChange(new DataChangedEvent(key, 
MetadataUtil.getValue(instance), changedType));
+                        break;
+                    case DELETED:
+                        listener.onChange(new DataChangedEvent(key, 
MetadataUtil.getValue(preInstance), changedType));
+                        break;
+                    default:
+                }
+            });
+            setPreInstances(instances);
+        }
+    }
+    
+    private DataChangedEvent.Type getEventChangedType(final Instance 
preInstance, final Instance instance) {
+        DataChangedEvent.Type type;
+        if (Objects.isNull(preInstance) && Objects.nonNull(instance)) {
+            type = DataChangedEvent.Type.ADDED;
+        } else if (Objects.nonNull(preInstance) && Objects.nonNull(instance)
+                && MetadataUtil.getTimestamp(preInstance) != 
MetadataUtil.getTimestamp(instance)) {
+            type = DataChangedEvent.Type.UPDATED;
+        } else if (Objects.nonNull(preInstance) && Objects.isNull(instance)) {
+            type = DataChangedEvent.Type.DELETED;
+        } else {
+            return DataChangedEvent.Type.IGNORED;
+        }
+        return type;

Review Comment:
   OK



-- 
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