Marcosrico commented on code in PR #2343: URL: https://github.com/apache/helix/pull/2343#discussion_r1084429118
########## meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java: ########## @@ -0,0 +1,213 @@ +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) { + if (getOpMap().isEmpty()) { + 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() { + return OpMapHolder.OPMAP; + } + + private static void initializeOpMap() { + 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())); + } + + private static CreateMode convertMetaClientMode(MetaClientInterface.EntryMode entryMode) throws KeeperException { + switch (entryMode) { + case PERSISTENT: + return CreateMode.PERSISTENT; + case EPHEMERAL: + return CreateMode.EPHEMERAL; + case CONTAINER: + return CreateMode.CONTAINER; + default: + throw new IllegalArgumentException(entryMode.name() + " is not a supported EntryMode."); + } + } + + /** + * Helper function for transactionOP. Converts the result from calling zk transactional support into + * metaclient OpResults. + * @param zkResult + * @return + */ + public static List<OpResult> zkOpResultToMetaClientOpResults(List<org.apache.zookeeper.OpResult> zkResult) { + if (getOpResultMap().isEmpty()) { + initializeOpResultMap(); + } + List<OpResult> metaClientOpResult = new ArrayList<>(); + for (org.apache.zookeeper.OpResult opResult : zkResult) { + Function<org.apache.zookeeper.OpResult, OpResult> function = getOpResultMap().get(opResult.getClass()); + if (function != null) { + metaClientOpResult.add(function.apply(opResult)); + } else { + throw new IllegalArgumentException("OpResult type " + opResult.getType() + "is not supported."); + } + } + + return metaClientOpResult; + } + + private static final class OpResultMapHolder { + static final Map<Class<? extends org.apache.zookeeper.OpResult>, Function<org.apache.zookeeper.OpResult, OpResult>> OPRESULTMAP = + new HashMap<>(); + } Review Comment: I removed the unnecessary null check before each "op translate" as initializing the map guarantees non null value of opMap and opResultMap. Let me know if that addresses the comment, thanks! -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
