]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CodeGen.java
Use query caching previous to multi-query-thread to retain performance
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / CodeGen.java
index 89892ed56950c8f73846f0f55dba1d7a579210f6..8661428cb6b94349a034a095bb6e33c56365dc40 100644 (file)
@@ -35,51 +35,51 @@ public class CodeGen {
                content.append("\n");
        }
 
-       public void generateQuery(StringBuilder content, String clazz, String[] signature, boolean runnerShortcut, boolean genAsync) {
-               generateGetOrCreate(content, clazz, signature, genAsync);
+    public void generateQuery(StringBuilder content, String clazz, String[] signature, boolean runnerShortcut, boolean genReturn) {
+               generateGetOrCreate(content, clazz, signature);
                generateRemove(content, clazz, signature);
-               generateRunner(content, clazz, signature, runnerShortcut, genAsync);
+               generateRunner(content, clazz, signature, runnerShortcut, genReturn);
        }
 
-       public void generateRunner(StringBuilder content, String clazz, String[] signature, boolean shortcut, boolean genAsync) {
+       public void generateRunner(StringBuilder content, String clazz, String[] signature, boolean shortcut, boolean genReturn) {
 
-               line(content, "public static void runner" + clazz + "(ReadGraphImpl graph, " + signature[0] + ", CacheEntry parent, ListenerBase listener, final " + signature[4] + " procedure" + (genAsync ? ", boolean isSync" : "") + ") throws DatabaseException {");
+               line(content, "public static " + (genReturn ? "Object" : "void") + " runner" + clazz + "(ReadGraphImpl graph, " + signature[0] + ", CacheEntry parent, ListenerBase listener, final " + signature[4] + " procedure) throws DatabaseException {");
                line(content, "    QueryCache cache  = graph.processor.cache;");
                if(shortcut) {
                        line(content, "    if(parent == null && listener == null && !cache.shouldCache(graph.processor, " + signature[1] + ")) {");
-                       line(content, "        " + clazz + ".computeForEach(graph, " + signature[1] + ", null, procedure);");
-                       line(content, "        return;");
-                       line(content, "    }");
-               }
-               line(content, "    " + clazz + " entry = (" + clazz + ")cache.getOrCreate" + clazz + "(graph.processor, " + signature[1] + (genAsync ? ", isSync" : "") + ");");
-               if(genAsync) {
-                       line(content, "    if(entry == null) {");
-                       line(content, "        graph.processor.schedule(new SessionTask(false) {");
-                       line(content, "            @Override");
-                       line(content, "            public void run(int thread) {");
-                       line(content, "                try {");
-                       line(content, "                    assert(!isSync);");
-                       line(content, "                    runner" + clazz + "(graph, r, parent, listener, procedure, isSync);");
-                       line(content, "                } catch (DatabaseException e) {");
-                       line(content, "                    Logger.defaultLogError(e);");
-                       line(content, "                }");
+                       line(content, "        if (SINGLE) {");
+                       line(content, "            " + clazz + " e = cache.peek" + clazz + "(" + signature[1] + ");");
+                       line(content, "            if (e != null && e.isReady()) {");
+                       line(content, "                " + (genReturn ? "return " : "") + "e.performFromCache(graph, procedure);");
+                       if(!genReturn) line(content, "                return;");
                        line(content, "            }");
-                       line(content, "        });");
-                       line(content, "        return;");
+                       line(content, "        }");
+                       line(content, "        " + (genReturn ? "return " : "") + clazz + ".computeForEach(graph, " + signature[1] + ", null, procedure);");
+                       if(!genReturn) line(content, "        return;");
                        line(content, "    }");
                }
+               line(content, "    " + clazz + " entry = (" + clazz + ")cache.getOrCreate" + clazz + "(graph, " + signature[1] + ");");
                line(content, "    " + signature[4] + " procedure_ = procedure != null ? procedure : emptyProcedure" + clazz + ";");
                line(content, "    ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);");
-               line(content, "    if(entry.isReady()) entry.performFromCache(graph, procedure_);");
+               line(content, "    if(entry.isReady()) " + (genReturn ? "return " : "") + "entry.performFromCache(graph, procedure_);");
                line(content, "    else {");
                line(content, "      assert(entry.isPending());");
-               if(shortcut) line(content, "        " + clazz + ".computeForEach(graph, " + signature[1] + ", entry, procedure_);");
-               else line(content, "        entry.compute(graph, procedure_);"); 
-               line(content, "        if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());");
+               if(shortcut) line(content, "      " + (genReturn ? "Object result = " : "") + clazz + ".computeForEach(graph, " + signature[1] + ", entry, procedure_);");
+               else line(content, "      entry.compute(graph, procedure_);"); 
+               line(content, "      if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());");
+               if(genReturn) line(content, "      return result;");
                line(content, "    }");
                line(content, "}");
                line(content, "");
 
+        String lower = Character.toLowerCase(clazz.charAt(0)) + clazz.substring(1);
+
+               line(content, "private " + clazz + " peek" + clazz + "(" + signature[0] + ") {");
+               line(content, "    synchronized(" + lower +"Map) {");
+               line(content, "        return (" + clazz + ") " + lower + "Map.get(" + signature[1] + ");");
+               line(content, "    }");
+               line(content, "}");
+               line(content, "");
        }
 
        public void generateRemove(StringBuilder content, String clazz, String[] signature) {
@@ -95,11 +95,11 @@ public class CodeGen {
 
        }
 
-       public void generateGetOrCreate(StringBuilder content, String clazz, String[] signature, boolean genAsync) {
+       public void generateGetOrCreate(StringBuilder content, String clazz, String[] signature) {
 
                String lower = Character.toLowerCase(clazz.charAt(0)) + clazz.substring(1);
 
-               line(content, "" + clazz + " getOrCreate" + clazz + "(QueryProcessor processor, " + signature[0] + (genAsync ? ", boolean isSync" : "") + ") throws DatabaseException {");
+               line(content, "" + clazz + " getOrCreate" + clazz + "(ReadGraphImpl graph, " + signature[0] + ") throws DatabaseException {");
                line(content, "    " + clazz + " existing = null;");
                line(content, "    synchronized(" + lower + "Map) {");
                line(content, "        existing = (" + clazz + ")" + lower + "Map.get(" + signature[1] + ");");
@@ -116,14 +116,7 @@ public class CodeGen {
                line(content, "            return existing;");
                line(content, "        }");
                line(content, "    }");
-               if(genAsync) {
-                       line(content, "    if(existing.isPending()) {");
-                       line(content, "      if(isSync) waitPending(processor, existing);");
-                       line(content, "      else return null;");
-                       line(content, "    }");
-               } else {
-                       line(content, "    if(existing.isPending()) waitPending(processor, existing);");
-               }
+               line(content, "    if(existing.isPending()) waitPending(graph, existing);");
                line(content, "    return existing;");
                line(content, "}");
                line(content, "");
@@ -166,6 +159,10 @@ public class CodeGen {
 
                                        content.append("public class QueryCache extends QueryCacheBase {\n");
                                        content.append("\n");
+                                       
+                                       line(content, "private static final boolean SINGLE = true;");
+                                       content.append("\n");
+                                       
                                        line(content,"public QueryCache(QuerySupport querySupport, int threads) {");
                                        line(content,"    super(querySupport, threads);");
                                        line(content,"}");