@Override
  @Nullable
  public ObjectStubTree readFromVFile(Project project, final VirtualFile vFile) {
    if (DumbService.getInstance(project).isDumb()) {
      return null;
    }

    final int id = Math.abs(FileBasedIndex.getFileId(vFile));
    if (id <= 0) {
      return null;
    }

    boolean wasIndexedAlready = FileBasedIndexImpl.isFileIndexed(vFile, StubUpdatingIndex.INDEX_ID);

    final List<SerializedStubTree> datas =
        FileBasedIndex.getInstance()
            .getValues(StubUpdatingIndex.INDEX_ID, id, GlobalSearchScope.fileScope(project, vFile));
    final int size = datas.size();

    if (size == 1) {
      Stub stub;
      try {
        stub = datas.get(0).getStub(false);
      } catch (SerializerNotFoundException e) {
        return processError(
            vFile, "No stub serializer: " + vFile.getPresentableUrl() + ": " + e.getMessage(), e);
      }
      ObjectStubTree tree =
          stub instanceof PsiFileStub
              ? new StubTree((PsiFileStub) stub)
              : new ObjectStubTree((ObjectStubBase) stub, true);
      tree.setDebugInfo(
          "created from index: "
              + StubUpdatingIndex.getIndexingStampInfo(vFile)
              + ", wasIndexedAlready="
              + wasIndexedAlready
              + ", queried at "
              + vFile.getTimeStamp());
      return tree;
    } else if (size != 0) {
      return processError(
          vFile,
          "Twin stubs: "
              + vFile.getPresentableUrl()
              + " has "
              + size
              + " stub versions. Should only have one. id="
              + id,
          null);
    }

    return null;
  }
 public MyIndex(
     final ID<Integer, SerializedStubTree> indexId,
     final IndexStorage<Integer, SerializedStubTree> storage,
     final DataIndexer<Integer, SerializedStubTree, FileContent> indexer) {
   super(indexId, indexer, storage);
   try {
     checkNameStorage();
   } catch (StorageException e) {
     LOG.info(e);
     FileBasedIndex.requestRebuild(INDEX_ID);
   }
 }
  public static Collection<JSClass> searchClassInheritors(
      ID<String, Void> indexId, String name, Project project, final GlobalSearchScope scope) {
    final Collection<VirtualFile> files =
        FileBasedIndex.getInstance().getContainingFiles(indexId, name, scope);
    final PsiManager psiManager = PsiManager.getInstance(project);
    Collection<JSClass> classes = new ArrayList<JSClass>(files.size());
    for (VirtualFile file : files) {
      PsiFile psifile = psiManager.findFile(file);
      if (!(psifile instanceof XmlFile)) continue;
      classes.addAll(XmlBackedJSClassImpl.getClasses((XmlFile) psifile));
    }

    if (FlexNameAlias.CONTAINER_TYPE_NAME.equals(name)) {
      classes.addAll(
          searchClassInheritors(indexId, FlexNameAlias.COMPONENT_TYPE_NAME, project, scope));
    }
    return classes;
  }
  @Override
  public <K> Collection<K> getAllKeys(final StubIndexKey<K, ?> indexKey, @NotNull Project project) {
    FileBasedIndex.getInstance()
        .ensureUpToDate(StubUpdatingIndex.INDEX_ID, project, GlobalSearchScope.allScope(project));

    final MyIndex<K> index = (MyIndex<K>) myIndices.get(indexKey);
    try {
      return index.getAllKeys();
    } catch (StorageException e) {
      forceRebuild(e);
    } catch (RuntimeException e) {
      final Throwable cause = e.getCause();
      if (cause instanceof IOException || cause instanceof StorageException) {
        forceRebuild(e);
      }
      throw e;
    }
    return Collections.emptyList();
  }
  private static ObjectStubTree processError(
      final VirtualFile vFile, String message, @Nullable Exception e) {
    LOG.error(message, e);

    ApplicationManager.getApplication()
        .invokeLater(
            new Runnable() {
              @Override
              public void run() {
                final Document doc = FileDocumentManager.getInstance().getCachedDocument(vFile);
                if (doc != null) {
                  FileDocumentManager.getInstance().saveDocument(doc);
                }
              }
            },
            ModalityState.NON_MODAL);

    FileBasedIndex.getInstance().requestReindex(vFile);
    return null;
  }
 @NotNull
 public static List<PyFile> find(
     @NotNull String name, @NotNull Project project, boolean includeNonProjectItems) {
   final List<PyFile> results = new ArrayList<PyFile>();
   final GlobalSearchScope scope =
       includeNonProjectItems
           ? PyProjectScopeBuilder.excludeSdkTestsScope(project)
           : GlobalSearchScope.projectScope(project);
   final Collection<VirtualFile> files =
       FileBasedIndex.getInstance().getContainingFiles(NAME, name, scope);
   for (VirtualFile virtualFile : files) {
     final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
     if (psiFile instanceof PyFile) {
       if (!PyUserSkeletonsUtil.isUnderUserSkeletonsDirectory(psiFile)) {
         results.add((PyFile) psiFile);
       }
     }
   }
   return results;
 }
  public static <Key, Psi extends PsiElement> Collection<Psi> safeGet(
      @NotNull StubIndexKey<Key, Psi> indexKey,
      @NotNull Key key,
      final Project project,
      final GlobalSearchScope scope,
      Class<Psi> requiredClass) {
    Collection<Psi> collection = getInstance().get(indexKey, key, project, scope);
    for (Iterator<Psi> iterator = collection.iterator(); iterator.hasNext(); ) {
      Psi psi = iterator.next();
      if (!requiredClass.isInstance(psi)) {
        iterator.remove();

        VirtualFile faultyContainer = PsiUtilCore.getVirtualFile(psi);
        LOG.error("Invalid stub element type in index: " + faultyContainer + ". found: " + psi);
        if (faultyContainer != null && faultyContainer.isValid()) {
          FileBasedIndex.getInstance().requestReindex(faultyContainer);
        }
      }
    }

    return collection;
  }
Esempio n. 8
0
  @NotNull
  public static List<LinkReferenceResult> getReferencedFiles(
      @NotNull final VirtualFile _file, @NotNull final Project project) {
    final List<LinkReferenceResult> result = new ArrayList<LinkReferenceResult>();
    if (!(_file.getFileSystem() instanceof LocalFileSystem)) {
      return result;
    }

    FileBasedIndex.getInstance()
        .processValues(
            INDEX_ID,
            FileBasedIndex.getFileId(_file),
            null,
            new FileBasedIndex.ValueProcessor<InfoHolder<LinkInfo>>() {
              public boolean process(final VirtualFile file, final InfoHolder<LinkInfo> value) {
                final PsiManager psiManager = PsiManager.getInstance(project);
                final PsiFile psiFile = psiManager.findFile(file);
                if (psiFile != null) {
                  for (final LinkInfo linkInfo : value.myValues) {
                    if (linkInfo.value != null || linkInfo.scripted) {
                      final PsiFileSystemItem[] item = new PsiFileSystemItem[] {null};
                      if (linkInfo.value != null) {
                        final LeafElement newValueElement =
                            Factory.createSingleLeafElement(
                                XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN,
                                "\"" + linkInfo.value + "\"",
                                0,
                                linkInfo.value.length() + 2,
                                null,
                                psiManager,
                                psiFile);
                        final PsiElement element = newValueElement.getPsi();
                        final FileReferenceSet set =
                            new FileReferenceSet(
                                StringUtil.stripQuotesAroundValue(element.getText()),
                                element,
                                1,
                                null,
                                true);

                        final FileReference lastReference = set.getLastReference();

                        if (lastReference != null) {
                          final PsiFileSystemItem resolved = lastReference.resolve();
                          if (resolved instanceof PsiFile) {
                            item[0] = resolved;
                          }
                        }
                      }

                      result.add(new MyLinkReferenceResult(item, linkInfo, psiFile));
                    }
                  }
                }
                return true;
              }
            },
            GlobalSearchScope.allScope(project));

    return result;
  }
 private static void requestRebuild() {
   FileBasedIndex.requestRebuild(StubUpdatingIndex.INDEX_ID);
 }
 private static void forceRebuild(Throwable e) {
   LOG.info(e);
   requestRebuild();
   FileBasedIndex.getInstance().scheduleRebuild(StubUpdatingIndex.INDEX_ID, e);
 }
  @Override
  public <Key, Psi extends PsiElement> Collection<Psi> get(
      @NotNull final StubIndexKey<Key, Psi> indexKey,
      @NotNull final Key key,
      final Project project,
      final GlobalSearchScope scope) {
    FileBasedIndex.getInstance().ensureUpToDate(StubUpdatingIndex.INDEX_ID, project, scope);

    final PersistentFS fs = (PersistentFS) ManagingFS.getInstance();
    final PsiManager psiManager = PsiManager.getInstance(project);

    final List<Psi> result = new ArrayList<Psi>();
    final MyIndex<Key> index = (MyIndex<Key>) myIndices.get(indexKey);

    try {
      try {
        // disable up-to-date check to avoid locks on attempt to acquire index write lock while
        // holding at the same time the readLock for this index
        FileBasedIndex.disableUpToDateCheckForCurrentThread();
        index.getReadLock().lock();
        final ValueContainer<TIntArrayList> container = index.getData(key);

        container.forEach(
            new ValueContainer.ContainerAction<TIntArrayList>() {
              @Override
              public void perform(final int id, final TIntArrayList value) {
                final VirtualFile file = IndexInfrastructure.findFileByIdIfCached(fs, id);
                if (file != null && (scope == null || scope.contains(file))) {
                  StubTree stubTree = null;

                  final PsiFile _psifile = psiManager.findFile(file);
                  PsiFileWithStubSupport psiFile = null;

                  if (_psifile != null && !(_psifile instanceof PsiPlainTextFile)) {
                    if (_psifile instanceof PsiFileWithStubSupport) {
                      psiFile = (PsiFileWithStubSupport) _psifile;
                      stubTree = psiFile.getStubTree();
                      if (stubTree == null && psiFile instanceof PsiFileImpl) {
                        stubTree = ((PsiFileImpl) psiFile).calcStubTree();
                      }
                    }
                  }

                  if (stubTree != null || psiFile != null) {
                    if (stubTree == null) {
                      stubTree = StubTreeLoader.getInstance().readFromVFile(project, file);
                      if (stubTree != null) {
                        final List<StubElement<?>> plained = stubTree.getPlainList();
                        for (int i = 0; i < value.size(); i++) {
                          final StubElement<?> stub = plained.get(value.get(i));
                          final ASTNode tree = psiFile.findTreeForStub(stubTree, stub);

                          if (tree != null) {
                            if (tree.getElementType() == stubType(stub)) {
                              result.add((Psi) tree.getPsi());
                            } else {
                              String persistedStubTree =
                                  ((PsiFileStubImpl) stubTree.getRoot()).printTree();

                              String stubTreeJustBuilt =
                                  ((PsiFileStubImpl)
                                          ((IStubFileElementType)
                                                  ((PsiFileImpl) psiFile).getContentElementType())
                                              .getBuilder()
                                              .buildStubTree(psiFile))
                                      .printTree();

                              StringBuilder builder = new StringBuilder();
                              builder.append("Oops\n");

                              builder.append("Recorded stub:-----------------------------------\n");
                              builder.append(persistedStubTree);
                              builder.append(
                                  "\nAST built stub: ------------------------------------\n");
                              builder.append(stubTreeJustBuilt);
                              builder.append("\n");
                              LOG.info(builder.toString());

                              // requestReindex() may want to acquire write lock (for indices not
                              // requiring content loading)
                              // thus, because here we are under read lock, need to use invoke later
                              ApplicationManager.getApplication()
                                  .invokeLater(
                                      new Runnable() {
                                        @Override
                                        public void run() {
                                          FileBasedIndex.getInstance().requestReindex(file);
                                        }
                                      },
                                      ModalityState.NON_MODAL);
                            }
                          }
                        }
                      }
                    } else {
                      final List<StubElement<?>> plained = stubTree.getPlainList();
                      for (int i = 0; i < value.size(); i++) {
                        result.add((Psi) plained.get(value.get(i)).getPsi());
                      }
                    }
                  }
                }
              }
            });
      } finally {
        index.getReadLock().unlock();
        FileBasedIndex.enableUpToDateCheckForCurrentThread();
      }
    } catch (StorageException e) {
      forceRebuild(e);
    } catch (RuntimeException e) {
      final Throwable cause = FileBasedIndex.getCauseToRebuildIndex(e);
      if (cause != null) {
        forceRebuild(cause);
      } else {
        throw e;
      }
    }

    return result;
  }
  @Override
  @Nullable
  public ObjectStubTree readFromVFile(Project project, final VirtualFile vFile) {
    if (DumbService.getInstance(project).isDumb()) {
      return null;
    }

    final int id = Math.abs(FileBasedIndex.getFileId(vFile));
    if (id <= 0) {
      return null;
    }

    boolean wasIndexedAlready =
        ((FileBasedIndexImpl) FileBasedIndex.getInstance()).isFileUpToDate(vFile);

    Document document = FileDocumentManager.getInstance().getCachedDocument(vFile);
    boolean saved =
        document == null || !FileDocumentManager.getInstance().isDocumentUnsaved(document);

    final List<SerializedStubTree> datas =
        FileBasedIndex.getInstance()
            .getValues(StubUpdatingIndex.INDEX_ID, id, GlobalSearchScope.fileScope(project, vFile));
    final int size = datas.size();

    if (size == 1) {
      SerializedStubTree stubTree = datas.get(0);

      if (!stubTree.contentLengthMatches(
          vFile.getLength(), getCurrentTextContentLength(project, vFile, document))) {
        return processError(
            vFile,
            "Outdated stub in index: "
                + StubUpdatingIndex.getIndexingStampInfo(vFile)
                + ", doc="
                + document
                + ", docSaved="
                + saved
                + ", wasIndexedAlready="
                + wasIndexedAlready
                + ", queried at "
                + vFile.getTimeStamp(),
            null);
      }

      Stub stub;
      try {
        stub = stubTree.getStub(false);
      } catch (SerializerNotFoundException e) {
        return processError(
            vFile, "No stub serializer: " + vFile.getPresentableUrl() + ": " + e.getMessage(), e);
      }
      ObjectStubTree tree =
          stub instanceof PsiFileStub
              ? new StubTree((PsiFileStub) stub)
              : new ObjectStubTree((ObjectStubBase) stub, true);
      tree.setDebugInfo("created from index");
      return tree;
    } else if (size != 0) {
      return processError(
          vFile,
          "Twin stubs: "
              + vFile.getPresentableUrl()
              + " has "
              + size
              + " stub versions. Should only have one. id="
              + id,
          null);
    }

    return null;
  }
 @Override
 public void rebuildStubTree(VirtualFile virtualFile) {
   FileBasedIndex.getInstance().requestReindex(virtualFile);
 }
 @NotNull
 public static Collection<String> getAllKeys(@NotNull Project project) {
   return FileBasedIndex.getInstance().getAllKeys(NAME, project);
 }
/**
 * @author Eugene Zhuravlev
 * @since Jan 29, 2008
 */
public class UnindexedFilesUpdater extends DumbModeTask {
  private static final Logger LOG = Logger.getInstance("#com.intellij.util.indexing.UnindexedFilesUpdater");

  private final FileBasedIndexImpl myIndex = (FileBasedIndexImpl)FileBasedIndex.getInstance();
  private final Project myProject;
  private final boolean myOnStartup;

  public UnindexedFilesUpdater(final Project project, boolean onStartup) {
    myProject = project;
    myOnStartup = onStartup;
    project.getMessageBus().connect(this).subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootAdapter() {
      @Override
      public void rootsChanged(ModuleRootEvent event) {
        DumbService.getInstance(project).cancelTask(UnindexedFilesUpdater.this);
      }
    });
  }

  private void updateUnindexedFiles(ProgressIndicator indicator) {
    PerformanceWatcher.Snapshot snapshot = PerformanceWatcher.takeSnapshot();
    PushedFilePropertiesUpdater.getInstance(myProject).pushAllPropertiesNow();
    snapshot.logResponsivenessSinceCreation("Pushing properties");

    indicator.setIndeterminate(true);
    indicator.setText(IdeBundle.message("progress.indexing.scanning"));

    CollectingContentIterator finder = myIndex.createContentIterator(indicator);
    snapshot = PerformanceWatcher.takeSnapshot();

    myIndex.iterateIndexableFilesConcurrently(finder, myProject, indicator);

    myIndex.filesUpdateEnumerationFinished();

    snapshot.logResponsivenessSinceCreation("Indexable file iteration");

    List<VirtualFile> files = finder.getFiles();

    if (myOnStartup && !ApplicationManager.getApplication().isUnitTestMode()) {
      // full VFS refresh makes sense only after it's loaded, i.e. after scanning files to index is finished
      ((StartupManagerImpl)StartupManager.getInstance(myProject)).scheduleInitialVfsRefresh();
    }

    if (files.isEmpty()) {
      return;
    }

    snapshot = PerformanceWatcher.takeSnapshot();

    LOG.info("Unindexed files update started: " + files.size() + " files to update");

    indicator.setIndeterminate(false);
    indicator.setText(IdeBundle.message("progress.indexing.updating"));

    indexFiles(indicator, files);
    snapshot.logResponsivenessSinceCreation("Unindexed files update");

  }

  private void indexFiles(ProgressIndicator indicator, List<VirtualFile> files) {
    CacheUpdateRunner.processFiles(indicator, true, files, myProject, new Consumer<FileContent>() {
      @Override
      public void consume(FileContent content) {
        myIndex.indexFileContent(myProject, content);
      }
    });
  }

  @Override
  public void performInDumbMode(@NotNull ProgressIndicator indicator) {
    myIndex.filesUpdateStarted(myProject);
    try {
      updateUnindexedFiles(indicator);
    }
    catch (ProcessCanceledException e) {
      LOG.info("Unindexed files update canceled");
      throw e;
    } finally {
      myIndex.filesUpdateFinished(myProject);
    }
  }
}