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