protected boolean appendMetricNameClause()

in ambari-metrics-timelineservice/src/main/java/org/apache/ambari/metrics/core/timeline/query/TransientMetricCondition.java [85:168]


  protected boolean appendMetricNameClause(StringBuilder sb) {
    boolean appendConjunction = false;
    List<String> metricsLike = new ArrayList<>();
    List<String> metricsIn = new ArrayList<>();

    if (getMetricNames() != null) {
      for (String name : getMetricNames()) {
        if (name.contains("%")) {
          metricsLike.add(name);
        } else {
          metricsIn.add(name);
        }
      }

      // Put a '(' first
      sb.append("(");

      //IN clause
      // METRIC_NAME (NOT) IN (?,?,?,?)
      if (CollectionUtils.isNotEmpty(metricsIn)) {
        sb.append("METRIC_NAME");
        if (metricNamesNotCondition) {
          sb.append(" NOT");
        }
        sb.append(" IN (");
        //Append ?,?,?,?
        for (int i = 0; i < metricsIn.size(); i++) {
          sb.append("?");
          if (i < metricsIn.size() - 1) {
            sb.append(", ");
          }
        }
        sb.append(")");
        appendConjunction = true;
      }

      //Put an OR/AND if both types are present
      if (CollectionUtils.isNotEmpty(metricsIn) &&
        CollectionUtils.isNotEmpty(metricsLike)) {
        if (metricNamesNotCondition) {
          sb.append(" AND ");
        } else {
          sb.append(" OR ");
        }
      }

      //LIKE clause
      // METRIC_NAME (NOT) LIKE ? OR(AND) METRIC_NAME LIKE ?
      if (CollectionUtils.isNotEmpty(metricsLike)) {

        for (int i = 0; i < metricsLike.size(); i++) {
          sb.append("METRIC_NAME");
          if (metricNamesNotCondition) {
            sb.append(" NOT");
          }
          sb.append(" LIKE ");
          sb.append("?");

          if (i < metricsLike.size() - 1) {
            if (metricNamesNotCondition) {
              sb.append(" AND ");
            } else {
              sb.append(" OR ");
            }
          }
        }
        appendConjunction = true;
      }

      // Finish with a ')'
      if (appendConjunction) {
        sb.append(")");
      }

      metricNames.clear();
      if (CollectionUtils.isNotEmpty(metricsIn)) {
        metricNames.addAll(metricsIn);
      }
      if (CollectionUtils.isNotEmpty(metricsLike)) {
        metricNames.addAll(metricsLike);
      }
    }
    return appendConjunction;
  }