junkaixue commented on code in PR #2895:
URL: https://github.com/apache/helix/pull/2895#discussion_r1737439312


##########
helix-gateway/src/main/java/org/apache/helix/gateway/util/GatewayCurrentStateCache.java:
##########
@@ -0,0 +1,144 @@
+package org.apache.helix.gateway.util;
+
+/*
+ * 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.
+ */
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+
+public class GatewayCurrentStateCache {
+  String _clusterName;
+
+  // A cache of current state. It should be updated by the 
HelixGatewayServiceChannel
+  // instance -> instances assignments
+  Map<String, Map<String, String>> _currentStateMap;
+
+  // A cache of target state.
+  // instance -> assignments
+  Map<String, Map<String, String>> _targetStateMap;
+
+  boolean _targetStateChanged = false;
+
+  public GatewayCurrentStateCache(String clusterName) {
+    _clusterName = clusterName;
+    _currentStateMap = new ConcurrentHashMap<>();
+    _targetStateMap = new ConcurrentHashMap<>();
+  }
+
+  public String getCurrentState(String instance, String shard) {
+    return _currentStateMap.get(instance).get(shard);
+  }
+
+  /**
+   * Update the cached current state of instances in a cluster, and return the 
diff of the change.
+   * @param newCurrentStateMap The new current state map of instances in the 
cluster
+   * @return
+   */
+  public Map<String, Map<String, String>> 
updateCacheWithNewCurrentStateAndGetDiff(
+      Map<String, Map<String, String>> newCurrentStateMap) {
+    Map<String, Map<String, String>> diff = null;
+    for (Map.Entry<String, Map<String, String>> entry : 
newCurrentStateMap.entrySet()) {
+      String instance = entry.getKey();
+      Map<String, String> newCurrentState = entry.getValue();
+      Map<String, String> oldCurrentState = _currentStateMap.get(instance);
+      if (oldCurrentState == null || !oldCurrentState.equals(newCurrentState)) 
{
+        if (diff == null) {
+          diff = new HashMap<>();
+        }

Review Comment:
   Not really, comparing with looping the number of partitions for a cluster. 
Instantiate an empty map is very small portion.



-- 
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: reviews-unsubscr...@helix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@helix.apache.org
For additional commands, e-mail: reviews-h...@helix.apache.org

Reply via email to