6bbedf8e0b4005372b1308d940ac39279fb5d7ff
[idea/community.git] / platform / lang-impl / src / com / intellij / util / indexing / FileBasedIndexSwitcher.java
1 // 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.
2 package com.intellij.util.indexing;
3
4 import com.intellij.openapi.application.ApplicationManager;
5 import com.intellij.openapi.diagnostic.Logger;
6 import com.intellij.openapi.progress.ProgressIndicator;
7 import com.intellij.openapi.project.DumbModeTask;
8 import com.intellij.openapi.project.DumbService;
9 import com.intellij.openapi.project.Project;
10 import com.intellij.openapi.project.ProjectManager;
11 import com.intellij.util.concurrency.Semaphore;
12 import org.jetbrains.annotations.NotNull;
13 import org.jetbrains.annotations.TestOnly;
14
15 public class FileBasedIndexSwitcher {
16     private static final Logger LOG = Logger.getInstance(FileBasedIndexSwitcher.class);
17
18     @NotNull
19     private final FileBasedIndexImpl myFileBasedIndex;
20
21     // accessed only in EDT
22     private Semaphore myDumbModeSemaphore;
23
24     @TestOnly
25     public FileBasedIndexSwitcher() {
26         this(((FileBasedIndexImpl) FileBasedIndex.getInstance()));
27     }
28
29     public FileBasedIndexSwitcher(@NotNull FileBasedIndexImpl index) {
30         myFileBasedIndex = index;
31     }
32
33     public void turnOff() {
34         LOG.assertTrue(ApplicationManager.getApplication().isDispatchThread());
35         LOG.assertTrue(!ApplicationManager.getApplication().isWriteAccessAllowed());
36         Project[] projects = ProjectManager.getInstance().getOpenProjects();
37         boolean unitTestMode = ApplicationManager.getApplication().isUnitTestMode();
38         if (!unitTestMode) {
39             myDumbModeSemaphore = new Semaphore(1);
40             for (Project project : projects) {
41                 DumbService.getInstance(project).cancelAllTasksAndWait();
42                 DumbService.getInstance(project).queueTask(new DumbModeTask() {
43                     @Override
44                     public void performInDumbMode(@NotNull ProgressIndicator indicator) {
45                         indicator.setText(IndexingBundle.message("indexes.reloading"));
46                         myDumbModeSemaphore.waitFor();
47                     }
48                 });
49             }
50         }
51         myFileBasedIndex.performShutdown(true);
52         myFileBasedIndex.dropRegisteredIndexes();
53         IndexingStamp.flushCaches();
54     }
55
56     public void turnOn() {
57         LOG.assertTrue(ApplicationManager.getApplication().isWriteThread());
58         RebuildStatus.reset();
59         myFileBasedIndex.initComponent();
60         boolean unitTestMode = ApplicationManager.getApplication().isUnitTestMode();
61
62         if (unitTestMode) {
63             myFileBasedIndex.waitUntilIndicesAreInitialized();
64         }
65
66         if (!unitTestMode) {
67             myDumbModeSemaphore.up();
68         }
69
70         FileBasedIndexImpl.cleanupProcessedFlag();
71         for (Project project : ProjectManager.getInstance().getOpenProjects()) {
72             DumbService.getInstance(project).queueTask(new UnindexedFilesUpdater(project));
73         }
74     }
75 }