]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/RelationInfoQuery.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / RelationInfoQuery.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.InternalProcedure;
18 import org.simantics.db.request.RequestFlags;
19
20 final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<RelationInfo>> {
21
22         RelationInfoQuery(final int resource) {
23         super(resource);
24     }
25     
26         @Override
27         final public void removeEntry(QueryProcessor provider) {
28                 provider.cache.remove(this);
29         }
30
31         private static void computeAssertions(ReadGraphImpl graph, int r, final boolean isFinal, final boolean isFunctional, RelationInfoQuery entry, final InternalProcedure<RelationInfo> proc) throws DatabaseException {
32
33                 QueryProcessor processor = graph.processor;
34                 
35             final int isUsedInAssertion = processor.getHasPredicateInverse();
36         assert(isUsedInAssertion != 0);
37                 
38         QueryCache.runnerDirectObjects(graph, r, isUsedInAssertion, entry, null, new IntProcedure() {
39
40                 boolean done = false;
41                         
42                         @Override
43                         public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
44                                 done = true;
45                                 RelationInfo result = new RelationInfo(r, isFunctional, isFinal, true);
46                                 if(entry != null) entry.setResult(result);
47                                 proc.execute(graph, result);
48                         }
49
50                         @Override
51                         public void finished(ReadGraphImpl graph) throws DatabaseException {
52                                 if(!done) {
53                                         done = true;
54                                         RelationInfo result = new RelationInfo(r, isFunctional, isFinal, false);
55                                         if(entry != null) entry.setResult(result);
56                                         proc.execute(graph, result);
57                                 }
58                         }
59
60                         @Override
61                         public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
62                                 if(!done) {
63                                         done = true;
64                                         DatabaseException e = new DatabaseException("Internal error in RelationInfoQuery");
65                                         if(entry != null) entry.except(e);
66                                         proc.exception(graph, e);
67                                 }
68                         }
69                         
70                 });
71                 
72         }
73         
74         public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
75                 
76                 QueryProcessor provider = graph.processor;
77                 
78             final int superRelationOf = provider.getSuperrelationOf();
79         assert(superRelationOf != 0);
80         
81         IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null);
82         IntSet types = QueryCache.resultTypes(graph, r, entry, null);
83         
84         computeAssertions(graph, r, !direct.contains(superRelationOf), types.contains(graph.processor.getFunctionalRelation()), entry, procedure);
85         
86         }
87
88         @Override
89         public Object compute(ReadGraphImpl graph, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
90                 computeForEach(graph, id, this, procedure);
91                 return getResult();
92         }
93     
94     @Override
95     public String toString() {
96         return "RelationInfoQuery[" + id + "]";
97     }
98
99 //    public void addOrSet(ReadGraphImpl graph, final RelationInfo result, final QueryProcessor provider) {
100 //        
101 //      assert(isPending());
102 //      
103 //      synchronized(this) {
104 //
105 //          setResult(result);
106 //          setReady();
107 //              
108 //      }
109 //        
110 //    }
111     
112     @Override
113     public void setResult(Object result) {
114         super.setResult(result);
115         if(!(result instanceof RelationInfo) && !(result == NO_RESULT))
116                 System.err.println("foo");
117         setReady();
118     }
119
120     @Override
121     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
122
123         assert(isReady());
124         
125         if(handleException(graph, procedure)) return EXCEPTED;
126         
127         RelationInfo result = getResult();
128         
129         procedure.execute(graph, result);
130         
131         return result;
132
133     }
134     
135     @Override
136     public int type() {
137         return RequestFlags.IMMEDIATE_UPDATE;
138     }
139
140         @Override
141         public void recompute(ReadGraphImpl graph) throws DatabaseException {
142                 
143         compute(graph, new InternalProcedure<RelationInfo>() {
144
145             @Override
146             public void execute(ReadGraphImpl graph, RelationInfo result) {
147             }
148             
149             @Override
150             public void exception(ReadGraphImpl graph, Throwable t) {
151                 throw new Error("Error in recompute.", t);
152             }
153
154         });
155                 
156         }
157     
158 }