FileBasedIndex.getAllKeys filtered by module scope

Answered

It is possible to do something like mentioned in post topic? Or in other way - can I get collection of values matching  collection of given keys?
Actually I do something like this:

     FileBasedIndex.getInstance().getAllKeys(getName(), psiElement.getProject())

but I obtain all keys. I would like to get only keys from module in which is given psiElement or all values from this module.

0
5 comments

There is no method for it, also processAllKeys method will pass to your processor all keys of the project despite of given GlobalSearchScope, you should filter them by yourself.

-1

Sorry for resurrecting this old thread, but I believe I've hit a related problem and want to clarify the solution.  I'm calling:

FileBasedIndex.getInstance().getAllKeys(getName(), project)

and am getting back keys from all open projects.  It sounds like that's what Alexander is referencing, though he's speaking specifically about processAllKeys() (UPDATE: I just noticed that getAllKeys() simply calls processAllKeys() using allScope()).

Am I correct in assuming that I need to post-process the result of getAllKeys() to determine which are actually for the supplied project?  If so, there a more efficient way to do that than:

FileBasedIndex.getInstance().getContainingFiles(getName(), indexKey, GlobalSearchScope.projectScope(project))

with each returned index key and seeing if the resulting collection is empty or not?  That seems very inefficient.

Thanks!

 

 

1

Yes you have to post-process the keys in order to determine which of them have values in particular scope. E.g.:

 

for (String s : index.getAllKeys(indexId, project)) {
boolean inScope = !index.processValues(indexId, s, null, new FileBasedIndex.ValueProcessor<T>() {
@Override
public boolean process(VirtualFile file, T value) {
return false;
}
}, scope);

if (inScope) {
System.out.println("This key has values from this scope: " + s);
}
}
1

Perfect.  Thanks, Alexander!

0
Processor<String> variantTableProcessor = id -> {
results.add(id);
return true;
};
FileBasedIndex.getInstance().processAllKeys(indexId, variantTableProcessor, scope, null);

seems to work if the index has overriden `traceKeyHashToVirtualFileMapping` as following:

@Override
public boolean traceKeyHashToVirtualFileMapping() {
return true;
}


Works for me in IntelliJ 2021.3.

0

Please sign in to leave a comment.