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 UnaryQueryP<ObjectResourceIdMap<String>> {
26 ChildMap(final int r) {
31 final public void removeEntry(QueryProcessor provider) {
32 provider.cache.remove(this);
36 public void compute(ReadGraphImpl graph, final InternalProcedure<ObjectResourceIdMap<String>> procedure)
37 throws DatabaseException {
38 computeForEach(graph, id, this, procedure);
41 public static void computeForEach(ReadGraphImpl graph, final int root, final ChildMap entry,
42 final InternalProcedure<ObjectResourceIdMap<String>> procedure_) throws DatabaseException {
44 InternalProcedure<ObjectResourceIdMap<String>> procedure = entry != null ? entry : procedure_;
46 computeForEach2(graph, root, entry, procedure);
49 entry.performFromCache(graph, procedure_);
53 public static void computeForEach2(ReadGraphImpl graph, final int root, final ChildMap parent,
54 final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
57 procedure.execute(graph, null);
61 QueryProcessor processor = graph.processor;
63 final int consistsOf = processor.getConsistsOf();
64 final int hasName = processor.getHasName();
66 ObjectResourceIdMap<String> result = graph.getService(CollectionSupport.class)
67 .createObjectResourceMap(String.class);
69 QueryCache.runnerObjects(graph, root, consistsOf, parent, null, new SyncIntProcedure() {
72 public void run(ReadGraphImpl graph) throws DatabaseException {
73 procedure.execute(graph, result);
77 public void finished(ReadGraphImpl graph) throws DatabaseException {
82 public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
86 QueryCache.runnerObjects(graph, obj, hasName, parent, null, new IntProcedure() {
89 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
93 QueryCache.runnerValueQuery(graph, i, parent, null, new InternalProcedure<byte[]>() {
96 public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException {
102 Binding b = WriteBindings.STRING;
103 Serializer serializer = b.serializer();
104 final String part = (String) serializer.deserialize(value);
105 result.putId(part, obj);
107 } catch (Throwable e) {
108 if (DebugException.DEBUG)
109 new DebugException(e).printStackTrace();
119 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
128 public void finished(ReadGraphImpl graph) throws DatabaseException {
133 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
146 public String toString() {
147 return "ChildMap[" + id + "]";