X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FQueryCacheBase.java;h=f80fb5098b847d9264768be6c833c39d38a7ac24;hp=b3d1c5f8836dcfdc82637dc01fec5bd3dfebce82;hb=a88c02c2d0f4250caf887a130b9f4314c6564722;hpb=0d9b90834ce56b292c00b1a39850ed842c3e4d42 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 b3d1c5f88..f80fb5098 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 @@ -3,21 +3,19 @@ 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.databoard.Bindings; import org.simantics.db.AsyncReadGraph; +import org.simantics.db.DevelopmentKeys; import org.simantics.db.ObjectResourceIdMap; import org.simantics.db.ReadGraph; import org.simantics.db.RelationInfo; import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.impl.DebugPolicy; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; -import org.simantics.db.impl.query.QueryProcessor.SessionTask; 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.procedure.SyncMultiProcedure; @@ -26,6 +24,7 @@ import org.simantics.db.request.AsyncRead; import org.simantics.db.request.ExternalRead; import org.simantics.db.request.MultiRead; import org.simantics.db.request.Read; +import org.simantics.utils.Development; import gnu.trove.map.hash.THashMap; import gnu.trove.map.hash.TObjectIntHashMap; @@ -71,8 +70,6 @@ public class QueryCacheBase { protected final StableHashMap asyncMultiReadEntryMap; protected final StableHashMap externalReadEntryMap; - final THashMap> listeners; - public final QuerySupport querySupport; public QueryCacheBase(QuerySupport querySupport, int threads) { @@ -105,159 +102,11 @@ public class QueryCacheBase { asyncMultiReadEntryMap = new StableHashMap(); multiReadEntryMap = new StableHashMap(); externalReadEntryMap = new StableHashMap(); - listeners = new THashMap>(10, 0.75f); - } - -// public Object performQuery(ReadGraphImpl parentGraph, final AsyncRead query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException { -// -// AsyncReadEntry entry = (AsyncReadEntry)entry_; -// AsyncProcedure procedure = (AsyncProcedure)procedure_; -// -// ReadGraphImpl queryGraph = parentGraph.withParent(entry_); -// -// try { -// -// query.perform(queryGraph, new AsyncProcedure() { -// -// @Override -// public void execute(AsyncReadGraph returnGraph, T result) { -// ReadGraphImpl impl = (ReadGraphImpl)returnGraph; -// entry.addOrSet(parentGraph, result); -// try { -// procedure.execute(parentGraph, result); -// } catch (Throwable t) { -// t.printStackTrace(); -// } -//// parentBarrier.dec(query); -// } -// -// @Override -// public void exception(AsyncReadGraph returnGraph, Throwable t) { -// ReadGraphImpl impl = (ReadGraphImpl)returnGraph; -//// AsyncReadGraph resumeGraph = finalParentGraph.newAsync(); -// entry.except(parentGraph, t); -// try { -// procedure.exception(parentGraph, t); -// } catch (Throwable t2) { -// t2.printStackTrace(); -// } -//// parentBarrier.dec(query); -// } -// -// @Override -// public String toString() { -// return procedure.toString(); -// } -// -// }); -// -// } catch (Throwable t) { -// -// entry.except(t); -// try { -// procedure.exception(parentGraph, t); -// } catch (Throwable t2) { -// t2.printStackTrace(); -// } -//// parentBarrier.dec(query); -// -// } -// -// return null; -// -// } - -// public Object performQuery(ReadGraphImpl parentGraph, final Read query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException { -// -// ReadGraphImpl queryGraph = parentGraph.withParent(entry_); -// -// ReadEntry entry = (ReadEntry)entry_; -// -// try { -// -// T result = (T)query.perform(queryGraph); -// entry.addOrSet(queryGraph, result); -// -// return (T)entry.get(parentGraph, procedure_); -// -// } catch (Throwable t) { -// -// entry.except(t); -// return (T)entry.get(parentGraph, procedure_); -// -// } -// -// } - - public Object performQuery(ReadGraphImpl parentGraph, final ExternalRead query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException { - - ExternalReadEntry entry = (ExternalReadEntry)entry_; - AsyncProcedure procedure = (AsyncProcedure)procedure_; - - try { - - query.register(parentGraph, new Listener() { - - AtomicBoolean used = new AtomicBoolean(false); - - @Override - public void execute(T result) { - - // Just for safety - if(entry.isDiscarded()) return; - - if(used.compareAndSet(false, true)) { - //entry.setPending(); - entry.addOrSet(parentGraph.processor, result); - procedure.execute(parentGraph, result); - } else { - entry.queue(result); - parentGraph.processor.updatePrimitive(query); - } - - } - - @Override - public void exception(Throwable t) { - - entry.except(t); - - if(used.compareAndSet(false, true)) { - procedure.exception(parentGraph, t); - } else { -// entry.queue(result); - parentGraph.processor.updatePrimitive(query); - } - - } - - @Override - public String toString() { - return procedure.toString(); - } - - @Override - public boolean isDisposed() { - return entry.isDiscarded() || !parentGraph.processor.isBound(entry); - } - - }); - - return entry.getResult(); - - } catch (Throwable t) { - - entry.except(t); - procedure.exception(parentGraph, t); - return entry.getResult(); - - } - } public Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException { - ReadGraphImpl queryGraph = parentGraph.withParent(entry_); + ReadGraphImpl queryGraph = parentGraph.withParent(entry_, null, false); AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_; AsyncMultiProcedure procedure = (AsyncMultiProcedure)procedure_; @@ -320,7 +169,7 @@ public class QueryCacheBase { public Object performQuery(ReadGraphImpl parentGraph, final MultiRead query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException { - ReadGraphImpl queryGraph = parentGraph.withParent(entry_); + ReadGraphImpl queryGraph = parentGraph.withParent(entry_, null, true); MultiReadEntry entry = (MultiReadEntry)entry_; SyncMultiProcedure procedure = (SyncMultiProcedure)procedure_; @@ -381,76 +230,6 @@ public class QueryCacheBase { } - public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) { - - if (parent != null && !inferred) { - try { - if(!child.isImmutable(graph)) { - synchronized(child) { - child.addParent(parent); - } - } - } catch (DatabaseException e) { - Logger.defaultLogError(e); - } - if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent); - } - - if (listener != null) { - return registerListener(child, listener, procedure); - } else { - return null; - } - - } - - public synchronized ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) { - - assert (entry != null); - - if (base.isDisposed()) - return null; - - return addListener(entry, base, procedure); - - } - - protected void primeListenerEntry(final ListenerEntry entry, final Object result) { - entry.setLastKnown(result); - } - - private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) { - - assert (entry != null); - assert (procedure != null); - - ArrayList list = listeners.get(entry); - if (list == null) { - list = new ArrayList(1); - listeners.put(entry, list); - } - - ListenerEntry result = new ListenerEntry(entry, base, procedure); - int currentIndex = list.indexOf(result); - // There was already a listener - if(currentIndex > -1) { - ListenerEntry current = list.get(currentIndex); - if(!current.base.isDisposed()) return null; - list.set(currentIndex, result); - } else { - list.add(result); - } - - if(DebugPolicy.LISTENER) { - new Exception().printStackTrace(); - System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure); - } - - return result; - - } - - public Collection getRootList() { ArrayList result = new ArrayList(); @@ -637,18 +416,16 @@ public class QueryCacheBase { } } - public static void waitPending(QueryProcessor processor, CacheEntry entry) throws DatabaseException { + public static void waitPending(ReadGraphImpl graph, CacheEntry entry) throws DatabaseException { int counter = 0; while(entry.isPending()) { try { - SessionTask task = null;//processor.getOwnTask(processor.thread.get()); - if(task != null) { - task.run(processor.thread.get()); - } else { + boolean performed = false;//graph.performPending(); + if(!performed) { Thread.sleep(1); counter++; - if(counter > 5000) { + if(counter > 30000) { CacheEntryBase base = ((CacheEntryBase)entry); // if(base.created != null) { // System.err.println("created:"); @@ -662,8 +439,8 @@ public class QueryCacheBase { // System.err.println("ready:"); // base.ready.printStackTrace(); // } - new Exception("Timeout waiting for request to complete: " + entry.getOriginalRequest().toString()).printStackTrace(); - throw new DatabaseException("Timeout waiting for request to complete."); + new Exception("Timeout waiting for request to complete: " + entry.getOriginalRequest()).printStackTrace(); + throw new DatabaseException("Timeout waiting for request to complete." + entry.getOriginalRequest()); //System.err.println("asd"); //base.getQuery().recompute(null, null, entry); } @@ -1128,16 +905,8 @@ public class QueryCacheBase { 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, 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(); + return (T)QueryCache.runnerAsyncReadEntry(graph, r, parent, listener, procedure, true); } public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {