Search in sources :

Example 1 with DataExternalizer

use of com.intellij.util.io.DataExternalizer in project intellij-community by JetBrains.

the class StubIndexImpl method registerIndexer.

private static <K> boolean registerIndexer(@NotNull final StubIndexExtension<K, ?> extension, final boolean forceClean, AsyncState state) throws IOException {
    final StubIndexKey<K, ?> indexKey = extension.getKey();
    final int version = extension.getVersion();
    synchronized (state) {
        state.myIndexIdToVersionMap.put(indexKey, version);
    }
    final File indexRootDir = IndexInfrastructure.getIndexRootDir(indexKey);
    boolean needRebuild = false;
    if (forceClean || IndexingStamp.versionDiffers(indexKey, version)) {
        final File versionFile = IndexInfrastructure.getVersionFile(indexKey);
        final boolean versionFileExisted = versionFile.exists();
        final String[] children = indexRootDir.list();
        // rebuild only if there exists what to rebuild
        boolean indexRootHasChildren = children != null && children.length > 0;
        needRebuild = !forceClean && (versionFileExisted || indexRootHasChildren);
        if (needRebuild) {
            LOG.info("Version has changed for stub index " + extension.getKey() + ". The index will be rebuilt.");
        }
        if (indexRootHasChildren)
            FileUtil.deleteWithRenaming(indexRootDir);
        // todo snapshots indices
        IndexingStamp.rewriteVersion(indexKey, version);
    }
    for (int attempt = 0; attempt < 2; attempt++) {
        try {
            final VfsAwareMapIndexStorage<K, StubIdList> storage = new VfsAwareMapIndexStorage<>(IndexInfrastructure.getStorageFile(indexKey), extension.getKeyDescriptor(), StubIdExternalizer.INSTANCE, extension.getCacheSize(), false, extension instanceof StringStubIndexExtension && ((StringStubIndexExtension) extension).traceKeyHashToVirtualFileMapping());
            final MemoryIndexStorage<K, StubIdList> memStorage = new MemoryIndexStorage<>(storage, indexKey);
            MyIndex<K> index = new MyIndex<>(new IndexExtension<K, StubIdList, Void>() {

                @NotNull
                @Override
                public ID<K, StubIdList> getName() {
                    return (ID<K, StubIdList>) indexKey;
                }

                @NotNull
                @Override
                public DataIndexer<K, StubIdList, Void> getIndexer() {
                    return inputData -> Collections.emptyMap();
                }

                @NotNull
                @Override
                public KeyDescriptor<K> getKeyDescriptor() {
                    return extension.getKeyDescriptor();
                }

                @NotNull
                @Override
                public DataExternalizer<StubIdList> getValueExternalizer() {
                    return StubIdExternalizer.INSTANCE;
                }

                @Override
                public int getVersion() {
                    return extension.getVersion();
                }
            }, memStorage);
            synchronized (state) {
                state.myIndices.put(indexKey, index);
            }
            break;
        } catch (IOException e) {
            needRebuild = true;
            onExceptionInstantiatingIndex(indexKey, version, indexRootDir, e);
        } catch (RuntimeException e) {
            //noinspection ThrowableResultOfMethodCallIgnored
            Throwable cause = FileBasedIndexImpl.getCauseToRebuildIndex(e);
            if (cause == null)
                throw e;
            onExceptionInstantiatingIndex(indexKey, version, indexRootDir, e);
        }
    }
    return needRebuild;
}
Also used : KeyDescriptor(com.intellij.util.io.KeyDescriptor) NotNull(org.jetbrains.annotations.NotNull) DataExternalizer(com.intellij.util.io.DataExternalizer) VirtualFile(com.intellij.openapi.vfs.VirtualFile)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 DataExternalizer (com.intellij.util.io.DataExternalizer)1 KeyDescriptor (com.intellij.util.io.KeyDescriptor)1 NotNull (org.jetbrains.annotations.NotNull)1