X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FDirectObjects.java;h=9bcb6ddb8b4c61400194e2cec39aceb514b9c908;hb=a88c02c2d0f4250caf887a130b9f4314c6564722;hp=f42a1ef31793788942db11bd041b940ee6f52ca6;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java index f42a1ef31..9bcb6ddb8 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java @@ -1,200 +1,134 @@ -/******************************************************************************* - * 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 java.util.Collection; -import java.util.concurrent.Semaphore; - -import org.simantics.db.common.exception.DebugException; -import org.simantics.db.impl.graph.ReadGraphImpl; -import org.simantics.db.procedure.ListenerBase; -import org.simantics.db.request.RequestFlags; - -final public class DirectObjects extends CollectionBinaryQuery { - - private DirectObjects(final int r1, final int r2) { - super(r1, r2); - } - - @Override - public int type() { - return RequestFlags.INVALIDATE; - } - - @Override - public void clearResult(QuerySupport support) { - setResult(INVALID_RESULT); - } - - final static DirectObjects entry(final QueryProcessor provider, final int r1, final int r2) { - - return (DirectObjects)provider.directObjectsMap.get(id(r1,r2)); - - } - - final static Collection entries(final QueryProcessor processor, final int r1) { - DoubleKeyQueryHashMap hash = processor.directObjectsMap; - return hash.values(r1); - } - - final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) { - - QueryProcessor processor = graph.processor; - - DirectObjects entry = (DirectObjects)processor.directObjectsMap.get(id(r1,r2)); - if(entry == null) { - - entry = new DirectObjects(r1, r2); - entry.setPending(); - entry.clearResult(processor.querySupport); - entry.putEntry(processor); - - processor.performForEach(graph, entry, parent, listener, procedure); - - } else { - - if(entry.isPending()) { - synchronized(entry) { - if(entry.isPending()) { - processor.registerDependencies(graph, entry, parent, listener, procedure, false); - entry.computeForEach(graph, processor, procedure, true); - return; - } - } - } - - processor.performForEach(graph, entry, parent, listener, procedure); - - } - - } - - final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) { - - assert(r1 != 0); - assert(r2 != 0); - - if(parent == null && listener == null) { - DirectObjects.computeForEach(graph, r1, r2, null, procedure); - } else { - runner(graph, r1, r2, parent, listener, procedure); - } - - } - - @Override - public BinaryQuery getEntry(QueryProcessor provider) { - return provider.directObjectsMap.get(id); - } - - @Override - public void putEntry(QueryProcessor provider) { - provider.directObjectsMap.put(id, this); - } - - @Override - final public void removeEntry(QueryProcessor provider) { - provider.directObjectsMap.remove(id); - } - - @Override - public void computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure procedure, final boolean store) { - computeForEach(graph, r1(), r2(), this, procedure); - } - - static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) { - - QueryProcessor processor = graph.processor; - - processor.querySupport.ensureLoaded(graph, r1, r2); - - processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() { - - @Override - public void execute(ReadGraphImpl graph, int i) { - procedure.execute(graph, i); - } - - @Override - public void finished(ReadGraphImpl graph) { - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - if(DebugException.DEBUG) new DebugException(t).printStackTrace(); - } - - }); - - if(entry != null) entry.finish(graph, processor); - procedure.finished(graph); - - } - - @Override - public String toString() { - return "DirectObjects[" + r1() + " - " + r2() + "]"; - } - - @Override - public void setReady() { - statusOrException = READY; - } - - final private void finish(ReadGraphImpl graph, QueryProcessor provider) { - setReady(); - } - - @Override - public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) { - - assert(isReady()); - computeForEach(graph, provider, procedure, false); - - } - - @Override - public void recompute(ReadGraphImpl graph, QueryProcessor provider) { - - final Semaphore s = new Semaphore(0); - - computeForEach(graph, provider, new IntProcedure() { - - @Override - public void finished(ReadGraphImpl graph) { - s.release(); - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - throw new Error("Error in recompute.", t); - } - - @Override - public void execute(ReadGraphImpl graphd, int i) { - } - - }, true); - - while(!s.tryAcquire()) { - provider.resume(graph); - } - - } - - @Override - boolean isImmutable(ReadGraphImpl graph) { - return graph.processor.isImmutable(r1()); - } - -} - +/******************************************************************************* + * 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 org.simantics.db.exception.DatabaseException; +import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.request.RequestFlags; + +final public class DirectObjects extends CollectionBinaryQuery implements IntProcedure { + + DirectObjects(final int r1, final int r2) { + super(r1, r2); + } + + @Override + public int type() { + return RequestFlags.INVALIDATE; + } + + @Override + public final void removeEntry(QueryProcessor provider) { + provider.cache.remove(this); + } + + // @Override + public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { + computeForEach(graph, r1(), r2(), this, procedure); + return getResult(); + } + + public static void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, + final IntProcedure procedure_) throws DatabaseException { + + IntProcedure procedure = entry != null ? entry : procedure_; + + QueryProcessor processor = graph.processor; + + processor.querySupport.ensureLoaded(graph, r1, r2); + + processor.querySupport.getObjects(graph, r1, r2, procedure); + + procedure.finished(graph); + + if (entry != null) + entry.performFromCache(graph, procedure_); + + } + + @Override + public String toString() { + return "DirectObjects[" + r1() + " - " + r2() + "]"; + } + + @Override + public void setReady() { + statusOrException = READY; + } + + @Override + public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException { + + assert (isReady()); + + if (handleException(graph, procedure)) + return getResult(); + + 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 value; + + } + + @Override + public void recompute(ReadGraphImpl graph) throws DatabaseException { + + compute(graph, new IntProcedure() { + + @Override + public void finished(ReadGraphImpl graph) { + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) { + throw new Error("Error in recompute.", t); + } + + @Override + public void execute(ReadGraphImpl graphd, int i) { + } + + }); + + } + + @Override + boolean isImmutable(ReadGraphImpl graph) { + return graph.processor.isImmutable(r1()); + } + + @Override + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { + IntArray value = (IntArray) getResult(); + value.add(i); + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + setReady(); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException { + except(throwable); + } + +}