1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.impl.query;
\r
14 import java.util.concurrent.Semaphore;
\r
16 import org.simantics.db.impl.graph.ReadGraphImpl;
\r
17 import org.simantics.db.impl.procedure.InternalProcedure;
\r
18 import org.simantics.db.procedure.ListenerBase;
\r
20 final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {
\r
22 private ValueQuery(final int resource) {
\r
26 final static ValueQuery entry(final QueryProcessor provider, final int r) {
\r
27 return (ValueQuery)provider.valueMap.get(r);
\r
30 final static byte[] runner(final ReadGraphImpl graph, final int r, CacheEntry parent, final ListenerBase listener, final InternalProcedure<byte[]> procedure) {
\r
32 QueryProcessor processor = graph.processor;
\r
34 ValueQuery entry = (ValueQuery)processor.valueMap.get(r);
\r
37 entry = new ValueQuery(r);
\r
39 entry.clearResult(processor.querySupport);
\r
40 entry.putEntry(processor);
\r
42 return (byte[])processor.performForEach(graph, entry, parent, listener, procedure);
\r
46 return (byte[])processor.performForEach(graph, entry, parent, listener, procedure);
\r
52 final public static byte[] queryEach(ReadGraphImpl graph, final int r, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<byte[]> procedure) {
\r
56 if(graph.parent == null && listener == null) {
\r
57 return ValueQuery.computeForEach(graph, r, null, procedure);
\r
59 return runner(graph, r, parent, listener, procedure);
\r
64 final public static byte[] queryEach(ReadGraphImpl graph, final int r, final CacheEntry parent) {
\r
68 if(graph.parent == null) {
\r
69 return ValueQuery.computeForEach(graph, r);
\r
71 return runner(graph, r, parent, null, null);
\r
77 public UnaryQuery<InternalProcedure<byte[]>> getEntry(QueryProcessor provider) {
\r
78 return provider.valueMap.get(id);
\r
82 public void putEntry(QueryProcessor provider) {
\r
83 provider.valueMap.put(id, this);
\r
87 final public void removeEntry(QueryProcessor provider) {
\r
88 provider.valueMap.remove(id);
\r
92 public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure<byte[]> procedure) {
\r
94 graph.ensureLoaded(r);
\r
96 byte[] value = graph.getValue(r);
\r
98 entry.setResult(value);
\r
101 if(procedure != null) {
\r
102 procedure.execute(graph, value);
\r
109 public static byte[] computeForEach(ReadGraphImpl graph, final int r) {
\r
111 graph.ensureLoaded(r);
\r
113 return graph.getValue(r);
\r
118 public Object computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final InternalProcedure<byte[]> procedure, final boolean store) {
\r
119 return computeForEach(graph, id, this, procedure);
\r
123 public String toString() {
\r
124 return "Value[" + id + "]";
\r
128 public Object performFromCache(ReadGraphImpl graph, QueryProcessor queryProvider, InternalProcedure<byte[]> procedure) {
\r
129 return computeForEach(graph, queryProvider, procedure, false);
\r
133 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
\r
135 final Semaphore s = new Semaphore(0);
\r
137 computeForEach(graph, provider, new InternalProcedure<byte[]>() {
\r
140 public void execute(ReadGraphImpl graph, byte[] result) {
\r
145 public void exception(ReadGraphImpl graph, Throwable t) {
\r
146 throw new Error("Error in recompute.", t);
\r
151 while(!s.tryAcquire()) {
\r
152 provider.resume(graph);
\r
158 boolean isImmutable(ReadGraphImpl graph) {
\r
159 return graph.processor.isImmutable(id);
\r