X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.indexing%2Fsrc%2Forg%2Fsimantics%2Fdb%2Findexing%2FIndexedRelationsSearcherBase.java;h=dad5ad07b13ea49011ea03d0d70b5a254c62d32c;hb=da749c56c5caad4252cd27276bf19b08f4dcb778;hp=74d3299018553d1d37f3539ee5fd91cdd6f77dd6;hpb=55be1eddbfed0d3d9e9e95a7d2547e287fde84ca;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsSearcherBase.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsSearcherBase.java index 74d329901..dad5ad07b 100644 --- a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsSearcherBase.java +++ b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsSearcherBase.java @@ -62,8 +62,11 @@ 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; import org.simantics.db.request.Read; import org.simantics.db.service.CollectionSupport; import org.simantics.db.service.SerialisationSupport; @@ -115,14 +118,18 @@ abstract public class IndexedRelationsSearcherBase { this.state = State.READY; } + public State state() { + return state; + } + protected boolean checkState(State state) { return this.state == state; } - protected void assertState(State state) throws AssertionError { - - if(this.state != state) throw new AssertionError("Illegal state, expected " + state.name() + " but was in " + this.state.name()); - + protected void assertState(State state) throws IndexException { + State s = this.state; + if (s != state) + throw new IndexException("Illegal index searcher state, expected " + state.name() + " but state was " + s.name()); } public void changeState(IProgressMonitor monitor, Session session, State state) { @@ -264,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); } } @@ -340,6 +347,7 @@ abstract public class IndexedRelationsSearcherBase { removedTerm = new Term(key, (String) keyValue); } else { // FIXME: should throw an exception for illegal input data but this would leave the index in an incoherent state + getLogger().error("Attempting to remove document from index of {} with key {} and unrecognized key value type {} : {}", input, key, keyValue, keyValue != null ? keyValue.getClass() : "null"); continue; } @@ -426,7 +434,7 @@ abstract public class IndexedRelationsSearcherBase { boolean done = false; if(requireChangeInfoOnReplace()) { TopDocs exist = searcher.search(new TermQuery(removedTerm), null, 2); - if(exist.scoreDocs.length == 1 && requireChangeInfoOnReplace()) { + if(exist.scoreDocs.length == 1) { Document doc = reader.document(exist.scoreDocs[0].doc); if(!areSame(doc, document)) { writer.deleteDocuments(removedTerm); @@ -497,7 +505,7 @@ abstract public class IndexedRelationsSearcherBase { */ final IndexSchema schema; - Resource input; + final Resource input; Path indexPath; @@ -522,6 +530,14 @@ abstract public class IndexedRelationsSearcherBase { this.schema = IndexSchema.readFromRelation(session, relation); } + public Resource getRelation() { + return relation; + } + + public Resource getInput() { + return input; + } + Directory getDirectory(Session session) throws IOException { return FSDirectory.open(indexPath.toFile()); } @@ -596,7 +612,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); @@ -604,7 +620,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); } }); } @@ -624,7 +640,7 @@ abstract public class IndexedRelationsSearcherBase { mon.subTask("Erasing previous index"); if (getLogger().isDebugEnabled()) getLogger().debug("Erasing previous index {}", indexPath.toAbsolutePath()); - FileUtils.delete(indexPath); + FileUtils.emptyDirectory(indexPath); } } @@ -727,7 +743,7 @@ abstract public class IndexedRelationsSearcherBase { } - public List debugDocs(IProgressMonitor monitor) throws ParseException, IOException, DatabaseException { + public List debugDocs(IProgressMonitor monitor) throws ParseException, IOException, IndexingException { Query query = new MatchAllDocsQuery(); @@ -751,10 +767,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); } } @@ -765,7 +781,7 @@ abstract public class IndexedRelationsSearcherBase { List> 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. @@ -798,54 +814,62 @@ abstract public class IndexedRelationsSearcherBase { return Collections.emptyList(); } - return processor.syncRequest(new Read>>() { + try { + return processor.syncRequest(new Read>>() { - @Override - public List> perform(ReadGraph graph) throws DatabaseException { + @Override + public List> 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> result = new ArrayList>(docs.scoreDocs.length); - - final DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor(); - - for (ScoreDoc scoreDoc : docs.scoreDocs) { + List> result = new ArrayList>(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 fs = doc.getFields(); - Map entry = new THashMap(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 fs = doc.getFields(); + Map entry = new THashMap(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 { @@ -891,7 +915,7 @@ abstract public class IndexedRelationsSearcherBase { } List 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. @@ -924,37 +948,45 @@ abstract public class IndexedRelationsSearcherBase { return Collections.emptyList(); } - return processor.syncRequest(new Read>() { + try { + return processor.syncRequest(new Read>() { - @Override - public List perform(ReadGraph graph) throws DatabaseException { + @Override + public List perform(ReadGraph graph) throws DatabaseException { - CollectionSupport cs = graph.getService(CollectionSupport.class); - SerialisationSupport support = graph.getService(SerialisationSupport.class); - - List 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 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 doList(IProgressMonitor monitor, RequestProcessor processor) throws ParseException, IOException, - DatabaseException { + IndexingException { assertAccessOpen(false); @@ -974,10 +1006,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); } }