Make persistent action cache work on Windows

CompactPersistentActionCache didn't work because it trys to rename a file to an existing file which JavaIoFileSystem doesn't support. Solution is deleting the file before renaming.

--
Change-Id: I8d476cd2835c8cddbeed8f556361e0e35d1a2bd4
Reviewed-on: https://bazel-review.googlesource.com/#/c/3802
MOS_MIGRATED_REVID=124828957
diff --git a/src/main/java/com/google/devtools/build/lib/util/PersistentMap.java b/src/main/java/com/google/devtools/build/lib/util/PersistentMap.java
index 2835c5c..56ac9a1 100644
--- a/src/main/java/com/google/devtools/build/lib/util/PersistentMap.java
+++ b/src/main/java/com/google/devtools/build/lib/util/PersistentMap.java
@@ -114,7 +114,9 @@
     delegate = map;
   }
 
-  @Override protected Map<K, V> delegate() {
+
+  @Override
+  protected Map<K, V> delegate() {
     return delegate;
   }
 
@@ -222,7 +224,7 @@
         }
         // Force the map to be dirty, so that we can save it to disk.
         dirty = true;
-        save(/*fullSave=*/true);
+        save(/*fullSave=*/ true);
       } else {
         dirty = false;
       }
@@ -236,7 +238,7 @@
    * @throws IOException
    */
   public void load() throws IOException {
-    load(/*throwOnLoadFailure=*/false);
+    load(/*throwOnLoadFailure=*/ false);
   }
 
   @Override
@@ -289,6 +291,7 @@
             mapFile.getRelative(FileSystemUtils.replaceExtension(mapFile.asFragment(), ".tmp"));
         try {
           saveEntries(delegate(), mapTemp);
+          mapFile.delete();
           mapTemp.renameTo(mapFile);
         } finally {
           mapTemp.delete();
@@ -331,8 +334,7 @@
     if (!mapFile.exists()) {
       return;
     }
-    DataInputStream in =
-      new DataInputStream(new BufferedInputStream(mapFile.getInputStream()));
+    DataInputStream in = new DataInputStream(new BufferedInputStream(mapFile.getInputStream()));
     try {
       long fileSize = mapFile.getFileSize();
       if (fileSize < (16)) {
@@ -383,7 +385,7 @@
   private DataOutputStream createMapFile(Path mapFile) throws IOException {
     FileSystemUtils.createDirectoryAndParents(mapFile.getParentDirectory());
     DataOutputStream out =
-      new DataOutputStream(new BufferedOutputStream(mapFile.getOutputStream()));
+        new DataOutputStream(new BufferedOutputStream(mapFile.getOutputStream()));
     out.writeLong(MAGIC);
     out.writeLong(version);
     return out;
@@ -397,8 +399,7 @@
    *        DataOutputStream.
    * @throws IOException
    */
-  private void writeEntries(DataOutputStream out, Map<K, V> map)
-      throws IOException {
+  private void writeEntries(DataOutputStream out, Map<K, V> map) throws IOException {
     for (Map.Entry<K, V> entry : map.entrySet()) {
       out.writeByte(ENTRY_MAGIC);
       writeKey(entry.getKey(), out);
@@ -453,8 +454,7 @@
    * @param out the DataOutputStream to write the entry to.
    * @throws IOException
    */
-  protected abstract void writeKey(K key, DataOutputStream out)
-      throws IOException;
+  protected abstract void writeKey(K key, DataOutputStream out) throws IOException;
 
   /**
    * Writes a value of this map into the specified DataOutputStream.
@@ -463,8 +463,7 @@
    * @param out the DataOutputStream to write the entry to.
    * @throws IOException
    */
-  protected abstract void writeValue(V value, DataOutputStream out)
-      throws IOException;
+  protected abstract void writeValue(V value, DataOutputStream out) throws IOException;
 
   /**
    * Reads an entry of this map from the specified DataInputStream.
@@ -484,3 +483,4 @@
    */
   protected abstract V readValue(DataInputStream in) throws IOException;
 }
+