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%2FValueQuery.java;h=ad4017f9cff863c0a2bbe42a4fb9458a655e57f2;hp=d85595831851a5b87916d0edba978d2c67acef36;hb=0d9b90834ce56b292c00b1a39850ed842c3e4d42;hpb=e5db6157fd8722c946613d4e46d7aaf6bfa92609 diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java index d85595831..ad4017f9c 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.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,152 +11,44 @@ *******************************************************************************/ package org.simantics.db.impl.query; -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 ValueQuery extends UnaryQuery> { - - private ValueQuery(final int resource) { +public final class ValueQuery extends UnaryQueryP { + + ValueQuery(int resource) { super(resource); } - - final static ValueQuery entry(final QueryProcessor provider, final int r) { - return (ValueQuery)provider.valueMap.get(r); - } - final static byte[] runner(final ReadGraphImpl graph, final int r, CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { - - QueryProcessor processor = graph.processor; - - ValueQuery entry = (ValueQuery)processor.valueMap.get(r); - if(entry == null) { - - entry = new ValueQuery(r); - entry.setPending(); - entry.clearResult(processor.querySupport); - entry.putEntry(processor); - - return (byte[])processor.performForEach(graph, entry, parent, listener, procedure); - - } else { - - return (byte[])processor.performForEach(graph, entry, parent, listener, procedure); - - } - - } - - final public static byte[] queryEach(ReadGraphImpl graph, final int r, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { - - assert(r != 0); - - if(graph.parent == null && listener == null) { - return ValueQuery.computeForEach(graph, r, null, procedure); - } else { - return runner(graph, r, parent, listener, procedure); - } - + @Override + public final void removeEntry(QueryProcessor provider) { + provider.cache.remove(this); } - final public static byte[] queryEach(ReadGraphImpl graph, final int r, final CacheEntry parent) { - - assert(r != 0); - - if(graph.parent == null) { - return ValueQuery.computeForEach(graph, r); - } else { - return runner(graph, r, parent, null, null); - } - + @Override + public void compute(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { + computeForEach(graph, id, this, procedure); } - - @Override - public UnaryQuery> getEntry(QueryProcessor provider) { - return provider.valueMap.get(id); - } - - @Override - public void putEntry(QueryProcessor provider) { - provider.valueMap.put(id, this); - } - - @Override - final public void removeEntry(QueryProcessor provider) { - provider.valueMap.remove(id); - } - - - public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure procedure) { - graph.ensureLoaded(r); - - byte[] value = graph.getValue(r); - if(entry != null) { - entry.setResult(value); - entry.setReady(); - } - if(procedure != null) { - procedure.execute(graph, value); - } - - return value; - - } + public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure procedure_) throws DatabaseException { - public static byte[] computeForEach(ReadGraphImpl graph, final int r) { + InternalProcedure procedure = entry != null ? entry : procedure_; graph.ensureLoaded(r); - - return graph.getValue(r); - - } - - @Override - public Object computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final InternalProcedure procedure, final boolean store) { - return computeForEach(graph, id, this, procedure); - } - - @Override - public String toString() { - return "Value[" + id + "]"; - } - @Override - public Object performFromCache(ReadGraphImpl graph, QueryProcessor queryProvider, InternalProcedure procedure) { - return computeForEach(graph, queryProvider, procedure, false); - } - - @Override - public void recompute(ReadGraphImpl graph, QueryProcessor provider) { + byte[] value = graph.getValue(r); + if(procedure != null) procedure.execute(graph, value); - final Semaphore s = new Semaphore(0); - - computeForEach(graph, provider, new InternalProcedure() { + if(entry != null && procedure_ != null) entry.performFromCache(graph, procedure_); - @Override - public void execute(ReadGraphImpl graph, byte[] result) { - s.release(); - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - throw new Error("Error in recompute.", t); - } + return value; - }, true); - - while(!s.tryAcquire()) { - provider.resume(graph); - } - } - + @Override - boolean isImmutable(ReadGraphImpl graph) { - return graph.processor.isImmutable(id); - } - + public String toString() { + return "Value[" + id + "]"; + } + }