]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectPredicates.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DirectPredicates.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.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.impl.procedure.InternalProcedure;
18
19 final public class DirectPredicates extends CollectionUnaryQuery<InternalProcedure<IntSet>> {
20
21         DirectPredicates(final int resource) {
22                 super(resource);
23         }
24         
25         @Override
26         public void clearResult(QuerySupport support) {
27                 // The cached result is never used
28                 setResult(INVALID_RESULT);
29         }
30         
31         @Override
32         final public void removeEntry(QueryProcessor provider) {
33                 provider.cache.remove(this);
34         }
35
36         @Override
37         public Object compute(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
38             return computeForEach(graph, id, this, procedure);
39         }
40         
41         public static Object computeForEach(ReadGraphImpl graph, int id, final DirectPredicates entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
42
43                 graph.processor.querySupport.ensureLoaded(graph, id);
44                 
45                 final IntSet list = new IntSet(graph.processor.querySupport);
46
47                 graph.processor.querySupport.getPredicates(graph, id, new IntProcedure() {
48
49                         @Override
50                         public void execute(ReadGraphImpl graph, int i) {
51                                 list.add(i);
52                         }
53
54                         @Override
55                         public void finished(ReadGraphImpl graph) {
56                         }
57
58                         @Override
59                         public void exception(ReadGraphImpl graph, Throwable t) {
60                                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
61                         }
62
63                 });
64
65                 if(entry != null) {
66                     entry.setResult(list);
67                     entry.setReady();
68                 }
69                 
70                 procedure.execute(graph, list);
71                 
72                 return list;
73
74         }
75
76         @Override
77         public String toString() {
78                 return "DirectPredicates[" + id + "]";
79         }
80
81         @Override
82         public void setReady() {
83                 statusOrException = READY;
84         }
85
86         @Override
87         public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
88
89                 assert(isReady());
90
91         if(handleException(graph, procedure)) return EXCEPTED;
92         
93         IntSet result = getResult();
94         
95         procedure.execute(graph, result);
96         
97         return result;
98
99         }
100
101         @Override
102         public void recompute(ReadGraphImpl graph) throws DatabaseException {
103
104                 compute(graph, new InternalProcedure<IntSet>() {
105
106                         @Override
107                         public void execute(ReadGraphImpl graph, IntSet set) {
108                         }
109
110                         @Override
111                         public void exception(ReadGraphImpl graph, Throwable t) {
112                                 new Error("Error in recompute.", t).printStackTrace();
113                         }
114
115                 });
116
117         }
118
119
120     @Override
121     boolean isImmutable(ReadGraphImpl graph) {
122         return graph.processor.isImmutable(id);
123     }
124
125 }