title: Most efficient way to create a path in zookeeper where root elements of the path may or may not exist?
body: <p>Imagine a path "/root/child1/child2/child3"</p>
  
  <p>Imagine in zookeeper that maybe a part of this exists, say "/root/child1"</p>
  
  <p>There is no equivalent of "mkdir -p" in zookeeper; Also, ZooKeeper.multi() will fail if any one operation fails, so a "make path" couldn't really be baked into a multi call.   Additionally, you could have some other client trying to make the same path...</p>
  
  <p>This is what I have come up with for creating a path.  I wonder if it is even worth checking to see if a part exists or not, to save the round trip of the exists() call.</p>
  
  <pre><code>//String[] pathParts new String[] { "root", "child1", "child2", "child3" };
  
  public void savePath(String[] pathParts) {
      if (zooKeeper.exists(pathString, false) != null) return;
      StringBuilder path = new StringBuilder();
      for (String pathElement : pathParts) {
          path.append(UNIX_FILE_SEPARATOR).append(pathElement);
          String pathString = path.toString();
          try {
              //bother with the exists call or not?
              if (zooKeeper.exists(pathString, false) == null) {
                  zooKeeper.create(pathString, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
              }
          } catch (KeeperException e) {
              if (e.code() != KeeperException.Code.NODEEXISTS)
                  throw e;
          }
      }
  }
  </code></pre>
  
  <p>What would be the most efficient way to do this?   Assuming that a) you don't know ahead of time how much of the path already exists and b) some other client might be trying to write the same path (and we want to avoid locking).</p>