Example #1
0
  /**
   * Attempts to retrieve the specified tree rule from storage.
   *
   * @param tsdb The TSDB to use for storage access
   * @param tree_id ID of the tree the rule belongs to
   * @param level Level where the rule resides
   * @param order Order where the rule resides
   * @return A TreeRule object if found, null if it does not exist
   * @throws HBaseException if there was an issue
   * @throws IllegalArgumentException if the one of the required parameters was missing
   * @throws JSONException if the object could not be serialized
   */
  public static Deferred<TreeRule> fetchRule(
      final TSDB tsdb, final int tree_id, final int level, final int order) {
    if (tree_id < 1 || tree_id > 65535) {
      throw new IllegalArgumentException("Invalid Tree ID");
    }
    if (level < 0) {
      throw new IllegalArgumentException("Invalid rule level");
    }
    if (order < 0) {
      throw new IllegalArgumentException("Invalid rule order");
    }

    // fetch the whole row
    final GetRequest get = new GetRequest(tsdb.treeTable(), Tree.idToBytes(tree_id));
    get.family(Tree.TREE_FAMILY());
    get.qualifier(getQualifier(level, order));

    /** Called after fetching to parse the results */
    final class FetchCB implements Callback<Deferred<TreeRule>, ArrayList<KeyValue>> {

      @Override
      public Deferred<TreeRule> call(final ArrayList<KeyValue> row) {
        if (row == null || row.isEmpty()) {
          return Deferred.fromResult(null);
        }
        return Deferred.fromResult(parseFromStorage(row.get(0)));
      }
    }

    return tsdb.getClient().get(get).addCallbackDeferring(new FetchCB());
  }
Example #2
0
  /**
   * Attempts to delete all rules belonging to the given tree.
   *
   * @param tsdb The TSDB to use for storage access
   * @param tree_id ID of the tree the rules belongs to
   * @return A deferred to wait on for completion. The value has no meaning and may be null.
   * @throws HBaseException if there was an issue
   * @throws IllegalArgumentException if the one of the required parameters was missing
   */
  public static Deferred<Object> deleteAllRules(final TSDB tsdb, final int tree_id) {
    if (tree_id < 1 || tree_id > 65535) {
      throw new IllegalArgumentException("Invalid Tree ID");
    }

    // fetch the whole row
    final GetRequest get = new GetRequest(tsdb.treeTable(), Tree.idToBytes(tree_id));
    get.family(Tree.TREE_FAMILY());

    /**
     * Called after fetching the requested row. If the row is empty, we just return, otherwise we
     * compile a list of qualifiers to delete and submit a single delete request to storage.
     */
    final class GetCB implements Callback<Deferred<Object>, ArrayList<KeyValue>> {

      @Override
      public Deferred<Object> call(final ArrayList<KeyValue> row) throws Exception {
        if (row == null || row.isEmpty()) {
          return Deferred.fromResult(null);
        }

        final ArrayList<byte[]> qualifiers = new ArrayList<byte[]>(row.size());

        for (KeyValue column : row) {
          if (column.qualifier().length > RULE_PREFIX.length
              && Bytes.memcmp(RULE_PREFIX, column.qualifier(), 0, RULE_PREFIX.length) == 0) {
            qualifiers.add(column.qualifier());
          }
        }

        final DeleteRequest delete =
            new DeleteRequest(
                tsdb.treeTable(),
                Tree.idToBytes(tree_id),
                Tree.TREE_FAMILY(),
                qualifiers.toArray(new byte[qualifiers.size()][]));
        return tsdb.getClient().delete(delete);
      }
    }

    return tsdb.getClient().get(get).addCallbackDeferring(new GetCB());
  }