KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > eval > lib > ProjectEvaluatedTC


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 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
24 package org.objectweb.medor.eval.lib;
25
26 import org.objectweb.medor.api.MedorException;
27 import org.objectweb.medor.api.TupleStructure;
28 import org.objectweb.medor.eval.api.BinaryEvaluatedTC;
29 import org.objectweb.medor.eval.api.NodeEvaluator;
30 import org.objectweb.medor.eval.prefetch.api.PrefetchBuffer;
31 import org.objectweb.medor.eval.prefetch.api.PrefetchBufferHolder;
32 import org.objectweb.medor.expression.api.ParameterOperand;
33 import org.objectweb.medor.expression.api.VariableOperand;
34 import org.objectweb.medor.expression.api.ExpressionException;
35 import org.objectweb.medor.expression.lib.BasicVariableOperand;
36 import org.objectweb.medor.query.api.QueryNode;
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.util.monolog.api.BasicLevel;
41
42
43 /**
44  * A project Operation QueryNode is performed by this TupleCollection. This
45  * operation is not costs and doeas not uses buffers.
46  */

47 public class ProjectEvaluatedTC
48         extends BasicBinaryEvalutedTC
49         implements BinaryEvaluatedTC, PrefetchBufferHolder {
50
51     private QueryNode query;
52     private TupleCollection subResultTC;
53     private ParameterOperand[] parameters;
54     private Tuple currentBuffer;
55     private VariableOperand[] currentAttBuffers;
56     private int cursor,size;
57     private boolean empty;
58     //boolean to indicate whether close() has been explicitely called
59
private boolean userHasClosed = false;
60     private PrefetchBuffer prefetchBuffer;
61
62     public ProjectEvaluatedTC(QueryNode query,
63                               NodeEvaluator subNodeEvaluator,
64                               ParameterOperand[] parameters,
65                               PrefetchBuffer pb) throws MedorException {
66         super();
67         this.parameters = parameters;
68         this.query = query;
69         this.prefetchBuffer = pb;
70         if (prefetchBuffer != null) {
71             pb.setTupleCollection(this);
72         }
73         // the size of the result of the projection operation
74
size = query.getTupleStructure().getSize();
75         currentAttBuffers = new BasicVariableOperand[size];
76         for (int cpt = 0; (cpt < query.getTupleStructure().getSize()); cpt++) {
77             currentAttBuffers[cpt] = new BasicVariableOperand(
78                     query.getTupleStructure().getField(cpt + 1).getType());
79         }
80         // This object is used as a buffer
81
currentBuffer = new MemoryTuple(currentAttBuffers);
82         subResultTC = subNodeEvaluator.fetchData(parameters);
83         empty = (subResultTC.isEmpty());
84         if (empty) {
85             cursor = 0;
86         } else {
87             init();
88         }
89     }
90
91     public void invalidatePrefetchBuffer() throws MedorException {
92         if (debug) {
93             log.log(BasicLevel.DEBUG, "Invalidating PrefetchBuffer " + prefetchBuffer + " for " + this);
94         }
95         prefetchBuffer = null;
96         if (userHasClosed) {
97             if (debug) {
98                 log.log(BasicLevel.DEBUG, "The tupleCollection was closed earlier: closing it for real");
99             }
100             this.close();
101         }
102     }
103
104     public void close() throws MedorException {
105         if (debug) {
106             log.log(BasicLevel.DEBUG, "Closing TupleCollection " + this);
107         }
108         userHasClosed = true;
109         //closing only if the prefetchBuffer is null or has been closed.
110
if (prefetchBuffer == null || prefetchBuffer.isClosed()) {
111             if (debug) {
112                 log.log(BasicLevel.DEBUG, "PrefetchBuffer " + prefetchBuffer + " is null or was closed previously: real close of the TupleCollection");
113             }
114             prefetchBuffer = null;
115             super.close();
116             if (subResultTC != null) {
117                 subResultTC.close();
118             }
119         }
120     }
121
122     public TupleStructure getMetaData() throws MedorException {
123         return query.getTupleStructure();
124     }
125
126     public boolean isLast() throws MedorException {
127         return subResultTC.isLast();
128     }
129
130     public int card() throws MedorException {
131         return cursor;
132     }
133
134     public boolean next() throws MedorException {
135         boolean moved;
136         if (subResultTC.isLast()) {
137             // we have already iterate the last tuple
138
cursor = -2;
139             moved = false;
140         } else {
141             cursor++;
142             subResultTC.next();
143             //Upgrade the buffer
144
getNextResult();
145             if (prefetchBuffer != null) {
146                 prefetchBuffer.addPrefetchTuple();
147             }
148             moved = true;
149         }
150         return moved;
151     }
152
153     private void init() throws MedorException {
154         subResultTC.first();
155         getNextResult();
156         cursor = 1;
157         if (prefetchBuffer != null) {
158             prefetchBuffer.addPrefetchTuple();
159         }
160     }
161
162     public void first() throws MedorException {
163         //The Initialisation
164
init();
165     }
166
167     public int getRow() throws MedorException {
168         return cursor;
169     }
170
171     public Tuple getTuple() throws MedorException {
172         if (!isEmpty() && (getRow() >= 1))
173             return currentBuffer;
174         else
175             throw new MedorException(" No elements fetched in this TupleCollection " + getRow());
176     }
177
178     public synchronized Tuple getTuple(int numTuple) throws MedorException {
179         row(numTuple);
180         return getTuple();
181     }
182
183     public boolean isEmpty() throws MedorException {
184         return (subResultTC.isEmpty());
185     }
186
187     public boolean row(int numTuple) throws MedorException {
188         boolean go = subResultTC.row(numTuple);
189         if (!go) {
190             return false;
191         } else {
192             getNextResult();
193             cursor = numTuple;
194             return true;
195         }
196     }
197
198     public int getLeftTCCursor() throws MedorException {
199         return subResultTC.getRow();
200     }
201
202     public int getRightTCCursor() throws MedorException {
203         return -1;
204     }
205
206     private void getNextResult() throws MedorException {
207         try {
208             query.getTupleLoader().loadTuple(
209                     subResultTC.getTuple(), currentAttBuffers, parameters);
210         }
211         catch (ExpressionException e) {
212             throw new MedorException(e);
213         }
214     }
215 }
216
Popular Tags