]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / URIToResource.java
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
new file mode 100644 (file)
index 0000000..251939c
--- /dev/null
@@ -0,0 +1,269 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.impl.query;\r
+\r
+import gnu.trove.map.hash.TObjectIntHashMap;\r
+\r
+import java.util.concurrent.Semaphore;\r
+\r
+import org.simantics.databoard.util.URIStringUtils;\r
+import org.simantics.db.common.exception.DebugException;\r
+import org.simantics.db.impl.graph.ReadGraphImpl;\r
+import org.simantics.db.impl.procedure.InternalProcedure;\r
+import org.simantics.db.procedure.ListenerBase;\r
+\r
+public class URIToResource extends StringQuery<InternalProcedure<Integer>> {\r
+\r
+//    public ArrayList<InternalProcedure<Integer>> procs = null;\r
+    \r
+    private URIToResource(final String id) {\r
+        super(id);\r
+    }\r
+    \r
+    final static URIToResource entry(final QueryProcessor provider, final String id) {\r
+        return (URIToResource)provider.uriToResourceMap.get(id);\r
+    }\r
+\r
+    final static void runner(ReadGraphImpl graph, final String id, CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {\r
+\r
+       QueryProcessor processor = graph.processor;\r
+       \r
+        URIToResource entry = (URIToResource)processor.uriToResourceMap.get(id); \r
+        if(entry == null) {\r
+\r
+               entry = new URIToResource(id);\r
+               entry.setPending();\r
+               entry.clearResult(processor.querySupport);\r
+               entry.putEntry(processor);\r
+\r
+               processor.performForEach(graph, entry, parent, listener, procedure);\r
+            \r
+        } else {\r
+               \r
+            if(entry.isPending()) {\r
+                synchronized(entry) {\r
+                    if(entry.isPending()) {\r
+                        throw new IllegalStateException();\r
+//                     if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<Integer>>();\r
+//                        entry.procs.add(procedure);\r
+//                        processor.registerDependencies(graph, entry, parent, listener, procedure, false);\r
+//                        return;\r
+                    }\r
+                }\r
+            }\r
+            \r
+            processor.performForEach(graph, entry, parent, listener, procedure);\r
+            \r
+        }\r
+        \r
+    }\r
+    \r
+    final public static void queryEach(ReadGraphImpl graph, final String id, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {\r
+        \r
+        assert(id != null);\r
+        \r
+        if(graph.parent == null && listener == null) {\r
+               URIToResource.computeForEach(graph, id, null, procedure);\r
+        } else {\r
+            runner(graph, id, parent, listener, procedure);\r
+        }\r
+         \r
+    }\r
+     \r
+    @Override\r
+    public URIToResource getEntry(QueryProcessor provider) {\r
+        return provider.uriToResourceMap.get(id);\r
+    }\r
+    \r
+    @Override\r
+    public void putEntry(QueryProcessor provider) {\r
+        provider.uriToResourceMap.put(id, this);\r
+    }\r
+\r
+    @Override\r
+    final public void removeEntry(QueryProcessor provider) {\r
+        provider.uriToResourceMap.remove(id);\r
+    }\r
+\r
+    private void lookup(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure, final String namespace, final String name) {\r
+        \r
+        NamespaceIndex.queryEach(graph, namespace, processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {\r
+\r
+            @Override\r
+            public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {\r
+\r
+                if(index != null) {\r
+                    int result = index.get(name);\r
+                    if(result != 0) {\r
+                        addOrSet(graph, processor, result);\r
+                        procedure.execute(graph, result);\r
+                        return;\r
+                    }\r
+                }\r
+                \r
+                addOrSet(graph, processor, new Integer(0));\r
+                procedure.execute(graph, new Integer(0));\r
+\r
+            }\r
+\r
+            @Override\r
+            public void exception(ReadGraphImpl graph, Throwable t) {\r
+                except(t);\r
+                procedure.exception(graph, t);\r
+            }\r
+\r
+        });\r
+\r
+    }\r
+\r
+    private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure<Integer> procedure, final String namespace, final String name) {\r
+        \r
+        NamespaceIndex.queryEach(graph, namespace, graph.processor, entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {\r
+\r
+            @Override\r
+            public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {\r
+\r
+                if(index != null) {\r
+                    int result = index.get(name);\r
+                    if(result != 0) {\r
+                        if(entry != null) entry.addOrSet(graph, graph.processor, result);\r
+                        procedure.execute(graph, result);\r
+                        return;\r
+                    }\r
+                }\r
+                \r
+                if(entry != null) entry.addOrSet(graph, graph.processor, new Integer(0));\r
+                procedure.execute(graph, new Integer(0));\r
+\r
+            }\r
+\r
+            @Override\r
+            public void exception(ReadGraphImpl graph, Throwable t) {\r
+                if(entry != null) entry.except(t);\r
+                procedure.exception(graph, t);\r
+            }\r
+\r
+        });\r
+\r
+    }\r
+    \r
+    @Override\r
+    public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure) {\r
+        \r
+//     new Exception("URIToResource " + id).printStackTrace();\r
+       \r
+        if("http://".equals(id) || "http:/".equals(id)) {\r
+            \r
+            addOrSet(graph, processor, processor.getRootLibrary());\r
+            procedure.execute(graph, processor.getRootLibrary());\r
+\r
+        } else {\r
+            \r
+            final String[] parts = URIStringUtils.splitURI(id);\r
+            if (parts != null) {\r
+                lookup(graph, processor, procedure, parts[0], parts[1]);\r
+            } else {\r
+                lookup(graph, processor, procedure, "http://", id.replaceFirst("http://", ""));\r
+            }\r
+\r
+        }\r
+        \r
+    }\r
+\r
+    final private static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) {\r
+        \r
+        if("http://".equals(id) || "http:/".equals(id)) {\r
+            \r
+               QueryProcessor processor = graph.processor;\r
+            if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary());\r
+            procedure.execute(graph, processor.getRootLibrary());\r
+\r
+        } else {\r
+            \r
+            final String[] parts = URIStringUtils.splitURI(id);\r
+            if (parts != null) {\r
+                lookup(graph, entry, procedure, parts[0], parts[1]);\r
+            } else {\r
+                lookup(graph, entry, procedure, "http://", id.replaceFirst("http://", ""));\r
+            }\r
+\r
+        }\r
+        \r
+    }\r
+    \r
+    public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {\r
+\r
+        assert(isPending());\r
+\r
+//        ArrayList<InternalProcedure<Integer>> p = null;\r
+\r
+        synchronized(this) {\r
+\r
+            setResult(result);\r
+            setReady();\r
+//            p = procs;\r
+//            procs = null;\r
+            \r
+        }\r
+\r
+//        if(p != null)\r
+//             for(InternalProcedure<Integer> proc : p) proc.execute(graph, result);\r
+        \r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+        return "URIToResource[" + id + "]";\r
+    }\r
+\r
+    @Override\r
+    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<Integer> procedure) {\r
+        \r
+        assert(isReady());\r
+        \r
+       if(handleException(graph, procedure)) return;\r
+        \r
+        if(isExcepted()) {\r
+               procedure.exception(graph, (Throwable)statusOrException);\r
+        } else {\r
+            procedure.execute(graph, (Integer)getResult());\r
+        }\r
+        \r
+    }\r
+    \r
+    @Override\r
+    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {\r
+        \r
+        final Semaphore s = new Semaphore(0);\r
+        \r
+        computeForEach(graph, provider, new InternalProcedure<Integer>() {\r
+\r
+            @Override\r
+            public void execute(ReadGraphImpl graph, Integer result) {\r
+                s.release();\r
+            }\r
+            \r
+            @Override\r
+            public void exception(ReadGraphImpl graph, Throwable t) {\r
+                if(DebugException.DEBUG) new DebugException(t).printStackTrace();\r
+                throw new Error("Error in recompute.", t);\r
+            }\r
+\r
+        });\r
+        \r
+       while(!s.tryAcquire()) {\r
+               provider.resume(graph);\r
+       }\r
+        \r
+    }\r
+       \r
+}\r