1 // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.util.indexing;
4 import com.intellij.openapi.fileTypes.FileType;
5 import com.intellij.openapi.util.Pair;
6 import it.unimi.dsi.fastutil.objects.Object2IntMap;
7 import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
8 import org.jetbrains.annotations.NotNull;
9 import org.jetbrains.annotations.Nullable;
13 final class IndexConfiguration {
14 private final Map<ID<?, ?>, Pair<UpdatableIndex<?, ?, FileContent>, FileBasedIndex.InputFilter>> myIndices = new HashMap<>();
15 private final Object2IntMap<ID<?, ?>> myIndexIdToVersionMap = new Object2IntOpenHashMap<>();
16 private final List<ID<?, ?>> myIndicesWithoutFileTypeInfo = new ArrayList<>();
17 private final Map<FileType, List<ID<?, ?>>> myFileType2IndicesWithFileTypeInfoMap = new HashMap<>();
18 private volatile boolean myFreezed;
20 <K, V> UpdatableIndex<K, V, FileContent> getIndex(@NotNull ID<K, V> indexId) {
22 final Pair<UpdatableIndex<?, ?, FileContent>, FileBasedIndex.InputFilter> pair = myIndices.get(indexId);
24 //noinspection unchecked
25 return (UpdatableIndex<K, V, FileContent>)Pair.getFirst(pair);
29 FileBasedIndex.InputFilter getInputFilter(@NotNull ID<?, ?> indexId) {
31 final Pair<UpdatableIndex<?, ?, FileContent>, FileBasedIndex.InputFilter> pair = myIndices.get(indexId);
33 assert pair != null : "Index data is absent for index " + indexId;
35 return pair.getSecond();
42 <K, V> void registerIndex(@NotNull ID<K, V> name,
43 @NotNull UpdatableIndex<K, V, FileContent> index,
44 @NotNull FileBasedIndex.InputFilter inputFilter,
46 @Nullable Collection<? extends FileType> associatedFileTypes) {
49 synchronized (myIndices) {
50 myIndexIdToVersionMap.put(name, version);
52 if (associatedFileTypes != null) {
53 for(FileType fileType:associatedFileTypes) {
54 List<ID<?, ?>> ids = myFileType2IndicesWithFileTypeInfoMap.computeIfAbsent(fileType, __ -> new ArrayList<>(5));
59 myIndicesWithoutFileTypeInfo.add(name);
62 Pair<UpdatableIndex<?, ?, FileContent>, FileBasedIndex.InputFilter> old = myIndices.put(name, new Pair<>(index, inputFilter));
64 throw new IllegalStateException("Index " + old.first + " already registered for the name '" + name + "'");
70 List<ID<?, ?>> getFileTypesForIndex(@NotNull FileType fileType) {
72 List<ID<?, ?>> ids = myFileType2IndicesWithFileTypeInfoMap.get(fileType);
73 if (ids == null) ids = myIndicesWithoutFileTypeInfo;
77 void finalizeFileTypeMappingForIndices() {
79 synchronized (myIndices) {
80 for (List<ID<?, ?>> value : myFileType2IndicesWithFileTypeInfoMap.values()) {
81 value.addAll(myIndicesWithoutFileTypeInfo);
87 Collection<ID<?, ?>> getIndexIDs() {
89 return myIndices.keySet();
92 int getIndexVersion(@NotNull ID<?, ?> id) {
94 return myIndexIdToVersionMap.getInt(id);