]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java
1521d31936d8e3946addd015b62ac9ce68b77cbe
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ValueQuery.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.impl.procedure.InternalProcedure;
17
18 final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {
19     
20     ValueQuery(final int resource) {
21         super(resource);
22     }
23
24         @Override
25         final public void removeEntry(QueryProcessor provider) {
26                 provider.cache.remove(this);
27         }
28                 
29         public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure<byte[]> procedure) throws DatabaseException {
30
31                 graph.ensureLoaded(r);
32                 
33                 byte[] value = graph.getValue(r);
34                 if(entry != null) {
35                         entry.setResult(value);
36                         entry.setReady();
37                 }
38                 if(procedure != null) {
39                         procedure.execute(graph, value);
40                 }
41                 
42                 return value;
43                 
44         }
45
46     public static byte[] computeForEach(ReadGraphImpl graph, final int r) {
47
48         graph.ensureLoaded(r);
49         
50         return graph.getValue(r);
51         
52     }
53         
54         @Override
55         public Object compute(ReadGraphImpl graph, final InternalProcedure<byte[]> procedure) throws DatabaseException {
56                 return computeForEach(graph, id, this, procedure);
57     }
58     
59     @Override
60     public String toString() {
61         return "Value[" + id + "]";
62     }
63
64     @Override
65     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<byte[]> procedure) throws DatabaseException {
66         return compute(graph, procedure);
67     }
68     
69     @Override
70     public void recompute(ReadGraphImpl graph) throws DatabaseException {
71         
72         compute(graph,  new InternalProcedure<byte[]>() {
73
74             @Override
75             public void execute(ReadGraphImpl graph, byte[] result) {
76             }
77                         
78                         @Override
79                         public void exception(ReadGraphImpl graph, Throwable t) {
80                                 throw new Error("Error in recompute.", t);
81             }
82
83         });
84         
85     }
86     
87     @Override
88     boolean isImmutable(ReadGraphImpl graph) {
89         return graph.processor.isImmutable(id);
90     }
91     
92 }