From 91485cc6b2db5f5c52c171550f2d100fff8296dd Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Mon, 6 Mar 2017 17:40:44 +0100 Subject: [PATCH] refactoring: use ID for IndexingStamp.rewriteVersion / IndexingStamp.versionDiffers --- .../com/intellij/psi/stubs/StubIndexImpl.java | 22 ++++++++++++------- .../util/indexing/FileBasedIndexImpl.java | 18 +++++++++------ .../intellij/util/indexing/IndexingStamp.java | 6 +++-- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java b/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java index fef4d1b1e9f2..50be888f819e 100644 --- a/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java @@ -107,11 +107,14 @@ public class StubIndexImpl extends StubIndex implements ApplicationComponentAdap synchronized (state) { state.myIndexIdToVersionMap.put(indexKey, version); } - final File versionFile = IndexInfrastructure.getVersionFile(indexKey); - final boolean versionFileExisted = versionFile.exists(); + final File indexRootDir = IndexInfrastructure.getIndexRootDir(indexKey); boolean needRebuild = false; - if (forceClean || IndexingStamp.versionDiffers(versionFile, version)) { + + 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; @@ -120,7 +123,7 @@ public class StubIndexImpl extends StubIndex implements ApplicationComponentAdap LOG.info("Version has changed for stub index " + extension.getKey() + ". The index will be rebuilt."); } if (indexRootHasChildren) FileUtil.deleteWithRenaming(indexRootDir); - IndexingStamp.rewriteVersion(versionFile, version); // todo snapshots indices + IndexingStamp.rewriteVersion(indexKey, version); // todo snapshots indices } for (int attempt = 0; attempt < 2; attempt++) { @@ -172,21 +175,24 @@ public class StubIndexImpl extends StubIndex implements ApplicationComponentAdap } catch (IOException e) { needRebuild = true; - onExceptionInstantiatingIndex(version, versionFile, indexRootDir, e); + onExceptionInstantiatingIndex(indexKey, version, indexRootDir, e); } catch (RuntimeException e) { //noinspection ThrowableResultOfMethodCallIgnored Throwable cause = FileBasedIndexImpl.getCauseToRebuildIndex(e); if (cause == null) throw e; - onExceptionInstantiatingIndex(version, versionFile, indexRootDir, e); + onExceptionInstantiatingIndex(indexKey, version, indexRootDir, e); } } return needRebuild; } - private static void onExceptionInstantiatingIndex(int version, File versionFile, File indexRootDir, Exception e) throws IOException { + private static void onExceptionInstantiatingIndex(StubIndexKey indexKey, + int version, + File indexRootDir, + Exception e) throws IOException { LOG.info(e); FileUtil.deleteWithRenaming(indexRootDir); - IndexingStamp.rewriteVersion(versionFile, version); // todo snapshots indices + IndexingStamp.rewriteVersion(indexKey, version); // todo snapshots indices } public long getIndexModificationStamp(StubIndexKey indexId, @NotNull Project project) { diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java index 1ba84333fa16..109274dc349d 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java @@ -329,9 +329,11 @@ public class FileBasedIndexImpl extends FileBasedIndex { final int version = extension.getVersion(); final File versionFile = IndexInfrastructure.getVersionFile(name); - final boolean versionFileExisted = versionFile.exists(); boolean versionChanged = false; - if (IndexingStamp.versionDiffers(versionFile, version)) { + + if (IndexingStamp.versionDiffers(name, version)) { + final boolean versionFileExisted = versionFile.exists(); + if (versionFileExisted) { versionChanged = true; LOG.info("Version has changed for index " + name + ". The index will be rebuilt."); @@ -341,15 +343,17 @@ public class FileBasedIndexImpl extends FileBasedIndex { } File rootDir = IndexInfrastructure.getIndexRootDir(name); if (versionFileExisted) FileUtil.deleteWithRenaming(rootDir); - IndexingStamp.rewriteVersion(versionFile, version); + IndexingStamp.rewriteVersion(name, version); } - initIndexStorage(extension, version, versionFile, state); + initIndexStorage(extension, version, state); return versionChanged; } - private static void initIndexStorage(@NotNull FileBasedIndexExtension extension, int version, @NotNull File versionFile, IndexConfiguration state) + private static void initIndexStorage(@NotNull FileBasedIndexExtension extension, + int version, + IndexConfiguration state) throws IOException { VfsAwareMapIndexStorage storage = null; final ID name = extension.getName(); @@ -401,7 +405,7 @@ public class FileBasedIndexImpl extends FileBasedIndex { if (extension.hasSnapshotMapping() && (!contentHashesEnumeratorOk || instantiatedStorage)) { FileUtil.deleteWithRenaming(IndexInfrastructure.getPersistentIndexRootDir(name)); // todo there is possibility of corruption of storage and content hashes } - IndexingStamp.rewriteVersion(versionFile, version); + IndexingStamp.rewriteVersion(name, version); } } } @@ -1137,7 +1141,7 @@ public class FileBasedIndexImpl extends FileBasedIndex { private void advanceIndexVersion(ID indexId) { try { - IndexingStamp.rewriteVersion(IndexInfrastructure.getVersionFile(indexId), myState.getIndexVersion(indexId)); + IndexingStamp.rewriteVersion(indexId, myState.getIndexVersion(indexId)); } catch (IOException e) { LOG.error(e); diff --git a/platform/lang-impl/src/com/intellij/util/indexing/IndexingStamp.java b/platform/lang-impl/src/com/intellij/util/indexing/IndexingStamp.java index 514634c79856..1627100427f8 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/IndexingStamp.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/IndexingStamp.java @@ -71,7 +71,8 @@ public class IndexingStamp { private IndexingStamp() {} - public static synchronized void rewriteVersion(@NotNull final File file, final int version) throws IOException { + public static synchronized void rewriteVersion(@NotNull ID indexId, final int version) throws IOException { + File file = IndexInfrastructure.getVersionFile(indexId); if (FileBasedIndexImpl.LOG.isDebugEnabled()) { FileBasedIndexImpl.LOG.debug("Rewriting " + file + "," + version); } @@ -122,7 +123,8 @@ public class IndexingStamp { private static final int OUR_INDICES_TIMESTAMP_INCREMENT = SystemProperties.getIntProperty("idea.indices.timestamp.resolution", 1); - public static boolean versionDiffers(@NotNull File versionFile, final int currentIndexVersion) { + public static boolean versionDiffers(@NotNull ID indexId, final int currentIndexVersion) { + File versionFile = IndexInfrastructure.getVersionFile(indexId); try { ourLastStamp = Math.max(ourLastStamp, versionFile.lastModified()); final DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(versionFile)));