KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > datasource > lib > TCWrapper


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2004 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23 package org.objectweb.medor.datasource.lib;
24
25 import org.objectweb.medor.api.EvaluationException;
26 import org.objectweb.medor.api.MedorException;
27 import org.objectweb.medor.api.TupleStructure;
28 import org.objectweb.medor.datasource.api.Wrapper;
29 import org.objectweb.medor.expression.api.ParameterOperand;
30 import org.objectweb.medor.expression.api.Expression;
31 import org.objectweb.medor.expression.api.VariableOperand;
32 import org.objectweb.medor.expression.api.Operand;
33 import org.objectweb.medor.expression.api.ExpressionException;
34 import org.objectweb.medor.expression.lib.BasicVariableOperand;
35 import org.objectweb.medor.query.api.QueryLeaf;
36 import org.objectweb.medor.query.api.TCQueryLeaf;
37 import org.objectweb.medor.tuple.api.Tuple;
38 import org.objectweb.medor.tuple.api.TupleCollection;
39 import org.objectweb.medor.tuple.lib.MemoryTuple;
40 import org.objectweb.medor.eval.api.EvaluationMetaData;
41
42 public class TCWrapper implements Wrapper {
43
44     public TupleCollection fetchData(QueryLeaf ql,
45                                      ParameterOperand[] parameters,
46                                      Object JavaDoc conn,
47                                      EvaluationMetaData evalmd)
48             throws EvaluationException {
49         //TODO take evaluation meta data into account
50
return new FilteredTC((TCQueryLeaf) ql, parameters);
51     }
52
53     class FilteredTC implements TupleCollection {
54
55         private TCQueryLeaf tcQueryLeaf;
56         private Expression filter;
57         private TupleCollection subTupleCollection;
58         private ParameterOperand[] parameters;
59         private Tuple tupleBuffer1,tupleBuffer2;
60         private VariableOperand[] operandBuffer1,operandBuffer2;
61         private int cursor;
62         private boolean empty, isTheLast = false, isBuffer2Free = false;
63         Operand filterResult;
64         private boolean closed = false;
65
66         public FilteredTC(TCQueryLeaf tcQueryLeaf, ParameterOperand[] parameters)
67                 throws EvaluationException {
68             this.tcQueryLeaf = tcQueryLeaf;
69             this.parameters = parameters;
70             this.filter = tcQueryLeaf.getQueryFilter();
71             try {
72                 this.filterResult = filter.compileExpression();
73             }
74             catch (ExpressionException e) {
75                 throw new EvaluationException(e);
76             }
77
78             // Init the two buffers
79
try {
80                 operandBuffer1 = new BasicVariableOperand[tcQueryLeaf.getTupleStructure().getSize()];
81                 operandBuffer2 = new BasicVariableOperand[tcQueryLeaf.getTupleStructure().getSize()];
82                 for (int cpt = 0; (cpt < tcQueryLeaf.getTupleStructure().getSize()); cpt++) {
83                     operandBuffer1[cpt] = new BasicVariableOperand(tcQueryLeaf.getTupleStructure().getField(cpt + 1).getType());
84                     operandBuffer2[cpt] = new BasicVariableOperand(tcQueryLeaf.getTupleStructure().getField(cpt + 1).getType());
85                 }
86
87                 tupleBuffer1 = new MemoryTuple(operandBuffer1); // Creating the Buffer1
88
tupleBuffer2 = new MemoryTuple(operandBuffer2);
89                 subTupleCollection = tcQueryLeaf.getTupleCollection();
90                 empty = (subTupleCollection.isEmpty());
91                 if (empty)
92                     cursor = 0;
93                 else
94                     this.init();
95             } catch (MedorException mexp) {
96                 throw new EvaluationException(mexp);
97             }
98         }
99
100         public void close() throws MedorException {
101             closed = true;
102         }
103
104         public TupleStructure getMetaData() throws MedorException {
105             return tcQueryLeaf.getTupleStructure();
106         }
107
108         public boolean isLast() throws MedorException {
109             return isTheLast;
110         }
111
112         public int card() throws MedorException {
113             return cursor;
114         }
115
116         public boolean next() throws MedorException {
117             Tuple currentTuple = null;
118             boolean moved = false;
119
120             if (isEmpty() || (cursor == -2))
121                 moved = false;
122             else if (isLast()) {
123                 // we have iterated all tuples
124
cursor = -2;
125                 moved = false;
126             } else {
127                 try {
128                     // we move to the next Result
129
moved = true;
130                     cursor++;
131                     do {
132                         //Verify if there is others results
133
if (subTupleCollection.next()) {
134                             currentTuple = subTupleCollection.getTuple();
135                             filter.evaluate(parameters, currentTuple);
136                         } else {
137                             isTheLast = true;
138                         }
139                     } while (!(isTheLast) && !(filterResult.getBoolean()));
140                     if (!isTheLast) {
141                         if (isBuffer2Free) {
142                             //Load the next Result in the buffer2
143
tcQueryLeaf.getTupleLoader().loadTuple(currentTuple, operandBuffer2, parameters);
144                             isBuffer2Free = false;
145                         } else {
146                             //Load the next Result in the buffer1
147
tcQueryLeaf.getTupleLoader().loadTuple(currentTuple, operandBuffer1, parameters);
148                             isBuffer2Free = true;
149                         }
150                     } else
151                         isBuffer2Free = !isBuffer2Free;
152                 }
153                 catch (ExpressionException e) {
154                     throw new MedorException(e);
155                 }
156             }
157             return moved;
158         }
159
160
161         /**
162          * This method Move the cursor to the first result if there is one, load it, then it verfy if it is the last one.
163          * In the case of the last one it returns false whether it returns true.
164          */

165         private boolean init() throws MedorException {
166             isTheLast = false;
167             Tuple currentTuple;
168             isBuffer2Free = false;
169             subTupleCollection.first();
170             currentTuple = subTupleCollection.getTuple();
171             try {
172                 filter.evaluate(parameters, currentTuple);
173                 while (!empty && !filterResult.getBoolean()) {
174                     if (subTupleCollection.next()) {
175                         currentTuple = subTupleCollection.getTuple();
176                         filter.evaluate(parameters, currentTuple);
177                     } else {
178                         empty = true;//This TupleCollection result is empty
179
}
180                 }
181                 if (empty) {
182                     cursor = -2;
183                 } else {
184                     // Loading the first result in the buffer1
185
tcQueryLeaf.getTupleLoader().loadTuple(currentTuple, operandBuffer1, parameters);
186                     cursor = 1;
187                     do {
188                         // Verfy if there is other result
189
if (subTupleCollection.next()) {
190                             currentTuple = subTupleCollection.getTuple();
191                             filter.evaluate(parameters, currentTuple);
192                         } else {
193                             // There is no more results
194
isTheLast = true;
195                         }
196                     } while (!(isTheLast) && !(filterResult.getBoolean()));
197                     if (!isTheLast) {
198                         // Loading the next Result in the buffer2
199
tcQueryLeaf.getTupleLoader().loadTuple(currentTuple, operandBuffer2, parameters);
200                     }
201                 }
202             }
203             catch (ExpressionException e) {
204                 throw new MedorException(e);
205             }
206             return (!empty);
207         }
208
209         public void first() throws MedorException {
210             //The Initialisation
211
init();
212         }
213
214         public int getRow() throws MedorException {
215             return cursor;
216         }
217
218         public Tuple getTuple() throws MedorException {
219             if (!isEmpty() && getRow() >= 1) {
220                 if (isBuffer2Free)
221                     return tupleBuffer2;
222                 else
223                     return tupleBuffer1;
224             } else
225                 throw new MedorException(" No elements fetched in this TupleCollection");
226         }
227
228         public synchronized Tuple getTuple(int numTuple) throws MedorException {
229             row(numTuple);
230             return getTuple();
231         }
232
233         public boolean isEmpty() throws MedorException {
234             return empty;
235         }
236
237         public boolean row(int numTuple) throws MedorException {
238             first();
239             int cpt = 1;
240             boolean stop = false;
241             while ((cpt < numTuple) && !stop) {
242                 if (!next())
243                     stop = true;
244                 else
245                     cpt++;
246             }
247             if (cpt < numTuple)
248                 return false;
249             else {
250                 cursor = numTuple;
251                 return true;
252             }
253         }
254     }
255 }
256
Popular Tags