]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java
Still working for multiple readers
[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 Object compute(ReadGraphImpl graph, final InternalProcedure<byte[]> procedure) throws DatabaseException {
47 //              return computeForEach(graph, id, this, procedure);
48 //    }
49     
50     @Override
51     public String toString() {
52         return "Value[" + id + "]";
53     }
54
55     @Override
56     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<byte[]> procedure) throws DatabaseException {
57         return computeForEach(graph, id, this, procedure);
58     }
59     
60     @Override
61     public void recompute(ReadGraphImpl graph) throws DatabaseException {
62         
63         computeForEach(graph, id, this, new InternalProcedure<byte[]>() {
64
65             @Override
66             public void execute(ReadGraphImpl graph, byte[] result) {
67             }
68                         
69                         @Override
70                         public void exception(ReadGraphImpl graph, Throwable t) {
71                                 throw new Error("Error in recompute.", t);
72             }
73
74         });
75         
76     }
77     
78     @Override
79     boolean isImmutable(ReadGraphImpl graph) {
80         return graph.processor.isImmutable(id);
81     }
82     
83 }