Updated cache loading (fixed #55)

Change-Id: Ie77a84ca666325eef35adf40fc841962631ad8c1
diff --git a/Changes b/Changes
index 68caaa5..23d6a7d 100644
--- a/Changes
+++ b/Changes
@@ -1,3 +1,6 @@
+0.58.6 2019-05-02
+	- [bugfix] Updated cache loading (fixed #55) (diewald, margaretha)
+
 0.58.5 2019-03-18
     - [bugfix] Fix bug where duplicate keys occured in
       field data output (diewald)
diff --git a/pom.xml b/pom.xml
index 26308bf..76a0896 100644
--- a/pom.xml
+++ b/pom.xml
@@ -35,7 +35,7 @@
 
 	<groupId>de.ids_mannheim.korap</groupId>
 	<artifactId>Krill</artifactId>
-	<version>0.58.5</version>
+	<version>0.58.6</version>
 	<packaging>jar</packaging>
 
 	<name>Krill</name>
@@ -286,7 +286,7 @@
 				<version>2.22.1</version>
 				<configuration>
           		    <useSystemClassLoader>false</useSystemClassLoader>
-          		    <argLine>-Xms512m</argLine>
+          		    <argLine>-Xmx512m</argLine>
 					<excludes>
 						<exclude>**/benchmark/*</exclude>
 						<exclude>**/TestWPDIndex.java</exclude>
diff --git a/src/main/java/de/ids_mannheim/korap/KrillCollection.java b/src/main/java/de/ids_mannheim/korap/KrillCollection.java
index 3d8a3a6..11469e6 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillCollection.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillCollection.java
@@ -80,14 +80,16 @@
     // This advices the java compiler to ignore all loggings
     public static final boolean DEBUG = false;
 
-    public final static CacheManager cacheManager = CacheManager.newInstance();
-    public static Cache cache = cacheManager.getCache("named_vc");
+    public static CacheManager cacheManager;
+    public static Cache cache;
 
     /**
      * Construct a new KrillCollection.
      * 
      */
-    public KrillCollection () {};
+    public KrillCollection () {
+        initializeCache();
+    };
 
 
     /**
@@ -97,6 +99,7 @@
      *            The {@link KrillIndex} object.
      */
     public KrillCollection (KrillIndex index) {
+        initializeCache();
         this.index = index;
     };
 
@@ -107,7 +110,7 @@
      *            The KoralQuery document as a JSON string.
      */
     public KrillCollection (String jsonString) {
-
+        initializeCache();
         try {
             JsonNode json = mapper.readTree(jsonString);
 
@@ -142,7 +145,16 @@
         };
     };
 
-
+    public static void initializeCache () {
+        if (cacheManager == null) {
+            cacheManager = CacheManager.newInstance();
+        }
+        if (cache == null) {
+            cache = cacheManager.getCache("named_vc");
+        }
+    }
+    
+    
     /**
      * Set the {@link KrillIndex} the virtual collection refers to.
      * 
diff --git a/src/main/java/de/ids_mannheim/korap/collection/CollectionBuilder.java b/src/main/java/de/ids_mannheim/korap/collection/CollectionBuilder.java
index 07a1598..9f6dbe7 100644
--- a/src/main/java/de/ids_mannheim/korap/collection/CollectionBuilder.java
+++ b/src/main/java/de/ids_mannheim/korap/collection/CollectionBuilder.java
@@ -286,8 +286,10 @@
         };
 
         public Filter toFilter () throws QueryException {
-			Element element = KrillCollection.cache.get(this.reference);
-			
+            Element element = null;
+            if (KrillCollection.cache != null){
+                element = KrillCollection.cache.get(this.reference);
+            }
             if (element == null) {
                 if (DEBUG) {
                     log.debug(reference + " is NOT found in the cache");
diff --git a/src/main/java/de/ids_mannheim/korap/collection/ToCacheVCFilter.java b/src/main/java/de/ids_mannheim/korap/collection/ToCacheVCFilter.java
index 4e638ee..072e1dc 100644
--- a/src/main/java/de/ids_mannheim/korap/collection/ToCacheVCFilter.java
+++ b/src/main/java/de/ids_mannheim/korap/collection/ToCacheVCFilter.java
@@ -73,6 +73,9 @@
             CachedVCData cachedVCData =
                     new CachedVCData(new HashMap<>(docIdMap));
 
+            if (KrillCollection.cache == null){
+                KrillCollection.initializeCache();
+            }
             KrillCollection.cache.remove(cacheKey);
             KrillCollection.cache.put(new Element(cacheKey, cachedVCData));
         }
diff --git a/src/main/java/de/ids_mannheim/korap/index/Indexer.java b/src/main/java/de/ids_mannheim/korap/index/Indexer.java
index 005ac2e..3759329 100644
--- a/src/main/java/de/ids_mannheim/korap/index/Indexer.java
+++ b/src/main/java/de/ids_mannheim/korap/index/Indexer.java
@@ -157,6 +157,7 @@
     }
 
     private void closeIndex() throws IOException{
+        this.commit();
         index.close();
     }
 
@@ -238,7 +239,9 @@
             if (f.isDirectory())
                 indexer.parse(f);
         }
+        
         indexer.closeIndex();
+        
         // Final commit
         log.info("Finished indexing.");
         // Finish indexing