]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java
5262d99a2d85690715f4264a9a01cfaf664bcf95
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / IndexUtils.java
1 package org.simantics.db.indexing;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8
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;
26
27 public class IndexUtils {
28
29     public static Collection<Map<String, Object>> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {
30         
31         Collection<Map<String, Object>> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.<Collection<Map<String, Object>>>instance());
32
33         Layer0 L0 = Layer0.getInstance(graph);
34         Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
35         if (linkedRoots.isEmpty())
36             return indexResult;
37
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())
42                 continue;
43             if (result == indexResult) {
44                 result = new ArrayList<Map<String, Object>>(indexResult.size() + linkedIndexResults.size());
45                 result.addAll(indexResult);
46             } else {
47             }
48             result.addAll(linkedIndexResults);
49         }
50         
51         return result;
52         
53     }
54
55     public static List<Resource> findResources(ReadGraph graph, Resource index, String filter) throws DatabaseException {
56         
57         List<Resource> indexResult = graph.syncRequest(new QueryIndexResources(index, filter), TransientCacheListener.<List<Resource>>instance());
58
59         Layer0 L0 = Layer0.getInstance(graph);
60         CollectionSupport coll = graph.getService(CollectionSupport.class);
61         
62         Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
63         if (linkedRoots.isEmpty())
64             return indexResult;
65
66         List<Resource> result = indexResult;
67         for (Resource dep : linkedRoots) {
68             Collection<Resource> linkedIndexResults = findResources(graph, dep, filter);
69             if (linkedIndexResults.isEmpty())
70                 continue;
71             if (result == indexResult) {
72                 result = coll.createList();
73                 result.addAll(indexResult);
74             }
75             result.addAll(linkedIndexResults);
76         }
77         
78         Layer0Utils.sort(graph, result);
79         return result;
80         
81     }
82     
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>();
86         
87                 String search = "Name:" + name;
88
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);
92         }
93         return results;
94     }
95
96     public static Collection<Resource> findByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
97         
98         HashSet<Resource> results = new HashSet<Resource>();
99         
100                 String search = "Types:*" + NameUtils.getSafeName(graph, type);
101
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);
105         }
106         return results;
107     }
108
109     public static Collection<Resource> findByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
110         
111         Layer0 L0 = Layer0.getInstance(graph);
112         
113         HashSet<Resource> results = new HashSet<Resource>();
114         
115                 String search = "Types:*" + type + " AND Name:" + name;
116
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);
120         }
121         return results;
122     }
123     
124     public static void flushIndexCaches(IProgressMonitor progress, Session session) throws Exception {
125
126         MemoryIndexing mem = MemoryIndexing.getInstance(session);
127         mem.flush(progress);
128         
129     }
130     
131     public static List<Object> list(IProgressMonitor progress, Session session, Resource indexRoot) throws Exception {
132         
133         if(progress == null) progress = new NullProgressMonitor();
134
135         MemoryIndexing mem = MemoryIndexing.getInstance(session);
136         Layer0X L0X = Layer0X.getInstance(session);
137
138         mem.flush(progress);
139                 
140         IndexedRelationsSearcher searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);
141         return searcher.doList(progress, session);
142         
143     }
144     
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);
149     }
150
151 }