1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 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.db.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.request.RequestFlags;
18 final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> implements IntProcedure {
20 DirectObjects(final int r1, final int r2) {
26 return RequestFlags.INVALIDATE;
30 public final void removeEntry(QueryProcessor provider) {
31 provider.cache.remove(this);
35 public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
36 computeForEach(graph, r1(), r2(), this, procedure);
40 public static void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry,
41 final IntProcedure procedure_) throws DatabaseException {
43 IntProcedure procedure = entry != null ? entry : procedure_;
45 QueryProcessor processor = graph.processor;
47 processor.querySupport.ensureLoaded(graph, r1, r2);
49 processor.querySupport.getObjects(graph, r1, r2, procedure);
51 procedure.finished(graph);
54 entry.performFromCache(graph, procedure_);
59 public String toString() {
60 return "DirectObjects[" + r1() + " - " + r2() + "]";
64 public void setReady() {
65 statusOrException = READY;
69 public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
73 if (handleException(graph, procedure))
76 final IntArray value = (IntArray) getResult();
77 if (value.data == null) {
78 if (value.sizeOrData != IntArray.NO_DATA)
79 procedure.execute(graph, value.sizeOrData);
81 for (int i = 0; i < value.sizeOrData; i++)
82 procedure.execute(graph, value.data[i]);
85 procedure.finished(graph);
92 public void recompute(ReadGraphImpl graph) throws DatabaseException {
94 compute(graph, new IntProcedure() {
97 public void finished(ReadGraphImpl graph) {
101 public void exception(ReadGraphImpl graph, Throwable t) {
102 throw new Error("Error in recompute.", t);
106 public void execute(ReadGraphImpl graphd, int i) {
114 boolean isImmutable(ReadGraphImpl graph) {
115 return graph.processor.isImmutable(r1());
119 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
120 IntArray value = (IntArray) getResult();
125 public void finished(ReadGraphImpl graph) throws DatabaseException {
130 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {