]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ChildMap.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.databoard.binding.Binding;
15 import org.simantics.databoard.serialization.Serializer;
16 import org.simantics.db.ObjectResourceIdMap;
17 import org.simantics.db.common.WriteBindings;
18 import org.simantics.db.common.exception.DebugException;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.impl.procedure.InternalProcedure;
22 import org.simantics.db.service.CollectionSupport;
23
24 final public class ChildMap extends UnaryQuery<InternalProcedure<ObjectResourceIdMap<String>>> {
25         
26     ChildMap(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     @Override
36         public Object compute(ReadGraphImpl graph, final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
37                 computeForEach(graph, id, this, procedure);
38                 return getResult();
39     }
40
41         public static void computeForEach(ReadGraphImpl graph, final int root, final ChildMap entry, final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
42                 
43                 if(root == 0) {
44                         if(entry != null)
45                                 entry.add2(graph, null);
46             procedure.execute(graph, null);
47             return;
48                 }
49
50                 QueryProcessor processor = graph.processor;
51                 
52                 final int consistsOf = processor.getConsistsOf();
53                 final int hasName = processor.getHasName();
54         
55                 ObjectResourceIdMap<String> result = graph.getService(CollectionSupport.class).createObjectResourceMap(String.class);
56         
57         QueryCache.runnerObjects(graph, root, consistsOf, entry, null, new SyncIntProcedure() {
58                 
59                 @Override
60                         public void run(ReadGraphImpl graph) throws DatabaseException {
61                         
62                         if(entry != null) entry.add2(graph, result);
63                         procedure.execute(graph, result);
64                                 
65                         }
66
67                         @Override
68                         public void finished(ReadGraphImpl graph) throws DatabaseException {
69                                 dec(graph);
70                         }
71
72                 @Override
73                 public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
74
75                         inc();
76                         
77                         QueryCache.runnerObjects(graph, obj, hasName, entry, null, new IntProcedure() {
78                         
79                         @Override
80                         public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
81
82                                 inc();
83
84                                 QueryCache.runnerValueQuery(graph, i, entry, null, new InternalProcedure<byte[]>() {
85                                         
86                                         @Override
87                                         public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException {
88                                                 
89                                                 if(value != null) {
90
91                                         try {
92
93                                                 Binding b = WriteBindings.STRING;
94                                             Serializer serializer = b.serializer();
95                                             final String part = (String)serializer.deserialize(value);
96                                             result.putId(part, obj);
97                                             
98                                                 } catch (Throwable e) {
99                                             if(DebugException.DEBUG) new DebugException(e).printStackTrace();
100                                         }
101                                                 
102                                                 }
103                                                 
104                                         dec(graph);
105                                         
106                                         }
107                                         
108                                         @Override
109                                         public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
110                                                         if(entry != null) entry.except(t);
111                                                 dec(graph);
112                             }
113
114                                 });
115                                 
116                         }
117                         
118                         @Override
119                         public void finished(ReadGraphImpl graph) throws DatabaseException {
120                                 dec(graph);
121                         }
122                                 
123                                 @Override
124                                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
125                                         if(entry != null) entry.except(t);
126                         dec(graph);
127                     }
128
129                 });
130
131                 }
132                 
133         });
134         
135     }
136
137     @Override
138     public String toString() {
139         return "ChildMap[" + id + "]";
140     }
141
142     private void add2(ReadGraphImpl graph, ObjectResourceIdMap<String> result) {
143         
144         assert(isPending());
145
146         synchronized(this) {
147
148             setResult(result);
149                 setReady();
150         
151         }
152         
153     }
154     
155     @Override
156     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
157         
158         assert(isReady());
159         
160         if(handleException(graph, procedure)) return (Throwable)statusOrException;
161         
162         ObjectResourceIdMap<String> result = (ObjectResourceIdMap<String>)getResult();
163         
164         procedure.execute(graph, result);
165         
166         return result;
167         
168     }
169     
170     @Override
171     public void recompute(ReadGraphImpl graph) throws DatabaseException {
172         
173         compute(graph, new InternalProcedure<ObjectResourceIdMap<String>>() {
174
175             @Override
176             public void execute(ReadGraphImpl graph, ObjectResourceIdMap<String> result) {
177             }
178                         
179                         @Override
180                         public void exception(ReadGraphImpl graph, Throwable t) {
181                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
182                                 throw new Error("Error in recompute.", t);
183             }
184
185         });
186         
187     }
188     
189 }
190