import java.util.ArrayList;
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;
-import org.simantics.db.procedure.ListenerBase;
import gnu.trove.procedure.TIntProcedure;
import gnu.trove.set.hash.TIntHashSet;
-final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
+final public class PrincipalTypes extends CollectionUnaryQuery {
PrincipalTypes(final int resource) {
super(resource);
}
- //@Override
- public Object compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
- return computeForEach(graph, id, this, proc);
+ @Override
+ public void compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
+ computeForEach(graph, id, this, proc);
}
- public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException {
+ public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure procedure_) throws DatabaseException {
+
+ IntProcedure procedure = entry != null ? entry : procedure_;
+
+ Object result = computeForEach2(graph, id, entry, procedure);
+
+ if(entry != null) entry.performFromCache(graph, procedure_);
+
+ return result;
+
+ }
+
+ public static Object computeForEach2(final ReadGraphImpl graph, final int id, final PrincipalTypes parent, final IntProcedure procedure) throws DatabaseException {
- QueryProcessor provider = graph.processor;
+ QueryProcessor provider = graph.processor;
provider.querySupport.ensureLoaded(graph, id);
assert(id != 0);
int ret = provider.querySupport.getSingleInstance(id);
if(ret > 0) {
- if(entry != null) {
- entry.add(ret);
- entry.finish(graph, provider);
- }
- proc.execute(graph, ret);
- proc.finished(graph);
+ procedure.execute(graph, ret);
+ procedure.finished(graph);
return ret;
}
@Override
public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
- proc.exception(graph, t);
+ procedure.exception(graph, t);
}
@Override
@Override
public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
- proc.exception(graph, t);
+ procedure.exception(graph, t);
}
@Override
if(indirect.size() == 0) {
int size = material.size();
if(size == 0) {
- if(entry != null) entry.finish(graph, provider);
- proc.finished(graph);
+ procedure.finished(graph);
return null;
} else if(size == 1) {
int single = material.single;
- if(entry != null) {
- entry.add(single);
- entry.finish(graph, provider);
- }
- proc.execute(graph, single);
- proc.finished(graph);
+ procedure.execute(graph, single);
+ procedure.finished(graph);
return single;
} else {
- addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
+ addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
return null;
}
}
-// final AtomicInteger finishes = new AtomicInteger(0);
-
indirect.forEach(new TIntProcedure() {
int finishes = 0;
if(current == indirect.size()) {
int size = material.size();
if(size == 0) {
- if(entry != null) entry.finish(graph, provider);
- proc.finished(graph);
+ procedure.finished(graph);
return true;
} else if(size == 1) {
int single = material.single;
- if(entry != null) {
- entry.add(single);
- entry.finish(graph, provider);
- }
- proc.execute(graph, single);
- proc.finished(graph);
+ procedure.execute(graph, single);
+ procedure.finished(graph);
return true;
} else {
- addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
+ addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
return true;
}
}
return true;
}
- QueryCache.runnerPrincipalTypes(graph, arg0, entry, null, new IntProcedure() {
+ QueryCache.runnerPrincipalTypes(graph, arg0, parent, null, new IntProcedure() {
@Override
public void execute(ReadGraphImpl graph, int i) {
if(current == indirect.size()) {
int size = material.size();
if(size == 0) {
- if(entry != null) entry.finish(graph, provider);
- proc.finished(graph);
+ procedure.finished(graph);
return;
} else if(size == 1) {
int single = material.single;
- if(entry != null) {
- entry.add(single);
- entry.finish(graph, provider);
- }
- proc.execute(graph, single);
- proc.finished(graph);
+ procedure.execute(graph, single);
+ procedure.finished(graph);
return;
} else {
- addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
+ addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
return;
}
}
@Override
public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
- proc.exception(graph, t);
+ procedure.exception(graph, t);
}
});
}
- 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) {
- if(!rejects.contains(i)) {
- entry.add(i);
- }
- }
- entry.finish(graph, provider);
- }
+ private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final IntProcedure proc) throws DatabaseException {
for(int i : material) {
if(!rejects.contains(i)) {
}
- 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);
+ private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes parent, final IntProcedure proc) throws DatabaseException {
if(index == material.length) {
- finish(graph, rejects, material, entry, provider, proc);
+ finish(graph, rejects, material, proc);
return;
}
int type = material[index++];
while(rejects.contains(type)) {
if(index == material.length) {
- finish(graph, rejects, material, entry, provider, proc);
+ finish(graph, rejects, material, proc);
return;
}
type = material[index++];
}
+
final int nextIndex = index;
- QueryCache.runnerSuperTypes(graph, type, entry, null, new InternalProcedure<IntSet>() {
+ QueryCache.runnerSuperTypes(graph, type, parent, null, new InternalProcedure<IntSet>() {
@Override
public void execute(ReadGraphImpl graph, IntSet supers) throws DatabaseException {
}
- addPrincipalType(graph, rejects, material, nextIndex, provider, entry, proc);
+ addPrincipalType(graph, rejects, material, nextIndex, provider, parent, proc);
}
public String toString() {
return "PrincipalTypes[" + id + "]";
}
-
- final private void add(int val) {
- assert(isPending());
- IntArray v = (IntArray)getResult();
- v.add(val);
- }
-
- final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
-
- assert(isPending());
- synchronized(this) {
- setReady();
- }
-
- }
-
- @Override
- public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
-
- assert(isReady());
-
- if(handleException(graph, procedure)) return EXCEPTED;
-
- final IntArray value = (IntArray)getResult();
- if(value.data == null) {
- if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
- } else {
- for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
- }
-
- procedure.finished(graph);
-
- return getResult();
-
- }
-
- @Override
- public void recompute(ReadGraphImpl graph) throws DatabaseException {
-
- compute(graph, new IntProcedureAdapter() {
-
- @Override
- public void finished(ReadGraphImpl graph) {
- }
-
- @Override
- public void exception(ReadGraphImpl graph, Throwable t) {
- new Error("Error in recompute.", t).printStackTrace();
- }
-
- });
-
- }
-
-
- @Override
- boolean isImmutable(ReadGraphImpl graph) {
- return graph.processor.isImmutable(id);
- }
@Override
protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {