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%2FTypes.java;h=bb0d11a243193da746bb88804696317503dc941f;hp=2d3962aa177d2a33ae67b1b5fea6354fb53ca681;hb=e460fd6f0af60314e2ca28391ef7ff2043016d97;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java index 2d3962aa1..bb0d11a24 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java @@ -1,383 +1,243 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 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 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -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.impl.graph.ReadGraphImpl; -import org.simantics.db.impl.procedure.InternalProcedure; -import org.simantics.db.procedure.ListenerBase; - -final public class Types extends UnaryQuery> { - -// public ArrayList> procs; - - private 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 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 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> getEntry(QueryProcessor provider) { - return provider.typesMap.get(id); - } - - @Override - public void putEntry(QueryProcessor provider) { - provider.typesMap.put(id, this); - } - - @Override - final public void removeEntry(QueryProcessor provider) { - provider.typesMap.remove(id); - } - - @Override - public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor queryProvider, final InternalProcedure procedure, final boolean store) { - - queryProvider.querySupport.ensureLoaded(graph, id); - int ret = queryProvider.querySupport.getSingleInstance(id); - if(ret > 0) { - - TypeHierarchy.queryEach(graph, ret, queryProvider, store ? Types.this : null, null, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, IntSet types) { - - addOrSet(graph, types, queryProvider); - procedure.execute(graph, types); - - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - procedure.exception(graph, t); - } - - }); - - return getResult(); - - } - - final int instanceOf = queryProvider.getInstanceOf(); - final int inherits = queryProvider.getInherits(); - final int subrelationOf = queryProvider.getSubrelationOf(); - - final IntSet result = new IntSet(queryProvider.querySupport); - - final TIntProcedure addToResult = new TIntProcedure() { - @Override - public boolean execute(int r) { - synchronized(result) { - result.add(r); - } - return true; - } - }; - - final AtomicInteger finishes = new AtomicInteger(0); - - SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() { - - @Override - public void run(ReadGraphImpl graph) { - - if(finishes.addAndGet(1) == 3) { - if(store) addOrSet(graph, result, queryProvider); - procedure.execute(graph, result); - } - - } - - @Override - public void execute(ReadGraphImpl graph, int i) { - - synchronized(result) { - result.add(i); - } - - inc(); - - SuperTypes.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, IntSet types) { - types.forEach(addToResult); - dec(graph); - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - procedure.exception(graph, t); - dec(graph); - } - - }); - - } - - @Override - public void finished(ReadGraphImpl graph) { - dec(graph); - } - - }; - - SyncIntProcedure inheritsProcedure = new SyncIntProcedure() { - - @Override - public void run(ReadGraphImpl graph) { - - int current = finishes.addAndGet(1); - if(current == 3) { - if(store) addOrSet(graph, result, queryProvider); - procedure.execute(graph, result); - } - - } - - @Override - public void execute(ReadGraphImpl graph, int i) { - - inc(); - - Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, IntSet types) { - types.forEach(addToResult); - dec(graph); - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - procedure.exception(graph, t); - dec(graph); - } - - }); - - } - - @Override - public void finished(ReadGraphImpl graph) { - - dec(graph); - - } - - }; - - SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() { - - @Override - public void run(ReadGraphImpl graph) { - - int current = finishes.addAndGet(1); - if(current == 3) { - if(store) addOrSet(graph, result, queryProvider); - procedure.execute(graph, result); - } - - } - - @Override - public void execute(ReadGraphImpl graph, int i) { - - inc(); - - Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, IntSet types) { - - types.forEach(addToResult); - dec(graph); - - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - procedure.exception(graph, t); - dec(graph); - } - - }); - - } - - @Override - public void finished(ReadGraphImpl graph) { - - dec(graph); - - } - - }; - - queryProvider.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure); - instanceOfProcedure.finished(graph); - queryProvider.querySupport.getObjects(graph, id, inherits, inheritsProcedure); - inheritsProcedure.finished(graph); - queryProvider.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure); - subrelationOfProcedure.finished(graph); - - return result; - - } - - @Override - public String toString() { - return "Types[" + id + "]"; - } - - private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) { - - assert(!isReady()); - - synchronized(this) { - - value.trim(); - setResult(value); - setReady(); - - } - - } - - @Override - final public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure procedure) { - - assert(isReady()); - - if(handleException(graph, procedure)) return EXCEPTED; - - 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); - } - -} +/******************************************************************************* + * 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 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.impl.query; + +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 gnu.trove.procedure.TIntProcedure; + +public final class Types extends UnaryQueryPIntSet { + + Types(int resource) { + super(resource); + } + + @Override + public final void removeEntry(QueryProcessor provider) { + provider.cache.remove(this); + } + + @Override + public void compute(final ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { + computeForEach(graph, id, this, procedure); + } + + public static void computeForEach(final ReadGraphImpl graph, int id, Types entry, + final InternalProcedure procedure_) throws DatabaseException { + + InternalProcedure procedure = entry != null ? entry : procedure_; + + computeForEach2(graph, id, entry, procedure); + + if (entry != null) + entry.performFromCache(graph, procedure_); + + } + + public static void computeForEach2(final ReadGraphImpl graph, int id, Types parent, + final InternalProcedure procedure) throws DatabaseException { + + QueryProcessor processor = graph.processor; + + processor.querySupport.ensureLoaded(graph, id); + + int ret = processor.querySupport.getSingleInstance(id); + if (ret > 0) { + + TypeHierarchy.queryEach(graph, ret, processor, parent, null, new InternalProcedure() { + + @Override + public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException { + procedure.execute(graph, types); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); + } + + }); + + return; + + } + + final int instanceOf = processor.getInstanceOf(); + final int inherits = processor.getInherits(); + final int subrelationOf = processor.getSubrelationOf(); + + final IntSet result = new IntSet(processor.querySupport); + + final TIntProcedure addToResult = new TIntProcedure() { + @Override + public boolean execute(int r) { + synchronized (result) { + result.add(r); + } + return true; + } + }; + + final AtomicInteger finishes = new AtomicInteger(0); + + SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() { + + @Override + public void run(ReadGraphImpl graph) throws DatabaseException { + + if (finishes.addAndGet(1) == 3) { + procedure.execute(graph, result); + } + + } + + @Override + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { + + result.add(i); + + inc(); + + QueryCache.runnerSuperTypes(graph, i, parent, null, new InternalProcedure() { + + @Override + public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException { + types.forEach(addToResult); + dec(graph); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); + dec(graph); + } + + }); + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + dec(graph); + } + + }; + + SyncIntProcedure inheritsProcedure = new SyncIntProcedure() { + + @Override + public void run(ReadGraphImpl graph) throws DatabaseException { + + int current = finishes.addAndGet(1); + if (current == 3) { + procedure.execute(graph, result); + } + + } + + @Override + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { + + inc(); + + QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure() { + + @Override + public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException { + types.forEach(addToResult); + dec(graph); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); + dec(graph); + } + + }); + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + + dec(graph); + + } + + }; + + SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() { + + @Override + public void run(ReadGraphImpl graph) throws DatabaseException { + + int current = finishes.addAndGet(1); + if (current == 3) { + procedure.execute(graph, result); + } + + } + + @Override + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { + + inc(); + + QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure() { + + @Override + public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException { + + types.forEach(addToResult); + dec(graph); + + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); + dec(graph); + } + + }); + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + + dec(graph); + + } + + }; + + processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure); + instanceOfProcedure.finished(graph); + processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure); + inheritsProcedure.finished(graph); + processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure); + subrelationOfProcedure.finished(graph); + + } + + @Override + public String toString() { + return "Types[" + id + "]"; + } + +}