// This file is part of OpenTSDB.
// Copyright (C) 2010-2012  The OpenTSDB Authors.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.core;

import java.util.Arrays;

import com.stumbleupon.async.Deferred;

/** Helper functions to deal with the row key. */
final class RowKey {

  private RowKey() {
    // Can't create instances of this utility class.
  }

  /**
   * Extracts the name of the metric ID contained in a row key.
   * @param tsdb The TSDB to use.
   * @param row The actual row key.
   * @return The name of the metric.
   */
  static String metricName(final TSDB tsdb, final byte[] row) {
    try {
      return metricNameAsync(tsdb, row).joinUninterruptibly();
    } catch (RuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw new RuntimeException("Should never be here", e);
    }
  }

  /**
   * Extracts the name of the metric ID contained in a row key.
   * @param tsdb The TSDB to use.
   * @param row The actual row key.
   * @return The name of the metric.
   * @since 1.2
   */
  static Deferred<String> metricNameAsync(final TSDB tsdb, final byte[] row) {
    final byte[] id = Arrays.copyOfRange(row, 0, tsdb.metrics.width());
    return tsdb.metrics.getNameAsync(id);
  }
}

@@ -796,16 +796,47 @@
 rrays;%0A%0A
+import org.hbase.async.Bytes;%0A%0A
 import c
@@ -866,16 +866,16 @@
 ferred;%0A
-
 %0A/** Hel
@@ -923,16 +923,23 @@
 /%0Afinal 
+public 
 class Ro
@@ -1845,16 +1845,16 @@
 dth());%0A
-
     retu
@@ -1891,10 +1891,1222 @@
 d);%0A  %7D%0A
+  %0A  /**%0A   * Generates a row key given a TSUID and an absolute timestamp. The timestamp%0A   * will be normalized to an hourly base time.%0A   * @param tsdb The TSDB to use for fetching tag widths%0A   * @param tsuid The TSUID to use for the key%0A   * @param timestamp An absolute time from which we generate the row base time%0A   * @return A row key for use in fetching data from OpenTSDB%0A   * @since 2.0%0A   */%0A  public static byte%5B%5D rowKeyFromTSUID(final TSDB tsdb, final byte%5B%5D tsuid, %0A      final long timestamp) %7B%0A    final long base_time;%0A    if ((timestamp & Const.SECOND_MASK) != 0) %7B%0A      // drop the ms timestamp to seconds to calculate the base timestamp%0A      base_time = ((timestamp / 1000) - %0A          ((timestamp / 1000) %25 Const.MAX_TIMESPAN));%0A    %7D else %7B%0A      base_time = (timestamp - (timestamp %25 Const.MAX_TIMESPAN));%0A    %7D%0A    final byte%5B%5D row = new byte%5Btsuid.length + Const.TIMESTAMP_BYTES%5D;%0A    System.arraycopy(tsuid, 0, row, 0, TSDB.metrics_width());%0A    Bytes.setInt(row, (int) base_time, TSDB.metrics_width());%0A    System.arraycopy(tsuid, TSDB.metrics_width(), row, %0A        TSDB.metrics_width() + Const.TIMESTAMP_BYTES, %0A        tsuid.length - TSDB.metrics_width());%0A    return row;%0A  %7D%0A
 %7D%0A

Add RowKey.rowKeyFromTSUID() to generate a row key given a TSUID and a timestamp