]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsSearcherBase.java
Add logging to indexing & replace File-API with NIO Path-API
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / IndexedRelationsSearcherBase.java
index b303dfc4c4b1fc73f2b10eba9363cab20ddabfb0..9441bb75b8ee5a4a3c7fd419ac725e70778cd3ca 100644 (file)
@@ -12,7 +12,6 @@
  *******************************************************************************/
 package org.simantics.db.indexing;
 
-import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -63,7 +62,6 @@ import org.simantics.db.RequestProcessor;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.common.request.SafeName;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.indexing.internal.IndexingJob;
@@ -73,6 +71,7 @@ import org.simantics.db.service.CollectionSupport;
 import org.simantics.db.service.SerialisationSupport;
 import org.simantics.utils.FileUtils;
 import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
 
 import gnu.trove.map.hash.THashMap;
 
@@ -103,6 +102,8 @@ abstract public class IndexedRelationsSearcherBase {
     }
 
     public void setProblem(Throwable t) {
+        if (t != null)
+            getLogger().error("Setting problem for {} and previous state {}", this, this.state, t);
        this.state = State.PROBLEM;
        this.exception = t;
     }
@@ -131,7 +132,11 @@ abstract public class IndexedRelationsSearcherBase {
 
     protected void changeState(IProgressMonitor monitor, Session session, State state, int depth) {
 
-       if(this.state == state) return;
+       if (this.state == state) {
+           if (getLogger().isDebugEnabled())
+               getLogger().debug("Trying to change state {} to the same as previous state {} in depth {} with {}", state, this.state, depth, this);
+           return;
+       }
 
        if (IndexPolicy.TRACE_INDEX_MANAGEMENT)
                System.err.println("Index state " + this.state.name() + " => " + state.name() + " " + this);
@@ -140,20 +145,29 @@ abstract public class IndexedRelationsSearcherBase {
        
        // Try to exit problem state
        if (State.PROBLEM == this.state && depth > 0) {
+           getLogger().info("Try to exit problem state for {} and state {}", this, state);
                Throwable t = bestEffortClear(monitor, session);
                if(t != null) {
+                   getLogger().error("Best effort clear has failed for state {} and this {}", state, this, t);
                                exception = t;
                                return;
                }
                // Managed to get into initial state
                this.state = State.NONE;
+               getLogger().info("Managed to get into initial state {}", this.state);
                return;
        }
 
        // Cannot move into read from no index
-       if (State.NONE ==  this.state && State.READ == state) return;
+       if (State.NONE ==  this.state && State.READ == state) {
+           getLogger().info("Cannot move into read from no index in {} with state {}", this, state);
+           return;
+       }
        // Cannot move into write from no index
-       if (State.NONE ==  this.state && State.WRITE == state) return;
+       if (State.NONE ==  this.state && State.WRITE == state) {
+           getLogger().info("Cannot move into write from no index in {} with state {}", this, state);
+           return;
+       }
        
                boolean success = false;
 
@@ -225,9 +239,7 @@ abstract public class IndexedRelationsSearcherBase {
                }
                 
        } catch (Throwable t) {
-
                setProblem(t);
-
        } finally {
 
                if(!success) {
@@ -486,7 +498,7 @@ abstract public class IndexedRelationsSearcherBase {
 
     Resource         input;
 
-    File             indexPath;
+    Path             indexPath;
 
     Directory        directory;
 
@@ -510,7 +522,7 @@ abstract public class IndexedRelationsSearcherBase {
     }
 
     Directory getDirectory(Session session) throws IOException {
-        return FSDirectory.open(indexPath);
+        return FSDirectory.open(indexPath.toFile());
     }
 
     abstract String getDescriptor();
@@ -597,6 +609,7 @@ abstract public class IndexedRelationsSearcherBase {
                     try {
                         initializeIndexImpl(mon, graph, bound, overwrite);
                     } catch (IOException e) {
+                        getLogger().error("Index is in problematic state! {}", this, e);
                         throw new DatabaseException(e);
                     }
                 });
@@ -612,8 +625,11 @@ abstract public class IndexedRelationsSearcherBase {
         mon.beginTask("Initializing Index", 100);
 
         if (overwrite) {
-            mon.subTask("Erasing previous index");
-            FileUtils.deleteAll(indexPath);
+            if (Files.exists(indexPath)) {
+                mon.subTask("Erasing previous index");
+                getLogger().info("Erasing previous index {}", indexPath.toAbsolutePath());
+                FileUtils.delete(indexPath);
+            }
         }
 
         final AtomicReference<FSDirectory> directory = new AtomicReference<FSDirectory>();
@@ -623,7 +639,7 @@ abstract public class IndexedRelationsSearcherBase {
             mon.subTask("Start index write");
             createDirectory(indexPath);
 
-            directory.set(FSDirectory.open(indexPath));
+            directory.set(FSDirectory.open(indexPath.toFile()));
             IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_4_9, Queries.getAnalyzer()).setOpenMode(OpenMode.CREATE);
             writer.set(new IndexWriter(directory.get(), conf));
 
@@ -668,8 +684,10 @@ abstract public class IndexedRelationsSearcherBase {
                             try {
                                 writer.get().addDocument(document);
                             } catch (CorruptIndexException e) {
+                                getLogger().error("Index is corrupted! {}", this, e);
                                 throw new IllegalStateException(e);
                             } catch (IOException e) {
+                                getLogger().error("Index is in problematic state! {}", this, e);
                                 throw new IllegalStateException(e);
                             } finally {
                                 synchronized (mon) {
@@ -688,7 +706,7 @@ abstract public class IndexedRelationsSearcherBase {
             try {
                 s.acquire(INDEXING_THREAD_COUNT);
             } catch (InterruptedException e) {
-                Logger.defaultLogError(e);
+                getLogger().error("Could not initialize index {}", this, e);
             }
 
             // http://www.gossamer-threads.com/lists/lucene/java-dev/47895
@@ -703,9 +721,7 @@ abstract public class IndexedRelationsSearcherBase {
                 System.out.println(getDescriptor() + "Wrote index at " + indexPath + " in " + (1e-9 * (System.nanoTime()-start)) + " seconds.");
 
         } catch (DatabaseException e) {
-            
-            Logger.defaultLogError(e);
-            
+            getLogger().error("Could not initialize index due to db {}", this, e);
         } finally {
             try {
                 closeWriter(writer.getAndSet(null));
@@ -738,11 +754,13 @@ abstract public class IndexedRelationsSearcherBase {
                    }
                    result.add(o);
        
-               } catch (CorruptIndexException e) {
-                   throw new DatabaseException(e);
-               } catch (IOException e) {
-                   throw new DatabaseException(e);
-               }
+            } catch (CorruptIndexException e) {
+                getLogger().error("Index is corrupted! {}", this, e);
+                throw new DatabaseException(e);
+            } catch (IOException e) {
+                getLogger().error("Index is in problematic state! {}", this, e);
+                throw new DatabaseException(e);
+            }
 
            }
            
@@ -823,15 +841,14 @@ abstract public class IndexedRelationsSearcherBase {
                         result.add(entry);
 
                     } catch (CorruptIndexException e) {
+                        getLogger().error("Index is corrupted! {}", this, e);
                         throw new DatabaseException(e);
                     } catch (IOException e) {
+                        getLogger().error("Index is in problematic state! {}", this, e);
                         throw new DatabaseException(e);
                     }
-
                 }
-
                 return result;
-
             }
         });
     }
@@ -925,22 +942,18 @@ abstract public class IndexedRelationsSearcherBase {
                ResourceVisitor visitor = new ResourceVisitor();
                 
                 for (ScoreDoc scoreDoc : docs.scoreDocs) {
-
                     try {
-                       
                        reader.document(scoreDoc.doc, visitor);
                        result.add(support.getResource(visitor.id));
-
                     } catch (CorruptIndexException e) {
+                        getLogger().error("Index is corrupted! {}", this, e);
                         throw new DatabaseException(e);
                     } catch (IOException e) {
+                        getLogger().error("Index is in problematic state! {}", this, e);
                         throw new DatabaseException(e);
                     }
-
                 }
-
                 return result;
-
             }
         });
     }
@@ -965,8 +978,10 @@ abstract public class IndexedRelationsSearcherBase {
                        reader.document(scoreDoc.doc, visitor);
 
                } catch (CorruptIndexException e) {
+                   getLogger().error("Index is corrupted! {}", this, e);
                        throw new DatabaseException(e);
                } catch (IOException e) {
+                   getLogger().error("Index is in problematic state! {}", this, e);
                        throw new DatabaseException(e);
                }
 
@@ -976,59 +991,44 @@ abstract public class IndexedRelationsSearcherBase {
 
     }
     
-    protected static File getIndexDirectory(Session session, Resource relation, Resource input) {
-        File path = DatabaseIndexing.getIndexLocation(session, relation, input);
+    protected static Path getIndexDirectory(Session session, Resource relation, Resource input) {
+        Path path = DatabaseIndexing.getIndexLocation(session, relation, input);
 //        System.out.println("getIndexDirectory = " + path);
         return path;
     }
 
-    private static void createDirectory(File path) throws IOException {
-        Path p = path.toPath();
-        if (Files.exists(p) && !Files.isDirectory(p))
+    private static void createDirectory(Path path) throws IOException {
+        if (Files.exists(path) && !Files.isDirectory(path))
             throw new IOException("Could not create index directory " + path + ", a file by that name already exists");
-        Files.createDirectories(p);
+        Files.createDirectories(path);
     }
 
-    File getIndexPath() {
+    Path getIndexPath() {
         return indexPath;
     }
 
     boolean isIndexAvailable() {
-        return (indexPath.exists() && indexPath.isDirectory());
+        return (Files.exists(indexPath) && Files.isDirectory(indexPath));
     }
     
-    Throwable bestEffortClear(IProgressMonitor monitor, Session session) {
-       return null;
-    }
+    abstract Throwable bestEffortClear(IProgressMonitor monitor, Session session);
 
     /*
      * Start from scratch. Clear all caches and rebuild the index. 
      */
     Throwable clearDirectory(IProgressMonitor monitor, Session session) {
         
-               File file = getIndexPath();
+               Path file = getIndexPath();
 
        try {
-
-               for(int i=0;i<15;i++) {
-                       FileUtils.deleteDir(file);
-                       if(!file.exists()) {
-                               return null;
-                       }
-                       try {
-                               Thread.sleep(i*100);
-                       } catch (InterruptedException e) {
-                       }
-               }
-
+                       FileUtils.delete(file);
        } catch (Throwable t) {
-
+               getLogger().error("Could not delete directory {}", file.toAbsolutePath(), t);
                return t;
-               
        }
-
-       return new IllegalStateException("Failed to delete directory " + file.getAbsolutePath());
-
+       if (Files.exists(file))
+            return new IllegalStateException("Failed to delete directory " + file.toAbsolutePath());
+        return null;
     }
 
     private Field[] setFields(Field[] fs, Object[] result) {
@@ -1043,11 +1043,17 @@ abstract public class IndexedRelationsSearcherBase {
                     System.out.println(getDescriptor() + "index " + fs[i].name() + " = " + value + " : Long");
                 fs[i].setLongValue((Long) value);
             } else {
-                Logger.defaultLogError("Can only index Long and String fields, encountered " + value);
+                getLogger().error("Can only index Long and String fields, encountered " + value);
                 return null;
             }
         }
         return fs;
     }
 
+    protected abstract Logger getLogger();
+    
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + " [" + String.valueOf(schema) + ", " + String.valueOf(relation) + ", " + String.valueOf(input) + ", " + String.valueOf(indexPath) + ", " + String.valueOf(directory) + ", " + String.valueOf(state) + "]";
+    }
 }