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 java.util.concurrent.atomic.AtomicInteger;
16 import org.simantics.db.common.exception.DebugException;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.impl.procedure.IntProcedureAdapter;
20 import org.simantics.db.impl.procedure.InternalProcedure;
21 import org.simantics.db.procedure.ListenerBase;
23 final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
25 public OrderedSet(final int r) {
30 final public void removeEntry(QueryProcessor provider) {
31 provider.cache.remove(this);
34 private static int nextElement(ReadGraphImpl graph, int current, int orderedSet, OrderedSet entry, final IntProcedure procedure) throws DatabaseException {
36 QueryProcessor processor = graph.processor;
38 processor.querySupport.ensureLoaded(graph, current);
40 AtomicInteger res = new AtomicInteger(0);
42 boolean found = processor.querySupport.getObjects(graph, current, orderedSet, new IntProcedure() {
45 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
47 if(entry != null) entry.addOrSet(i);
48 procedure.execute(graph, i);
54 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
55 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
56 procedure.exception(graph, t);
60 public void finished(ReadGraphImpl graph) {
65 if(res.get() == orderedSet) {
66 if(entry != null) entry.finish(graph, processor);
67 procedure.finished(graph);
75 public void clearResult(QuerySupport support) {
76 setResult(new IntArray());
80 public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
81 computeForEach(graph, id, this, procedure);
85 static void computeForEach(ReadGraphImpl graph, int orderedSet, final OrderedSet entry, final IntProcedure procedure) throws DatabaseException {
87 int current = nextElement(graph, orderedSet, orderedSet, entry, procedure);
88 while(current != orderedSet) {
89 current = nextElement(graph, current, orderedSet, entry, procedure);
95 public String toString() {
96 return "OrderedSet[" + id + "]";
99 final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
109 final public void addOrSet(int add) {
113 IntArray value = (IntArray)getResult();
119 public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
123 if(handleException(graph, procedure)) return EXCEPTED;
125 final IntArray value = (IntArray)getResult();
126 if(value.data == null) {
127 if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
129 for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
132 procedure.finished(graph);
139 public void recompute(ReadGraphImpl graph) throws DatabaseException {
141 compute(graph, new IntProcedureAdapter() {
144 public void finished(ReadGraphImpl graph) {
148 public void exception(ReadGraphImpl graph, Throwable t) {
149 throw new Error("Error in recompute.", t);