Skip to content

Commit 2a21503

Browse files
faster lookup
1 parent 6b27e42 commit 2a21503

File tree

1 file changed

+106
-2
lines changed

1 file changed

+106
-2
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/ProjectMetadata.java

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.io.IOException;
4747
import java.util.ArrayList;
4848
import java.util.Arrays;
49+
import java.util.Collection;
4950
import java.util.Collections;
5051
import java.util.Comparator;
5152
import java.util.HashMap;
@@ -1761,15 +1762,118 @@ static SortedMap<String, IndexAbstraction> buildIndicesLookup(
17611762
if (indices.isEmpty()) {
17621763
return Collections.emptySortedMap();
17631764
}
1764-
SortedMap<String, IndexAbstraction> indicesLookup = new TreeMap<>();
1765+
Map<String, IndexAbstraction> indicesLookup = new HashMap<>();
17651766
Map<String, DataStream> indexToDataStreamLookup = new HashMap<>();
17661767
collectDataStreams(dataStreamMetadata, indicesLookup, indexToDataStreamLookup);
17671768

17681769
Map<String, List<IndexMetadata>> aliasToIndices = new HashMap<>();
17691770
collectIndices(indices, indexToDataStreamLookup, indicesLookup, aliasToIndices);
17701771
collectAliases(aliasToIndices, indicesLookup);
17711772

1772-
return Collections.unmodifiableSortedMap(indicesLookup);
1773+
return new SortedMap<>() {
1774+
1775+
private final SortedMap<String, IndexAbstraction> map = new TreeMap<>(indicesLookup);
1776+
1777+
@Override
1778+
public Comparator<? super String> comparator() {
1779+
return map.comparator();
1780+
}
1781+
1782+
@Override
1783+
public SortedMap<String, IndexAbstraction> subMap(String fromKey, String toKey) {
1784+
return Collections.unmodifiableSortedMap(map.subMap(fromKey, toKey));
1785+
}
1786+
1787+
@Override
1788+
public SortedMap<String, IndexAbstraction> headMap(String toKey) {
1789+
return Collections.unmodifiableSortedMap(map.headMap(toKey));
1790+
}
1791+
1792+
@Override
1793+
public SortedMap<String, IndexAbstraction> tailMap(String fromKey) {
1794+
return Collections.unmodifiableSortedMap(map.tailMap(fromKey));
1795+
}
1796+
1797+
@Override
1798+
public String firstKey() {
1799+
return map.firstKey();
1800+
}
1801+
1802+
@Override
1803+
public String lastKey() {
1804+
return map.lastKey();
1805+
}
1806+
1807+
@Override
1808+
public Set<String> keySet() {
1809+
return Collections.unmodifiableSet(map.keySet());
1810+
}
1811+
1812+
@Override
1813+
public Collection<IndexAbstraction> values() {
1814+
return Collections.unmodifiableCollection(map.values());
1815+
}
1816+
1817+
@Override
1818+
public Set<Entry<String, IndexAbstraction>> entrySet() {
1819+
return Collections.unmodifiableSet(map.entrySet());
1820+
}
1821+
1822+
@Override
1823+
public int size() {
1824+
return indicesLookup.size();
1825+
}
1826+
1827+
@Override
1828+
public boolean isEmpty() {
1829+
return indicesLookup.isEmpty();
1830+
}
1831+
1832+
@Override
1833+
public boolean containsKey(Object key) {
1834+
return indicesLookup.containsKey(key);
1835+
}
1836+
1837+
@Override
1838+
public boolean containsValue(Object value) {
1839+
return indicesLookup.containsValue(value);
1840+
}
1841+
1842+
@Override
1843+
public IndexAbstraction get(Object key) {
1844+
return indicesLookup.get(key);
1845+
}
1846+
1847+
@Override
1848+
public IndexAbstraction put(String key, IndexAbstraction value) {
1849+
throw new UnsupportedOperationException();
1850+
}
1851+
1852+
@Override
1853+
public IndexAbstraction remove(Object key) {
1854+
throw new UnsupportedOperationException();
1855+
}
1856+
1857+
@Override
1858+
public void putAll(Map<? extends String, ? extends IndexAbstraction> m) {
1859+
throw new UnsupportedOperationException();
1860+
}
1861+
1862+
@Override
1863+
public void clear() {
1864+
throw new UnsupportedOperationException();
1865+
}
1866+
1867+
@Override
1868+
public boolean equals(Object obj) {
1869+
return indicesLookup.equals(obj);
1870+
}
1871+
1872+
@Override
1873+
public int hashCode() {
1874+
return indicesLookup.hashCode();
1875+
}
1876+
};
17731877
}
17741878

17751879
private static void collectAliases(Map<String, List<IndexMetadata>> aliasToIndices, Map<String, IndexAbstraction> indicesLookup) {

0 commit comments

Comments
 (0)