private void forEachMatchImpl()

in spectator-reg-atlas/src/main/java/com/netflix/spectator/atlas/impl/QueryIndex.java [502:565]


  private void forEachMatchImpl(Function<String, String> tags, Consumer<T> consumer) {
    // Matches for this level
    matches.forEach(consumer);

    boolean keyPresent = false;
    final String keyRef = key;
    if (keyRef != null) {
      String v = tags.apply(keyRef);
      if (v != null) {
        keyPresent = true;

        // Find exact matches
        QueryIndex<T> eqIdx = equalChecks.get(v);
        if (eqIdx != null) {
          eqIdx.forEachMatch(tags, consumer);
        }

        // Scan for matches with other conditions
        List<QueryIndex<T>> otherMatches = otherChecksCache.get(v);
        if (otherMatches == null) {
          // Avoid the list and cache allocations if there are no other checks at
          // this level
          if (!otherChecks.isEmpty()) {
            List<QueryIndex<T>> tmp = new ArrayList<>();
            otherChecksTree.forEach(v, kq -> {
              if (kq instanceof Query.In || matches(kq, v)) {
                QueryIndex<T> idx = otherChecks.get(kq);
                if (idx != null) {
                  tmp.add(idx);
                  idx.forEachMatch(tags, consumer);
                }
              }
            });
            otherChecksCache.put(v, tmp);
          }
        } else {
          // Enhanced for loop typically results in iterator being allocated. Using
          // size/get avoids the allocation and has better throughput.
          final int n = otherMatches.size();
          for (int p = 0; p < n; ++p) {
            otherMatches.get(p).forEachMatch(tags, consumer);
          }
        }

        // Check matches for has key
        final QueryIndex<T> hasKeyIdxRef = hasKeyIdx;
        if (hasKeyIdxRef != null) {
          hasKeyIdxRef.forEachMatch(tags, consumer);
        }
      }
    }

    // Check matches with other keys
    final QueryIndex<T> otherKeysIdxRef = otherKeysIdx;
    if (otherKeysIdxRef != null) {
      otherKeysIdxRef.forEachMatch(tags, consumer);
    }

    // Check matches with missing keys
    final QueryIndex<T> missingKeysIdxRef = missingKeysIdx;
    if (missingKeysIdxRef != null && !keyPresent) {
      missingKeysIdxRef.forEachMatch(tags, consumer);
    }
  }