6df3afeb0c416652e086ec89b28425e6c8459d74
[idea/community.git] / platform / lang-impl / src / com / intellij / util / indexing / IndexConfiguration.java
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;
3
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;
10
11 import java.util.*;
12
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;
19
20   <K, V> UpdatableIndex<K, V, FileContent> getIndex(@NotNull ID<K, V> indexId) {
21     assert myFreezed;
22     final Pair<UpdatableIndex<?, ?, FileContent>, FileBasedIndex.InputFilter> pair = myIndices.get(indexId);
23
24     //noinspection unchecked
25     return (UpdatableIndex<K, V, FileContent>)Pair.getFirst(pair);
26   }
27
28   @NotNull
29   FileBasedIndex.InputFilter getInputFilter(@NotNull ID<?, ?> indexId) {
30     assert myFreezed;
31     final Pair<UpdatableIndex<?, ?, FileContent>, FileBasedIndex.InputFilter> pair = myIndices.get(indexId);
32
33     assert pair != null : "Index data is absent for index " + indexId;
34
35     return pair.getSecond();
36   }
37
38   void freeze() {
39     myFreezed = true;
40   }
41
42   <K, V> void registerIndex(@NotNull ID<K, V> name,
43                             @NotNull UpdatableIndex<K, V, FileContent> index,
44                             @NotNull FileBasedIndex.InputFilter inputFilter,
45                             int version,
46                             @Nullable Collection<? extends FileType> associatedFileTypes) {
47     assert !myFreezed;
48
49     synchronized (myIndices) {
50       myIndexIdToVersionMap.put(name, version);
51
52       if (associatedFileTypes != null) {
53         for(FileType fileType:associatedFileTypes) {
54           List<ID<?, ?>> ids = myFileType2IndicesWithFileTypeInfoMap.computeIfAbsent(fileType, __ -> new ArrayList<>(5));
55           ids.add(name);
56         }
57       }
58       else {
59         myIndicesWithoutFileTypeInfo.add(name);
60       }
61
62       Pair<UpdatableIndex<?, ?, FileContent>, FileBasedIndex.InputFilter> old = myIndices.put(name, new Pair<>(index, inputFilter));
63       if (old != null) {
64         throw new IllegalStateException("Index " + old.first + " already registered for the name '" + name + "'");
65       }
66     }
67   }
68
69   @NotNull
70   List<ID<?, ?>> getFileTypesForIndex(@NotNull FileType fileType) {
71     assert myFreezed;
72     List<ID<?, ?>> ids = myFileType2IndicesWithFileTypeInfoMap.get(fileType);
73     if (ids == null) ids = myIndicesWithoutFileTypeInfo;
74     return ids;
75   }
76
77   void finalizeFileTypeMappingForIndices() {
78     assert !myFreezed;
79     synchronized (myIndices) {
80       for (List<ID<?, ?>> value : myFileType2IndicesWithFileTypeInfoMap.values()) {
81         value.addAll(myIndicesWithoutFileTypeInfo);
82       }
83     }
84   }
85
86   @NotNull
87   Collection<ID<?, ?>> getIndexIDs() {
88     assert myFreezed;
89     return myIndices.keySet();
90   }
91
92   boolean hasIndex(@NotNull ID<?, ?> name) {
93     assert myFreezed;
94     return myIndices.containsKey(name);
95   }
96
97   int getIndexVersion(@NotNull ID<?, ?> id) {
98     assert myFreezed;
99     return myIndexIdToVersionMap.getInt(id);
100   }
101 }