indexes: shared index should check existing state on project loading

GitOrigin-RevId: 847adda1e1d8a2ba9112dc33d9b27185b25ae0eb
This commit is contained in:
Dmitro Batko
2020-02-18 21:44:56 +03:00
committed by intellij-monorepo-bot
parent 931eca1d8f
commit fbb142194b
6 changed files with 53 additions and 2 deletions

View File

@@ -100,6 +100,10 @@ class SharedIndexesTest : LightJavaCodeInsightFixtureTestCase() {
throw AssertionFailedError()
}
override fun attachExistingChunk(chunkId: Int, project: Project): Boolean {
throw AssertionFailedError()
}
override fun <Value : Any?, Key : Any?> getChunk(indexId: ID<Key, Value>, chunkId: Int): HashBasedMapReduceIndex<Key, Value>? {
throw AssertionFailedError()
}

View File

@@ -1,6 +1,7 @@
// Copyright 2000-2019 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.
package com.intellij.util.indexing;
import com.intellij.index.SharedIndexExtensions;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.FileType;
@@ -12,6 +13,8 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileWithId;
import com.intellij.openapi.vfs.newvfs.impl.VirtualFileSystemEntry;
import com.intellij.psi.search.FileTypeIndex;
import com.intellij.util.indexing.hash.FileContentHashIndex;
import com.intellij.util.indexing.hash.SharedIndexChunkConfiguration;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
@@ -26,10 +29,12 @@ class UnindexedFilesFinder implements CollectingContentIterator {
private final Project myProject;
private final boolean myDoTraceForFilesToBeIndexed = FileBasedIndexImpl.LOG.isTraceEnabled();
private final FileBasedIndexImpl myFileBasedIndex;
private final FileContentHashIndex myFileContentHashIndex;
UnindexedFilesFinder(@NotNull Project project) {
myProject = project;
myFileBasedIndex = ((FileBasedIndexImpl)FileBasedIndex.getInstance());
myFileContentHashIndex = SharedIndexExtensions.areSharedIndexesEnabled() ? myFileBasedIndex.getOrCreateFileContentHashIndex() : null;
}
@NotNull
@@ -98,6 +103,18 @@ class UnindexedFilesFinder implements CollectingContentIterator {
final ID<?, ?> indexId = affectedIndexCandidates.get(i);
try {
if (myFileBasedIndex.needsFileContentLoading(indexId) && myFileBasedIndex.shouldIndexFile(fileContent, indexId)) {
if (myFileContentHashIndex != null) {
//append existing chunk
int chunkId = myFileContentHashIndex.getAssociatedChunkId(inputId, file);
if (!SharedIndexChunkConfiguration.getInstance().attachExistingChunk(chunkId, myProject)) {
myFileContentHashIndex.update(inputId, null).compute();
for (ID<?, ?> state : IndexingStamp.getNontrivialFileIndexedStates(inputId)) {
myFileBasedIndex.getIndex(state).resetIndexedStateForFile(inputId);
}
}
}
if (myDoTraceForFilesToBeIndexed) {
LOG.trace("Scheduling indexing of " + file + " by request of index " + indexId);
}

View File

@@ -1,9 +1,10 @@
// Copyright 2000-2019 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.
package com.intellij.util.indexing.hash;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IntIntFunction;
import com.intellij.util.indexing.*;
import com.intellij.util.indexing.impl.AbstractUpdateData;
import com.intellij.util.indexing.impl.IndexStorage;
@@ -16,6 +17,8 @@ import java.io.IOException;
import java.util.Map;
public class FileContentHashIndex extends VfsAwareMapReduceIndex<Long, Void> {
private static final Logger LOG = Logger.getInstance(FileContentHashIndex.class);
public FileContentHashIndex(@NotNull FileContentHashIndexExtension extension, IndexStorage<Long, Void> storage) throws IOException {
super(extension,
storage,
@@ -29,6 +32,19 @@ public class FileContentHashIndex extends VfsAwareMapReduceIndex<Long, Void> {
return new HashIndexUpdateComputable(super.createIndexUpdateComputation(updateData), updateData.newDataIsEmpty());
}
public int getAssociatedChunkId(int fileId, VirtualFile file) {
try {
Map<Long, Void> data = getIndexedFileData(fileId);
if (data.isEmpty()) return FileContentHashIndexExtension.NULL_INDEX_ID;
return FileContentHashIndexExtension.getIndexId(data.keySet().iterator().next());
}
catch (StorageException e) {
LOG.error(e);
FileBasedIndex.getInstance().requestReindex(file);
return FileContentHashIndexExtension.NULL_INDEX_ID;
}
}
public long getHashId(int fileId) throws StorageException {
Map<Long, Void> data = getIndexedFileData(fileId);
if (data.isEmpty()) return FileContentHashIndexExtension.NULL_HASH_ID;

View File

@@ -132,7 +132,8 @@ public class FileContentHashIndexExtension extends FileBasedIndexExtension<Long,
return (((long) indexId) << 32) | (internalHashId & 0xffffffffL);
}
public static final long NULL_HASH_ID = getHashId(0, -1);
public static final int NULL_INDEX_ID = -1;
public static final long NULL_HASH_ID = getHashId(0, NULL_INDEX_ID);
private static final Key<Long> HASH_ID_KEY = Key.create("file.content.hash.id");

View File

@@ -49,4 +49,6 @@ public interface SharedIndexChunkConfiguration {
void locateIndexes(@NotNull Project project,
@NotNull Set<OrderEntry> entries,
@NotNull ProgressIndicator indicator);
boolean attachExistingChunk(int chunkId, @NotNull Project project);
}

View File

@@ -274,6 +274,17 @@ public class SharedIndexChunkConfigurationImpl implements SharedIndexChunkConfig
mySharedIndexExecutor.execute(runnable);
}
@Override
public boolean attachExistingChunk(int chunkId, @NotNull Project project) {
try {
return bindChunkToProject(registerChunk(chunkId), project);
}
catch (IOException e) {
LOG.error(e);
return false;
}
}
@NotNull
private List<SharedIndexChunk> registerChunk(@NotNull ChunkDescriptor descriptor) throws IOException {
String chunkRootName = descriptor.getChunkUniqueId();