]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java
New splash.bmp with version 1.36.0
[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 {
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 void addOrSet(AsyncReadGraph graph, Object item) {
48
49         assert(assertPending());
50         
51 //        ArrayList<Pair<AsyncProcedure<T>, AsyncBarrier>> p = null;
52
53         synchronized(this) {
54             
55             setResult(item);
56                 setReady();
57 //              p = procs;
58 //              procs = null;
59                 
60         }
61
62 //        if(p != null)
63 //              for(Pair<AsyncProcedure<T>, AsyncBarrier> proc : p) {
64 //                      proc.first.execute(graph, (T)item);
65 //                      proc.second.dec();
66 //              }
67         
68     }
69
70     @Override
71     final public Query getQuery() {
72         
73         return new Query() {
74
75                         @Override
76                         public void recompute(ReadGraphImpl graph_, Object provider, CacheEntry entry) {
77                                 
78                                 QueryProcessor qp = (QueryProcessor)provider;
79                                 
80                         WriteGraphImpl write = qp.getCore().getSession().getService(WriteGraphImpl.class);
81                                 
82                         ReadGraphImpl graph = write.newSync(entry);
83
84                                 try {
85
86                                         entry.setPending();
87                                     T result = request.perform(graph);
88                                     addOrSet(graph, result);
89
90                                 } catch (Throwable t) {
91
92                                         except(t);
93                     
94                 }
95                                 
96                         }
97
98                         @Override
99                         public void removeEntry(QueryProcessor processor) {
100                                 processor.readMap.remove(request);
101                         }
102
103                         @Override
104                         public int type() {
105                                 if(request instanceof ReadExt) {
106                                         return ((ReadExt)request).getType();
107                                 } else {
108                                         return RequestFlags.INVALIDATE;
109                                 }
110                         }
111                         
112                         @Override
113                         public String toString() {
114                                 if(request == null) return "DISCARDED";
115                                 else return request.toString() + statusOrException;
116                         }
117                 
118         };
119         
120     }
121     
122         public void performFromCache(ReadGraphImpl graph, Object provider,      Object procedure) {
123             
124         AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
125
126             if(isExcepted()) {
127
128             try {
129                 proc.exception(graph, (Throwable)getResult());
130             } catch (Throwable t) {
131                 t.printStackTrace();
132             }
133                 
134             } else {
135                 
136             try {
137                 proc.execute(graph, (T)getResult());
138             } catch (Throwable t) {
139                 t.printStackTrace();
140             }
141
142             }
143                 
144         }
145         
146         @Override
147         public String toString() {
148                 if(request == null) return "DISCARDED";
149                 else return request.toString() + " - " + statusOrException;
150         }
151         
152         public Object get(ReadGraphImpl graph, QueryProcessor processor, Object procedure) throws DatabaseException {
153                 if(procedure != null) performFromCache(graph, processor, procedure);
154                 checkAndThrow();
155                 return getResult();
156         }
157         
158         @Override
159         boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
160                 if(request instanceof ReadExt) {
161                         return ((ReadExt)request).isImmutable(graph);
162                 }
163                 return false;
164         }
165
166 }