-// public <T> void performForEach(ReadGraphImpl parentGraph, final AsyncRead<T> query, final AsyncReadEntry<T> entry, final CacheEntry parent, final ListenerBase base, final AsyncProcedure<T> procedure,
-// boolean inferredDependency) {
-//
-// if (DebugPolicy.PERFORM)
-// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-//
-// assert (!dirty);
-// assert (!collecting);
-//
-// assert(!entry.isDiscarded());
-//
-// final ListenerEntry listenerEntry = registerDependencies(parentGraph, entry, parent, base, procedure, inferredDependency);
-//
-// // FRESH, REFUTED, EXCEPTED go here
-// if (!entry.isReady()) {
-//
-// entry.setPending();
-//
-// size++;
-//
-// try {
-//
-// final ReadGraphImpl finalParentGraph = parentGraph;
-//
-// query.perform(parentGraph.withParent(entry), new AsyncProcedure<T>() {
-//
-// @Override
-// public void execute(AsyncReadGraph returnGraph, T result) {
-// ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
-// //AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
-// entry.addOrSet(finalParentGraph, result);
-// if(listenerEntry != null) {
-// primeListenerEntry(listenerEntry, result);
-// }
-// try {
-// procedure.execute(finalParentGraph, 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(finalParentGraph, t);
-// try {
-// procedure.exception(finalParentGraph, 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);
-//
-// }
-//
-// misses++;
-//
-// } else {
-//
-// entry.performFromCache(parentGraph, this, new AsyncProcedure<T>() {
-//
-// @Override
-// public void exception(AsyncReadGraph graph, Throwable throwable) {
-// procedure.exception(graph, throwable);
-// }
-//
-// @Override
-// public void execute(AsyncReadGraph graph, T result) {
-// procedure.execute(graph, result);
-// if(listenerEntry != null) {
-// primeListenerEntry(listenerEntry, result);
-// }
-// }
-//
-// });
-//
-//// parentBarrier.dec(query);
-//
-// hits++;
-//
-// }
-//
-// assert (!entry.isDiscarded());
-//
-// }
-
-// public <T> T performForEach(final ReadGraphImpl graph, final Read<T> query, final ReadEntry<T> entry, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure<T> procedure,
-// boolean inferredDependency) throws Throwable {
-//
-// if (DebugPolicy.PERFORM)
-// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-//
-// assert (!dirty);
-// assert (!collecting);
-//
-// entry.assertNotDiscarded();
-//
-// if(entry.isReady()) {
-//
-// // EXCEPTED goes here
-//
-//// if(procedure != null) entry.performFromCache(graph, this, procedure);
-//// parentBarrier.dec(query);
-// hits++;
-//
-// ListenerEntry listenerEntry = registerDependencies(graph, entry, parent, listener, procedure, inferredDependency);
-//
-// T result = (T)entry.get(graph, this, procedure);
-//
-// if(listenerEntry != null) primeListenerEntry(listenerEntry, result);
-//
-// return result;
-//
-// } else {
-//
-// // FRESH, REFUTED, PENDING go here
-//
-// entry.setPending();
-//
-// size++;
-// misses++;
-//
-// ListenerEntry listenerEntry = registerDependencies(graph, entry, parent, listener, procedure, inferredDependency);
-//
-// final ReadGraphImpl performGraph = graph.newSync(entry);
-//
-// try {
-//
-// if(Development.DEVELOPMENT)
-// Development.recordHistogram("run " + query);
-//
-// T result = query.perform(performGraph);
-// entry.addOrSet(performGraph, result);
-//
-// if(listenerEntry != null) primeListenerEntry(listenerEntry, result);
-//
-// return (T)entry.get(graph, this, procedure);
-//
-// } catch (Throwable t) {
-//
-// entry.except(t);
-// return (T)entry.get(graph, this, procedure);
-//
-// }
-//
-// }
-//
-// }
-
-// public <T> void performForEach(final ReadGraphImpl graph, final MultiRead<T> query, final MultiReadEntry<T> entry, CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure,
-// boolean inferredDependency) {
-//
-// if (DebugPolicy.PERFORM)
-// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-//
-// assert (!dirty);
-// assert (!collecting);
-//
-// assert(!entry.isPending());
-// assert(!entry.isDiscarded());
-//
-// // FRESH, REFUTED, EXCEPTED go here
-// if (!entry.isReady()) {
-//
-// entry.setPending();
-// entry.clearResult();
-//
-// multiReadMap.put(query, entry);
-// size++;
-//
-// final ReadGraphImpl newGraph = graph.newSync(entry);
-//// newGraph.state.barrier.inc();
-//
-// try {
-//
-// query.perform(newGraph, new AsyncMultiProcedure<T>() {
-//
-// @Override
-// public void execute(AsyncReadGraph graph, T result) {
-// entry.addOrSet(result);
-// try {
-// procedure.execute(graph, result);
-// } catch (Throwable t) {
-// t.printStackTrace();
-// }
-// }
-//
-// @Override
-// public void finished(AsyncReadGraph graph) {
-// entry.finish(graph);
-// try {
-// procedure.finished(graph);
-// } catch (Throwable t) {
-// t.printStackTrace();
-// }
-//// newGraph.state.barrier.dec();
-//// parentBarrier.dec();
-// }
-//
-// @Override
-// public void exception(AsyncReadGraph graph, Throwable t) {
-// entry.except(t);
-// try {
-// procedure.exception(graph, t);
-// } catch (Throwable t2) {
-// t2.printStackTrace();
-// }
-//// newGraph.state.barrier.dec();
-//// parentBarrier.dec();
-// }
-//
-// });
-//
-// } catch (DatabaseException e) {
-//
-// entry.except(e);
-// try {
-// procedure.exception(graph, e);
-// } catch (Throwable t2) {
-// t2.printStackTrace();
-// }
-//// newGraph.state.barrier.dec();
-//// parentBarrier.dec();
-//
-// } catch (Throwable t) {
-//
-// DatabaseException e = new DatabaseException(t);
-//
-// entry.except(e);
-// try {
-// procedure.exception(graph, e);
-// } catch (Throwable t2) {
-// t2.printStackTrace();
-// }
-//// newGraph.state.barrier.dec();
-//// parentBarrier.dec();
-//
-// }
-//
-// misses++;
-//
-// } else {
-//
-// entry.performFromCache(graph, this, procedure);
-// hits++;
-//
-//
-// }
-//
-// assert (!entry.isDiscarded());
-//
-// registerDependencies(graph, entry, parent, listener, procedure, inferredDependency);
-//
-// }
-//
-//
-// public <T> void performForEach(final ReadGraphImpl callerGraph, AsyncMultiRead<T> query, final AsyncMultiReadEntry<T> entry, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure,
-// boolean inferredDependency) {
-//
-// if (DebugPolicy.PERFORM)
-// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-//
-// assert (!dirty);
-// assert (!collecting);
-//
-// try {
-//
-// assert(!entry.isDiscarded());
-//
-// // FRESH, REFUTED, EXCEPTED go here
-// if (!entry.isReady()) {
-//
-// size++;
-//
-// try {
-//
-// ReadGraphImpl performGraph = callerGraph.withAsyncParent(entry);
-//
-// query.perform(performGraph, new AsyncMultiProcedure<T>() {
-//
-// @Override
-// public void execute(AsyncReadGraph graph, T result) {
-// ReadGraphImpl impl = (ReadGraphImpl)graph;
-//// ReadGraphImpl executeGraph = callerGraph.newAsync();
-// entry.addOrSet(result);
-// try {
-// procedure.execute(callerGraph, result);
-// } catch (Throwable t) {
-// t.printStackTrace();
-// }
-// }
-//
-// @Override
-// public void finished(AsyncReadGraph graph) {
-// ReadGraphImpl impl = (ReadGraphImpl)graph;
-//// ReadGraphImpl executeGraph = callerGraph.newAsync();
-// entry.finish(callerGraph);
-// try {
-// procedure.finished(callerGraph);
-// } catch (Throwable t) {
-// t.printStackTrace();
-// }
-// }
-//
-// @Override
-// public void exception(AsyncReadGraph graph, Throwable t) {
-// ReadGraphImpl impl = (ReadGraphImpl)graph;
-//// ReadGraphImpl executeGraph = callerGraph.newAsync();
-// entry.except(callerGraph, t);
-// try {
-// procedure.exception(callerGraph, t);
-// } catch (Throwable t2) {
-// t2.printStackTrace();
-// }
-// }
-//
-// });
-//
-// } catch (Throwable t) {
-//
-// entry.except(t);
-// try {
-// procedure.exception(callerGraph, t);
-// } catch (Throwable t2) {
-// t2.printStackTrace();
-// }
-//
-// }
-//
-//
-// misses++;
-//
-// } else {
-//
-// entry.performFromCache(callerGraph, this, procedure);
-//
-// hits++;
-//
-// }
-//
-// assert (!entry.isDiscarded());
-//
-// registerDependencies(callerGraph, entry, parent, listener, procedure, inferredDependency);
-//
-// } catch (Throwable t) {
-//
-// Logger.defaultLogError(t);
-//
-// } finally {
-//
-// }
-//
-// }
-//
-// public <T> void performForEach(ReadGraphImpl graph, final ExternalRead<T> query, final ExternalReadEntry<T> entry, final CacheEntry parent, final ListenerBase base, final Procedure<T> procedure,
-// boolean inferredDependency) {
-//
-// if (DebugPolicy.PERFORM)
-// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-//
-// assert (!dirty);
-// assert (!collecting);
-//
-// assert(!entry.isPending());
-// assert(!entry.isDiscarded());
-//
-// registerDependencies(graph, entry, parent, base, procedure, inferredDependency);
-//
-// // FRESH, REFUTED, EXCEPTED go here
-// if (!entry.isReady()) {
-//
-// entry.setPending();
-// entry.clearResult(querySupport);
-//
-// externalReadMap.put(query, entry);
-// size++;
-//
-// try {
-//
-// query.register(graph, new Listener<T>() {
-//
-// AtomicBoolean used = new AtomicBoolean(false);
-//
-// @Override
-// public void execute(T result) {
-//
-// // Just for safety
-// if(entry.isDiscarded()) return;
-// if(entry.isExcepted()) entry.setPending();
-//
-// if(used.compareAndSet(false, true)) {
-// entry.addOrSet(QueryProcessor.this, result);
-// procedure.execute(result);
-// } else {
-// entry.queue(result);
-// updatePrimitive(query);
-// }
-//
-// }
-//
-// @Override
-// public void exception(Throwable t) {
-//
-// entry.except(t);
-//
-// if(used.compareAndSet(false, true)) {
-// procedure.exception(t);
-// } else {
-//// entry.queue(result);
-// updatePrimitive(query);
-// }
-//
-// }
-//
-// @Override
-// public String toString() {
-// return procedure.toString();
-// }
-//
-// @Override
-// public boolean isDisposed() {
-// return entry.isDiscarded() || !isBound(entry);
-// }
-//
-// });
-//
-// } catch (Throwable t) {
-//
-// entry.except(t);
-// procedure.exception(t);
-//
-// }
-//
-// misses++;
-//
-// } else {
-//
-// entry.performFromCache(procedure);
-//
-// hits++;
-//
-// }
-//
-// assert (!entry.isDiscarded());
-//
-// }
-
- boolean isBound(ExternalReadEntry<?> entry) {
- if(entry.hasParents()) return true;
- else if(hasListener(entry)) return true;
- else return false;
- }
-
- synchronized public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
-
- if (parent != null && !inferred) {
- try {
- if(!child.isImmutable(graph))
- 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 <Procedure> void performForEach(ReadGraphImpl graph, BinaryQuery<Procedure> query, CacheEntry parent, ListenerBase listener, Procedure procedure) {
-
- if (DebugPolicy.PERFORM)
- System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-
- assert (!dirty);
- assert (!collecting);
-
- try {
-
- registerDependencies(graph, query, parent, listener, procedure, false);
-
- // FRESH, REFUTED, EXCEPTED go here
- if (!query.isReady()) {
-
- boolean fresh = query.isFresh();
-
- if(fresh) {
- size++;
- }
-
- query.computeForEach(graph, this, procedure, true);
-
- misses++;
-
- } else {
-
- query.performFromCache(graph, this, procedure);
-
- hits++;
-
- }
-
- } catch (Throwable t) {
-
- Logger.defaultLogError(t);
-
- }
- }
-
- public <Procedure> Object performForEach(ReadGraphImpl graph, UnaryQuery<Procedure> query, CacheEntry parent, ListenerBase listener, Procedure procedure) {
-
- if (DebugPolicy.PERFORM)
- System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-
- assert (!dirty);
- assert (!collecting);
-
- try {
-
- assert(query.assertNotDiscarded());
-
- registerDependencies(graph, query, parent, listener, procedure, false);
-
- // FRESH, REFUTED, EXCEPTED go here
- if (!query.isReady()) {
-
- size++;
- misses++;
-
- return query.computeForEach(graph, this, procedure, true);
-
-
- } else {
-
- hits++;
-
- return query.performFromCache(graph, this, procedure);
-
- }
-
- } catch (Throwable t) {
-
- Logger.defaultLogError(t);
- return null;
-
- }
-
- }
-
- static class Dummy implements InternalProcedure<Object>, IntProcedure {
-
- @Override
- public void execute(ReadGraphImpl graph, int i) {
- }
-
- @Override
- public void finished(ReadGraphImpl graph) {
- }
-
- @Override
- public void execute(ReadGraphImpl graph, Object result) {
- }
-
- @Override
- public void exception(ReadGraphImpl graph, Throwable throwable) {
- }
-
- }
-
- private static final Dummy dummy = new Dummy();
-
- public <Procedure> Object performForEach2(ReadGraphImpl graph, UnaryQuery<Procedure> query, CacheEntry parent, ListenerBase listener, Procedure procedure) throws Throwable {
-
- if (DebugPolicy.PERFORM)
- System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-
- assert (!dirty);
- assert (!collecting);
-
- assert(query.assertNotDiscarded());
-
- registerDependencies(graph, query, parent, listener, procedure, false);
-
- // FRESH, REFUTED, EXCEPTED go here
- if (!query.isReady()) {
-
- size++;
- misses++;
-
- query.computeForEach(graph, this, (Procedure)dummy, true);
- return query.get(graph, this, null);
-
- } else {