]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsSearcherBase.java
Replace instantiations of DatabaseException in indexing
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / IndexedRelationsSearcherBase.java
index 82f4abdd905d570600a9a10871e4d7f7e43cb414..afcaafd51858229f8ac153c535493c1711717cef 100644 (file)
@@ -62,6 +62,8 @@ import org.simantics.db.Session;
 import org.simantics.db.common.request.SafeName;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.indexing.exception.IndexCorruptedException;
+import org.simantics.db.indexing.exception.IndexingException;
 import org.simantics.db.indexing.internal.IndexingJob;
 import org.simantics.db.layer0.adapter.GenericRelation;
 import org.simantics.db.layer0.genericrelation.IndexException;
@@ -269,13 +271,13 @@ abstract public class IndexedRelationsSearcherBase {
       STRING_TYPE.freeze();
     }
 
-    protected static Field makeField(String fieldName, String fieldClass) throws DatabaseException {
+    protected static Field makeField(String fieldName, String fieldClass) throws IndexingException {
         switch (fieldClass) {
         case "Long":   return new LongField(fieldName, 0L, Field.Store.YES);
         case "String": return new Field    (fieldName, "", STRING_TYPE);
         case "Text":   return new TextField(fieldName, "", Field.Store.YES);
         default:
-            throw new DatabaseException("Can only index Long, String and Text fields, encountered field type " + fieldClass);
+            throw new IndexingException("Can only index Long, String and Text fields, encountered field type " + fieldClass);
         }
     }
 
@@ -601,7 +603,7 @@ abstract public class IndexedRelationsSearcherBase {
                     try {
                         GenericRelation r = graph.adapt(relation, GenericRelation.class);
                         if (r == null)
-                            throw new DatabaseException("Given resource " + relation + "could not be adapted to GenericRelation.");
+                            throw new IndexingException("Given resource " + relation + "could not be adapted to GenericRelation.");
 
                         GenericRelation selection = r.select(getPattern(r, bound.length), bound);
 
@@ -609,7 +611,7 @@ abstract public class IndexedRelationsSearcherBase {
                         initializeIndexImpl(new CompletableFuture<>(), mon, r, results, bound, overwrite);
                     } catch (IOException e) {
                         getLogger().error("Index is in problematic state! {}", this, e);
-                        throw new DatabaseException(e);
+                        throw new IndexingException(e);
                     }
                 });
     }
@@ -732,7 +734,7 @@ abstract public class IndexedRelationsSearcherBase {
     }
 
     
-    public List<Object[]> debugDocs(IProgressMonitor monitor) throws ParseException, IOException, DatabaseException {
+    public List<Object[]> debugDocs(IProgressMonitor monitor) throws ParseException, IOException, IndexingException {
     
            Query query = new MatchAllDocsQuery(); 
        
@@ -756,10 +758,10 @@ abstract public class IndexedRelationsSearcherBase {
        
             } catch (CorruptIndexException e) {
                 getLogger().error("Index is corrupted! {}", this, e);
-                throw new DatabaseException(e);
+                throw new IndexCorruptedException("Index is corrupted! " + this, e);
             } catch (IOException e) {
                 getLogger().error("Index is in problematic state! {}", this, e);
-                throw new DatabaseException(e);
+                throw new IndexingException(e);
             }
 
            }
@@ -770,7 +772,7 @@ abstract public class IndexedRelationsSearcherBase {
 
     
     List<Map<String, Object>> doSearch(IProgressMonitor monitor, RequestProcessor processor, String search, int maxResultCount) throws ParseException, IOException,
-    DatabaseException {
+    IndexingException {
 
         // An empty search string will crash QueryParser
         // Just return no results for empty queries.
@@ -803,54 +805,62 @@ abstract public class IndexedRelationsSearcherBase {
             return Collections.emptyList();
         }
 
-        return processor.syncRequest(new Read<List<Map<String, Object>>>() {
+        try {
+            return processor.syncRequest(new Read<List<Map<String, Object>>>() {
 
-            @Override
-            public List<Map<String, Object>> perform(ReadGraph graph) throws DatabaseException {
+                @Override
+                public List<Map<String, Object>> perform(ReadGraph graph) throws DatabaseException {
 
-                GenericRelation r = graph.adapt(relation, GenericRelation.class);
-                if (r == null)
-                    throw new DatabaseException("Given resource " + graph.syncRequest(new SafeName(relation))
-                            + "could not be adapted to GenericRelation.");
+                    GenericRelation r = graph.adapt(relation, GenericRelation.class);
+                    if (r == null)
+                        throw new IndexingException("Given resource " + graph.syncRequest(new SafeName(relation))
+                                + "could not be adapted to GenericRelation.");
 
-                SerialisationSupport support = graph.getService(SerialisationSupport.class);
+                    SerialisationSupport support = graph.getService(SerialisationSupport.class);
 
-                List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(docs.scoreDocs.length);
-                
-                final DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
-                
-                for (ScoreDoc scoreDoc : docs.scoreDocs) {
+                    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(docs.scoreDocs.length);
+                    
+                    final DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
+                    
+                    for (ScoreDoc scoreDoc : docs.scoreDocs) {
 
-                    try {
+                        try {
 
-                        reader.document(scoreDoc.doc, visitor);
-                        
-                        Document doc = visitor.getDocument();
-
-                        List<IndexableField> fs = doc.getFields();
-                        Map<String, Object> entry = new THashMap<String, Object>(fs.size());
-                        for (IndexableField f : fs) {
-                            IndexSchema.Type type = schema.typeMap.get(f.name());
-                            if (type == IndexSchema.Type.LONG) {
-                                entry.put(f.name(), support.getResource(f.numericValue().longValue()));
-                            } else {
-                                entry.put(f.name(), f.stringValue());
+                            reader.document(scoreDoc.doc, visitor);
+                            
+                            Document doc = visitor.getDocument();
+
+                            List<IndexableField> fs = doc.getFields();
+                            Map<String, Object> entry = new THashMap<String, Object>(fs.size());
+                            for (IndexableField f : fs) {
+                                IndexSchema.Type type = schema.typeMap.get(f.name());
+                                if (type == IndexSchema.Type.LONG) {
+                                    entry.put(f.name(), support.getResource(f.numericValue().longValue()));
+                                } else {
+                                    entry.put(f.name(), f.stringValue());
+                                }
                             }
+                            
+                            result.add(entry);
+
+                        } catch (CorruptIndexException e) {
+                            getLogger().error("Index is corrupted! {}", this, e);
+                            throw new IndexCorruptedException("Index is corrupted! " + " " + this + " " + scoreDoc, e);
+                        } catch (IOException e) {
+                            getLogger().error("Index is in problematic state! {}", this, e);
+                            throw new IndexingException(e);
                         }
-                        
-                        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;
                 }
-                return result;
+            });
+        } catch (DatabaseException e) {
+            if (e instanceof IndexingException) {
+                throw (IndexingException) e;
+            } else {
+                throw new IndexingException(e);
             }
-        });
+        }
     }
 
     static class ResourceVisitor extends StoredFieldVisitor {
@@ -896,7 +906,7 @@ abstract public class IndexedRelationsSearcherBase {
     }
 
     List<Resource> doSearchResources(IProgressMonitor monitor, RequestProcessor processor, String search, int maxResultCount) throws ParseException, IOException,
-    DatabaseException {
+    IndexingException {
 
         // An empty search string will crash QueryParser
         // Just return no results for empty queries.
@@ -929,37 +939,45 @@ abstract public class IndexedRelationsSearcherBase {
             return Collections.emptyList();
         }
         
-        return processor.syncRequest(new Read<List<Resource>>() {
+        try {
+            return processor.syncRequest(new Read<List<Resource>>() {
 
-            @Override
-            public List<Resource> perform(ReadGraph graph) throws DatabaseException {
+                @Override
+                public List<Resource> perform(ReadGraph graph) throws DatabaseException {
 
-               CollectionSupport cs = graph.getService(CollectionSupport.class);
-                SerialisationSupport support = graph.getService(SerialisationSupport.class);
-                
-               List<Resource> result = cs.createList();
-                
-               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);
+                       CollectionSupport cs = graph.getService(CollectionSupport.class);
+                    SerialisationSupport support = graph.getService(SerialisationSupport.class);
+                    
+                       List<Resource> result = cs.createList();
+                    
+                       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 IndexCorruptedException("Index is corrupted! " + " " + this + " " + scoreDoc, e);
+                        } catch (IOException e) {
+                            getLogger().error("Index is in problematic state! {}", this, e);
+                            throw new IndexingException(e);
+                        }
                     }
+                    return result;
                 }
-                return result;
+            });
+        } catch (DatabaseException e) {
+            if (e instanceof IndexingException) {
+                throw (IndexingException) e;
+            } else {
+                throw new IndexingException(e);
             }
-        });
+        }
     }
 
     List<Object> doList(IProgressMonitor monitor, RequestProcessor processor) throws ParseException, IOException,
-    DatabaseException {
+    IndexingException {
 
         assertAccessOpen(false);
 
@@ -979,10 +997,10 @@ abstract public class IndexedRelationsSearcherBase {
 
                } catch (CorruptIndexException e) {
                    getLogger().error("Index is corrupted! {}", this, e);
-                       throw new DatabaseException(e);
+                   throw new IndexCorruptedException("Index is corrupted! " + " " + this + " " + scoreDoc, e);
                } catch (IOException e) {
                    getLogger().error("Index is in problematic state! {}", this, e);
-                       throw new DatabaseException(e);
+                       throw new IndexingException(e);
                }
 
         }