[vcs-log] remove all storage files starting with logId when cleaning up broken indexes
authorJulia Beliaeva <Julia.Beliaeva@jetbrains.com>
Sat, 12 Nov 2016 16:59:47 +0000 (19:59 +0300)
committerJulia Beliaeva <Julia.Beliaeva@jetbrains.com>
Sat, 12 Nov 2016 17:27:05 +0000 (20:27 +0300)
Introduce deleteWithRenamingAllFilesStartingWith for that purpose.

platform/vcs-log/impl/src/com/intellij/vcs/log/data/index/VcsLogMessagesTrigramIndex.java
platform/vcs-log/impl/src/com/intellij/vcs/log/data/index/VcsLogPathsIndex.java
platform/vcs-log/impl/src/com/intellij/vcs/log/data/index/VcsLogPersistentIndex.java
platform/vcs-log/impl/src/com/intellij/vcs/log/data/index/VcsLogUserIndex.java
platform/vcs-log/impl/src/com/intellij/vcs/log/util/PersistentUtil.java

index c62dc82d592624100a8230d3f19219c77486ae75..31386ddc7a67d2b8ffec2a845e882740e1fc5fd9 100644 (file)
@@ -45,11 +45,6 @@ public class VcsLogMessagesTrigramIndex extends VcsLogFullDetailsIndex<Void> {
           fatalErrorHandler, disposableParent);
   }
 
-  @NotNull
-  public static Collection<File> getStorageFiles(@NotNull String logId) {
-    return Collections.singletonList(getStorageFile(TRIGRAMS, logId));
-  }
-
   @Nullable
   public ValueContainer.IntIterator getCommitsForSubstring(@NotNull String string) throws StorageException {
     MyTrigramProcessor trigramProcessor = new MyTrigramProcessor();
index 1c8d679595adf6bfe8bd64ac9c2cdcea59dbcd16..7679c6603ae8720e0d8478efcdddf2a4621d5d36 100644 (file)
@@ -76,12 +76,6 @@ public class VcsLogPathsIndex extends VcsLogFullDetailsIndex<Integer> {
                                            Page.PAGE_SIZE, null, getVersion());
   }
 
-  @NotNull
-  public static Collection<File> getStorageFiles(@NotNull String logId) {
-    return Arrays.asList(getStorageFile(INDEX_PATHS_IDS, logId),
-                         getStorageFile(PATHS, logId));
-  }
-
   @Override
   public void flush() throws StorageException {
     super.flush();
index ad8e8d697e04e87ff1cc4187c9744fe9a9bb5960..b5f41d1bb4e7eae76d36763e65b5b660854d9670 100644 (file)
@@ -412,12 +412,9 @@ public class VcsLogPersistentIndex implements VcsLogIndex, Disposable {
     }
 
     private static void cleanup(@NotNull String logId) {
-      IOUtil.deleteAllFilesStartingWith(getStorageFile(INDEX, COMMITS, logId, getVersion(), false));
-      IOUtil.deleteAllFilesStartingWith(getStorageFile(INDEX, MESSAGES, logId, VcsLogStorageImpl.VERSION + MESSAGES_VERSION, false));
-
-      VcsLogMessagesTrigramIndex.getStorageFiles(logId).forEach(IOUtil::deleteAllFilesStartingWith);
-      VcsLogUserIndex.getStorageFiles(logId).forEach(IOUtil::deleteAllFilesStartingWith);
-      VcsLogPathsIndex.getStorageFiles(logId).forEach(IOUtil::deleteAllFilesStartingWith);
+      if (!PersistentUtil.cleanupStorageFiles(INDEX, logId)) {
+        LOG.error("Could not clean up storage files in " + new File(PersistentUtil.LOG_CACHE, INDEX) + " starting with " + logId);
+      }
     }
   }
 
index 49b78b16b48a8331594e051d91f45d6f16d87a02..f9373877f6e8ec4fc692b429b08a598af1723ac9 100644 (file)
@@ -54,11 +54,6 @@ public class VcsLogUserIndex extends VcsLogFullDetailsIndex<Void> {
     ((UserIndexer)myIndexer).setFatalErrorConsumer(e -> consumer.consume(this, e));
   }
 
-  @NotNull
-  public static Collection<File> getStorageFiles(@NotNull String logId) {
-    return Collections.singletonList(getStorageFile(USERS, logId));
-  }
-
   public TIntHashSet getCommitsForUsers(@NotNull Set<VcsUser> users) throws IOException, StorageException {
     Set<Integer> ids = ContainerUtil.newHashSet();
     for (VcsUser user : users) {
index fe95a0c7e70074278c9a92b66455b0a45c66fcce..1fe13b30b7741566d956cdda8c7c61c540ef80f4 100644 (file)
@@ -17,6 +17,7 @@ package com.intellij.vcs.log.util;
 
 import com.intellij.openapi.application.PathManager;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.util.PathUtilRt;
@@ -80,6 +81,31 @@ public class PersistentUtil {
                                          storageFile);
   }
 
+  public static boolean deleteWithRenamingAllFilesStartingWith(@NotNull File baseFile) {
+    File parentFile = baseFile.getParentFile();
+    if (parentFile == null) return false;
+
+    File[] files = parentFile.listFiles(pathname -> pathname.getName().startsWith(baseFile.getName()));
+    if (files == null) return true;
+
+    boolean deleted = true;
+    for (File f : files) {
+      deleted &= FileUtil.deleteWithRenaming(f);
+    }
+    return deleted;
+  }
+
+  // this method cleans up all storage files for a project in a specified subdir
+  // it assumes that these storage files all start with "safeLogId."
+  // as method getStorageFile creates them
+  // so these two methods should be changed in sync
+  public static boolean cleanupStorageFiles(@NotNull String subdirName, @NotNull String id) {
+    File subdir = new File(LOG_CACHE, subdirName);
+    String safeLogId = PathUtilRt.suggestFileName(id, true, true);
+    return deleteWithRenamingAllFilesStartingWith(new File(subdir, safeLogId + "."));
+  }
+
+  // do not forget to change cleanupStorageFiles method when editing this one
   @NotNull
   public static File getStorageFile(@NotNull String subdirName,
                                     @NotNull String kind,