]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Predicates.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Predicates.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.common.exception.DebugException;
15 import org.simantics.db.common.utils.Logger;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
18 import org.simantics.db.impl.procedure.InternalProcedure;
19 import org.simantics.db.procedure.ListenerBase;
20 import org.simantics.db.request.RequestFlags;
21
22 import gnu.trove.procedure.TIntProcedure;
23
24 final public class Predicates extends UnaryQuery<InternalProcedure<IntSet>> {
25
26         Predicates(final int r) {
27         super(r);
28     }
29
30         @Override
31         final public void removeEntry(QueryProcessor provider) {
32         provider.cache.remove(this);
33         }
34     
35     final static private void forAssertions(ReadGraphImpl graph, int r, Predicates entry, final IntSet set) throws DatabaseException {
36
37         QueryCache.runnerPrincipalTypes(graph, r, entry, null, new SyncIntProcedure() {
38             
39             @Override
40             public void run(ReadGraphImpl graph) throws DatabaseException {
41             }
42             
43             IntProcedure proc = new IntProcedure() {
44
45                 @Override
46                 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
47                         set.add(i);
48                 }
49
50                 @Override
51                 public void finished(ReadGraphImpl graph) throws DatabaseException {
52                     dec(graph);
53                 }
54                                 
55                                 @Override
56                                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
57                     if(DebugException.DEBUG) new DebugException(t).printStackTrace();
58                     }
59
60             }; 
61
62             @Override
63             public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
64
65                 inc();
66                 QueryCache.runnerAssertedPredicates(graph, type, entry, null, proc);
67                 
68             }
69             
70             @Override
71             public void finished(ReadGraphImpl graph) throws DatabaseException {
72                 dec(graph);       
73             }
74             
75         });
76         
77
78     }
79
80         @Override
81         public Object compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
82                 computeForEach(graph, id, this, procedure);
83                 return getResult();
84         }
85
86         public static void computeForEach(ReadGraphImpl graph, final int r, final Predicates entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
87         
88         IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null);
89                 
90                 IntSet result = new IntSet(graph.processor.querySupport);
91         forAssertions(graph, r, entry, result);
92         
93         if(result.isEmpty()) {
94                 
95                 if(entry != null) {
96                         entry.setResult(direct);
97                         entry.setReady();
98                 }
99                 
100                 procedure.execute(graph, direct);
101                 
102         } else {
103                 
104                 direct.forEach(new TIntProcedure() {
105                         @Override
106                         public boolean execute(int value) {
107                                 result.add(value);
108                                 return true;
109                         }
110                 });
111
112                 if(entry != null) {
113                         entry.setResult(result);
114                         entry.setReady();
115                 }
116                 
117                 procedure.execute(graph, result);
118                 
119         }
120                 
121         
122     }
123     
124     @Override
125     public String toString() {
126         return "Predicates[" + id + "]";
127     }
128
129     final public void finish(final ReadGraphImpl graph, QueryProcessor provider) {
130         
131         synchronized(this) {
132                 setReady();
133         }
134
135     }
136
137     @Override
138     public void clearResult(QuerySupport support) {
139         setResult(new IntSet(support));
140     }
141     
142     @Override
143     public Object performFromCache(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
144         
145         assert(isReady());
146
147         if(handleException(graph, procedure)) return EXCEPTED;
148         
149         IntSet result = getResult();
150         
151         procedure.execute(graph, result);
152         
153         return result;
154         
155     }
156     
157     @Override
158     public void recompute(ReadGraphImpl graph) throws DatabaseException {
159         
160         compute(graph, new InternalProcedure<IntSet>() {
161
162                         @Override
163                         public void exception(ReadGraphImpl graph, Throwable t) {
164                                 throw new Error("Error in recompute.", t);
165             }
166
167                         @Override
168                         public void execute(ReadGraphImpl graph, IntSet i) {
169                         }
170
171         });
172         
173     }
174     
175     @Override
176     public int type() {
177         return RequestFlags.IMMEDIATE_UPDATE;
178     }
179
180
181     @Override
182     boolean isImmutable(ReadGraphImpl graph) {
183         return graph.processor.isImmutable(id);
184     }
185     
186 }