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.

4 comments
Comment actions Permalink

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.

0
Comment actions Permalink

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
Comment actions Permalink

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);
}
}
0
Comment actions Permalink

Perfect.  Thanks, Alexander!

0

Please sign in to leave a comment.