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.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
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>();
100 String search = "Types:*" + NameUtils.getSafeName(graph, type);
102 for(Map<String, Object> entry : find(graph, model, search)) {
103 Resource resource = (Resource)entry.get("Resource");
104 if(graph.isInstanceOf(resource, type)) results.add(resource);
109 public static Collection<Resource> findByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
111 Layer0 L0 = Layer0.getInstance(graph);
113 HashSet<Resource> results = new HashSet<Resource>();
115 String search = "Types:*" + type + " AND Name:" + name;
117 for(Map<String, Object> entry : find(graph, model, search)) {
118 Resource resource = (Resource)entry.get("Resource");
119 if(graph.isInstanceOf(resource, type) && name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource);
124 public static void flushIndexCaches(IProgressMonitor progress, Session session) throws Exception {
126 MemoryIndexing mem = MemoryIndexing.getInstance(session);
131 public static List<Object> list(IProgressMonitor progress, Session session, Resource indexRoot) throws Exception {
133 if(progress == null) progress = new NullProgressMonitor();
135 MemoryIndexing mem = MemoryIndexing.getInstance(session);
136 Layer0X L0X = Layer0X.getInstance(session);
140 IndexedRelationsSearcher searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);
141 return searcher.doList(progress, session);
145 public static Term longTerm(String key, Long value) {
146 BytesRef ref = new BytesRef();
147 NumericUtils.longToPrefixCoded( value, 0, ref );
148 return new Term(key, ref);