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