]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java
2c106476ef263cf36c1a23835d7bc5fbd24152e0
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DirectObjects.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 java.util.Collection;
15 import java.util.concurrent.Semaphore;
16
17 import org.simantics.db.common.exception.DebugException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.procedure.ListenerBase;
20 import org.simantics.db.request.RequestFlags;
21
22 final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
23
24         private DirectObjects(final int r1, final int r2) {
25                 super(r1, r2);
26         }
27
28         @Override
29         public int type() {
30                 return RequestFlags.INVALIDATE;
31         }
32         
33     @Override
34     public void clearResult(QuerySupport support) {
35         setResult(INVALID_RESULT);
36     }
37
38         final static DirectObjects entry(final QueryProcessor provider, final int r1, final int r2) {
39
40                 return (DirectObjects)provider.directObjectsMap.get(id(r1,r2));
41
42         }
43         
44         final static Collection<DirectObjects> entries(final QueryProcessor processor, final int r1) {
45                 DoubleKeyQueryHashMap<IntProcedure> hash = processor.directObjectsMap;
46                 return hash.values(r1);
47         }
48
49         final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
50
51         QueryProcessor processor = graph.processor;
52                 
53                 DirectObjects entry = (DirectObjects)processor.directObjectsMap.get(id(r1,r2));
54                 if(entry == null) {
55                         
56                 entry = new DirectObjects(r1, r2);
57                 entry.setPending();
58                 entry.clearResult(processor.querySupport);
59                 entry.putEntry(processor);
60                 
61                         processor.performForEach(graph, entry, parent, listener, procedure);
62                         
63                 } else {
64                         
65             if(entry.isPending()) {
66                 synchronized(entry) {
67                     if(entry.isPending()) {
68                         processor.registerDependencies(graph, entry, parent, listener, procedure, false);
69                                                 entry.computeForEach(graph, processor, procedure, true);
70                         return;
71                     }
72                 }
73             }
74                 
75                         processor.performForEach(graph, entry, parent, listener, procedure);
76                         
77                 }
78
79         }
80
81         final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
82
83         assert(r1 != 0);
84         assert(r2 != 0);
85         
86         if(parent == null && listener == null) {
87                 DirectObjects.computeForEach(graph, r1, r2, null, procedure);
88         } else {
89                 runner(graph, r1, r2, parent, listener, procedure);
90         }
91
92         }
93
94         @Override
95         public BinaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
96                 return provider.directObjectsMap.get(id);
97         }
98
99         @Override
100         public void putEntry(QueryProcessor provider) {
101                 provider.directObjectsMap.put(id, this);
102         }
103
104         @Override
105         final public void removeEntry(QueryProcessor provider) {
106                 provider.directObjectsMap.remove(id);
107         }
108
109         @Override
110         public void computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure procedure, final boolean store) {
111                 computeForEach(graph, r1(), r2(), this, procedure);
112         }
113
114         static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) {
115
116                 QueryProcessor processor = graph.processor;
117                 
118                 processor.querySupport.ensureLoaded(graph, r1, r2);
119
120                 processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
121
122                         @Override
123                         public void execute(ReadGraphImpl graph, int i) {
124                                 procedure.execute(graph, i);
125                         }
126
127                         @Override
128                         public void finished(ReadGraphImpl graph) {
129                         }
130
131                         @Override
132                         public void exception(ReadGraphImpl graph, Throwable t) {
133                                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
134                         }
135
136                 });
137
138                 if(entry != null) entry.finish(graph, processor);
139                 procedure.finished(graph);
140
141         }
142
143         @Override
144         public String toString() {
145                 return "DirectObjects[" + r1() + " - " + r2() + "]";
146         }
147
148         @Override
149         public void setReady() {
150                 statusOrException = READY;
151         }
152         
153         final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
154                 setReady();
155         }
156
157         @Override
158         public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
159
160                 assert(isReady());
161                 computeForEach(graph, provider, procedure, false);
162
163         }
164
165         @Override
166         public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
167
168         final Semaphore s = new Semaphore(0);
169                 
170                 computeForEach(graph, provider, new IntProcedure() {
171
172                         @Override
173                         public void finished(ReadGraphImpl graph) {
174                                 s.release();
175                         }
176
177                         @Override
178                         public void exception(ReadGraphImpl graph, Throwable t) {
179                                 throw new Error("Error in recompute.", t);
180                         }
181
182                         @Override
183                         public void execute(ReadGraphImpl graphd, int i) {
184                         }
185
186                 }, true);
187
188         while(!s.tryAcquire()) {
189                 provider.resume(graph);
190         }
191
192         }
193
194     @Override
195     boolean isImmutable(ReadGraphImpl graph) {
196         return graph.processor.isImmutable(r1());
197     }
198         
199 }
200