Improve reader/writer handling (fixes #55)

Change-Id: Ia2e81a211b6a8c0ee7c2de133accfdb1e6bc3bc2
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 3759329..d685b63 100644
--- a/src/main/java/de/ids_mannheim/korap/index/Indexer.java
+++ b/src/main/java/de/ids_mannheim/korap/index/Indexer.java
@@ -107,7 +107,7 @@
 
                 try {
                     if (addInsteadOfUpsert) {
-                        log.info("Add " + file + " to the index. ");
+                        log.info("{} Add {} to the index. ", this.count, file);
                         if (this.index.addDoc(new FileInputStream(file),
                                               true) == null) {
                             log.warn("fail.");
@@ -115,7 +115,7 @@
                         }
                     }
                     else {
-                        log.info("Add or update " + file + " to the index. ");
+                        log.info("{} Add or update {} to the index. ", this.count, file);
                         if (this.index.upsertDoc(new FileInputStream(file),
                                                  true) == null) {
                             log.warn("fail.");
@@ -128,8 +128,12 @@
                     }
 
                     // Commit in case the commit count is reached
-                    if ((this.count % this.commitCount) == 0)
+                    if ((this.count % this.commitCount) == 0) {
+
+                        // This will be done in addition to the
+                        // autocommit initiated by KrillIndex
                         this.commit();
+                    }
                 }
                 catch (FileNotFoundException e) {
                     log.error("File " + file + " is not found!");
@@ -156,9 +160,10 @@
         }
     }
 
-    private void closeIndex() throws IOException{
+    private void closeIndex() throws IOException {
         this.commit();
-        index.close();
+        this.index.closeReader();
+        this.index.closeWriter();
     }
 
     /**
@@ -170,7 +175,7 @@
      *            and a list of directories
      * @throws IOException
      */
-    public static void main (String[] argv) throws IOException {
+    public static void main (String[] argv) {
         
         Options options = new Options();
         options.addOption(Option.builder("c").longOpt("config")
@@ -190,7 +195,6 @@
         options.addOption(Option.builder("a").longOpt("addInsteadofUpsert")
                 .desc("Always add files to the index, never update")
                 .build());
-
         
         CommandLineParser parser = new DefaultParser();
 
@@ -212,6 +216,7 @@
             if (cmd.hasOption("a")) {
                 addInsteadOfUpsert = true;
             };
+
         }
         catch (MissingOptionException e) {
             HelpFormatter formatter = new HelpFormatter();
@@ -229,30 +234,35 @@
         // Load properties
         Properties prop = KrillProperties.loadProperties(propFile);
 
-        // Get indexer object
-        Indexer indexer = new Indexer(prop);
+        try {
+            // Get indexer object
+            Indexer indexer = new Indexer(prop);
 
-        // Iterate over list of directories
-        for (String arg : inputDirectories) {
-            log.info("Indexing files in " + arg);
-            File f = new File(arg);
-            if (f.isDirectory())
-                indexer.parse(f);
+            // Iterate over list of directories
+            for (String arg : inputDirectories) {
+                log.info("Indexing files in " + arg);
+                File f = new File(arg);
+                if (f.isDirectory())
+                    indexer.parse(f);
+            }
+            indexer.closeIndex();
+
+            // Final commit
+            log.info("Finished indexing.");
+            // Finish indexing
+            String message = "Added ";
+            if (!addInsteadOfUpsert)
+                message += "or updated ";
+            message += indexer.count + " file";
+            if (indexer.count > 1) {
+                message += "s";
+            }
+            System.out.println(message + ".");
         }
-        
-        indexer.closeIndex();
-        
-        // Final commit
-        log.info("Finished indexing.");
-        // Finish indexing
-        String message = "Added ";
-        if (!addInsteadOfUpsert)
-            message += "or updated ";
-        message += indexer.count + " file";
-        if (indexer.count > 1) {
-            message += "s";
+
+        catch (IOException e) {
+            log.error("Unexpected error: " + e);
+            e.printStackTrace();
         }
-        System.out.print(message + ".");
-        
     }
 }