]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CodeGen.java
Attempt to fix regressions in new code base
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / CodeGen.java
index 89892ed56950c8f73846f0f55dba1d7a579210f6..2f14b98a54cf6725adc148cc49d623973711bb20 100644 (file)
@@ -35,47 +35,32 @@ 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, "            }");
-                       line(content, "        });");
-                       line(content, "        return;");
+                       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, "");
@@ -95,11 +80,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 +101,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, "");