KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
25  * Package definition.
26  */

27
28 package org.objectweb.medor.eval.lib;
29
30 import org.objectweb.medor.api.MedorException;
31 import org.objectweb.medor.api.TupleStructure;
32 import org.objectweb.medor.eval.api.NodeEvaluator;
33 import org.objectweb.medor.expression.api.ParameterOperand;
34 import org.objectweb.medor.expression.api.VariableOperand;
35 import org.objectweb.medor.query.api.NestedField;
36 import org.objectweb.medor.query.api.PropagatedField;
37 import org.objectweb.medor.query.api.QueryTree;
38 import org.objectweb.medor.query.api.UnnestQueryNode;
39 import org.objectweb.medor.tuple.api.Tuple;
40 import org.objectweb.medor.tuple.api.TupleCollection;
41 import org.objectweb.medor.tuple.lib.MemoryTuple;
42
43 /**
44  * This class represent a TupleCollection Result of the Unnest Operation.
45  * We don't store buffers in this operation because all Tuples of the sub
46  * TupleCollection will be in this TupleCollection, there no filtering.
47  */

48 public class UnnestEvaluatedTC implements TupleCollection {
49     private TupleStructure tcStructure;
50     private ParameterOperand[] parameters;
51     private int sizeOfResult, sizeOfNotUnnestedFields, nestedFieldRank;
52     private NestedField unnestField;
53     private VariableOperand[] currentAttBuffers;
54     private Tuple nestedCurrentTuple, currentBuffer;
55     private TupleCollection nestedFieldTC, unnestedTupleCollection;
56     private int cursor;
57     private int[] indexes;
58     private boolean closed = false;
59
60     public UnnestEvaluatedTC(UnnestQueryNode unnestQueryTree,
61                              NodeEvaluator subNodeEvaluator,
62                              ParameterOperand[] parameters)
63         throws MedorException {
64         this.tcStructure = unnestQueryTree.getTupleStructure();
65         this.parameters = parameters;
66
67         // the Field wich will be degrouped
68
unnestField = unnestQueryTree.getUnnestedField();
69
70         // the sub TupleCollection to Unnest
71
unnestedTupleCollection = subNodeEvaluator.fetchData(parameters);
72
73         // the size of this TupleCollection
74
sizeOfResult = unnestQueryTree.getTupleStructure().getSize();
75
76         //the number of fields wich will be projected without the fields
77
//of the nestedField to unnest
78
sizeOfNotUnnestedFields = unnestQueryTree.getProjectedFields().length;
79
80         /**
81          * This int array contains indexes of the not projectd fields in the
82          * sub TupleCollection
83          * This operation should be moved when constructing or compiling the
84          * Query
85          */

86         indexes = new int[sizeOfNotUnnestedFields];
87         QueryTree[] childs = unnestQueryTree.getChildren();
88         PropagatedField[] notNestedFields =
89             unnestQueryTree.getProjectedFields();
90
91         for (int cpt = 0; (cpt < sizeOfNotUnnestedFields); cpt++) {
92             indexes[cpt] =
93                 childs[0].getTupleStructure().getFieldRank(
94                     notNestedFields[cpt].getPreviousFields()[0]);
95         }
96         // the rank of the field to unnest
97
nestedFieldRank =
98             childs[0].getTupleStructure().getFieldRank(unnestField);
99         currentAttBuffers = new VariableOperand[sizeOfResult];
100         currentBuffer = new MemoryTuple(currentAttBuffers);
101         init();
102     }
103
104     private void init() throws MedorException {
105         cursor = 1;
106         if (!isEmpty()) {
107             nestedCurrentTuple = unnestedTupleCollection.getTuple();
108             nestedFieldTC = nestedCurrentTuple.getTupleCollection(
109                 indexes[nestedFieldRank]);
110             for (int cpt = 0; (cpt < sizeOfNotUnnestedFields); cpt++) {
111                 currentAttBuffers[cpt] =
112                     (VariableOperand) nestedCurrentTuple.getLikeOperand(
113                         indexes[cpt]);
114             }
115             if (!(nestedFieldTC.isEmpty())) {
116                 for (int cpt = sizeOfNotUnnestedFields;
117                      (cpt < sizeOfResult);
118                      cpt++) {
119                     currentAttBuffers[cpt] =
120                         (VariableOperand)
121                         nestedFieldTC.getTuple().getLikeOperand(
122                             cpt - sizeOfNotUnnestedFields + 1);
123                 } // currentBuffer contain now the first result
124
}
125         }
126     }
127
128     public void close() throws MedorException {
129         closed = true;
130         if (nestedFieldTC != null) {
131             nestedFieldTC.close();
132         }
133         if (unnestedTupleCollection != null) {
134             unnestedTupleCollection.close();
135         }
136     }
137
138     /**
139      * It contains metadata of fields wich will be projected
140      */

141     public TupleStructure getMetaData() throws MedorException {
142         return tcStructure;
143     }
144
145     public boolean isLast() throws MedorException {
146         return ((unnestedTupleCollection.isLast()) && (nestedFieldTC.isLast()));
147     }
148
149     public boolean next() throws MedorException {
150         if (nestedFieldTC.next()) {
151             for (int cpt = sizeOfNotUnnestedFields;
152                  (cpt < sizeOfResult);
153                  cpt++) {
154                 currentAttBuffers[cpt] =
155                     (VariableOperand) nestedFieldTC.getTuple().getLikeOperand(
156                         cpt - sizeOfNotUnnestedFields + 1);
157             }
158             cursor++;
159             return true;
160         }
161         else {
162             if (unnestedTupleCollection.next()) {
163                 nestedCurrentTuple = unnestedTupleCollection.getTuple();
164                 nestedFieldTC =
165                     nestedCurrentTuple.getTupleCollection(nestedFieldRank);
166                 for (int cpt = 0; (cpt < sizeOfNotUnnestedFields); cpt++) {
167                     currentAttBuffers[cpt] =
168                         (VariableOperand) nestedCurrentTuple.getLikeOperand(
169                             indexes[cpt]);
170                 }
171
172                 // we supose that the nestedFieldTC is not empty
173
if (!(nestedFieldTC.isEmpty())) {
174                     for (int cpt = sizeOfNotUnnestedFields;
175                          (cpt < sizeOfResult);
176                          cpt++) {
177                         currentAttBuffers[cpt] =
178                             (VariableOperand)
179                             nestedFieldTC.getTuple().getLikeOperand(
180                                 cpt - sizeOfNotUnnestedFields + 1);
181                     }
182                 }
183                 cursor++;
184                 return true;
185             }
186             else
187                 return false;
188         }
189     }
190
191     public void first() throws MedorException {
192         init();
193     }
194
195     public int getRow() throws MedorException {
196         return cursor;
197     }
198
199     public Tuple getTuple() throws MedorException {
200         return currentBuffer;
201     }
202
203     public synchronized Tuple getTuple(int numTuple) throws MedorException {
204         return getTuple();
205     }
206
207     public boolean row(int row) throws MedorException {
208         return false;
209
210     }
211
212     public int card() throws MedorException {
213         return 0;
214     }
215
216     public boolean isEmpty() throws MedorException {
217         return unnestedTupleCollection.isEmpty();
218     }
219 }
220
Popular Tags