How to rebuild index on demand?

I'm trying to manually rebuild my custom file based index by invoking an action from menu (i.e. for debug purposes). I'm doing this by com.intellij.util.indexing.FileBasedIndex#requestRebuild method, but this method not cause index rebuild right now, but rather at next usage of this index. What should I do to force reindexing at this moment from action?

0
6 comments

why not query your index after requestRebuild in your action?

0

Yes, accessing index triggers index rebuild. Also changing com.intellij.util.indexing.FileBasedIndex#requestRebuild to com.intellij.util.indexing.FileBasedIndex#scheduleRebuild do the trick. But in real I need to refresh 3 indices at once and this seems to be a problem for IDEA. I do this in actionPerformed:

        fileBasedIndex.scheduleRebuild(IndexA.ID, new Throwable("Refresh Index: A"));         fileBasedIndex.scheduleRebuild(IndexB.ID, new Throwable("Refresh Index: B"));         fileBasedIndex.scheduleRebuild(IndexC.ID, new Throwable("Refresh Index: C"));


IDEA triggers rebuild for all 3 indices, but only first one is properly rebuild - second and third are marked as REBUILD_IN_PROGRESS and then as OK, but they are invalid (no values in index). Also, accessing index after this do not triggers index refresh, so data are still invalid. This seems like a bug or thread racing issue to me but maybe I use it in wrong way? Problems probably start from com.intellij.util.indexing.FileBasedIndexImpl#checkRebuild
I just want to refresh selected indices without restarting IDEA.

0

Workaround:

    public void actionPerformed(AnActionEvent e) {         final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();         fileBasedIndex.scheduleRebuild(IndexA.ID, new Throwable("Refresh Index: A"));         final Project project = ObjectUtils.assertNotNull(e.getProject());         final DumbService dumbService = DumbService.getInstance(project);         dumbService.smartInvokeLater(new Runnable() {             @Override             public void run() {                 fileBasedIndex.scheduleRebuild(IndexB.ID, new Throwable("Refresh Index: B"));                 dumbService.smartInvokeLater(new Runnable() {                     @Override                     public void run() {                         fileBasedIndex.scheduleRebuild(IndexC.ID, new Throwable("Refresh Index: C"));                     }                 });             }         });     }

0
Avatar
Permanently deleted user

Is this workaround the correct way to do this? I'm having the same problem, I can't force the reindex, and this workaround is not working either.

0

Currently I do this in similar way, but using queue mechanism instead nested runnables (only for debugging purposes in IDEA 12.1). Can You describe what kind of problem You have?

0
Avatar
Permanently deleted user

Actually I think is working fine now, I had an error in one of my indexes and I guess that was the problem with the reindex. Thanks !!

0

Please sign in to leave a comment.