code: public Deferred<Integer> createNewTree(final TSDB tsdb) {
      if (tree_id > 0) {
        throw new IllegalArgumentException("Tree ID has already been set");
      }
      if (name == null || name.isEmpty()) {
        throw new IllegalArgumentException("Tree was missing the name");
      }
      
      /**
       * Called after a successful CAS to store the new tree with the new ID.
       * Returns the new ID if successful, 0 if there was an error
       */
      final class CreatedCB implements Callback<Deferred<Integer>, Boolean> {
        
        @Override
        public Deferred<Integer> call(final Boolean cas_success) 
          throws Exception {
          return Deferred.fromResult(tree_id);
        }
        
      }
      
      /**
       * Called after fetching all trees. Loops through the tree definitions and
       * determines the max ID so we can increment and write a new one
       */
      final class CreateNewCB implements Callback<Deferred<Integer>, List<Tree>> {
  
        @Override
        public Deferred<Integer> call(List<Tree> trees) throws Exception {
          int max_id = 0;
          if (trees != null) {
            for (Tree tree : trees) {
              if (tree.tree_id > max_id) {
                max_id = tree.tree_id;
              }
            }
          }
          
          tree_id = max_id + 1;
          if (tree_id > 65535) {
            throw new IllegalStateException("Exhausted all Tree IDs");
          }
          
          return storeTree(tsdb, true).addCallbackDeferring(new CreatedCB());
        }
        
      }
      
      // starts the process by fetching all tree definitions from storage
      return fetchAllTrees(tsdb).addCallbackDeferring(new CreateNewCB());
    }
docs: Attempts to store the local tree in a new row, automatically assigning a
  new tree ID and returning the value.
  This method will scan the UID table for the maximum tree ID, increment it,
  store the new tree, and return the new ID. If no trees have been created,
  the returned ID will be "1". If we have reached the limit of trees for the
  system, as determined by {@link #TREE_ID_WIDTH}, we will throw an exception.
  @param tsdb The TSDB to use for storage access
  @return A positive ID, greater than 0 if successful, 0 if there was
  an error