import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
import org.simantics.db.exception.NoInverseException;
import org.simantics.db.exception.ResourceNotFoundException;
-import org.simantics.db.impl.DebugPolicy;
import org.simantics.db.impl.ResourceImpl;
+import org.simantics.db.impl.graph.BarrierTracing;
import org.simantics.db.impl.graph.ReadGraphImpl;
import org.simantics.db.impl.graph.ReadGraphSupport;
import org.simantics.db.impl.graph.WriteGraphImpl;
public void close() {
}
- SessionTask getOwnTask(ReadGraphImpl impl) {
+ public SessionTask getOwnTask(ReadGraphImpl impl) {
Set<ReadGraphImpl> ancestors = impl.ancestorSet();
synchronized(querySupportLock) {
int index = 0;
return null;
}
+ public SessionTask getSubTask(ReadGraphImpl impl) {
+ Set<ReadGraphImpl> onlyThis = Collections.singleton(impl);
+ synchronized(querySupportLock) {
+ int index = 0;
+ while(index < freeScheduling.size()) {
+ SessionTask task = freeScheduling.get(index);
+ if(task.hasCommonParent(onlyThis)) {
+ return freeScheduling.remove(index);
+ }
+ index++;
+ }
+ }
+ return null;
+ }
+
public boolean performPending(ReadGraphImpl graph) {
SessionTask task = getOwnTask(graph);
if(task != null) {
// }
final public void schedule(SessionTask request) {
-
+
//int performer = request.thread;
// if(DebugPolicy.SCHEDULE)
// if(performer == THREADS) {
synchronized(querySupportLock) {
-
- //new Exception().printStackTrace();
-
+
+ if(BarrierTracing.BOOKKEEPING) {
+ Exception current = new Exception();
+ Exception previous = BarrierTracing.tasks.put(request, current);
+ if(previous != null) {
+ previous.printStackTrace();
+ current.printStackTrace();
+ }
+ }
+
freeScheduling.add(request);
querySupportLock.notifyAll();
- //System.err.println("schedule free task " + request + " => " + freeScheduling.size());
-
-// for(int i=0;i<THREADS;i++) {
-// ReentrantLock queueLock = threadLocks[i];
-// queueLock.lock();
-// //queues[performer].add(request);
-// //if(ThreadState.SLEEP == threadStates[i]) sleepers.decrementAndGet();
-// threadConditions[i].signalAll();
-// queueLock.unlock();
-// }
-
}
return;
public final ReadGraphImpl graph;
private Set<ReadGraphImpl> ancestors;
+ private int counter = 0;
+ private Exception trace;
public SessionTask(ReadGraphImpl graph) {
this.graph = graph;
+ if(graph != null) graph.asyncBarrier.inc();
}
public boolean hasCommonParent(Set<ReadGraphImpl> otherAncestors) {
return !Collections.disjoint(ancestors, otherAncestors);
}
- public abstract void run(int thread);
+ public abstract void run0(int thread);
+
+ public final void run(int thread) {
+ if(counter++ > 0) {
+ if(BarrierTracing.BOOKKEEPING) {
+ trace.printStackTrace();
+ new Exception().printStackTrace();
+ }
+ throw new IllegalStateException("Multiple invocations of SessionTask!");
+ }
+ if(BarrierTracing.BOOKKEEPING) {
+ trace = new Exception();
+ }
+ run0(thread);
+ if(graph != null) graph.asyncBarrier.dec();
+ }
@Override
public String toString() {
} catch (DatabaseException e) {
Logger.defaultLogError(e);
}
- if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent);
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_DEPENDENCIES, Bindings.BOOLEAN)) {
+ System.out.println(child + " -> " + parent);
+ }
+ }
}
if (listener != null) {
list.add(result);
}
- if(DebugPolicy.LISTENER) {
- new Exception().printStackTrace();
- System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_LISTENERS, Bindings.BOOLEAN)) {
+ new Exception().printStackTrace();
+ System.err.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
+ }
}
return result;
private void scheduleListener(ListenerEntry entry) {
assert (entry != null);
- if(DebugPolicy.LISTENER) System.out.println("Scheduled " + entry.procedure);
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_LISTENERS, Bindings.BOOLEAN)) {
+ System.err.println("Scheduled " + entry.procedure);
+ }
+ }
scheduledListeners.add(entry);
}
CacheEntry entry = e.entry;
- //System.err.println("updateQuery " + entry);
-
/*
* If the dependency graph forms a DAG, some entries are inserted in the
* todo list many times. They only need to be processed once though.
if (entry.isDiscarded()) {
if (Development.DEVELOPMENT) {
if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
- System.out.print("D");
+ System.err.print("D");
for (int i = 0; i < e.indent; i++)
- System.out.print(" ");
- System.out.println(entry.getQuery());
+ System.err.print(" ");
+ System.err.println(entry.getQuery());
}
}
// System.err.println(" => DISCARDED");
if (entry.isRefuted()) {
if (Development.DEVELOPMENT) {
if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
- System.out.print("R");
+ System.err.print("R");
for (int i = 0; i < e.indent; i++)
- System.out.print(" ");
- System.out.println(entry.getQuery());
+ System.err.print(" ");
+ System.err.println(entry.getQuery());
}
}
return false;
if (entry.isExcepted()) {
if (Development.DEVELOPMENT) {
if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
- System.out.print("E");
+ System.err.print("E");
}
}
}
if (entry.isPending()) {
if (Development.DEVELOPMENT) {
if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
- System.out.print("P");
+ System.err.print("P");
}
}
}
if (Development.DEVELOPMENT) {
if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
- System.out.print("U ");
+ System.err.print("U ");
for (int i = 0; i < e.indent; i++)
- System.out.print(" ");
- System.out.print(entry.getQuery());
+ System.err.print(" ");
+ System.err.print(entry.getQuery());
}
}
if (Development.DEVELOPMENT) {
if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
if(hasListener(entry)) {
- System.out.println(" (L)");
+ System.err.println(" (L)");
} else {
- System.out.println("");
+ System.err.println("");
}
}
}
Query query = entry.getQuery();
- if(DebugPolicy.RECOMPUTE) System.out.println("R " + query);
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_RECOMPUTE, Bindings.BOOLEAN)) {
+ System.err.println("R " + query);
+ }
+ }
entry.prepareRecompute(querySupport);
Object newValue = entry.getResult();
if (ListenerEntry.NO_VALUE == oldValue) {
- if(DebugPolicy.CHANGES) {
- System.out.println("C " + query);
- System.out.println("- " + oldValue);
- System.out.println("- " + newValue);
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_CHANGES, Bindings.BOOLEAN)) {
+ System.out.println("C " + query);
+ System.out.println("- " + oldValue);
+ System.out.println("- " + newValue);
+ }
}
return newValue;
}
} else
changed = (oldValue != null);
- if(DebugPolicy.CHANGES && changed) {
- System.out.println("C " + query);
- System.out.println("- " + oldValue);
- System.out.println("- " + newValue);
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_CHANGES, Bindings.BOOLEAN)) {
+ System.err.println("C " + query);
+ System.err.println("- " + oldValue);
+ System.err.println("- " + newValue);
+ }
}
return changed ? newValue : ListenerEntry.NOT_CHANGED;
for (ListenerEntry listenerEntry : entries) {
if (pruneListener(listenerEntry)) {
- if(DebugPolicy.LISTENER) System.out.println("Pruned " + listenerEntry.procedure);
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_LISTENERS, Bindings.BOOLEAN)) {
+ new Exception().printStackTrace();
+ System.err.println("Pruned " + listenerEntry.procedure);
+ }
+ }
continue;
}
Object newValue = compareTo(graph, entry, listenerEntry.getLastKnown());
if (newValue != ListenerEntry.NOT_CHANGED) {
- if(DebugPolicy.LISTENER)
- System.out.println("Add to schedule " + listenerEntry.procedure + " with " + newValue);
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_LISTENERS, Bindings.BOOLEAN)) {
+ new Exception().printStackTrace();
+ System.err.println("Add to schedule " + listenerEntry.procedure + " with " + newValue);
+ }
+ }
schedule.add(listenerEntry);
listenerEntry.setLastKnown(entry.getResult());
}
for(ListenerEntry listenerEntry : schedule) {
final CacheEntry entry = listenerEntry.entry;
- if(DebugPolicy.LISTENER)
- System.out.println("Firing " + listenerEntry.procedure);
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_LISTENERS, Bindings.BOOLEAN)) {
+ System.err.println("Firing " + listenerEntry.procedure);
+ }
+ }
try {
- if(DebugPolicy.LISTENER)
- System.out.println("Firing " + listenerEntry.procedure + " for " + listenerEntry.entry);
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_LISTENERS, Bindings.BOOLEAN)) {
+ System.err.println("Firing " + listenerEntry.procedure + " for " + listenerEntry.entry);
+ }
+ }
entry.performFromCache(graph, listenerEntry.procedure);
} catch (Throwable t) {
t.printStackTrace();
@Override
final public void forEachPredicate(final ReadGraphImpl impl, final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
- throw new UnsupportedOperationException();
+ try {
-// assert(subject != null);
-// assert(procedure != null);
-//
-// final ListenerBase listener = getListenerBase(procedure);
-//
-// IntProcedure ip = new IntProcedure() {
-//
-// AtomicBoolean first = new AtomicBoolean(true);
-//
-// @Override
-// public void execute(ReadGraphImpl graph, int i) {
-// try {
-// if(first.get()) {
-// procedure.execute(graph, querySupport.getResource(i));
-// } else {
-// procedure.execute(impl.newRestart(graph), querySupport.getResource(i));
-// }
-// } catch (Throwable t2) {
-// Logger.defaultLogError(t2);
-// }
-// }
-//
-// @Override
-// public void finished(ReadGraphImpl graph) {
-// try {
-// if(first.compareAndSet(true, false)) {
-// procedure.finished(graph);
-//// impl.state.barrier.dec(this);
-// } else {
-// procedure.finished(impl.newRestart(graph));
-// }
-//
-// } catch (Throwable t2) {
-// Logger.defaultLogError(t2);
-// }
-// }
-//
-// @Override
-// public void exception(ReadGraphImpl graph, Throwable t) {
-// try {
-// if(first.compareAndSet(true, false)) {
-// procedure.exception(graph, t);
-// } else {
-// procedure.exception(impl.newRestart(graph), t);
-// }
-// } catch (Throwable t2) {
-// Logger.defaultLogError(t2);
-// }
-// }
-//
-// };
-//
-// int sId = querySupport.getId(subject);
-//
-// try {
-// QueryCache.runnerPredicates(impl, sId, impl.parent, listener, ip);
-// } catch (DatabaseException e) {
-// Logger.defaultLogError(e);
-// }
+ for(Resource predicate : getPredicates(impl, subject))
+ procedure.execute(impl, predicate);
+
+ procedure.finished(impl);
+
+ } catch (Throwable e) {
+ procedure.exception(impl, e);
+ }
}