ElasticsearchColumnMetadataDao implements ColumnMetadataDao { String[] getLatestIndices(List<String> includeIndices) throws IOException { LOG.debug("Getting latest indices; indices={}", includeIndices); Map<String, String> latestIndices = new HashMap<>(); String[] indices = esClient.getIndices(); for (String index : indices) { int prefixEnd = index.indexOf(INDEX_NAME_DELIMITER); if (prefixEnd != -1) { String prefix = index.substring(0, prefixEnd); if (includeIndices.contains(prefix)) { String latestIndex = latestIndices.get(prefix); if (latestIndex == null || index.compareTo(latestIndex) > 0) { latestIndices.put(prefix, index); } } } } return latestIndices.values().toArray(new String[latestIndices.size()]); } ElasticsearchColumnMetadataDao(ElasticsearchClient esClient); @SuppressWarnings("unchecked") @Override Map<String, FieldType> getColumnMetadata(List<String> indices);  }
@Test public void testGetOneLatestIndex() throws IOException { String[] existingIndices = new String[] { "bro_index_2017.10.03.19", "bro_index_2017.10.03.20", "bro_index_2017.10.03.21", "snort_index_2017.10.03.19", "snort_index_2017.10.03.20", "snort_index_2017.10.03.21" }; ElasticsearchColumnMetadataDao dao = setup(existingIndices); List<String> args = Collections.singletonList("bro"); String[] actual = dao.getLatestIndices(args); String [] expected = new String[] { "bro_index_2017.10.03.21" }; assertArrayEquals(expected, actual); }
@Test public void testGetLatestIndices() throws IOException { String[] existingIndices = new String[] { "bro_index_2017.10.03.19", "bro_index_2017.10.03.20", "bro_index_2017.10.03.21", "snort_index_2017.10.03.19", "snort_index_2017.10.03.19", "snort_index_2017.10.03.21" }; ElasticsearchColumnMetadataDao dao = setup(existingIndices); List<String> args = Arrays.asList("bro", "snort"); String[] actual = dao.getLatestIndices(args); String [] expected = new String[] { "bro_index_2017.10.03.21", "snort_index_2017.10.03.21" }; assertArrayEquals(expected, actual); }
@Test public void testLatestIndicesWhereNoneExist() throws IOException { String[] existingIndices = new String[] {}; ElasticsearchColumnMetadataDao dao = setup(existingIndices); List<String> args = Arrays.asList("bro", "snort"); String[] actual = dao.getLatestIndices(args); String [] expected = new String[] {}; assertArrayEquals(expected, actual); }