*******************************************************************************/
package org.simantics.db.impl.query;
-import java.util.ArrayList;
-import java.util.concurrent.Semaphore;
-
import org.simantics.db.RelationInfo;
+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 org.simantics.db.impl.procedure.IntProcedureAdapter;
-final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure> {
-
-// public ArrayList<IntProcedure> procs = null;
+final public class AssertedPredicates extends UnaryQuery<IntProcedure> {
- public AssertedPredicates(final int r) {
+ AssertedPredicates(final int r) {
super(r);
}
public static AssertedPredicates newInstance(final int r) {
return new AssertedPredicates(r);
}
-
- final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-
- AssertedPredicates entry = (AssertedPredicates)provider.assertedPredicatesMap.get(r);
- if(entry == null) {
-
- entry = new AssertedPredicates(r);
- entry.setPending();
- entry.clearResult(provider.querySupport);
- entry.putEntry(provider);
-
- provider.performForEach(graph, entry, parent, listener, procedure);
-
- } else {
-
- if(!entry.isReady()) {
- synchronized(entry) {
- if(!entry.isReady()) {
- throw new IllegalStateException();
-// if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>();
-// entry.procs.add(procedure);
-// return;
- }
- }
- }
- provider.performForEach(graph, entry, parent, listener, procedure);
- }
+ @Override
+ final public void clearResult(QuerySupport support) {
+ setResult(new IntArray());
}
- final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-
- if(parent == null && listener == null) {
- AssertedPredicates entry = (AssertedPredicates)provider.assertedPredicatesMap.get(r);
- if(entry != null && entry.isReady()) {
- entry.performFromCache(graph, provider, procedure);
- return;
- }
- }
-
- runner(graph, r, provider, parent, listener, procedure);
-
- }
-
@Override
- public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
- return provider.assertedPredicatesMap.get(id);
+ final public void setReady() {
+ super.setReady();
+ IntArray v = (IntArray)getResult();
+ int size = v.size();
+ if(size == 0) setResult(IntArray.EMPTY);
+ else v.trim();
}
-
- @Override
- public void putEntry(QueryProcessor provider) {
- provider.assertedPredicatesMap.put(id, this);
- }
-
+
@Override
final public void removeEntry(QueryProcessor provider) {
- provider.assertedPredicatesMap.remove(id);
+ provider.cache.remove(this);
}
- void computeInheritedAssertions(ReadGraphImpl graph, int type, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) {
+ void computeInheritedAssertions(ReadGraphImpl graph, int type, final IntProcedure proc) throws DatabaseException {
+
+ QueryProcessor processor = graph.processor;
- DirectObjects.queryEach(graph, type, queryProvider.getInherits(), queryProvider, this, null, new SyncIntProcedure() {
+ QueryCache.runnerDirectObjects(graph, type, processor.getInherits(), this, null, new SyncIntProcedure() {
@Override
public void run(ReadGraphImpl graph) {
-
-// finish(graph, queryProvider);
-// proc.finished(graph);
-
}
@Override
- public void execute(ReadGraphImpl graph,int inh) {
+ public void execute(ReadGraphImpl graph,int inh) throws DatabaseException {
inc();
- AssertedPredicates.queryEach(graph, inh, queryProvider, AssertedPredicates.this, null, new IntProcedure() {
+ QueryCache.runnerAssertedPredicates(graph, inh, AssertedPredicates.this, null, new IntProcedure() {
@Override
public void execute(ReadGraphImpl graph, int ass) {
-
addOrSet(ass);
-// proc.execute(graph, ass);
-
}
@Override
- public void finished(ReadGraphImpl graph) {
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
dec(graph);
}
@Override
public void exception(ReadGraphImpl graph, Throwable t) {
-// proc.exception(graph, t);
}
});
}
@Override
- public void finished(ReadGraphImpl graph) {
-
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
dec(graph);
-
}
});
}
- @Override
- public Object computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) {
+ //@Override
+ public Object compute(ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
- computeInheritedAssertions(graph, id, queryProvider, proc, store);
+ QueryProcessor processor = graph.processor;
+
+ computeInheritedAssertions(graph, id, proc);
- DirectObjects.queryEach(graph, id, queryProvider.getAsserts(), queryProvider, this, null, new IntProcedure() {
+ QueryCache.runnerDirectObjects(graph, id, processor.getAsserts(), this, null, new IntProcedure() {
@Override
- public void execute(ReadGraphImpl graph, final int ass) {
+ public void execute(ReadGraphImpl graph, final int ass) throws DatabaseException {
- DirectObjects.queryEach(graph, ass, queryProvider.getHasPredicate(), queryProvider, AssertedPredicates.this, null, new IntProcedure() {
+ QueryCache.runnerDirectObjects(graph, ass, processor.getHasPredicate(), AssertedPredicates.this, null, new IntProcedure() {
@Override
- public void execute(ReadGraphImpl graph, final int pred) {
+ public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
addOrSetHiding(graph, pred, AssertedPredicates.this);
-// proc.execute(graph, pred);
return;
}
});
- finish(graph, queryProvider);
+ finish(graph, processor);
- performFromCache(graph, queryProvider, proc);
+ performFromCache(graph, proc);
return getResult();
assert(!isReady());
-// ArrayList<IntProcedure> p = null;
-
synchronized(this) {
-
setReady();
-// p = procs;
-// procs = null;
-
}
-// if(p != null) {
-// IntArray v = (IntArray)getResult();
-// 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);
-// }
-
}
synchronized private void addOrSet(int add) {
}
- final static InternalProcedure<RelationInfo> ip = new InternalProcedure<RelationInfo>() {
-
- @Override
- public void execute(ReadGraphImpl graph, RelationInfo result) {
- }
-
- @Override
- public void exception(ReadGraphImpl graph, Throwable throwable) {
- }
-
- };
-
- final static InternalProcedure<IntSet> ip2 = new InternalProcedure<IntSet>() {
-
- @Override
- public void execute(ReadGraphImpl graph, IntSet result) {
- }
-
- @Override
- public void exception(ReadGraphImpl graph, Throwable throwable) {
- }
-
- };
-
- synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) {
+ synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) throws DatabaseException {
assert(isPending());
IntArray value = (IntArray)getResult();
- RelationInfo ri = RelationInfoQuery.queryEach(graph, add, graph.processor, parent, null, ip);
+ RelationInfo ri = QueryCacheBase.resultRelationInfoQuery(graph, add, parent, null);
if(ri.isFunctional) {
// Replace existing functional predicate if found
try {
- IntSet supers = SuperRelations.queryEach2(graph, add, graph.processor, parent, null, ip2);
+ IntSet supers = QueryCache.resultSuperRelations(graph, add, parent, null);
if(value.data == null) {
if(value.sizeOrData != IntArray.NO_DATA) {
if(supers.contains(value.sizeOrData)) {
}
@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);
- }
+ });
}
-
}