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