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