1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.zookeeper; 20 21 import org.apache.yetus.audience.InterfaceAudience; 22 import org.slf4j.Logger; 23 import org.slf4j.LoggerFactory; 24 25 /*** 26 * CreateMode value determines how the znode is created on ZooKeeper. 27 */ 28 @InterfaceAudience.Public 29 public enum CreateMode { 30 31 /** 32 * The znode will not be automatically deleted upon client's disconnect. 33 */ 34 PERSISTENT(0, false, false, false, false), 35 /** 36 * The znode will not be automatically deleted upon client's disconnect, 37 * and its name will be appended with a monotonically increasing number. 38 */ 39 PERSISTENT_SEQUENTIAL(2, false, true, false, false), 40 /** 41 * The znode will be deleted upon the client's disconnect. 42 */ 43 EPHEMERAL(1, true, false, false, false), 44 /** 45 * The znode will be deleted upon the client's disconnect, and its name 46 * will be appended with a monotonically increasing number. 47 */ 48 EPHEMERAL_SEQUENTIAL(3, true, true, false, false), 49 /** 50 * The znode will be a container node. Container 51 * nodes are special purpose nodes useful for recipes such as leader, lock, 52 * etc. When the last child of a container is deleted, the container becomes 53 * a candidate to be deleted by the server at some point in the future. 54 * Given this property, you should be prepared to get 55 * {@link org.apache.zookeeper.KeeperException.NoNodeException} 56 * when creating children inside of this container node. 57 */ 58 CONTAINER(4, false, false, true, false), 59 /** 60 * The znode will not be automatically deleted upon client's disconnect. 61 * However if the znode has not been modified within the given TTL, it 62 * will be deleted once it has no children. 63 */ 64 PERSISTENT_WITH_TTL(5, false, false, false, true), 65 /** 66 * The znode will not be automatically deleted upon client's disconnect, 67 * and its name will be appended with a monotonically increasing number. 68 * However if the znode has not been modified within the given TTL, it 69 * will be deleted once it has no children. 70 */ 71 PERSISTENT_SEQUENTIAL_WITH_TTL(6, false, true, false, true); 72 73 private static final Logger LOG = LoggerFactory.getLogger(CreateMode.class); 74 75 private boolean ephemeral; 76 private boolean sequential; 77 private final boolean isContainer; 78 private int flag; 79 private boolean isTTL; 80 CreateMode(int flag, boolean ephemeral, boolean sequential, boolean isContainer, boolean isTTL)81 CreateMode(int flag, boolean ephemeral, boolean sequential, boolean isContainer, boolean isTTL) { 82 this.flag = flag; 83 this.ephemeral = ephemeral; 84 this.sequential = sequential; 85 this.isContainer = isContainer; 86 this.isTTL = isTTL; 87 } 88 isEphemeral()89 public boolean isEphemeral() { 90 return ephemeral; 91 } 92 isSequential()93 public boolean isSequential() { 94 return sequential; 95 } 96 isContainer()97 public boolean isContainer() { 98 return isContainer; 99 } 100 isTTL()101 public boolean isTTL() { 102 return isTTL; 103 } 104 toFlag()105 public int toFlag() { 106 return flag; 107 } 108 109 /** 110 * Map an integer value to a CreateMode value 111 */ fromFlag(int flag)112 public static CreateMode fromFlag(int flag) throws KeeperException { 113 switch (flag) { 114 case 0: 115 return CreateMode.PERSISTENT; 116 117 case 1: 118 return CreateMode.EPHEMERAL; 119 120 case 2: 121 return CreateMode.PERSISTENT_SEQUENTIAL; 122 123 case 3: 124 return CreateMode.EPHEMERAL_SEQUENTIAL; 125 126 case 4: 127 return CreateMode.CONTAINER; 128 129 case 5: 130 return CreateMode.PERSISTENT_WITH_TTL; 131 132 case 6: 133 return CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL; 134 135 default: 136 String errMsg = "Received an invalid flag value: " + flag + " to convert to a CreateMode"; 137 LOG.error(errMsg); 138 throw new KeeperException.BadArgumentsException(errMsg); 139 } 140 } 141 142 /** 143 * Map an integer value to a CreateMode value 144 */ fromFlag(int flag, CreateMode defaultMode)145 public static CreateMode fromFlag(int flag, CreateMode defaultMode) { 146 switch (flag) { 147 case 0: 148 return CreateMode.PERSISTENT; 149 150 case 1: 151 return CreateMode.EPHEMERAL; 152 153 case 2: 154 return CreateMode.PERSISTENT_SEQUENTIAL; 155 156 case 3: 157 return CreateMode.EPHEMERAL_SEQUENTIAL; 158 159 case 4: 160 return CreateMode.CONTAINER; 161 162 case 5: 163 return CreateMode.PERSISTENT_WITH_TTL; 164 165 case 6: 166 return CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL; 167 168 default: 169 return defaultMode; 170 } 171 } 172 } 173