]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AsyncReadEntry.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.DebugPolicy;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.db.request.AsyncRead;
20
21 final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
22
23     protected AsyncRead<T> request;
24
25     AsyncReadEntry(AsyncRead<T> request) {
26         this.request = request;
27         if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
28     }
29
30     @Override
31     int makeHash() {
32         return request.hashCode();
33     }
34     
35     @Override
36     public Object getOriginalRequest() {
37         return request;
38     }
39     
40     @Override
41     public void discard() {
42         super.discard();
43         setResult(null);
44     }
45     
46     final public void addOrSet(AsyncReadGraph graph, Object item) {
47
48         assert(isPending());
49
50         synchronized(this) {
51             setResult(item);
52                 setReady();
53         }
54         
55     }
56     
57     
58     public void except(AsyncReadGraph graph, Throwable t) {
59         
60         assert(isPending());
61
62         synchronized(this) {
63             except(t);
64         }
65         
66     }
67     
68     
69     @Override
70     final public Query getQuery() {
71         
72         return new Query() {
73
74                         @Override
75                         public void recompute(ReadGraphImpl graph) {
76
77                                 try {
78
79                                     request.perform(graph , new AsyncProcedure<T>() {
80
81                         @Override
82                         public void execute(AsyncReadGraph graph, T result) {
83                             addOrSet(graph, result);
84                         }
85                                 
86                                 @Override
87                                 public void exception(AsyncReadGraph graph, Throwable t) {
88                                     except(t);
89                         }
90
91                     });
92
93                                 } catch (Throwable t) {
94                                     except(t);
95                 }
96                                 
97                         }
98
99                         @Override
100                         public void removeEntry(QueryProcessor qp) {
101                         qp.cache.remove(AsyncReadEntry.this);
102                         }
103
104                         @Override
105                         public int type() {
106                                 return request.getFlags();
107                         }
108                         
109                         @Override
110                         public String toString() {
111                                 if(request == null) return "DISCARDED";
112                                 else if(isExcepted()) return request.toString() + " " + getResult();
113                                 else return request.toString() + " " + statusOrException;
114                         }
115                 
116         };
117         
118     }
119
120     @Override
121         public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
122                 
123             if(isExcepted()) {
124             
125             try {
126                 proc.exception(graph, (Throwable)getResult());
127             } catch (Throwable t) {
128                 t.printStackTrace();
129             }
130             
131         } else {
132             
133             try {
134                 proc.execute(graph, (T)getResult());
135             } catch (Throwable t) {
136                 t.printStackTrace();
137             }
138             
139         }
140                 
141             return getResult();
142             
143         }
144
145     @Override
146     public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
147         return graph.processor.cache.performQuery(graph, request, this, procedure);
148     }
149     
150         @Override
151         public String toString() {
152                 if(isDiscarded()) return "DISCARDED " + request.toString();
153                 else if(isExcepted()) return request.toString() + " " + getResult();
154                 else return request.toString() + " " + statusOrException;
155         }
156
157 }