X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FQueryCacheBase.java;h=24a2dbe960be9ec03877a241ca0580f79ef4b3ab;hb=68ce0966a57f5153b133c6283fdbae10f683b745;hp=f75cd9dcd80b790727383a5d078f84f9c374c994;hpb=ded784594eb0e1fb318fbb931135288152691cf2;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java index f75cd9dcd..24a2dbe96 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java @@ -2,6 +2,7 @@ package org.simantics.db.impl.query; import java.util.ArrayList; import java.util.Collection; +import java.util.concurrent.Semaphore; import java.util.concurrent.atomic.AtomicBoolean; import org.simantics.db.AsyncReadGraph; @@ -16,6 +17,7 @@ import org.simantics.db.procedure.AsyncMultiProcedure; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.db.procedure.Listener; import org.simantics.db.procedure.ListenerBase; +import org.simantics.db.procedure.Procedure; import org.simantics.db.request.AsyncMultiRead; import org.simantics.db.request.AsyncRead; import org.simantics.db.request.ExternalRead; @@ -200,9 +202,9 @@ public class QueryCacheBase { // Just for safety if(entry.isDiscarded()) return; - if(entry.isExcepted()) entry.setPending(); if(used.compareAndSet(false, true)) { + //entry.setPending(); entry.addOrSet(parentGraph.processor, result); procedure.execute(parentGraph, result); } else { @@ -639,7 +641,7 @@ public class QueryCacheBase { try { Thread.sleep(1); counter++; - if(counter > 1000) { + if(counter > 5000) { CacheEntryBase base = ((CacheEntryBase)entry); // if(base.created != null) { // System.err.println("created:"); @@ -917,6 +919,7 @@ public class QueryCacheBase { private AsyncProcedure procedure; private T result = null; private Throwable throwable = null; + private Semaphore s = new Semaphore(0); AsyncProcedureWrapper(AsyncProcedure procedure) { this.procedure = procedure; @@ -926,15 +929,22 @@ public class QueryCacheBase { public void execute(AsyncReadGraph graph, T result) { if(procedure != null) procedure.execute(graph, result); this.result = result; + s.release(); } @Override public void exception(AsyncReadGraph graph, Throwable throwable) { if(procedure != null) procedure.exception(graph, throwable); this.throwable = throwable; + s.release(); } public T get() throws DatabaseException { + try { + s.acquire(); + } catch (InterruptedException e) { + e.printStackTrace(); + } if(throwable != null) { if(throwable instanceof DatabaseException) throw (DatabaseException)throwable; else throw new DatabaseException(throwable); @@ -945,6 +955,39 @@ public class QueryCacheBase { } + static class ExternalProcedureWrapper implements AsyncProcedure { + + private Procedure procedure; + private T result = null; + private Throwable throwable = null; + + ExternalProcedureWrapper(Procedure procedure) { + this.procedure = procedure; + } + + @Override + public void execute(AsyncReadGraph graph, T result) { + if(procedure != null) procedure.execute(result); + this.result = result; + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + if(procedure != null) procedure.exception(throwable); + this.throwable = throwable; + } + + public T get() throws DatabaseException { + if(throwable != null) { + if(throwable instanceof DatabaseException) throw (DatabaseException)throwable; + else throw new DatabaseException(throwable); + } else { + return result; + } + } + + } + static class InternalProcedureWrapper implements InternalProcedure { @@ -1058,9 +1101,21 @@ public class QueryCacheBase { } + public static T resultExternalReadEntry(ReadGraphImpl graph, ExternalRead r, CacheEntry parent, ListenerBase listener, Procedure procedure) throws DatabaseException { + ExternalProcedureWrapper wrap = new ExternalProcedureWrapper<>(procedure); + QueryCache.runnerExternalReadEntry(graph, r, parent, listener, wrap); + return wrap.get(); + } + public static T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure procedure) throws DatabaseException { AsyncProcedureWrapper wrap = new AsyncProcedureWrapper<>(procedure); - QueryCache.runnerReadEntry(graph, r, parent, listener, wrap); + QueryCache.runnerReadEntry(graph, r, parent, listener, wrap, true); + return wrap.get(); + } + + public static T resultAsyncReadEntry(ReadGraphImpl graph, AsyncRead r, CacheEntry parent, ListenerBase listener, AsyncProcedure procedure) throws DatabaseException { + AsyncProcedureWrapper wrap = new AsyncProcedureWrapper<>(procedure); + QueryCache.runnerAsyncReadEntry(graph, r, parent, listener, wrap, true); return wrap.get(); }