Search in sources :

Example 1 with IdIndexEntry

use of com.intellij.psi.impl.cache.impl.id.IdIndexEntry in project intellij-community by JetBrains.

the class PsiSearchHelperImpl method isCheapEnoughToSearch.

@NotNull
@Override
public SearchCostResult isCheapEnoughToSearch(@NotNull String name, @NotNull final GlobalSearchScope scope, @Nullable final PsiFile fileToIgnoreOccurrencesIn, @Nullable final ProgressIndicator progress) {
    final AtomicInteger count = new AtomicInteger();
    final ProgressIndicator indicator = progress == null ? new EmptyProgressIndicator() : progress;
    final Processor<VirtualFile> processor = new Processor<VirtualFile>() {

        private final VirtualFile virtualFileToIgnoreOccurrencesIn = fileToIgnoreOccurrencesIn == null ? null : fileToIgnoreOccurrencesIn.getVirtualFile();

        @Override
        public boolean process(VirtualFile file) {
            indicator.checkCanceled();
            if (Comparing.equal(file, virtualFileToIgnoreOccurrencesIn))
                return true;
            final int value = count.incrementAndGet();
            return value < 10;
        }
    };
    List<IdIndexEntry> keys = getWordEntries(name, true);
    boolean cheap = keys.isEmpty() || processFilesContainingAllKeys(myManager.getProject(), scope, null, keys, processor);
    if (!cheap) {
        return SearchCostResult.TOO_MANY_OCCURRENCES;
    }
    return count.get() == 0 ? SearchCostResult.ZERO_OCCURRENCES : SearchCostResult.FEW_OCCURRENCES;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) Processor(com.intellij.util.Processor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) IdIndexEntry(com.intellij.psi.impl.cache.impl.id.IdIndexEntry) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with IdIndexEntry

use of com.intellij.psi.impl.cache.impl.id.IdIndexEntry in project intellij-community by JetBrains.

the class PsiSearchHelperImpl method distributePrimitives.

private static void distributePrimitives(@NotNull Map<SearchRequestCollector, Processor<PsiReference>> collectors, @NotNull Set<RequestWithProcessor> locals, @NotNull MultiMap<Set<IdIndexEntry>, RequestWithProcessor> globals, @NotNull List<Computable<Boolean>> customs, @NotNull Map<RequestWithProcessor, Processor<PsiElement>> localProcessors, @NotNull ProgressIndicator progress) {
    for (final Map.Entry<SearchRequestCollector, Processor<PsiReference>> entry : collectors.entrySet()) {
        final Processor<PsiReference> processor = entry.getValue();
        SearchRequestCollector collector = entry.getKey();
        for (final PsiSearchRequest primitive : collector.takeSearchRequests()) {
            final SearchScope scope = primitive.searchScope;
            if (scope instanceof LocalSearchScope) {
                registerRequest(locals, primitive, processor);
            } else {
                Set<IdIndexEntry> key = new HashSet<>(getWordEntries(primitive.word, primitive.caseSensitive));
                registerRequest(globals.getModifiable(key), primitive, processor);
            }
        }
        for (final Processor<Processor<PsiReference>> customAction : collector.takeCustomSearchActions()) {
            customs.add(() -> customAction.process(processor));
        }
    }
    for (Map.Entry<Set<IdIndexEntry>, Collection<RequestWithProcessor>> entry : globals.entrySet()) {
        for (RequestWithProcessor singleRequest : entry.getValue()) {
            PsiSearchRequest primitive = singleRequest.request;
            StringSearcher searcher = new StringSearcher(primitive.word, primitive.caseSensitive, true, false);
            BulkOccurrenceProcessor adapted = adaptProcessor(primitive, singleRequest.refProcessor);
            Processor<PsiElement> localProcessor = localProcessor(adapted, progress, searcher);
            assert !localProcessors.containsKey(singleRequest) || localProcessors.get(singleRequest) == localProcessor;
            localProcessors.put(singleRequest, localProcessor);
        }
    }
}
Also used : ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) Processor(com.intellij.util.Processor) THashSet(gnu.trove.THashSet) IdIndexEntry(com.intellij.psi.impl.cache.impl.id.IdIndexEntry) THashMap(gnu.trove.THashMap) MultiMap(com.intellij.util.containers.MultiMap) THashSet(gnu.trove.THashSet) StringSearcher(com.intellij.util.text.StringSearcher)

Example 3 with IdIndexEntry

use of com.intellij.psi.impl.cache.impl.id.IdIndexEntry in project intellij-community by JetBrains.

the class IndexCacheManagerImpl method collectVirtualFilesWithWord.

// IMPORTANT!!!
// Since implementation of virtualFileProcessor.process() may call indices directly or indirectly,
// we cannot call it inside FileBasedIndex.processValues() method except in collecting form
// If we do, deadlocks are possible (IDEADEV-42137). Process the files without not holding indices' read lock.
private boolean collectVirtualFilesWithWord(@NotNull final String word, final short occurrenceMask, @NotNull final GlobalSearchScope scope, final boolean caseSensitively, @NotNull final Processor<VirtualFile> fileProcessor) {
    if (myProject.isDefault()) {
        return true;
    }
    try {
        return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

            @Override
            public Boolean compute() {
                return FileBasedIndex.getInstance().processValues(IdIndex.NAME, new IdIndexEntry(word, caseSensitively), null, new FileBasedIndex.ValueProcessor<Integer>() {

                    final FileIndexFacade index = FileIndexFacade.getInstance(myProject);

                    @Override
                    public boolean process(final VirtualFile file, final Integer value) {
                        ProgressIndicatorProvider.checkCanceled();
                        final int mask = value.intValue();
                        if ((mask & occurrenceMask) != 0 && index.shouldBeFound(scope, file)) {
                            if (!fileProcessor.process(file))
                                return false;
                        }
                        return true;
                    }
                }, scope);
            }
        });
    } catch (IndexNotReadyException e) {
        throw new ProcessCanceledException();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) IdIndexEntry(com.intellij.psi.impl.cache.impl.id.IdIndexEntry) FileIndexFacade(com.intellij.openapi.roots.FileIndexFacade) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 4 with IdIndexEntry

use of com.intellij.psi.impl.cache.impl.id.IdIndexEntry in project intellij-community by JetBrains.

the class PsiSearchHelperImpl method collectFiles.

private void collectFiles(@NotNull MultiMap<Set<IdIndexEntry>, RequestWithProcessor> singles, @NotNull ProgressIndicator progress, @NotNull final MultiMap<VirtualFile, RequestWithProcessor> intersectionResult, @NotNull final MultiMap<VirtualFile, RequestWithProcessor> restResult) {
    for (Map.Entry<Set<IdIndexEntry>, Collection<RequestWithProcessor>> entry : singles.entrySet()) {
        final Set<IdIndexEntry> keys = entry.getKey();
        if (keys.isEmpty()) {
            continue;
        }
        final Collection<RequestWithProcessor> processors = entry.getValue();
        final GlobalSearchScope commonScope = uniteScopes(processors);
        final Set<VirtualFile> intersectionWithContainerNameFiles = intersectionWithContainerNameFiles(commonScope, processors, keys);
        List<VirtualFile> result = new ArrayList<>();
        Processor<VirtualFile> processor = Processors.cancelableCollectProcessor(result);
        processFilesContainingAllKeys(myManager.getProject(), commonScope, null, keys, processor);
        for (final VirtualFile file : result) {
            progress.checkCanceled();
            for (final IdIndexEntry indexEntry : keys) {
                DumbService.getInstance(myManager.getProject()).runReadActionInSmartMode(() -> FileBasedIndex.getInstance().processValues(IdIndex.NAME, indexEntry, file, (file1, value) -> {
                    int mask = value.intValue();
                    for (RequestWithProcessor single : processors) {
                        final PsiSearchRequest request = single.request;
                        if ((mask & request.searchContext) != 0 && request.searchScope.contains(file1)) {
                            MultiMap<VirtualFile, RequestWithProcessor> result1 = intersectionWithContainerNameFiles == null || !intersectionWithContainerNameFiles.contains(file1) ? restResult : intersectionResult;
                            result1.putValue(file1, single);
                        }
                    }
                    return true;
                }, commonScope));
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) CommentUtilCore(com.intellij.util.codeInsight.CommentUtilCore) JobLauncher(com.intellij.concurrency.JobLauncher) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ApplicationUtil(com.intellij.openapi.application.ex.ApplicationUtil) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UsageInfo(com.intellij.usageView.UsageInfo) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) ReadAction(com.intellij.openapi.application.ReadAction) ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) StringSearcher(com.intellij.util.text.StringSearcher) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) TooManyUsagesStatus(com.intellij.openapi.progress.util.TooManyUsagesStatus) SmartList(com.intellij.util.SmartList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(com.intellij.psi.impl.cache.CacheManager) Project(com.intellij.openapi.project.Project) FileIndexFacade(com.intellij.openapi.roots.FileIndexFacade) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) DumbService(com.intellij.openapi.project.DumbService) StringUtil(com.intellij.openapi.util.text.StringUtil) Processors(com.intellij.util.Processors) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) IdIndexEntry(com.intellij.psi.impl.cache.impl.id.IdIndexEntry) ProgressIndicatorProvider(com.intellij.openapi.progress.ProgressIndicatorProvider) ExtensionPointName(com.intellij.openapi.extensions.ExtensionPointName) AsyncFuture(com.intellij.concurrency.AsyncFuture) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) com.intellij.psi.search(com.intellij.psi.search) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) IdIndex(com.intellij.psi.impl.cache.impl.id.IdIndex) com.intellij.psi(com.intellij.psi) UsageInfoFactory(com.intellij.usageView.UsageInfoFactory) AsyncUtil(com.intellij.concurrency.AsyncUtil) NotNull(org.jetbrains.annotations.NotNull) THashSet(gnu.trove.THashSet) MultiMap(com.intellij.util.containers.MultiMap) IdIndexEntry(com.intellij.psi.impl.cache.impl.id.IdIndexEntry) THashMap(gnu.trove.THashMap) MultiMap(com.intellij.util.containers.MultiMap)

Example 5 with IdIndexEntry

use of com.intellij.psi.impl.cache.impl.id.IdIndexEntry in project intellij-community by JetBrains.

the class IdDataConsumer method getResult.

public Map<IdIndexEntry, Integer> getResult() {
    final Map<IdIndexEntry, Integer> result = new THashMap<>(myResult.size());
    myResult.forEachEntry((key, value) -> {
        result.put(new IdIndexEntry(key), value);
        return true;
    });
    return result;
}
Also used : THashMap(gnu.trove.THashMap) IdIndexEntry(com.intellij.psi.impl.cache.impl.id.IdIndexEntry)

Aggregations

IdIndexEntry (com.intellij.psi.impl.cache.impl.id.IdIndexEntry)5 ReadActionProcessor (com.intellij.openapi.application.ReadActionProcessor)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 Processor (com.intellij.util.Processor)3 THashMap (gnu.trove.THashMap)3 EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 FileIndexFacade (com.intellij.openapi.roots.FileIndexFacade)2 MultiMap (com.intellij.util.containers.MultiMap)2 StringSearcher (com.intellij.util.text.StringSearcher)2 AsyncFuture (com.intellij.concurrency.AsyncFuture)1 AsyncUtil (com.intellij.concurrency.AsyncUtil)1 JobLauncher (com.intellij.concurrency.JobLauncher)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ReadAction (com.intellij.openapi.application.ReadAction)1 ApplicationEx (com.intellij.openapi.application.ex.ApplicationEx)1 ApplicationUtil (com.intellij.openapi.application.ex.ApplicationUtil)1 Logger (com.intellij.openapi.diagnostic.Logger)1 ExtensionPointName (com.intellij.openapi.extensions.ExtensionPointName)1