Completely separate ChangedFilesCollector and FilesToUpdateCollector

GitOrigin-RevId: bf0525c4c5188b4b95cd2be20e2cb794a54b287d
This commit is contained in:
Liudmila Kornilova
2024-02-06 21:15:38 +01:00
committed by intellij-monorepo-bot
parent 498795527f
commit f4003210c3
5 changed files with 32 additions and 38 deletions

View File

@@ -137,6 +137,7 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
// findExtensionOrFail is thread safe
private final NotNullLazyValue<ChangedFilesCollector> myChangedFilesCollector =
NotNullLazyValue.createValue(() -> AsyncEventSupport.EP_NAME.findExtensionOrFail(ChangedFilesCollector.class));
private final FilesToUpdateCollector myFilesToUpdateCollector = new FilesToUpdateCollector();
private final List<Pair<IndexableFileSet, Project>> myIndexableSets = ContainerUtil.createLockFreeCopyOnWriteList();
@@ -294,7 +295,7 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
myIndexableSets.removeIf(p -> p.second.equals(project));
persistDirtyFiles(project);
getChangedFilesCollector().getDirtyFiles().removeProject(project);
getChangedFilesCollector().getFilesToUpdateCollector().getDirtyFiles().removeProject(project);
myFilesToUpdateCollector.getDirtyFiles().removeProject(project);
}
boolean processChangedFiles(@NotNull Project project, @NotNull Processor<? super VirtualFile> processor) {
@@ -302,7 +303,7 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
// avoid missing files when events are processed concurrently
Iterator<VirtualFile> iterator = Iterators.concat(
getChangedFilesCollector().getEventMerger().getChangedFiles(),
getFilesToUpdateCollector().getFilesToUpdate()
getFilesToUpdateCollector().getFilesToUpdateAsIterator()
);
HashSet<VirtualFile> checkedFiles = new HashSet<>();
@@ -574,7 +575,7 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
private void persistProjectsDirtyFiles() {
Set<Project> projects = new HashSet<>();
projects.addAll(getChangedFilesCollector().getDirtyFiles().getProjects());
projects.addAll(getChangedFilesCollector().getFilesToUpdateCollector().getDirtyFiles().getProjects());
projects.addAll(myFilesToUpdateCollector.getDirtyFiles().getProjects());
for (Project project : projects) {
persistDirtyFiles(project);
}
@@ -590,7 +591,7 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
IntSet dirtyFileIds = new IntOpenHashSet();
ProjectDirtyFiles dirtyFiles1 = getChangedFilesCollector().getDirtyFiles().getProjectDirtyFiles(project);
if (dirtyFiles1 != null) dirtyFiles1.addAllTo(dirtyFileIds);
ProjectDirtyFiles dirtyFiles2 = getChangedFilesCollector().getFilesToUpdateCollector().getDirtyFiles().getProjectDirtyFiles(project);
ProjectDirtyFiles dirtyFiles2 = myFilesToUpdateCollector.getDirtyFiles().getProjectDirtyFiles(project);
if (dirtyFiles2 != null) dirtyFiles2.addAllTo(dirtyFileIds);
return dirtyFileIds;
}
@@ -660,7 +661,7 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
dirtyFilesWithoutProject, vfsCreationStamp);
// remove events from event merger, so they don't show up after FileBasedIndex is restarted using tumbler
getChangedFilesCollector().clear();
getChangedFilesCollector().getFilesToUpdateCollector().clear();
myFilesToUpdateCollector.clear();
vfsCreationStamp = 0;
// TODO-ank: Should we catch and ignore CancellationException here to allow other lines to execute?
@@ -1311,7 +1312,7 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
@NotNull
Collection<VirtualFile> getFilesToUpdate(final Project project) {
return ContainerUtil.filter(getChangedFilesCollector().getAllFilesToUpdate(), filesToBeIndexedForProjectCondition(project)::test);
return ContainerUtil.filter(getAllFilesToUpdate(), filesToBeIndexedForProjectCondition(project)::test);
}
@NotNull
@@ -1720,7 +1721,7 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
private final VirtualFileUpdateTask myForceUpdateTask = new VirtualFileUpdateTask();
private void forceUpdate(@Nullable Project project, @Nullable final GlobalSearchScope filter, @Nullable final VirtualFile restrictedTo) {
Collection<VirtualFile> allFilesToUpdate = getChangedFilesCollector().getAllFilesToUpdate();
Collection<VirtualFile> allFilesToUpdate = getAllFilesToUpdate();
if (!allFilesToUpdate.isEmpty()) {
boolean includeFilesFromOtherProjects = restrictedTo == null && project == null;
@@ -1735,6 +1736,13 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
}
}
@ApiStatus.Internal
@NotNull
public Collection<VirtualFile> getAllFilesToUpdate() {
getChangedFilesCollector().ensureUpToDate();
return myFilesToUpdateCollector.getFilesToUpdate();
}
public boolean needsFileContentLoading(@NotNull ID<?, ?> indexId) {
return myRegisteredIndexes.isContentDependentIndex(indexId);
}
@@ -1828,9 +1836,10 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
}
}
@ApiStatus.Internal
@NotNull
public FilesToUpdateCollector getFilesToUpdateCollector() {
return getChangedFilesCollector().getFilesToUpdateCollector();
return myFilesToUpdateCollector;
}
public void scheduleFileForIndexing(int fileId, @NotNull VirtualFile file, boolean onlyContentChanged, @NotNull List<Project> dirtyQueueProjects) {
@@ -1961,7 +1970,7 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
public void registerIndexableSet(@NotNull IndexableFileSet set, @NotNull Project project) {
myIndexableSets.add(Pair.create(set, project));
getChangedFilesCollector().getDirtyFiles().addProject(project);
getChangedFilesCollector().getFilesToUpdateCollector().getDirtyFiles().addProject(project);
myFilesToUpdateCollector.getDirtyFiles().addProject(project);
}
public void onProjectClosing(@NotNull IndexableFileSet set) {
@@ -1969,25 +1978,24 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
if (p == null) return;
myIndexableSets.remove(p);
ChangedFilesCollector changedFilesCollector = getChangedFilesCollector();
for (VirtualFile file : changedFilesCollector.getAllFilesToUpdate()) {
for (VirtualFile file : getAllFilesToUpdate()) {
final int fileId = getFileId(file);
if (!file.isValid()) {
removeDataFromIndicesForFile(fileId, file, "invalid_file");
getIndexableFilesFilterHolder().removeFile(fileId);
changedFilesCollector.getFilesToUpdateCollector().removeFileIdFromFilesScheduledForUpdate(fileId);
myFilesToUpdateCollector.removeFileIdFromFilesScheduledForUpdate(fileId);
}
else if (!belongsToIndexableFiles(file)) {
if (ChangedFilesCollector.CLEAR_NON_INDEXABLE_FILE_DATA) {
removeDataFromIndicesForFile(fileId, file, "non_indexable_file");
}
getIndexableFilesFilterHolder().removeFile(fileId);
changedFilesCollector.getFilesToUpdateCollector().removeFileIdFromFilesScheduledForUpdate(fileId);
myFilesToUpdateCollector.removeFileIdFromFilesScheduledForUpdate(fileId);
}
}
persistDirtyFiles(p.second);
getChangedFilesCollector().getDirtyFiles().removeProject(p.second);
getChangedFilesCollector().getFilesToUpdateCollector().getDirtyFiles().removeProject(p.second);
myFilesToUpdateCollector.getDirtyFiles().removeProject(p.second);
IndexingStamp.flushCaches();
}

View File

@@ -30,7 +30,6 @@ import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;
@@ -44,8 +43,6 @@ public final class ChangedFilesCollector extends IndexedFilesListener {
private final DirtyFiles myDirtyFiles = new DirtyFiles();
private final FilesToUpdateCollector myFilesToUpdate = new FilesToUpdateCollector();
private final AtomicInteger myProcessedEventIndex = new AtomicInteger();
private final Phaser myWorkersFinishedSync = new Phaser() {
@Override
@@ -73,16 +70,6 @@ public final class ChangedFilesCollector extends IndexedFilesListener {
}
}
@NotNull
public FilesToUpdateCollector getFilesToUpdateCollector() {
return myFilesToUpdate;
}
public Collection<VirtualFile> getAllFilesToUpdate() {
ensureUpToDate();
return myFilesToUpdate.getAllFilesToUpdate();
}
public void clear() {
myDirtyFiles.clear();
ReadAction.run(() -> {

View File

@@ -67,8 +67,6 @@ class DirtyFiles {
}
}
fun getDirtyFilesWithoutProject(): ProjectDirtyFiles = myDirtyFilesWithoutProject
fun getProjectDirtyFiles(project: Project?): ProjectDirtyFiles? {
if (project == null) return myDirtyFilesWithoutProject
return myDirtyFiles.first { it.first == project }.second

View File

@@ -5,6 +5,7 @@ import com.intellij.concurrency.ConcurrentCollectionFactory;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.IntObjectMap;
@@ -18,7 +19,7 @@ import static com.intellij.util.indexing.events.ChangedFilesCollector.CLEAR_NON_
public class FilesToUpdateCollector {
private static final Logger LOG = Logger.getInstance(FilesToUpdateCollector.class);
private final FileBasedIndexImpl myFileBasedIndex = (FileBasedIndexImpl)FileBasedIndex.getInstance();
private final NotNullLazyValue<FileBasedIndexImpl> myFileBasedIndex = NotNullLazyValue.createValue(() -> (FileBasedIndexImpl)FileBasedIndex.getInstance());
private final IntObjectMap<VirtualFile> myFilesToUpdate = ConcurrentCollectionFactory.createConcurrentIntObjectMap();
private final DirtyFiles myDirtyFiles = new DirtyFiles();
@@ -26,10 +27,10 @@ public class FilesToUpdateCollector {
public void scheduleForUpdate(@NotNull VirtualFile file, @NotNull List<Project> dirtyQueueProjects) {
int fileId = FileBasedIndex.getFileId(file);
if (!(file instanceof DeletedVirtualFileStub)) {
Set<Project> projects = myFileBasedIndex.getContainingProjects(file);
Set<Project> projects = myFileBasedIndex.get().getContainingProjects(file);
if (projects.isEmpty()) {
removeNonIndexableFileData(file, fileId);
myFileBasedIndex.getIndexableFilesFilterHolder().removeFile(fileId);
myFileBasedIndex.get().getIndexableFilesFilterHolder().removeFile(fileId);
return;
}
}
@@ -48,7 +49,7 @@ public class FilesToUpdateCollector {
if (CLEAR_NON_INDEXABLE_FILE_DATA) {
List<ID<?, ?>> extensions = getIndexedContentDependentExtensions(fileId);
if (!extensions.isEmpty()) {
myFileBasedIndex.removeDataFromIndicesForFile(fileId, file, "non_indexable_file");
myFileBasedIndex.get().removeDataFromIndicesForFile(fileId, file, "non_indexable_file");
}
IndexingFlag.cleanProcessingFlag(file);
}
@@ -66,7 +67,7 @@ public class FilesToUpdateCollector {
private @NotNull List<ID<?, ?>> getIndexedContentDependentExtensions(int fileId) {
List<ID<?, ?>> indexedStates = IndexingStamp.getNontrivialFileIndexedStates(fileId);
RegisteredIndexes registeredIndexes = myFileBasedIndex.getRegisteredIndexes();
RegisteredIndexes registeredIndexes = myFileBasedIndex.get().getRegisteredIndexes();
List<ID<?, ?>> contentDependentIndexes;
if (registeredIndexes == null) {
Set<? extends ID<?, ?>> allContentDependentIndexes = FileBasedIndexExtension.EXTENSION_POINT_NAME.getExtensionList().stream()
@@ -107,11 +108,11 @@ public class FilesToUpdateCollector {
return myFilesToUpdate.containsKey(fileId);
}
public Iterator<@NotNull VirtualFile> getFilesToUpdate() {
public Iterator<@NotNull VirtualFile> getFilesToUpdateAsIterator() {
return myFilesToUpdate.values().iterator();
}
public Collection<VirtualFile> getAllFilesToUpdate() {
public Collection<VirtualFile> getFilesToUpdate() {
return myFilesToUpdate.isEmpty()
? Collections.emptyList()
: Collections.unmodifiableCollection(myFilesToUpdate.values());

View File

@@ -113,7 +113,7 @@ class CollectFilesNotMarkedAsIndex(text: String, line: Int) : PerformanceCommand
}
try {
fileOrDir.contentsToByteArray()
if (fbi.changedFilesCollector.filesToUpdateCollector.containsFileId(fileOrDir.id)) {
if (fbi.filesToUpdateCollector.containsFileId(fileOrDir.id)) {
logIndexingIssue("$fileOrDir (id=${fileOrDir.id}) $errorMessagePart because is changed\n")
}
else {
@@ -158,7 +158,7 @@ class CollectFilesNotMarkedAsIndex(text: String, line: Int) : PerformanceCommand
if (!cachedUnfinishedFiles.isEmpty()) {
textConsumer.accept("Unfinished cached files' ids in IndexingStamp: ${cachedUnfinishedFiles.contentToString()}\n")
dumpIdsWithPaths(cachedUnfinishedFiles, textConsumer)
textConsumer.accept("All changed unindexed files: ${fbi.changedFilesCollector.allFilesToUpdate}")
textConsumer.accept("All changed unindexed files: ${fbi.allFilesToUpdate}")
}
return@use
}