*******************************************************************************/
package org.simantics.db.impl.query;
-import gnu.trove.procedure.TIntProcedure;
-
-import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
+import org.simantics.db.exception.DatabaseException;
import org.simantics.db.impl.graph.ReadGraphImpl;
import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.procedure.ListenerBase;
+
+import gnu.trove.procedure.TIntProcedure;
final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
-
-// public ArrayList<InternalProcedure<IntSet>> procs;
- private Types(final int resource) {
+ Types(final int resource) {
super(resource);
}
-
- final static Types entry(final QueryProcessor provider, final int r) {
- return (Types)provider.typesMap.get(r);
- }
-
- final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, Types cached, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
-
- Types entry = cached != null ? cached : (Types)provider.typesMap.get(r);
- if(entry == null) {
-
- entry = new Types(r);
- entry.setPending();
- entry.clearResult(provider.querySupport);
- entry.putEntry(provider);
-
- provider.performForEach(graph, entry, parent, listener, procedure);
-
- } else {
-
- if(!entry.isReady()) {
- throw new IllegalStateException();
- }
- provider.performForEach(graph, entry, parent, listener, procedure);
- }
-
- }
-
- final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
-
- Types entry = (Types)provider.typesMap.get(r);
- if(entry == null) {
-
- entry = new Types(r);
- entry.setPending();
- entry.clearResult(provider.querySupport);
- entry.putEntry(provider);
-
- return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
-
- } else {
-
- if(!entry.isReady()) {
- throw new IllegalStateException();
- }
- return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
-
- }
-
- }
-
- final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
-
- final Types entry = (Types)provider.typesMap.get(r);
-
- if(parent == null && listener == null) {
- if(entry != null && entry.isReady()) {
- entry.performFromCache(graph, provider, procedure);
- return;
- }
- }
-
- runner(graph, r, provider, entry, parent, listener, procedure);
-
- }
- final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
-
- if(parent == null) {
- Types entry = (Types)provider.typesMap.get(r);
- if(entry != null && entry.isReady()) {
- return (IntSet)entry.get(graph, provider, null);
- }
- }
-
- return runner2(graph, r, provider, parent);
-
- }
-
- @Override
- public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
- return provider.typesMap.get(id);
- }
-
@Override
- public void putEntry(QueryProcessor provider) {
- provider.typesMap.put(id, this);
+ final public void removeEntry(QueryProcessor provider) {
+ provider.cache.remove(this);
}
- @Override
- final public void removeEntry(QueryProcessor provider) {
- provider.typesMap.remove(id);
+ //@Override
+ public Object compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+ computeForEach(graph, id, this, procedure);
+ return getResult();
}
- @Override
- public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor queryProvider, final InternalProcedure<IntSet> procedure, final boolean store) {
+ public static void computeForEach(final ReadGraphImpl graph, int id, Types entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+
+ assert(procedure != null);
+
+ QueryProcessor processor = graph.processor;
- queryProvider.querySupport.ensureLoaded(graph, id);
- int ret = queryProvider.querySupport.getSingleInstance(id);
+ processor.querySupport.ensureLoaded(graph, id);
+
+ int ret = processor.querySupport.getSingleInstance(id);
if(ret > 0) {
- TypeHierarchy.queryEach(graph, ret, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+ TypeHierarchy.queryEach(graph, ret, processor, entry, null, new InternalProcedure<IntSet>() {
@Override
- public void execute(ReadGraphImpl graph, IntSet types) {
+ public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
- addOrSet(graph, types, queryProvider);
+ if(entry != null) {
+ entry.addOrSet(graph, types, processor);
+ entry.finish();
+ }
procedure.execute(graph, types);
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
procedure.exception(graph, t);
}
});
-
- return getResult();
+
+ return;
}
- final int instanceOf = queryProvider.getInstanceOf();
- final int inherits = queryProvider.getInherits();
- final int subrelationOf = queryProvider.getSubrelationOf();
+ final int instanceOf = processor.getInstanceOf();
+ final int inherits = processor.getInherits();
+ final int subrelationOf = processor.getSubrelationOf();
- final IntSet result = new IntSet(queryProvider.querySupport);
+ final IntSet result = new IntSet(processor.querySupport);
final TIntProcedure addToResult = new TIntProcedure() {
@Override
SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
@Override
- public void run(ReadGraphImpl graph) {
+ public void run(ReadGraphImpl graph) throws DatabaseException {
if(finishes.addAndGet(1) == 3) {
- if(store) addOrSet(graph, result, queryProvider);
+ if(entry != null) entry.addOrSet(graph, result, processor);
procedure.execute(graph, result);
}
}
@Override
- public void execute(ReadGraphImpl graph, int i) {
+ public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
- synchronized(result) {
- result.add(i);
- }
+ result.add(i);
inc();
- SuperTypes.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+ QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
@Override
- public void execute(ReadGraphImpl graph, IntSet types) {
+ public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
types.forEach(addToResult);
dec(graph);
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
procedure.exception(graph, t);
dec(graph);
}
}
@Override
- public void finished(ReadGraphImpl graph) {
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
dec(graph);
}
SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
@Override
- public void run(ReadGraphImpl graph) {
+ public void run(ReadGraphImpl graph) throws DatabaseException {
int current = finishes.addAndGet(1);
if(current == 3) {
- if(store) addOrSet(graph, result, queryProvider);
+ if(entry != null) entry.addOrSet(graph, result, processor);
procedure.execute(graph, result);
}
}
@Override
- public void execute(ReadGraphImpl graph, int i) {
+ public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
inc();
- Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+ QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
@Override
- public void execute(ReadGraphImpl graph, IntSet types) {
+ public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
types.forEach(addToResult);
dec(graph);
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
procedure.exception(graph, t);
dec(graph);
}
}
@Override
- public void finished(ReadGraphImpl graph) {
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
dec(graph);
SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
@Override
- public void run(ReadGraphImpl graph) {
+ public void run(ReadGraphImpl graph) throws DatabaseException {
int current = finishes.addAndGet(1);
if(current == 3) {
- if(store) addOrSet(graph, result, queryProvider);
+ if(entry != null) entry.addOrSet(graph, result, processor);
procedure.execute(graph, result);
}
}
@Override
- public void execute(ReadGraphImpl graph, int i) {
+ public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
inc();
- Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+ QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
@Override
- public void execute(ReadGraphImpl graph, IntSet types) {
+ public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
types.forEach(addToResult);
dec(graph);
}
@Override
- public void exception(ReadGraphImpl graph, Throwable t) {
+ public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
procedure.exception(graph, t);
dec(graph);
}
}
@Override
- public void finished(ReadGraphImpl graph) {
+ public void finished(ReadGraphImpl graph) throws DatabaseException {
dec(graph);
};
- queryProvider.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
+ processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
instanceOfProcedure.finished(graph);
- queryProvider.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
+ processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
inheritsProcedure.finished(graph);
- queryProvider.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
+ processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
subrelationOfProcedure.finished(graph);
- return result;
+ if(entry != null) entry.finish();
}
assert(!isReady());
- synchronized(this) {
-
- value.trim();
- setResult(value);
- setReady();
-
- }
+ setResult(value);
}
+ void finish() {
+
+ IntSet result = getResult();
+ result.trim();
+ setReady();
+
+ }
+
@Override
- final public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
+ final public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> 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 InternalProcedure<IntSet>() {
+ compute(graph, new InternalProcedure<IntSet>() {
@Override
public void execute(ReadGraphImpl graph, IntSet result) {
- 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);
- }
+ });
}