]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ValueQuery.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.impl.query;\r
13 \r
14 import java.util.concurrent.Semaphore;\r
15 \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
19 \r
20 final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {\r
21     \r
22     private ValueQuery(final int resource) {\r
23         super(resource);\r
24     }\r
25     \r
26     final static ValueQuery entry(final QueryProcessor provider, final int r) {\r
27         return (ValueQuery)provider.valueMap.get(r);\r
28     }\r
29 \r
30     final static byte[] runner(final ReadGraphImpl graph, final int r, CacheEntry parent, final ListenerBase listener, final InternalProcedure<byte[]> procedure) {\r
31 \r
32         QueryProcessor processor = graph.processor;\r
33         \r
34         ValueQuery entry = (ValueQuery)processor.valueMap.get(r); \r
35         if(entry == null) {\r
36                 \r
37                 entry = new ValueQuery(r);\r
38                 entry.setPending();\r
39                 entry.clearResult(processor.querySupport);\r
40                 entry.putEntry(processor);\r
41                 \r
42                 return (byte[])processor.performForEach(graph, entry, parent, listener, procedure);\r
43             \r
44         } else {\r
45                 \r
46                 return (byte[])processor.performForEach(graph, entry, parent, listener, procedure);\r
47             \r
48         }\r
49 \r
50     }\r
51     \r
52     final public static byte[] queryEach(ReadGraphImpl graph, final int r, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<byte[]> procedure) {\r
53         \r
54         assert(r != 0);\r
55         \r
56         if(graph.parent == null && listener == null) {\r
57                 return ValueQuery.computeForEach(graph, r, null, procedure);\r
58         } else {\r
59                 return runner(graph, r, parent, listener, procedure);\r
60         }\r
61          \r
62     }\r
63 \r
64     final public static byte[] queryEach(ReadGraphImpl graph, final int r, final CacheEntry parent) {\r
65         \r
66         assert(r != 0);\r
67         \r
68         if(graph.parent == null) {\r
69             return ValueQuery.computeForEach(graph, r);\r
70         } else {\r
71             return runner(graph, r, parent, null, null);\r
72         }\r
73          \r
74     }\r
75     \r
76         @Override\r
77         public UnaryQuery<InternalProcedure<byte[]>> getEntry(QueryProcessor provider) {\r
78         return provider.valueMap.get(id);\r
79         }\r
80         \r
81         @Override\r
82         public void putEntry(QueryProcessor provider) {\r
83         provider.valueMap.put(id, this);\r
84         }\r
85 \r
86         @Override\r
87         final public void removeEntry(QueryProcessor provider) {\r
88                 provider.valueMap.remove(id);\r
89         }\r
90         \r
91                 \r
92         public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure<byte[]> procedure) {\r
93 \r
94                 graph.ensureLoaded(r);\r
95                 \r
96                 byte[] value = graph.getValue(r);\r
97                 if(entry != null) {\r
98                         entry.setResult(value);\r
99                         entry.setReady();\r
100                 }\r
101                 if(procedure != null) {\r
102                         procedure.execute(graph, value);\r
103                 }\r
104                 \r
105                 return value;\r
106                 \r
107         }\r
108 \r
109     public static byte[] computeForEach(ReadGraphImpl graph, final int r) {\r
110 \r
111         graph.ensureLoaded(r);\r
112         \r
113         return graph.getValue(r);\r
114         \r
115     }\r
116         \r
117         @Override\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
120     }\r
121     \r
122     @Override\r
123     public String toString() {\r
124         return "Value[" + id + "]";\r
125     }\r
126 \r
127     @Override\r
128     public Object performFromCache(ReadGraphImpl graph, QueryProcessor queryProvider, InternalProcedure<byte[]> procedure) {\r
129         return computeForEach(graph, queryProvider, procedure, false);\r
130     }\r
131     \r
132     @Override\r
133     public void recompute(ReadGraphImpl graph, QueryProcessor provider) {\r
134 \r
135         final Semaphore s = new Semaphore(0);\r
136         \r
137         computeForEach(graph, provider, new InternalProcedure<byte[]>() {\r
138 \r
139             @Override\r
140             public void execute(ReadGraphImpl graph, byte[] result) {\r
141                 s.release();\r
142             }\r
143                         \r
144                         @Override\r
145                         public void exception(ReadGraphImpl graph, Throwable t) {\r
146                                 throw new Error("Error in recompute.", t);\r
147             }\r
148 \r
149         }, true);\r
150         \r
151         while(!s.tryAcquire()) {\r
152                 provider.resume(graph);\r
153         }\r
154         \r
155     }\r
156     \r
157     @Override\r
158     boolean isImmutable(ReadGraphImpl graph) {\r
159         return graph.processor.isImmutable(id);\r
160     }    \r
161     \r
162 }\r