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.Dependencies;
22 import org.simantics.db.layer0.genericrelation.IndexQueries;
23 import org.simantics.db.layer0.util.Layer0Utils;
24 import org.simantics.db.service.CollectionSupport;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.operation.Layer0X;
28 public class IndexUtils {
30 public static Collection<Map<String, Object>> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {
32 Collection<Map<String, Object>> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.<Collection<Map<String, Object>>>instance());
34 Layer0 L0 = Layer0.getInstance(graph);
35 Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
36 if (linkedRoots.isEmpty())
39 Collection<Map<String, Object>> result = indexResult;
40 for (Resource dep : linkedRoots) {
41 Collection<Map<String, Object>> linkedIndexResults = find(graph, dep, filter);
42 if (linkedIndexResults.isEmpty())
44 if (result == indexResult) {
45 result = new ArrayList<Map<String, Object>>(indexResult.size() + linkedIndexResults.size());
46 result.addAll(indexResult);
49 result.addAll(linkedIndexResults);
56 public static List<Resource> findResources(ReadGraph graph, Resource index, String filter) throws DatabaseException {
58 List<Resource> indexResult = graph.syncRequest(new QueryIndexResources(index, filter), TransientCacheListener.<List<Resource>>instance());
60 Layer0 L0 = Layer0.getInstance(graph);
61 CollectionSupport coll = graph.getService(CollectionSupport.class);
63 Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
64 if (linkedRoots.isEmpty())
67 List<Resource> result = indexResult;
68 for (Resource dep : linkedRoots) {
69 Collection<Resource> linkedIndexResults = findResources(graph, dep, filter);
70 if (linkedIndexResults.isEmpty())
72 if (result == indexResult) {
73 result = coll.createList();
74 result.addAll(indexResult);
76 result.addAll(linkedIndexResults);
79 Layer0Utils.sort(graph, result);
84 public static Collection<Resource> findByName(ReadGraph graph, Resource model, String name) throws DatabaseException {
85 Layer0 L0 = Layer0.getInstance(graph);
86 HashSet<Resource> results = new HashSet<Resource>();
88 String search = IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name);
90 for(Resource resource : findResources(graph, model, search)) {
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 = IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName);
103 for(Resource resource : findResources(graph, model, search)) {
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>();
114 String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
116 String search = IndexQueries.and(IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName), IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name));
118 for(Resource resource : findResources(graph, model, search)) {
119 if(graph.isInstanceOf(resource, type)) 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);