aa3bddddb68edbcec112a1b95da16446a07400cd
[idea/community.git] / platform / lang-impl / src / com / intellij / util / indexing / UnindexedFilesUpdater.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.util.indexing;
17
18 import com.intellij.ProjectTopics;
19 import com.intellij.ide.IdeBundle;
20 import com.intellij.ide.caches.FileContent;
21 import com.intellij.ide.startup.impl.StartupManagerImpl;
22 import com.intellij.openapi.application.ApplicationManager;
23 import com.intellij.openapi.diagnostic.Logger;
24 import com.intellij.openapi.progress.ProcessCanceledException;
25 import com.intellij.openapi.progress.ProgressIndicator;
26 import com.intellij.openapi.project.CacheUpdateRunner;
27 import com.intellij.openapi.project.DumbModeTask;
28 import com.intellij.openapi.project.DumbService;
29 import com.intellij.openapi.project.Project;
30 import com.intellij.openapi.roots.CollectingContentIterator;
31 import com.intellij.openapi.roots.ModuleRootAdapter;
32 import com.intellij.openapi.roots.ModuleRootEvent;
33 import com.intellij.openapi.roots.impl.PushedFilePropertiesUpdater;
34 import com.intellij.openapi.startup.StartupManager;
35 import com.intellij.openapi.vfs.VirtualFile;
36 import com.intellij.util.Consumer;
37 import org.jetbrains.annotations.NotNull;
38
39 import java.util.List;
40
41 /**
42  * @author Eugene Zhuravlev
43  * @since Jan 29, 2008
44  */
45 public class UnindexedFilesUpdater extends DumbModeTask {
46   private static final Logger LOG = Logger.getInstance("#com.intellij.util.indexing.UnindexedFilesUpdater");
47
48   private final FileBasedIndexImpl myIndex = (FileBasedIndexImpl)FileBasedIndex.getInstance();
49   private final Project myProject;
50   private final boolean myOnStartup;
51
52   public UnindexedFilesUpdater(final Project project, boolean onStartup) {
53     myProject = project;
54     myOnStartup = onStartup;
55     project.getMessageBus().connect(this).subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootAdapter() {
56       @Override
57       public void rootsChanged(ModuleRootEvent event) {
58         DumbService.getInstance(project).cancelTask(UnindexedFilesUpdater.this);
59       }
60     });
61   }
62
63   private void updateUnindexedFiles(ProgressIndicator indicator) {
64     long started = System.currentTimeMillis();
65     PushedFilePropertiesUpdater.getInstance(myProject).pushAllPropertiesNow();
66     LOG.info("Pushed properties in " + (System.currentTimeMillis() - started) + " ms");
67
68     indicator.setIndeterminate(true);
69     indicator.setText(IdeBundle.message("progress.indexing.scanning"));
70
71     CollectingContentIterator finder = myIndex.createContentIterator(indicator);
72     long l = System.currentTimeMillis();
73     myIndex.iterateIndexableFiles(finder, myProject, indicator);
74     myIndex.filesUpdateEnumerationFinished();
75
76     LOG.info("Indexable files iterated in " + (System.currentTimeMillis() - l) + " ms");
77     List<VirtualFile> files = finder.getFiles();
78
79     if (myOnStartup && !ApplicationManager.getApplication().isUnitTestMode()) {
80       // full VFS refresh makes sense only after it's loaded, i.e. after scanning files to index is finished
81       ((StartupManagerImpl)StartupManager.getInstance(myProject)).scheduleInitialVfsRefresh();
82     }
83
84     if (files.isEmpty()) {
85       return;
86     }
87
88     started = System.currentTimeMillis();
89     LOG.info("Unindexed files update started: " + files.size() + " files to update");
90
91     indicator.setIndeterminate(false);
92     indicator.setText(IdeBundle.message("progress.indexing.updating"));
93
94     indexFiles(indicator, files);
95     LOG.info("Unindexed files update done in " + (System.currentTimeMillis() - started) + " ms");
96   }
97
98   private void indexFiles(ProgressIndicator indicator, List<VirtualFile> files) {
99     CacheUpdateRunner.processFiles(indicator, true, files, myProject, new Consumer<FileContent>() {
100       @Override
101       public void consume(FileContent content) {
102         try {
103           myIndex.indexFileContent(myProject, content);
104         }
105         finally {
106           IndexingStamp.flushCache(content.getVirtualFile());
107         }
108       }
109     });
110   }
111
112   @Override
113   public void performInDumbMode(@NotNull ProgressIndicator indicator) {
114     myIndex.filesUpdateStarted(myProject);
115     try {
116       updateUnindexedFiles(indicator);
117     }
118     catch (ProcessCanceledException e) {
119       LOG.info("Unindexed files update canceled");
120       throw e;
121     } finally {
122       myIndex.filesUpdateFinished(myProject);
123     }
124   }
125 }