]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java
new file mode 100644 (file)
index 0000000..e089deb
--- /dev/null
@@ -0,0 +1,151 @@
+package org.simantics.db.indexing;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.apache.lucene.index.Term;\r
+import org.apache.lucene.util.BytesRef;\r
+import org.apache.lucene.util.NumericUtils;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.NullProgressMonitor;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.db.service.CollectionSupport;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.operation.Layer0X;\r
+\r
+public class IndexUtils {\r
+\r
+    public static Collection<Map<String, Object>> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {\r
+       \r
+        Collection<Map<String, Object>> indexResult = graph.syncRequest(new QueryIndex(index, filter), TransientCacheListener.<Collection<Map<String, Object>>>instance());\r
+\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));\r
+        if (linkedRoots.isEmpty())\r
+            return indexResult;\r
+\r
+        Collection<Map<String, Object>> result = indexResult;\r
+        for (Resource dep : linkedRoots) {\r
+            Collection<Map<String, Object>> linkedIndexResults = find(graph, dep, filter);\r
+            if (linkedIndexResults.isEmpty())\r
+                continue;\r
+            if (result == indexResult) {\r
+                result = new ArrayList<Map<String, Object>>(indexResult.size() + linkedIndexResults.size());\r
+                result.addAll(indexResult);\r
+            } else {\r
+            }\r
+            result.addAll(linkedIndexResults);\r
+        }\r
+        \r
+        return result;\r
+        \r
+    }\r
+\r
+    public static List<Resource> findResources(ReadGraph graph, Resource index, String filter) throws DatabaseException {\r
+       \r
+       List<Resource> indexResult = graph.syncRequest(new QueryIndexResources(index, filter), TransientCacheListener.<List<Resource>>instance());\r
+\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        CollectionSupport coll = graph.getService(CollectionSupport.class);\r
+        \r
+        Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));\r
+        if (linkedRoots.isEmpty())\r
+            return indexResult;\r
+\r
+        List<Resource> result = indexResult;\r
+        for (Resource dep : linkedRoots) {\r
+            Collection<Resource> linkedIndexResults = findResources(graph, dep, filter);\r
+            if (linkedIndexResults.isEmpty())\r
+                continue;\r
+            if (result == indexResult) {\r
+               result = coll.createList();\r
+                result.addAll(indexResult);\r
+            }\r
+            result.addAll(linkedIndexResults);\r
+        }\r
+        \r
+        Layer0Utils.sort(graph, result);\r
+        return result;\r
+        \r
+    }\r
+    \r
+    public static Collection<Resource> findByName(ReadGraph graph, Resource model, String name) throws DatabaseException {\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        HashSet<Resource> results = new HashSet<Resource>();\r
+        \r
+               String search = "Name:" + name;\r
+\r
+        for(Map<String, Object> entry : find(graph, model, search)) {\r
+               Resource resource = (Resource)entry.get("Resource");\r
+            if(name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource);\r
+        }\r
+        return results;\r
+    }\r
+\r
+    public static Collection<Resource> findByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException {\r
+       \r
+        HashSet<Resource> results = new HashSet<Resource>();\r
+        \r
+               String search = "Types:*" + NameUtils.getSafeName(graph, type);\r
+\r
+        for(Map<String, Object> entry : find(graph, model, search)) {\r
+               Resource resource = (Resource)entry.get("Resource");\r
+               if(graph.isInstanceOf(resource, type)) results.add(resource);\r
+        }\r
+        return results;\r
+    }\r
+\r
+    public static Collection<Resource> findByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {\r
+       \r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        \r
+        HashSet<Resource> results = new HashSet<Resource>();\r
+        \r
+               String search = "Types:*" + type + " AND Name:" + name;\r
+\r
+        for(Map<String, Object> entry : find(graph, model, search)) {\r
+               Resource resource = (Resource)entry.get("Resource");\r
+               if(graph.isInstanceOf(resource, type) && name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource);\r
+        }\r
+        return results;\r
+    }\r
+    \r
+    public static void flushIndexCaches(IProgressMonitor progress, Session session) throws Exception {\r
+\r
+       MemoryIndexing mem = MemoryIndexing.getInstance(session);\r
+       mem.flush(progress);\r
+       \r
+    }\r
+    \r
+    public static List<Object> list(IProgressMonitor progress, Session session, Resource indexRoot) throws Exception {\r
+       \r
+       if(progress == null) progress = new NullProgressMonitor();\r
+\r
+       MemoryIndexing mem = MemoryIndexing.getInstance(session);\r
+       Layer0X L0X = Layer0X.getInstance(session);\r
+\r
+       mem.flush(progress);\r
+               \r
+       IndexedRelationsSearcher searcher = mem.get(session, L0X.DependenciesRelation, indexRoot);\r
+       return searcher.doList(progress, session);\r
+       \r
+    }\r
+    \r
+    public static Term longTerm(String key, Long value) {\r
+       BytesRef ref = new BytesRef();    \r
+       NumericUtils.longToPrefixCoded( value, 0, ref );\r
+       return new Term(key, ref);\r
+    }\r
+\r
+}\r