X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.indexing%2Fsrc%2Forg%2Fsimantics%2Fdb%2Findexing%2FIndexUtils.java;h=26a6e68ae9f9378dd4b7a429c7ac2ccdbe317684;hb=99fde2d98f5eda43f77c3fad4c07a2f4f234380c;hp=23e93ea1c9a7c743f80118dab680ca3026d4fa09;hpb=3c524553c98b56075d854f355bc7bab2e3ae17f7;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java index 23e93ea1c..26a6e68ae 100644 --- a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java +++ b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java @@ -11,6 +11,7 @@ import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.NumericUtils; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -18,14 +19,21 @@ import org.simantics.db.Session; import org.simantics.db.common.procedure.adapter.TransientCacheListener; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.indexing.exception.IndexCorruptedException; +import org.simantics.db.layer0.genericrelation.Dependencies; import org.simantics.db.layer0.genericrelation.IndexQueries; +import org.simantics.db.layer0.genericrelation.IndexedRelations; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.service.CollectionSupport; import org.simantics.layer0.Layer0; import org.simantics.operation.Layer0X; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class IndexUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(IndexUtils.class); + public static Collection> find(ReadGraph graph, Resource index, String filter) throws DatabaseException { Collection> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.>>instance()); @@ -84,10 +92,9 @@ public class IndexUtils { Layer0 L0 = Layer0.getInstance(graph); HashSet results = new HashSet(); - String search = "Name:" + name; + String search = IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name); - for(Map entry : find(graph, model, search)) { - Resource resource = (Resource)entry.get("Resource"); + for(Resource resource : findResources(graph, model, search)) { if(name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource); } return results; @@ -98,10 +105,9 @@ public class IndexUtils { HashSet results = new HashSet(); Layer0 L0 = Layer0.getInstance(graph); String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING); - String search = "Types:" + IndexQueries.quoteTerm(typeName); + String search = IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName); - for(Map entry : find(graph, model, search)) { - Resource resource = (Resource)entry.get("Resource"); + for(Resource resource : findResources(graph, model, search)) { if(graph.isInstanceOf(resource, type)) results.add(resource); } return results; @@ -113,10 +119,10 @@ public class IndexUtils { HashSet results = new HashSet(); String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING); - String search = "Types:" + IndexQueries.quoteTerm(typeName) + " AND Name:" + IndexQueries.quoteTerm(name); + + String search = IndexQueries.and(IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName), IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name)); - for(Map entry : find(graph, model, search)) { - Resource resource = (Resource)entry.get("Resource"); + for(Resource resource : findResources(graph, model, search)) { if(graph.isInstanceOf(resource, type)) results.add(resource); } return results; @@ -139,10 +145,25 @@ public class IndexUtils { mem.flush(progress); IndexedRelationsSearcher searcher = mem.get(session, L0X.DependenciesRelation, indexRoot); - return searcher.doList(progress, session); - + List results; + try { + results = searcher.doList(progress, session); + } catch (IndexCorruptedException e) { + LOGGER.error("Index is corrupted for indexRoot {}", indexRoot, e); + rebuild(session, progress); + // if this fails then no can do + searcher = mem.get(session, L0X.DependenciesRelation, indexRoot); + results = searcher.doList(progress, session); + } + return results; } + private static void rebuild(Session session, IProgressMonitor monitor) throws Exception { + LOGGER.error("Trying to rebuild index"); + DatabaseIndexing.deleteAllIndexes(); + session.getService(IndexedRelations.class).fullRebuild(SubMonitor.convert(monitor, 100), session); + } + public static Term longTerm(String key, Long value) { BytesRef ref = new BytesRef(); NumericUtils.longToPrefixCoded( value, 0, ref );