Marcosrico commented on code in PR #2343:
URL: https://github.com/apache/helix/pull/2343#discussion_r1084648006


##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,208 @@
+package org.apache.helix.metaclient.impl.zk.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 org.apache.helix.metaclient.api.MetaClientInterface;
+import org.apache.helix.metaclient.api.OpResult;
+import org.apache.helix.metaclient.constants.*;
+import org.apache.helix.zookeeper.zkclient.exception.*;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Op;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.data.ACL;
+import org.apache.zookeeper.server.EphemeralType;
+
+import java.util.Collections;
+import java.util.EnumMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.function.Function;
+
+public class ZkMetaClientUtil {
+  //TODO Implement MetaClient ACL
+  //Default ACL value until metaClient Op has ACL of its own.
+  private static final List<ACL> DEFAULT_ACL = 
Collections.unmodifiableList(ZooDefs.Ids.OPEN_ACL_UNSAFE);
+
+  private ZkMetaClientUtil(){
+  }
+
+  /**
+   * Helper function for transactionOp. Converts MetaClient Op's into Zk Ops 
to execute
+   * zk transactional support.
+   * @param ops
+   * @return
+   */
+  public static List<Op> 
metaClientOpsToZkOps(Iterable<org.apache.helix.metaclient.api.Op> ops) {
+    initializeOpMap();
+    List<Op> zkOps = new ArrayList<>();
+    for (org.apache.helix.metaclient.api.Op op : ops) {
+      Function<org.apache.helix.metaclient.api.Op, Op> function = 
getOpMap().get(op.getType());
+      if (function != null) {
+        zkOps.add(function.apply(op));
+      } else {
+        throw new IllegalArgumentException("Op type " + op.getType().name() + 
" is not supported.");
+      }
+    }
+    return zkOps;
+  }
+
+  private static final class OpMapHolder {
+    static final Map<org.apache.helix.metaclient.api.Op.Type, 
Function<org.apache.helix.metaclient.api.Op, Op>> OPMAP =
+        new EnumMap<>(org.apache.helix.metaclient.api.Op.Type.class);
+  }
+
+  private static Map<org.apache.helix.metaclient.api.Op.Type, 
Function<org.apache.helix.metaclient.api.Op, Op>> getOpMap() {

Review Comment:
   Very true, calling OPMAP directly would still work for lazy instantiation. 
However, the get method provides a layer of abstraction in case the 
implementation of OPMAP changes, which in my opinion outweighs the minor 
overhead cost of calling the get method.
   
   And you bring up a good point, I was outweighing the costs of implementing 
lazy instantiation to begin with and having the map be eagerly instantiated in 
the beginning of ZkMetaClientUtil class. Considering that this class is a util 
class for anything zkmetaclient related, it can be used in the future for other 
actions not related to transactional support. While there isn't much else going 
on, by providing lazy instantiation we can minimize overhead in the long run if 
ZkMetaClientUtil has increased use cases and/or each map gets larger. 



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