Converts the given metric and tags to a TSUID by resolving the strings to their UIDs. Note that the resulting TSUID may not exist if the combination was not written to TSDB @param tsdb The TSDB to use for storage access @param metric The metric name to resolve @param tags The tags to resolve. May no
(final TSDB tsdb,
final String metric, final Map<String, String> tags)
| 473 | * empty. |
| 474 | */ |
| 475 | public static Deferred<byte[]> tsuidFromMetric(final TSDB tsdb, |
| 476 | final String metric, final Map<String, String> tags) { |
| 477 | if (metric == null || metric.isEmpty()) { |
| 478 | throw new IllegalArgumentException("The metric cannot be empty"); |
| 479 | } |
| 480 | if (tags == null || tags.isEmpty()) { |
| 481 | throw new IllegalArgumentException("Tags cannot be null or empty " |
| 482 | + "when getting a TSUID"); |
| 483 | } |
| 484 | |
| 485 | final byte[] metric_uid = new byte[TSDB.metrics_width()]; |
| 486 | |
| 487 | class TagsCB implements Callback<byte[], ArrayList<byte[]>> { |
| 488 | @Override |
| 489 | public byte[] call(final ArrayList<byte[]> tag_list) throws Exception { |
| 490 | final byte[] tsuid = new byte[metric_uid.length + |
| 491 | ((TSDB.tagk_width() + TSDB.tagv_width()) |
| 492 | * tag_list.size())]; |
| 493 | int idx = 0; |
| 494 | System.arraycopy(metric_uid, 0, tsuid, 0, metric_uid.length); |
| 495 | idx += metric_uid.length; |
| 496 | for (final byte[] t : tag_list) { |
| 497 | System.arraycopy(t, 0, tsuid, idx, t.length); |
| 498 | idx += t.length; |
| 499 | } |
| 500 | return tsuid; |
| 501 | } |
| 502 | @Override |
| 503 | public String toString() { |
| 504 | return "Tag resolution callback"; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | class MetricCB implements Callback<Deferred<byte[]>, byte[]> { |
| 509 | @Override |
| 510 | public Deferred<byte[]> call(final byte[] uid) |
| 511 | throws Exception { |
| 512 | System.arraycopy(uid, 0, metric_uid, 0, uid.length); |
| 513 | return Tags.resolveAllAsync(tsdb, tags).addCallback(new TagsCB()); |
| 514 | } |
| 515 | @Override |
| 516 | public String toString() { |
| 517 | return "Metric resolution callback"; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | return tsdb.getUIDAsync(UniqueIdType.METRIC, metric) |
| 522 | .addCallbackDeferring(new MetricCB()); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Attempts to retrieve the last data point for the given TSUID. |