]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DirectObjects.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 org.simantics.db.common.exception.DebugException;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.procedure.ListenerBase;
18 import org.simantics.db.request.RequestFlags;
19
20 final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
21
22         DirectObjects(final int r1, final int r2) {
23                 super(r1, r2);
24         }
25
26         @Override
27         public int type() {
28                 return RequestFlags.INVALIDATE;
29         }
30         
31 //    @Override
32 //    public void clearResult(QuerySupport support) {
33 //      setResult(INVALID_RESULT);
34 //    }
35
36 //      final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
37 //
38 //        assert(r1 != 0);
39 //        assert(r2 != 0);
40 //        
41 //        if(parent == null && listener == null) {
42 //              DirectObjects.computeForEach(graph, r1, r2, null, procedure);
43 //        } else {
44 //              QueryCache.runnerDirectObjects(graph, r1, r2, parent, listener, procedure);
45 //        }
46 //
47 //      }
48
49         @Override
50         final public void removeEntry(QueryProcessor provider) {
51                 provider.cache.remove(this);
52         }
53
54         @Override
55         public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
56                 computeForEach(graph, r1(), r2(), this, procedure);
57                 return getResult();
58         }
59
60         static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) throws DatabaseException {
61
62                 QueryProcessor processor = graph.processor;
63                 
64                 processor.querySupport.ensureLoaded(graph, r1, r2);
65
66                 processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
67
68                         @Override
69                         public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
70                                 procedure.execute(graph, i);
71                                 if(entry != null) entry.add(i);
72                         }
73
74                         @Override
75                         public void finished(ReadGraphImpl graph) {
76                         }
77
78                         @Override
79                         public void exception(ReadGraphImpl graph, Throwable t) {
80                                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
81                         }
82
83                 });
84
85                 if(entry != null) entry.finish(graph, processor);
86                 procedure.finished(graph);
87
88         }
89
90         @Override
91         public String toString() {
92                 return "DirectObjects[" + r1() + " - " + r2() + "]";
93         }
94
95         @Override
96         public void setReady() {
97                 statusOrException = READY;
98         }
99         
100         final public void add(int add) {
101
102                 assert(isPending());
103
104                 IntArray value = (IntArray)getResult();
105                 value.add(add);
106
107         }
108
109         final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
110                 setReady();
111         }
112
113         @Override
114         public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
115
116                 assert(isReady());
117
118                 if(handleException(graph, procedure)) return getResult();
119
120                 final IntArray value = (IntArray)getResult();
121                 if(value.data == null) {
122                         if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
123                 } else {
124                         for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
125                 }
126
127                 procedure.finished(graph);
128                 
129                 return value;
130
131         }
132
133         @Override
134         public void recompute(ReadGraphImpl graph) throws DatabaseException {
135                 
136                 compute(graph, new IntProcedure() {
137
138                         @Override
139                         public void finished(ReadGraphImpl graph) {
140                         }
141
142                         @Override
143                         public void exception(ReadGraphImpl graph, Throwable t) {
144                                 throw new Error("Error in recompute.", t);
145                         }
146
147                         @Override
148                         public void execute(ReadGraphImpl graphd, int i) {
149                         }
150
151                 });
152
153         }
154
155     @Override
156     boolean isImmutable(ReadGraphImpl graph) {
157         return graph.processor.isImmutable(r1());
158     }
159         
160 }
161