Ejemplo n.º 1
0
  /**
   * Parses a query string "m=metric{tagk1=tagv1,...}" type query and returns a tsuid.
   *
   * @param data_query The query we're building
   * @throws BadRequestException if we are unable to parse the query or it is missing components
   * @todo - make this asynchronous
   */
  private String getTSUIDForMetric(final String query_string, TSDB tsdb) {
    if (query_string == null || query_string.isEmpty()) {
      throw new BadRequestException("The query string was empty");
    }

    // m is of the following forms:
    // metric[{tag=value,...}]
    // where the parts in square brackets `[' .. `]' are optional.
    final HashMap<String, String> tags = new HashMap<String, String>();
    String metric = null;
    try {
      metric = Tags.parseWithMetric(query_string, tags);
    } catch (IllegalArgumentException e) {
      throw new BadRequestException(e);
    }

    // sort the UIDs on tagk values
    final ByteMap<byte[]> tag_uids = new ByteMap<byte[]>();
    for (final Entry<String, String> pair : tags.entrySet()) {
      tag_uids.put(
          tsdb.getUID(UniqueIdType.TAGK, pair.getKey()),
          tsdb.getUID(UniqueIdType.TAGV, pair.getValue()));
    }

    // Byte Buffer to generate TSUID, pre allocated to the size of the TSUID
    final ByteArrayOutputStream buf =
        new ByteArrayOutputStream(
            TSDB.metrics_width() + tag_uids.size() * (TSDB.tagk_width() + TSDB.tagv_width()));
    try {
      buf.write(tsdb.getUID(UniqueIdType.METRIC, metric));
      for (final Entry<byte[], byte[]> uids : tag_uids.entrySet()) {
        buf.write(uids.getKey());
        buf.write(uids.getValue());
      }
    } catch (IOException e) {
      throw new BadRequestException(e);
    }
    final String tsuid = UniqueId.uidToString(buf.toByteArray());

    return tsuid;
  }
Ejemplo n.º 2
0
 /**
  * Get the next random UID. Right now it uses 3 bytes as the metric with is 3. If it is 3 then it
  * can return only upto the max value a 3 byte integer can return, which is 2^31-1. In that case,
  * even though it is long, its range will be between 0 and 2^31-1
  *
  * @return random UID
  */
 public static long getRandomUID() {
   return getRandomUID(TSDB.metrics_width());
 }