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;fp=bundles%2Forg.simantics.db.indexing%2Fsrc%2Forg%2Fsimantics%2Fdb%2Findexing%2FIndexUtils.java;h=5262d99a2d85690715f4264a9a01cfaf664bcf95;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=e089deb029897f7c2a63a456c34ccf80aa4cd70a;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;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 e089deb02..5262d99a2 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 @@ -1,151 +1,151 @@ -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.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.common.utils.NameUtils; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.util.Layer0Utils; -import org.simantics.db.service.CollectionSupport; -import org.simantics.layer0.Layer0; -import org.simantics.operation.Layer0X; - -public class IndexUtils { - - 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 = "Name:" + name; - - for(Map entry : find(graph, model, search)) { - Resource resource = (Resource)entry.get("Resource"); - 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(); - - String search = "Types:*" + NameUtils.getSafeName(graph, type); - - for(Map entry : find(graph, model, search)) { - Resource resource = (Resource)entry.get("Resource"); - 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 search = "Types:*" + type + " AND Name:" + name; - - for(Map entry : find(graph, model, search)) { - Resource resource = (Resource)entry.get("Resource"); - if(graph.isInstanceOf(resource, type) && name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) 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); - return searcher.doList(progress, session); - - } - - public static Term longTerm(String key, Long value) { - BytesRef ref = new BytesRef(); - NumericUtils.longToPrefixCoded( value, 0, ref ); - return new Term(key, ref); - } - -} +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.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.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.Layer0Utils; +import org.simantics.db.service.CollectionSupport; +import org.simantics.layer0.Layer0; +import org.simantics.operation.Layer0X; + +public class IndexUtils { + + 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 = "Name:" + name; + + for(Map entry : find(graph, model, search)) { + Resource resource = (Resource)entry.get("Resource"); + 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(); + + String search = "Types:*" + NameUtils.getSafeName(graph, type); + + for(Map entry : find(graph, model, search)) { + Resource resource = (Resource)entry.get("Resource"); + 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 search = "Types:*" + type + " AND Name:" + name; + + for(Map entry : find(graph, model, search)) { + Resource resource = (Resource)entry.get("Resource"); + if(graph.isInstanceOf(resource, type) && name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) 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); + return searcher.doList(progress, session); + + } + + public static Term longTerm(String key, Long value) { + BytesRef ref = new BytesRef(); + NumericUtils.longToPrefixCoded( value, 0, ref ); + return new Term(key, ref); + } + +}