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.eclipse.core.runtime.SubMonitor;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.Session;
19 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
20 import org.simantics.db.common.request.ObjectsWithType;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.indexing.exception.IndexCorruptedException;
23 import org.simantics.db.layer0.genericrelation.Dependencies;
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;
33 public class IndexUtils {
35 private static final Logger LOGGER = LoggerFactory.getLogger(IndexUtils.class);
37 public static Collection<Map<String, Object>> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {
39 Collection<Map<String, Object>> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.<Collection<Map<String, Object>>>instance());
41 Layer0 L0 = Layer0.getInstance(graph);
42 Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
43 if (linkedRoots.isEmpty())
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())
51 if (result == indexResult) {
52 result = new ArrayList<Map<String, Object>>(indexResult.size() + linkedIndexResults.size());
53 result.addAll(indexResult);
56 result.addAll(linkedIndexResults);
63 public static List<Resource> findResources(ReadGraph graph, Resource index, String filter) throws DatabaseException {
65 List<Resource> indexResult = graph.syncRequest(new QueryIndexResources(index, filter), TransientCacheListener.<List<Resource>>instance());
67 Layer0 L0 = Layer0.getInstance(graph);
68 CollectionSupport coll = graph.getService(CollectionSupport.class);
70 Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
71 if (linkedRoots.isEmpty())
74 List<Resource> result = indexResult;
75 for (Resource dep : linkedRoots) {
76 Collection<Resource> linkedIndexResults = findResources(graph, dep, filter);
77 if (linkedIndexResults.isEmpty())
79 if (result == indexResult) {
80 result = coll.createList();
81 result.addAll(indexResult);
83 result.addAll(linkedIndexResults);
86 Layer0Utils.sort(graph, result);
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>();
95 String search = IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name);
97 for(Resource resource : findResources(graph, model, search)) {
98 if(name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource);
103 public static Collection<Resource> findByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
105 HashSet<Resource> results = new HashSet<Resource>();
106 Layer0 L0 = Layer0.getInstance(graph);
107 String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
108 String search = IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName);
110 for(Resource resource : findResources(graph, model, search)) {
111 if(graph.isInstanceOf(resource, type)) results.add(resource);
116 public static Collection<Resource> findByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
118 Layer0 L0 = Layer0.getInstance(graph);
120 HashSet<Resource> results = new HashSet<Resource>();
121 String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
123 String search = IndexQueries.and(IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName), IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name));
125 for(Resource resource : findResources(graph, model, search)) {
126 if(graph.isInstanceOf(resource, type)) results.add(resource);
131 public static void flushIndexCaches(IProgressMonitor progress, Session session) throws Exception {
133 MemoryIndexing mem = MemoryIndexing.getInstance(session);
138 public static List<Object> list(IProgressMonitor progress, Session session, Resource indexRoot) throws Exception {
140 if(progress == null) progress = new NullProgressMonitor();
142 MemoryIndexing mem = MemoryIndexing.getInstance(session);
143 Layer0X L0X = Layer0X.getInstance(session);
147 IndexedRelationsSearcher searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);
148 List<Object> results;
150 results = searcher.doList(progress, session);
151 } catch (IndexCorruptedException e) {
152 LOGGER.error("Index is corrupted for indexRoot {}", indexRoot, e);
153 rebuild(session, progress);
154 // if this fails then no can do
155 searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);
156 results = searcher.doList(progress, session);
161 private static void rebuild(Session session, IProgressMonitor monitor) throws Exception {
162 LOGGER.error("Trying to rebuild index");
163 DatabaseIndexing.deleteAllIndexes();
164 session.getService(IndexedRelations.class).fullRebuild(SubMonitor.convert(monitor, 100), session);
167 public static Term longTerm(String key, Long value) {
168 BytesRef ref = new BytesRef();
169 NumericUtils.longToPrefixCoded( value, 0, ref );
170 return new Term(key, ref);