Fixed ConcurrentModificationException occurred as storing and cleaning
up VC happening at the same time.

Change-Id: If667cb5b6a4b948ede4f37f4bd8b02c1a3f19e73
diff --git a/Changes b/Changes
index c2bb14d..ad70b06 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,7 @@
-0.60.3 2022-03-10
+0.60.3 2022-03-17
     - [cleanup] Updated fingerprints to base64url (closed #83)
+    - [bugfix] Fixed ConcurrentModificationException in VC cache
+      handling (margaretha)
 
     !!! This will invalidate all VC caches. Please recache!
 
diff --git a/src/main/java/de/ids_mannheim/korap/cache/VirtualCorpusCache.java b/src/main/java/de/ids_mannheim/korap/cache/VirtualCorpusCache.java
index b14aeb5..26d74e2 100644
--- a/src/main/java/de/ids_mannheim/korap/cache/VirtualCorpusCache.java
+++ b/src/main/java/de/ids_mannheim/korap/cache/VirtualCorpusCache.java
@@ -86,10 +86,9 @@
 
     public static void store (String vcId, Map<String, DocBits> vcData) {
         map.put(vcId, vcData);
-        for (String leafFingerprint : vcData.keySet()) {
+        vcData.keySet().forEach(leafFingerprint -> {
             storeOnDisk(vcId, leafFingerprint, vcData.get(leafFingerprint));
-        }
-
+        });
     }
 
     public static void store (String vcId, KrillIndex index)
@@ -116,10 +115,10 @@
 
 
     public static Map<String, DocBits> retrieve (String vcId) {
-        if (map.containsKey(vcId)) {
-            return map.get(vcId);
+        Map<String, DocBits> vcData = map.get(vcId);
+        if (vcData != null) {
+            return vcData;
         }
-        Map<String, DocBits> vcData = null;
         File dir = new File(CACHE_LOCATION + "/" + vcId);
         if (dir.exists()) {
             vcData = new HashMap<String, DocBits>();
@@ -135,6 +134,7 @@
                     return null;
                 }
             }
+            vcData = Collections.synchronizedMap(vcData);
             map.put(vcId, vcData);
         }
         return vcData;