]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedPredicates.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AssertedPredicates.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.RelationInfo;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.impl.procedure.IntProcedureAdapter;
18
19
20 final public class AssertedPredicates extends UnaryQuery<IntProcedure> {
21         
22     AssertedPredicates(final int r) {
23         super(r);
24     }
25
26     public static AssertedPredicates newInstance(final int r) {
27         return new AssertedPredicates(r);
28     }
29
30     @Override
31     final public void clearResult(QuerySupport support) {
32         setResult(new IntArray());
33     }
34     
35     @Override
36     final public void setReady() {
37         super.setReady();
38         IntArray v = (IntArray)getResult();
39         int size = v.size();
40         if(size == 0) setResult(IntArray.EMPTY);
41         else v.trim();
42     }
43     
44         @Override
45         final public void removeEntry(QueryProcessor provider) {
46             provider.cache.remove(this);
47         }
48         
49         void computeInheritedAssertions(ReadGraphImpl graph, int type, final IntProcedure proc) throws DatabaseException {
50                 
51                 QueryProcessor processor = graph.processor;
52
53                 QueryCache.runnerDirectObjects(graph, type, processor.getInherits(), this, null, new SyncIntProcedure() {
54
55             @Override
56             public void run(ReadGraphImpl graph) {
57             }
58
59             @Override
60             public void execute(ReadGraphImpl graph,int inh) throws DatabaseException {
61
62                 inc();
63                 
64                 QueryCache.runnerAssertedPredicates(graph, inh, AssertedPredicates.this, null, new IntProcedure() {
65
66                     @Override
67                     public void execute(ReadGraphImpl graph, int ass) {
68                         addOrSet(ass);
69                     }
70
71                     @Override
72                     public void finished(ReadGraphImpl graph) throws DatabaseException {
73                         dec(graph);
74                     }
75                                 
76                                 @Override
77                                 public void exception(ReadGraphImpl graph, Throwable t) {
78                     }
79                     
80                 });
81                 
82             }
83
84             @Override
85             public void finished(ReadGraphImpl graph) throws DatabaseException {
86                 dec(graph);
87             }
88             
89         });
90
91         }
92
93     //@Override
94     public Object compute(ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
95
96         QueryProcessor processor = graph.processor;
97         
98         computeInheritedAssertions(graph, id, proc);
99
100         QueryCache.runnerDirectObjects(graph, id, processor.getAsserts(), this, null, new IntProcedure() {
101
102             @Override
103             public void execute(ReadGraphImpl graph, final int ass) throws DatabaseException {
104                 
105                 QueryCache.runnerDirectObjects(graph, ass, processor.getHasPredicate(), AssertedPredicates.this, null, new IntProcedure() {
106
107                     @Override
108                     public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
109
110                         addOrSetHiding(graph, pred, AssertedPredicates.this);
111                         return;
112                         
113                     }
114
115                     @Override
116                     public void finished(ReadGraphImpl graph) {
117                     }
118                                 
119                                 @Override
120                                 public void exception(ReadGraphImpl graph, Throwable t) {
121 //                                      proc.exception(graph, t);
122                     }
123
124                 });
125                 
126             }
127
128             @Override
129             public void finished(ReadGraphImpl graph) {
130             }
131
132                         @Override
133                         public void exception(ReadGraphImpl graph, Throwable t) {
134 //                              proc.exception(graph, t);
135                         }
136             
137         });
138         
139         finish(graph, processor);
140
141         performFromCache(graph, proc);
142         
143         return getResult();
144         
145     }
146     
147     @Override
148     public String toString() {
149         return "AssertedPredicates[" + id + "]";
150     }
151     
152     final public void finish(ReadGraphImpl graph, QueryProcessor provider) {
153         
154         assert(!isReady());
155
156         synchronized(this) {
157                 setReady();
158         }
159
160     }
161
162     synchronized private void addOrSet(int add) {
163
164         assert(isPending());
165         
166         IntArray value = (IntArray)getResult(); 
167         value.add(add);
168         
169     }
170
171         synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) throws DatabaseException {
172
173         assert(isPending());
174         
175         IntArray value = (IntArray)getResult(); 
176                 RelationInfo ri = QueryCacheBase.resultRelationInfoQuery(graph, add, parent, null);
177                 if(ri.isFunctional) {
178                         // Replace existing functional predicate if found
179                         try {
180                                 IntSet supers = QueryCache.resultSuperRelations(graph, add, parent, null); 
181                         if(value.data == null) {
182                             if(value.sizeOrData != IntArray.NO_DATA) {
183                                 if(supers.contains(value.sizeOrData)) {
184                                         value.sizeOrData = add;
185                                         return;
186                                 }
187                             }
188                         } else {
189                             for(int i = 0;i < value.sizeOrData ; i++) {
190                                 if(supers.contains(value.data[i])) {
191                                         value.data[i] = add;
192                                         return;
193                                 }
194                             }
195                         }
196                         } catch (Throwable e) {
197                                 except(e);
198                                 return;
199                         }
200                 }
201                 
202             // No replacement - append
203         value.add(add);
204
205     }
206     
207     @Override
208     public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
209
210         assert(isReady());
211         
212         if(handleException(graph, procedure)) return EXCEPTED;
213         
214         final IntArray value = (IntArray)getResult();
215         if(value.data == null) {
216             if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
217         } else {
218             for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
219         }
220
221         procedure.finished(graph);
222         
223         return value;
224         
225     }
226     
227     @Override
228     public void recompute(ReadGraphImpl graph) throws DatabaseException {
229
230         compute(graph, new IntProcedureAdapter() {
231
232             @Override
233             public void finished(ReadGraphImpl graph) {
234             }
235
236             @Override
237             public void exception(ReadGraphImpl graph, Throwable t) {
238                 new Error("Error in recompute.", t).printStackTrace();
239             }
240
241         });
242         
243     }
244     
245 }