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


##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,201 @@
+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 {
+  //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> 
metaClientOpToZk(Iterable<org.apache.helix.metaclient.api.Op> ops) {
+    getOpMap().put(org.apache.helix.metaclient.api.Op.Type.CREATE, op -> {
+      try {
+        CreateMode mode = 
convertMetaClientMode(((org.apache.helix.metaclient.api.Op.Create) 
op).getEntryMode());
+        return Op.create(op.getPath(), 
((org.apache.helix.metaclient.api.Op.Create) op).getData(), DEFAULT_ACL, mode);
+      } catch (KeeperException e) {
+        throw translateZkExceptionToMetaclientException(ZkException.create(e));
+      }
+    });
+
+    getOpMap().put(org.apache.helix.metaclient.api.Op.Type.DELETE,
+        op -> Op.delete(op.getPath(), 
((org.apache.helix.metaclient.api.Op.Delete) op).getVersion()));
+
+    getOpMap().put(org.apache.helix.metaclient.api.Op.Type.SET,
+        op -> Op.setData(op.getPath(),
+            ((org.apache.helix.metaclient.api.Op.Set) op).getData(), 
((org.apache.helix.metaclient.api.Op.Set) op).getVersion()));
+
+    getOpMap().put(org.apache.helix.metaclient.api.Op.Type.CHECK,
+        op -> Op.check(op.getPath(), 
((org.apache.helix.metaclient.api.Op.Check) op).getVersion()));
+
+    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() {
+    return OpmapHolder.OPMAP;
+  }
+
+  private static CreateMode 
convertMetaClientMode(MetaClientInterface.EntryMode entryMode) throws 
KeeperException {
+    if (entryMode == MetaClientInterface.EntryMode.PERSISTENT) {
+      return CreateMode.fromFlag(0);

Review Comment:
   +1 to Rahul's comment, I think we can just clearly type to return 
CreateMode.PERSISTENT or CreateMode.EPHEMERAL etc.



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