import java.util.Arrays;
import java.util.concurrent.Semaphore;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
import org.simantics.db.impl.graph.ReadGraphImpl;
import org.simantics.db.impl.procedure.IntProcedureAdapter;
import org.simantics.db.impl.procedure.InternalProcedure;
final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
- private PrincipalTypes(final int resource) {
+ PrincipalTypes(final int resource) {
super(resource);
}
- final static PrincipalTypes entry(final QueryProcessor provider, final int r) {
- return (PrincipalTypes)provider.cache.principalTypesMap.get(r);
- }
-
- final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-
- QueryProcessor processor = graph.processor;
-
- PrincipalTypes entry = (PrincipalTypes)processor.cache.principalTypesMap.get(r);
- if(entry == null) {
-
- entry = new PrincipalTypes(r);
- entry.setPending();
- entry.clearResult(processor.querySupport);
- entry.putEntry(processor);
-
- processor.performForEach(graph, entry, parent, listener, procedure);
-
- } else {
-
- if(entry.isPending()) {
- synchronized(entry) {
- if(entry.isPending()) {
- throw new IllegalStateException();
-// if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>(1);
-// entry.procs.add(procedure);
-// processor.registerDependencies(graph, entry, parent, listener, procedure, false);
-// return;
- }
- }
- }
- processor.performForEach(graph, entry, parent, listener, procedure);
- }
-
- }
-
- final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-
- assert(r != 0);
-
- if(parent == null && listener == null) {
- PrincipalTypes.computeForEach(graph, r, null, graph.processor, procedure);
- } else {
- runner(graph, r, parent, listener, procedure);
- }
-
- }
-
- @Override
- public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
- return provider.cache.principalTypesMap.get(id);
- }
-
- @Override
- public void putEntry(QueryProcessor provider) {
- provider.cache.principalTypesMap.put(id, this);
- }
-
@Override
final public void removeEntry(QueryProcessor provider) {
- provider.cache.principalTypesMap.remove(id);
+ provider.cache.remove(this);
}
static class Koss {
}
@Override
- public Object computeForEach(final ReadGraphImpl procedureGraph, final QueryProcessor provider, final IntProcedure proc, final boolean store) {
-
- return computeForEach(procedureGraph, id, this, provider, proc);
-
+ public Object compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
+ return computeForEach(graph, id, this, proc);
}
- public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) {
+ public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException {
+
+ QueryProcessor provider = graph.processor;
provider.querySupport.ensureLoaded(graph, id);
assert(id != 0);
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
proc.exception(graph, t);
}
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
proc.exception(graph, t);
}
@Override
public boolean execute(final int arg0) {
+ try {
+ return execute0(arg0);
+ } catch (DatabaseException e) {
+ Logger.defaultLogError(e);
+ }
+ return false;
+ }
+
+ public boolean execute0(final int arg0) throws DatabaseException {
// No self-loops!
if(arg0 == id) {
return true;
}
- PrincipalTypes.queryEach(graph, arg0, provider, entry, null, new IntProcedure() {
+ QueryCache.runnerPrincipalTypes(graph, arg0, entry, null, new IntProcedure() {
@Override
public void execute(ReadGraphImpl graph, int i) {
}
@Override
- public void finished(ReadGraphImpl graph) {
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
int current = (++finishes);
if(current == indirect.size()) {
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
proc.exception(graph, t);
}
}
- private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) {
+ private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) throws DatabaseException {
if(entry != null) {
for(int i : material) {
}
- private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes entry, final IntProcedure proc) {
+ private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException {
// if((counter++ % 500) == 0) System.out.println("PT " + counter + " mat = " + material.length);
}
final int nextIndex = index;
- SuperTypes.queryEach(graph, type, provider, entry, null, new InternalProcedure<IntSet>() {
+ QueryCache.runnerSuperTypes(graph, type, entry, null, new InternalProcedure<IntSet>() {
@Override
- public void execute(ReadGraphImpl graph, IntSet supers) {
+ public void execute(ReadGraphImpl graph, IntSet supers) throws DatabaseException {
synchronized(rejects) {
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
proc.exception(graph, t);
}
final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
assert(isPending());
-
-// ArrayList<IntProcedure> p = null;
-
synchronized(this) {
-
setReady();
-// p = procs;
-// procs = null;
-
}
-// if(p != null) {
-//
-// IntArray v = (IntArray)getResult();
-// if(v != null) {
-// if(v.data == null) {
-// if(v.sizeOrData != IntArray.NO_DATA) {
-// for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);
-// }
-// } else {
-// for(IntProcedure proc : p) {
-// for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);
-// }
-// }
-// }
-//
-// for(IntProcedure proc : p) proc.finished(graph);
-//
-// }
-
-
}
@Override
- public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
+ public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
assert(isReady());
}
@Override
- public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-
- final Semaphore s = new Semaphore(0);
+ public void recompute(ReadGraphImpl graph) throws DatabaseException {
- computeForEach(graph, provider, new IntProcedureAdapter() {
+ compute(graph, new IntProcedureAdapter() {
@Override
public void finished(ReadGraphImpl graph) {
- s.release();
}
@Override
public void exception(ReadGraphImpl graph, Throwable t) {
- s.release();
new Error("Error in recompute.", t).printStackTrace();
}
- }, true);
-
- while(!s.tryAcquire()) {
- provider.resume(graph);
- }
+ });
}
}
@Override
- protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry> result) {
-// for(Objects o : Objects.entries(processor, id)) result.add(o);
+ protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {
}
}