]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java
Replace instantiations of DatabaseException in indexing
[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.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.apache.lucene.index.Term;
11 import org.apache.lucene.util.BytesRef;
12 import org.apache.lucene.util.NumericUtils;
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.NullProgressMonitor;
15 import org.eclipse.core.runtime.SubMonitor;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
21 import org.simantics.db.common.request.ObjectsWithType;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.indexing.exception.IndexCorruptedException;
24 import org.simantics.db.layer0.genericrelation.IndexQueries;
25 import org.simantics.db.layer0.genericrelation.IndexedRelations;
26 import org.simantics.db.layer0.util.Layer0Utils;
27 import org.simantics.db.service.CollectionSupport;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.operation.Layer0X;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class IndexUtils {
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(IndexUtils.class);
36
37     public static Collection<Map<String, Object>> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {
38         
39         Collection<Map<String, Object>> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.<Collection<Map<String, Object>>>instance());
40
41         Layer0 L0 = Layer0.getInstance(graph);
42         Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
43         if (linkedRoots.isEmpty())
44             return indexResult;
45
46         Collection<Map<String, Object>> result = indexResult;
47         for (Resource dep : linkedRoots) {
48             Collection<Map<String, Object>> linkedIndexResults = find(graph, dep, filter);
49             if (linkedIndexResults.isEmpty())
50                 continue;
51             if (result == indexResult) {
52                 result = new ArrayList<Map<String, Object>>(indexResult.size() + linkedIndexResults.size());
53                 result.addAll(indexResult);
54             } else {
55             }
56             result.addAll(linkedIndexResults);
57         }
58         
59         return result;
60         
61     }
62
63     public static List<Resource> findResources(ReadGraph graph, Resource index, String filter) throws DatabaseException {
64         
65         List<Resource> indexResult = graph.syncRequest(new QueryIndexResources(index, filter), TransientCacheListener.<List<Resource>>instance());
66
67         Layer0 L0 = Layer0.getInstance(graph);
68         CollectionSupport coll = graph.getService(CollectionSupport.class);
69         
70         Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
71         if (linkedRoots.isEmpty())
72             return indexResult;
73
74         List<Resource> result = indexResult;
75         for (Resource dep : linkedRoots) {
76             Collection<Resource> linkedIndexResults = findResources(graph, dep, filter);
77             if (linkedIndexResults.isEmpty())
78                 continue;
79             if (result == indexResult) {
80                 result = coll.createList();
81                 result.addAll(indexResult);
82             }
83             result.addAll(linkedIndexResults);
84         }
85         
86         Layer0Utils.sort(graph, result);
87         return result;
88         
89     }
90     
91     public static Collection<Resource> findByName(ReadGraph graph, Resource model, String name) throws DatabaseException {
92         Layer0 L0 = Layer0.getInstance(graph);
93         HashSet<Resource> results = new HashSet<Resource>();
94         
95                 String search = "Name:" + name;
96
97         for(Map<String, Object> entry : find(graph, model, search)) {
98                 Resource resource = (Resource)entry.get("Resource");
99             if(name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource);
100         }
101         return results;
102     }
103
104     public static Collection<Resource> findByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
105         
106         HashSet<Resource> results = new HashSet<Resource>();
107         Layer0 L0 = Layer0.getInstance(graph);
108         String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
109                 String search = "Types:" + IndexQueries.quoteTerm(typeName);
110
111         for(Map<String, Object> entry : find(graph, model, search)) {
112                 Resource resource = (Resource)entry.get("Resource");
113                 if(graph.isInstanceOf(resource, type)) results.add(resource);
114         }
115         return results;
116     }
117
118     public static Collection<Resource> findByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
119         
120         Layer0 L0 = Layer0.getInstance(graph);
121         
122         HashSet<Resource> results = new HashSet<Resource>();
123         String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
124                 String search = "Types:" + IndexQueries.quoteTerm(typeName) + " AND Name:" + IndexQueries.quoteTerm(name);
125
126         for(Map<String, Object> entry : find(graph, model, search)) {
127                 Resource resource = (Resource)entry.get("Resource");
128                 if(graph.isInstanceOf(resource, type)) results.add(resource);
129         }
130         return results;
131     }
132     
133     public static void flushIndexCaches(IProgressMonitor progress, Session session) throws Exception {
134
135         MemoryIndexing mem = MemoryIndexing.getInstance(session);
136         mem.flush(progress);
137         
138     }
139     
140     public static List<Object> list(IProgressMonitor progress, Session session, Resource indexRoot) throws Exception {
141         
142         if(progress == null) progress = new NullProgressMonitor();
143
144         MemoryIndexing mem = MemoryIndexing.getInstance(session);
145         Layer0X L0X = Layer0X.getInstance(session);
146
147         mem.flush(progress);
148                 
149         IndexedRelationsSearcher searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);
150         List<Object> results;
151         try {
152             results = searcher.doList(progress, session);
153         } catch (IndexCorruptedException e) {
154             LOGGER.error("Index is corrupted for indexRoot {}", indexRoot, e);
155             rebuild(session, progress);
156             // if this fails then no can do
157             searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);
158             results = searcher.doList(progress, session);
159         }
160         return results;
161     }
162     
163     private static void rebuild(Session session, IProgressMonitor monitor) throws Exception {
164         LOGGER.error("Trying to rebuild index");
165         DatabaseIndexing.deleteAllIndexes();
166         session.getService(IndexedRelations.class).fullRebuild(SubMonitor.convert(monitor, 100), session);
167     }
168
169     public static Term longTerm(String key, Long value) {
170         BytesRef ref = new BytesRef();    
171         NumericUtils.longToPrefixCoded( value, 0, ref );
172         return new Term(key, ref);
173     }
174
175 }