]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java
Added new field TypeId to dependency index for exact type searching
[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.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;
27
28 public class IndexUtils {
29
30     public static Collection<Map<String, Object>> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {
31         
32         Collection<Map<String, Object>> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.<Collection<Map<String, Object>>>instance());
33
34         Layer0 L0 = Layer0.getInstance(graph);
35         Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
36         if (linkedRoots.isEmpty())
37             return indexResult;
38
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())
43                 continue;
44             if (result == indexResult) {
45                 result = new ArrayList<Map<String, Object>>(indexResult.size() + linkedIndexResults.size());
46                 result.addAll(indexResult);
47             } else {
48             }
49             result.addAll(linkedIndexResults);
50         }
51         
52         return result;
53         
54     }
55
56     public static List<Resource> findResources(ReadGraph graph, Resource index, String filter) throws DatabaseException {
57         
58         List<Resource> indexResult = graph.syncRequest(new QueryIndexResources(index, filter), TransientCacheListener.<List<Resource>>instance());
59
60         Layer0 L0 = Layer0.getInstance(graph);
61         CollectionSupport coll = graph.getService(CollectionSupport.class);
62         
63         Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
64         if (linkedRoots.isEmpty())
65             return indexResult;
66
67         List<Resource> result = indexResult;
68         for (Resource dep : linkedRoots) {
69             Collection<Resource> linkedIndexResults = findResources(graph, dep, filter);
70             if (linkedIndexResults.isEmpty())
71                 continue;
72             if (result == indexResult) {
73                 result = coll.createList();
74                 result.addAll(indexResult);
75             }
76             result.addAll(linkedIndexResults);
77         }
78         
79         Layer0Utils.sort(graph, result);
80         return result;
81         
82     }
83     
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>();
87
88         String search = IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name);
89
90         for(Resource resource : findResources(graph, model, search)) {
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         HashSet<Resource> results = new HashSet<>();
98         String search = IndexQueries.resourceIdTerm(Dependencies.FIELD_TYPE_RESOURCE, type);
99
100         for(Resource resource : findResources(graph, model, search)) {
101             if(graph.isInstanceOf(resource, type)) results.add(resource);
102         }
103         return results;
104     }
105
106     public static Collection<Resource> findByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
107         HashSet<Resource> results = new HashSet<Resource>();
108         String search = IndexQueries.and(IndexQueries.resourceIdTerm(Dependencies.FIELD_TYPE_RESOURCE, type), IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name));
109
110         for(Resource resource : findResources(graph, model, search)) {
111             if(graph.isInstanceOf(resource, type)) results.add(resource);
112         }
113         return results;
114     }
115     
116     public static void flushIndexCaches(IProgressMonitor progress, Session session) throws Exception {
117
118         MemoryIndexing mem = MemoryIndexing.getInstance(session);
119         mem.flush(progress);
120         
121     }
122     
123     public static List<Object> list(IProgressMonitor progress, Session session, Resource indexRoot) throws Exception {
124         
125         if(progress == null) progress = new NullProgressMonitor();
126
127         MemoryIndexing mem = MemoryIndexing.getInstance(session);
128         Layer0X L0X = Layer0X.getInstance(session);
129
130         mem.flush(progress);
131                 
132         IndexedRelationsSearcher searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);
133         return searcher.doList(progress, session);
134         
135     }
136     
137     public static Term longTerm(String key, Long value) {
138         BytesRef ref = new BytesRef();    
139         NumericUtils.longToPrefixCoded( value, 0, ref );
140         return new Term(key, ref);
141     }
142
143 }