1 package org.simantics.db.indexing;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
9 import org.apache.lucene.index.Term;
10 import org.apache.lucene.util.BytesRef;
11 import org.apache.lucene.util.NumericUtils;
12 import org.eclipse.core.runtime.IProgressMonitor;
13 import org.eclipse.core.runtime.NullProgressMonitor;
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Session;
18 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
19 import org.simantics.db.common.request.ObjectsWithType;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.genericrelation.IndexQueries;
22 import org.simantics.db.layer0.util.Layer0Utils;
23 import org.simantics.db.service.CollectionSupport;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.operation.Layer0X;
27 public class IndexUtils {
29 public static Collection<Map<String, Object>> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {
31 Collection<Map<String, Object>> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.<Collection<Map<String, Object>>>instance());
33 Layer0 L0 = Layer0.getInstance(graph);
34 Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
35 if (linkedRoots.isEmpty())
38 Collection<Map<String, Object>> result = indexResult;
39 for (Resource dep : linkedRoots) {
40 Collection<Map<String, Object>> linkedIndexResults = find(graph, dep, filter);
41 if (linkedIndexResults.isEmpty())
43 if (result == indexResult) {
44 result = new ArrayList<Map<String, Object>>(indexResult.size() + linkedIndexResults.size());
45 result.addAll(indexResult);
48 result.addAll(linkedIndexResults);
55 public static List<Resource> findResources(ReadGraph graph, Resource index, String filter) throws DatabaseException {
57 List<Resource> indexResult = graph.syncRequest(new QueryIndexResources(index, filter), TransientCacheListener.<List<Resource>>instance());
59 Layer0 L0 = Layer0.getInstance(graph);
60 CollectionSupport coll = graph.getService(CollectionSupport.class);
62 Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
63 if (linkedRoots.isEmpty())
66 List<Resource> result = indexResult;
67 for (Resource dep : linkedRoots) {
68 Collection<Resource> linkedIndexResults = findResources(graph, dep, filter);
69 if (linkedIndexResults.isEmpty())
71 if (result == indexResult) {
72 result = coll.createList();
73 result.addAll(indexResult);
75 result.addAll(linkedIndexResults);
78 Layer0Utils.sort(graph, result);
83 public static Collection<Resource> findByName(ReadGraph graph, Resource model, String name) throws DatabaseException {
84 Layer0 L0 = Layer0.getInstance(graph);
85 HashSet<Resource> results = new HashSet<Resource>();
87 String search = "Name:" + name;
89 for(Map<String, Object> entry : find(graph, model, search)) {
90 Resource resource = (Resource)entry.get("Resource");
91 if(name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource);
96 public static Collection<Resource> findByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
98 HashSet<Resource> results = new HashSet<Resource>();
99 Layer0 L0 = Layer0.getInstance(graph);
100 String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
101 String search = "Types:" + IndexQueries.quoteTerm(typeName);
103 for(Map<String, Object> entry : find(graph, model, search)) {
104 Resource resource = (Resource)entry.get("Resource");
105 if(graph.isInstanceOf(resource, type)) results.add(resource);
110 public static Collection<Resource> findByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
112 Layer0 L0 = Layer0.getInstance(graph);
114 HashSet<Resource> results = new HashSet<Resource>();
115 String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
116 String search = "Types:" + IndexQueries.quoteTerm(typeName) + " AND Name:" + IndexQueries.quoteTerm(name);
118 for(Map<String, Object> entry : find(graph, model, search)) {
119 Resource resource = (Resource)entry.get("Resource");
120 if(graph.isInstanceOf(resource, type)) results.add(resource);
125 public static void flushIndexCaches(IProgressMonitor progress, Session session) throws Exception {
127 MemoryIndexing mem = MemoryIndexing.getInstance(session);
132 public static List<Object> list(IProgressMonitor progress, Session session, Resource indexRoot) throws Exception {
134 if(progress == null) progress = new NullProgressMonitor();
136 MemoryIndexing mem = MemoryIndexing.getInstance(session);
137 Layer0X L0X = Layer0X.getInstance(session);
141 IndexedRelationsSearcher searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);
142 return searcher.doList(progress, session);
146 public static Term longTerm(String key, Long value) {
147 BytesRef ref = new BytesRef();
148 NumericUtils.longToPrefixCoded( value, 0, ref );
149 return new Term(key, ref);