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