package org.simantics.db.indexing; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Map; import org.apache.lucene.index.Term; 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; 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()); Layer0 L0 = Layer0.getInstance(graph); Collection linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot)); if (linkedRoots.isEmpty()) return indexResult; Collection> result = indexResult; for (Resource dep : linkedRoots) { Collection> linkedIndexResults = find(graph, dep, filter); if (linkedIndexResults.isEmpty()) continue; if (result == indexResult) { result = new ArrayList>(indexResult.size() + linkedIndexResults.size()); result.addAll(indexResult); } else { } result.addAll(linkedIndexResults); } return result; } public static List findResources(ReadGraph graph, Resource index, String filter) throws DatabaseException { List indexResult = graph.syncRequest(new QueryIndexResources(index, filter), TransientCacheListener.>instance()); Layer0 L0 = Layer0.getInstance(graph); CollectionSupport coll = graph.getService(CollectionSupport.class); Collection linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot)); if (linkedRoots.isEmpty()) return indexResult; List result = indexResult; for (Resource dep : linkedRoots) { Collection linkedIndexResults = findResources(graph, dep, filter); if (linkedIndexResults.isEmpty()) continue; if (result == indexResult) { result = coll.createList(); result.addAll(indexResult); } result.addAll(linkedIndexResults); } Layer0Utils.sort(graph, result); return result; } public static Collection findByName(ReadGraph graph, Resource model, String name) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); HashSet results = new HashSet(); String search = IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name); for(Resource resource : findResources(graph, model, search)) { if(name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource); } return results; } public static Collection findByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException { HashSet results = new HashSet(); Layer0 L0 = Layer0.getInstance(graph); String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING); String search = IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName); for(Resource resource : findResources(graph, model, search)) { if(graph.isInstanceOf(resource, type)) results.add(resource); } return results; } public static Collection findByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); HashSet results = new HashSet(); String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING); String search = IndexQueries.and(IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName), IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name)); for(Resource resource : findResources(graph, model, search)) { if(graph.isInstanceOf(resource, type)) results.add(resource); } return results; } public static void flushIndexCaches(IProgressMonitor progress, Session session) throws Exception { MemoryIndexing mem = MemoryIndexing.getInstance(session); mem.flush(progress); } public static List list(IProgressMonitor progress, Session session, Resource indexRoot) throws Exception { if(progress == null) progress = new NullProgressMonitor(); MemoryIndexing mem = MemoryIndexing.getInstance(session); Layer0X L0X = Layer0X.getInstance(session); mem.flush(progress); IndexedRelationsSearcher searcher = mem.get(session, L0X.DependenciesRelation, indexRoot); 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 ); return new Term(key, ref); } }