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