]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ReadEntry.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.AsyncReadGraph;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.impl.graph.WriteGraphImpl;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.db.request.Read;
20 import org.simantics.db.request.ReadExt;
21 import org.simantics.db.request.RequestFlags;
22
23 final public class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
24
25         protected Read<T> request;
26
27         public ReadEntry(Read<T> request) {
28         this.request = request;
29     }
30     
31     @Override
32     int makeHash() {
33         return request.hashCode();
34     }
35         
36     @Override
37     public Object getOriginalRequest() {
38         return request;
39     }
40     
41     @Override
42     public void discard() {
43         super.discard();
44         setResult(null);
45     }
46         
47     final public Object addOrSet(AsyncReadGraph graph, Object result) {
48
49         assert(assertPending());
50         
51         setResult(result);
52         setReady();
53         
54         return result;
55         
56     }
57
58     @Override
59     final public Query getQuery() {
60         
61         return new Query() {
62
63                         @Override
64                         public void recompute(ReadGraphImpl graph) {
65                                 
66                                 try {
67
68                                     T result = request.perform(graph);
69                                     addOrSet(graph, result);
70
71                                 } catch (Throwable t) {
72
73                                         except(t);
74                     
75                 }
76                                 
77                         }
78
79                         @Override
80                         public void removeEntry(QueryProcessor processor) {
81                                 processor.cache.remove(ReadEntry.this);
82                         }
83
84                         @Override
85                         public int type() {
86                                 if(request instanceof ReadExt) {
87                                         return ((ReadExt)request).getType();
88                                 } else {
89                                         return RequestFlags.INVALIDATE;
90                                 }
91                         }
92                         
93                         @Override
94                         public String toString() {
95                                 if(request == null) return "DISCARDED";
96                                 else return request.toString() + statusOrException;
97                         }
98                 
99         };
100         
101     }
102     
103     @Override
104     public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
105
106                 ReadGraphImpl queryGraph = graph.withParent(this);
107
108                 try {
109
110                         addOrSet(queryGraph, request.perform(queryGraph));
111                         return get(queryGraph, procedure);
112
113                 }  catch (DatabaseException e) {
114
115                         except(e);
116                         return get(graph, procedure);
117
118                 }  catch (Throwable t) {
119
120                         except(new DatabaseException(t));
121                         return get(graph, procedure);
122
123                 }
124         
125     }
126
127     public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
128         
129                 ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
130
131                 try {
132
133                         T result = request.perform(queryGraph);
134                         if(entry != null) entry.addOrSet(queryGraph, result);
135                         procedure.execute(graph, result);
136
137                 }  catch (DatabaseException e) {
138
139                         if(entry != null) entry.except(e);
140                         procedure.exception(graph, e);
141
142                 }  catch (Throwable t) {
143
144                         DatabaseException dbe = new DatabaseException(t);
145                         if(entry != null) entry.except(dbe);
146                         procedure.exception(graph, dbe);
147
148                 }
149         
150     }
151     
152         public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
153             
154         AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
155
156         if(proc != null) {
157                 if(isExcepted()) {
158                         try {
159                                 proc.exception(graph, (Throwable)getResult());
160                         } catch (Throwable t) {
161                                 t.printStackTrace();
162                         }
163                 } else {
164                         try {
165                                 proc.execute(graph, (T)getResult());
166                         } catch (Throwable t) {
167                                 t.printStackTrace();
168                         }
169                 }
170         }
171                 
172             return (T)getResult();
173             
174         }
175         
176         @Override
177         public String toString() {
178                 if(request == null) return "DISCARDED";
179                 else return request.toString() + " - " + statusOrException;
180         }
181         
182         public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
183                 if(procedure != null) performFromCache(graph, procedure);
184                 checkAndThrow();
185                 return getResult();
186         }
187         
188         @Override
189         boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
190                 if(request instanceof ReadExt) {
191                         return ((ReadExt)request).isImmutable(graph);
192                 }
193                 return false;
194         }
195
196 }