### Question:
SimpleTagQueryParser { public Observable<Map<String, Set<String>>> getTagValues(String tenantId, MetricType<?> metricType, Map<String, String> tagsQueries) { return Observable.from(tagsQueries.entrySet()) .flatMap(e -> dataAccess.findMetricsByTagName(tenantId, e.getKey()) .filter(typeFilter(metricType, 1)) .filter(tagValueFilter(e.getValue(), 3)) .map(row -> { Map<String, Map<String, String>> idMap = new HashMap<>(); Map<String, String> valueMap = new HashMap<>(); valueMap.put(e.getKey(), row.getString(3)); idMap.put(row.getString(2), valueMap); return idMap; }) .switchIfEmpty(Observable.just(new HashMap<>())) .reduce((map1, map2) -> { map1.putAll(map2); return map1; })) .reduce((m1, m2) -> { Iterator<Map.Entry<String, Map<String, String>>> iterator = m1.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, Map<String, String>> next = iterator.next(); if (!m2.containsKey(next.getKey())) { iterator.remove(); } else { Map<String, String> map2 = m2.get(next.getKey()); map2.forEach((k, v) -> next.getValue().put(k, v)); } } return m1; }) .map(m -> { Map<String, Set<String>> tagValueMap = new HashMap<>(); m.forEach((k, v) -> v.forEach((subKey, subValue) -> { if (tagValueMap.containsKey(subKey)) { tagValueMap.get(subKey).add(subValue); } else { Set<String> values = new HashSet<>(); values.add(subValue); tagValueMap.put(subKey, values); } })); return tagValueMap; }); } SimpleTagQueryParser(DataAccess dataAccess, MetricsService metricsService, boolean disableACostQueries,
            int pageSize, int pageThreshold); Observable<MetricId<?>> findMetricIdentifiersWithFilters(String tenantId, MetricType<?> metricType,
                                                                    Map<String, String> tagsQueries); Observable<Map<String, Set<String>>> getTagValues(String tenantId, MetricType<?> metricType,
                                                             Map<String, String> tagsQueries); Observable<String> getTagNames(String tenantId, MetricType<?> metricType, String filter); Func1<Row, Boolean> typeFilter(MetricType<?> type, int index); Func1<MetricId<?>, Boolean> metricTypeFilter(MetricType<?> type);  }### Answer:
@Test public void testReOrder() { Map<String, String> tagsQuery = Maps.newHashMap(); tagsQuery.put("pod_hit", "norate"); tagsQuery.put("env", "qa|stage|prod"); tagsQuery.put("pod_id", "pod1|pod2|pod3"); tagsQuery.put("time", "*"); tagsQuery.put("!seek", ""); tagsQuery.put("pod_name", "pod1|!pod2"); Map<Long, List<SimpleTagQueryParser.Query>> entries = SimpleTagQueryParser.QueryOptimizer.reOrderTagsQuery(tagsQuery, true); assertEquals(1, entries.get(SimpleTagQueryParser.QueryOptimizer.GROUP_A_COST).size()); assertEquals(1, entries.get(SimpleTagQueryParser.QueryOptimizer.GROUP_A_OR_COST).size()); assertEquals(3, entries.get(SimpleTagQueryParser.QueryOptimizer.GROUP_A_OR_COST).get(0) .getTagValues().length); assertEquals(2, entries.get(SimpleTagQueryParser.QueryOptimizer.GROUP_B_COST).size()); assertEquals(1, entries.get(SimpleTagQueryParser.QueryOptimizer.GROUP_C_COST).size()); assertEquals(1, entries.get(SimpleTagQueryParser.QueryOptimizer.GROUP_OPENSHIFT_OPTIMIZATION).size()); assertEquals(3, entries.get(SimpleTagQueryParser.QueryOptimizer.GROUP_OPENSHIFT_OPTIMIZATION).get(0) .getTagValues().length); }