]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/OrderedSet.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / OrderedSet.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import java.util.concurrent.atomic.AtomicInteger;
15
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;
22
23 final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
24         
25         public OrderedSet(final int r) {
26         super(r);
27     }
28
29         @Override
30         final public void removeEntry(QueryProcessor provider) {
31         provider.cache.remove(this);
32         }
33
34         private static int nextElement(ReadGraphImpl graph, int current, int orderedSet, OrderedSet entry, final IntProcedure procedure) throws DatabaseException {
35             
36                 QueryProcessor processor = graph.processor;
37                 
38                 processor.querySupport.ensureLoaded(graph, current);
39                 
40                 AtomicInteger res = new AtomicInteger(0);
41                 
42                 boolean found = processor.querySupport.getObjects(graph, current, orderedSet, new IntProcedure() {
43
44                         @Override
45                         public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
46                                 if(i != orderedSet) {
47                                         if(entry != null) entry.addOrSet(i);
48                                         procedure.execute(graph, i);
49                                 }
50                                 res.set(i);
51                         }
52
53                         @Override
54                         public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
55                                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
56                                 procedure.exception(graph, t);
57                         }
58
59                         @Override
60                         public void finished(ReadGraphImpl graph) {
61                         }
62
63                 });
64
65                 if(res.get() == orderedSet) {
66                         if(entry != null) entry.finish(graph, processor);
67                         procedure.finished(graph);
68                 }
69                 
70                 return res.get();
71
72         }
73         
74     @Override
75     public void clearResult(QuerySupport support) {
76         setResult(new IntArray());
77     }
78         
79     @Override
80     public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
81         computeForEach(graph, id, this, procedure);
82         return getResult();
83     }
84
85     static void computeForEach(ReadGraphImpl graph, int orderedSet, final OrderedSet entry, final IntProcedure procedure) throws DatabaseException {
86
87         int current = nextElement(graph, orderedSet, orderedSet, entry, procedure);
88         while(current != orderedSet) {
89                 current = nextElement(graph, current, orderedSet, entry, procedure);
90         }
91
92     }
93     
94     @Override
95     public String toString() {
96         return "OrderedSet[" + id + "]";
97     }
98
99     final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
100         
101         assert(isPending());
102
103         synchronized(this) {
104                 setReady();
105         }
106         
107     }
108
109     final public void addOrSet(int add) {
110         
111         assert(isPending());
112         
113         IntArray value = (IntArray)getResult();
114         value.add(add);
115         
116     }
117
118     @Override
119     public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
120         
121         assert(isReady());
122
123         if(handleException(graph, procedure)) return EXCEPTED;
124         
125         final IntArray value = (IntArray)getResult();
126         if(value.data == null) {
127             if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
128         } else {
129             for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
130         }
131
132         procedure.finished(graph);
133         
134         return value;
135         
136     }
137     
138     @Override
139     public void recompute(ReadGraphImpl graph) throws DatabaseException {
140         
141         compute(graph, new IntProcedureAdapter() {
142
143             @Override
144             public void finished(ReadGraphImpl graph) {
145             }
146                         
147                         @Override
148                         public void exception(ReadGraphImpl graph, Throwable t) {
149                                 throw new Error("Error in recompute.", t);
150             }
151
152         });
153         
154     }
155     
156 }