Esempio n. 1
0
 public static void updateOrCreate(
     ZooKeeperConnection zooKeeper, String path, byte[] value, CreateMode createMode)
     throws InterruptedException, KeeperException {
   boolean success = false;
   if (zooKeeper.exists(path, false) == null) {
     success = createFullPath(zooKeeper, path, value, createMode, true);
   }
   if (!success) zooKeeper.setData(path, value, -1);
 }
Esempio n. 2
0
 public static boolean createIfNotExists(
     ZooKeeperConnection zooKeeper, String path, byte[] value, CreateMode createMode)
     throws KeeperException, InterruptedException {
   if (zooKeeper.exists(path, false) == null) {
     try {
       zooKeeper.create(path, value, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
     } catch (KeeperException e) {
       if (e.code() != KeeperException.Code.NODEEXISTS) throw e;
       return false;
     }
     return true;
   }
   return false;
 }
Esempio n. 3
0
 public static boolean createFullPath(
     ZooKeeperConnection zooKeeperConnection,
     String path,
     byte[] value,
     CreateMode createMode,
     boolean ignoreIfExists)
     throws KeeperException, InterruptedException {
   final byte[] empty = new byte[0];
   final String[] nodes = path.split("/");
   final StringBuilder pathBuilder = new StringBuilder();
   for (int i = 1; i < nodes.length - 1; i++) {
     pathBuilder.append("/").append(nodes[i]);
     createIfNotExists(zooKeeperConnection, pathBuilder.toString(), empty, CreateMode.PERSISTENT);
   }
   pathBuilder.append("/").append(nodes[nodes.length - 1]);
   if (ignoreIfExists) {
     return createIfNotExists(zooKeeperConnection, path, value, createMode);
   }
   zooKeeperConnection.create(
       pathBuilder.toString(), value, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
   return true;
 }