X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FSuperTypes.java;h=a02fda4623342fc7d80889f8c543f6d5aec70316;hp=0489546c55d6caa5fdd50f5537bf321302db4f7f;hb=0d9b90834ce56b292c00b1a39850ed842c3e4d42;hpb=e5db6157fd8722c946613d4e46d7aaf6bfa92609 diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java index 0489546c5..a02fda462 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * Copyright (c) 2007, 2018 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -11,231 +11,99 @@ *******************************************************************************/ package org.simantics.db.impl.query; -import gnu.trove.procedure.TIntProcedure; - -import java.util.concurrent.Semaphore; - +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; -final public class SuperTypes extends UnaryQuery> { - -// public ArrayList> procs = null; - - private SuperTypes(final int resource) { +import gnu.trove.procedure.TIntProcedure; + +public final class SuperTypes extends UnaryQueryP { + + SuperTypes(int resource) { super(resource); } - - final static SuperTypes runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure procedure) { - - SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r); - if(entry == null) { - - entry = new SuperTypes(r); - entry.setPending(); - entry.clearResult(provider.querySupport); - entry.putEntry(provider); - - provider.performForEach(graph, entry, parent, listener, procedure); - - return entry; - - } else { - - if(!entry.isReady()) { - synchronized(entry) { - if(!entry.isReady()) { - throw new IllegalStateException(); -// if(entry.procs == null) entry.procs = new ArrayList>(); -// entry.procs.add(procedure); -// provider.registerDependencies(graph, entry, parent, listener, procedure, false); -// return entry; - } - } - } - provider.performForEach(graph, entry, parent, listener, procedure); - } - - return entry; + @Override + public final void removeEntry(QueryProcessor provider) { + provider.cache.remove(this); } - - final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { - - if(parent == null && listener == null) { - SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r); - if(entry != null && entry.isReady()) { - entry.performFromCache(graph, provider, procedure); - return entry; - } - } - - return runner(graph, r, parent, provider, listener, procedure); - + + @Override + public void compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { + computeForEach(graph, id, this, procedure); } - - @Override - public UnaryQuery> getEntry(QueryProcessor provider) { - return provider.superTypesMap.get(id); - } - - @Override - public void putEntry(QueryProcessor provider) { - provider.superTypesMap.put(id, this); - } - - @Override - final public void removeEntry(QueryProcessor provider) { - provider.superTypesMap.remove(id); - } - - @Override - public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure procedure, boolean store) { + + public static Object computeForEach(ReadGraphImpl graph, int r, SuperTypes entry, final InternalProcedure procedure_) throws DatabaseException { + + InternalProcedure procedure = entry != null ? entry : procedure_; + + QueryProcessor provider = graph.processor; final int inherits = provider.getInherits(); - - final CacheEntry parent = graph.parent; - - assert(graph.parent != this); final IntSet result = new IntSet(provider.querySupport); - + final TIntProcedure addToResult = new TIntProcedure() { @Override public boolean execute(int r) { - synchronized(result) { - result.add(r); - } + synchronized(result) { + result.add(r); + } return true; } }; - DirectObjects.queryEach(graph, id, inherits, provider, this, null, new SyncIntProcedure() { - - @Override - public void run(ReadGraphImpl graph) { + QueryCache.runnerDirectObjects(graph, r, inherits, entry, null, new SyncIntProcedure() { - addOrSet(graph, result, provider); + @Override + public void run(ReadGraphImpl graph) throws DatabaseException { procedure.execute(graph, result); - } - + @Override - public void execute(ReadGraphImpl graph, final int i) { - - assert(graph.parent == parent); - - synchronized(result) { - result.add(i); - } - + public void execute(ReadGraphImpl graph, final int i) throws DatabaseException { + + synchronized(result) { + result.add(i); + } + inc(); - SuperTypes.queryEach(graph, i, provider, SuperTypes.this, null, new InternalProcedure() { + QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure() { @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) { - procedure.exception(graph, t); - } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); + } }); - + } @Override - public void finished(ReadGraphImpl graph) { + public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } - + }); - - return result; - - } - - @Override - public String toString() { - return "SuperTypes2[" + id + "]"; - } - private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) { - - assert(!isReady()); - -// ArrayList> p = null; - - synchronized(this) { - - value.trim(); - setResult(value); - setReady(); -// p = procs; -// procs = null; - - } - -// if(p != null) { -// IntSet v = (IntSet)getResult(); -// if(v != null) { -// for(InternalProcedure proc : p) proc.execute(graph, v); -// } -// } - - } + if(entry != null) entry.performFromCache(graph, procedure_); - @Override - public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure procedure) { - - assert(isReady()); - - if(handleException(graph, procedure)) return null; - - IntSet result = getResult(); - - procedure.execute(graph, result); - return result; - - } - - @Override - public void recompute(ReadGraphImpl graph, QueryProcessor provider) { - - final Semaphore s = new Semaphore(0); - - computeForEach(graph, provider, new InternalProcedure() { - - @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); - } - } - + @Override - boolean isImmutable(ReadGraphImpl graph) { - return graph.processor.isImmutable(id); + public String toString() { + return "SuperTypes[" + id + "]"; } - + }