]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ChildMap.java
1 /*******************************************************************************
2  * Copyright (c) 2018 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  *     Semantum Oy - 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 public final class ChildMap extends UnaryQueryP<ObjectResourceIdMap<String>> {
25
26     ChildMap(final int r) {
27         super(r);
28     }
29
30     @Override
31     public final void removeEntry(QueryProcessor provider) {
32         provider.cache.remove(this);
33     }
34
35     @Override
36     public void compute(ReadGraphImpl graph, final InternalProcedure<ObjectResourceIdMap<String>> procedure)
37             throws DatabaseException {
38         computeForEach(graph, id, this, procedure);
39     }
40
41     public static void computeForEach(ReadGraphImpl graph, final int root, final ChildMap entry,
42             final InternalProcedure<ObjectResourceIdMap<String>> procedure_) throws DatabaseException {
43
44         InternalProcedure<ObjectResourceIdMap<String>> procedure = entry != null ? entry : procedure_;
45
46         computeForEach2(graph, root, entry, procedure);
47
48         if (entry != null)
49             entry.performFromCache(graph, procedure_);
50
51     }
52
53     public static void computeForEach2(ReadGraphImpl graph, final int root, final ChildMap parent,
54             final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
55
56         if (root == 0) {
57             procedure.execute(graph, null);
58             return;
59         }
60
61         QueryProcessor processor = graph.processor;
62
63         final int consistsOf = processor.getConsistsOf();
64         final int hasName = processor.getHasName();
65
66         ObjectResourceIdMap<String> result = graph.getService(CollectionSupport.class)
67                 .createObjectResourceMap(String.class);
68
69         QueryCache.runnerObjects(graph, root, consistsOf, parent, null, new SyncIntProcedure() {
70
71             @Override
72             public void run(ReadGraphImpl graph) throws DatabaseException {
73                 procedure.execute(graph, result);
74             }
75
76             @Override
77             public void finished(ReadGraphImpl graph) throws DatabaseException {
78                 dec(graph);
79             }
80
81             @Override
82             public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
83
84                 inc();
85
86                 QueryCache.runnerObjects(graph, obj, hasName, parent, null, new IntProcedure() {
87
88                     @Override
89                     public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
90
91                         inc();
92
93                         QueryCache.runnerValueQuery(graph, i, parent, null, new InternalProcedure<byte[]>() {
94
95                             @Override
96                             public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException {
97
98                                 if (value != null) {
99
100                                     try {
101
102                                         Binding b = WriteBindings.STRING;
103                                         Serializer serializer = b.serializer();
104                                         final String part = (String) serializer.deserialize(value);
105                                         result.putId(part, obj);
106
107                                     } catch (Throwable e) {
108                                         if (DebugException.DEBUG)
109                                             new DebugException(e).printStackTrace();
110                                     }
111
112                                 }
113
114                                 dec(graph);
115
116                             }
117
118                             @Override
119                             public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
120                                 dec(graph);
121                             }
122
123                         });
124
125                     }
126
127                     @Override
128                     public void finished(ReadGraphImpl graph) throws DatabaseException {
129                         dec(graph);
130                     }
131
132                     @Override
133                     public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
134                         dec(graph);
135                     }
136
137                 });
138
139             }
140
141         });
142
143     }
144
145     @Override
146     public String toString() {
147         return "ChildMap[" + id + "]";
148     }
149
150 }