1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.query;
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;
24 final public class ChildMap extends UnaryQuery<InternalProcedure<ObjectResourceIdMap<String>>> {
26 ChildMap(final int r) {
31 final public void removeEntry(QueryProcessor provider) {
32 provider.cache.remove(this);
36 public Object compute(ReadGraphImpl graph, final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
37 computeForEach(graph, id, this, procedure);
41 public static void computeForEach(ReadGraphImpl graph, final int root, final ChildMap entry, final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
45 entry.add2(graph, null);
46 procedure.execute(graph, null);
50 QueryProcessor processor = graph.processor;
52 final int consistsOf = processor.getConsistsOf();
53 final int hasName = processor.getHasName();
55 ObjectResourceIdMap<String> result = graph.getService(CollectionSupport.class).createObjectResourceMap(String.class);
57 QueryCache.runnerObjects(graph, root, consistsOf, entry, null, new SyncIntProcedure() {
60 public void run(ReadGraphImpl graph) throws DatabaseException {
62 if(entry != null) entry.add2(graph, result);
63 procedure.execute(graph, result);
68 public void finished(ReadGraphImpl graph) throws DatabaseException {
73 public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
77 QueryCache.runnerObjects(graph, obj, hasName, entry, null, new IntProcedure() {
80 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
84 QueryCache.runnerValueQuery(graph, i, entry, null, new InternalProcedure<byte[]>() {
87 public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException {
93 Binding b = WriteBindings.STRING;
94 Serializer serializer = b.serializer();
95 final String part = (String)serializer.deserialize(value);
96 result.putId(part, obj);
98 } catch (Throwable e) {
99 if(DebugException.DEBUG) new DebugException(e).printStackTrace();
109 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
110 if(entry != null) entry.except(t);
119 public void finished(ReadGraphImpl graph) throws DatabaseException {
124 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
125 if(entry != null) entry.except(t);
138 public String toString() {
139 return "ChildMap[" + id + "]";
142 private void add2(ReadGraphImpl graph, ObjectResourceIdMap<String> result) {
156 public Object performFromCache(ReadGraphImpl graph, InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
160 if(handleException(graph, procedure)) return (Throwable)statusOrException;
162 ObjectResourceIdMap<String> result = (ObjectResourceIdMap<String>)getResult();
164 procedure.execute(graph, result);
171 public void recompute(ReadGraphImpl graph) throws DatabaseException {
173 compute(graph, new InternalProcedure<ObjectResourceIdMap<String>>() {
176 public void execute(ReadGraphImpl graph, ObjectResourceIdMap<String> result) {
180 public void exception(ReadGraphImpl graph, Throwable t) {
181 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
182 throw new Error("Error in recompute.", t);