KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > plan > OpSubQuery


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mediator.plan;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import org.xquark.mediator.DOMUtils.*;
29 import org.xquark.mediator.decomposer.Utils;
30 import org.xquark.mediator.runtime.MediatorException;
31 import org.xquark.xquery.parser.ListOpCompExpression;
32 import org.xquark.xquery.parser.XQueryException;
33 import org.xquark.xquery.parser.XQueryExpression;
34 import org.xquark.xquery.parser.hinttree.HintTree;
35 import org.xquark.xquery.parser.util.Constants;
36
37 public class OpSubQuery extends OpMult {
38     // **********************************************************************
39
// * VERSIONING
40
// **********************************************************************
41
private static final String JavaDoc RCSRevision = "$Revision: 1.17 $";
42     private static final String JavaDoc RCSName = "$Name: $";
43     // ***********************************************************************
44
// * VARIABLES
45
// ***********************************************************************
46
private int[] locations = null;
47     private int[] idlocations = null;
48     //private HashMap subelementsrestrict = new HashMap() ;
49
private boolean pivot = false;
50     private boolean imbricated = false;
51     protected int ordertype = ORDER_SERIALIZATION;
52     protected int outertype = HintTree.NESTED_TYPE;
53     protected ArrayList JavaDoc mergeRestrictions = null;
54     protected ArrayList JavaDoc outsideRestrictions = null;
55
56     // ***********************************************************************
57
// * INITIALIZATION
58
// ***********************************************************************
59
/**
60      *
61      */

62
63     public OpSubQuery(ExecutionPlan plan, XQueryExpression expression, Operator[] children, boolean pivot, boolean imbricated, ArrayList JavaDoc mergeRestrictions, ArrayList JavaDoc outsideRestrictions) throws MediatorException {
64         super(plan, expression, children);
65         ordertype = ORDER_PARALLELIZATION;
66         this.pivot = pivot;
67         this.imbricated = imbricated;
68         this.outertype = HintTree.MERGE_TYPE;
69         this.mergeRestrictions = mergeRestrictions;
70         this.outsideRestrictions = outsideRestrictions;
71         init();
72     }
73
74     public OpSubQuery(ExecutionPlan plan, XQueryExpression expression, Operator[] children, boolean pivot, boolean imbricated) throws MediatorException {
75         super(plan, expression, children);
76         ordertype = ORDER_PARALLELIZATION;
77         this.pivot = pivot;
78         this.imbricated = imbricated;
79         init();
80     }
81
82     public boolean getPivot() {
83         return this.pivot;
84     }
85     //public void setPivot(boolean pivot) { this.pivot = pivot; }
86
public boolean getImbricated() {
87         return this.imbricated;
88     }
89     //public void setImbricated(boolean imbricated) { this.imbricated = imbricated; }
90
public int getOuterType() {
91         return this.outertype;
92     }
93
94     public ArrayList JavaDoc getMergeRestrictions() {
95         return this.mergeRestrictions;
96     }
97     public ArrayList JavaDoc getOutsideRestrictions() {
98         return this.outsideRestrictions;
99     }
100
101     public void init() throws MediatorException {
102         // Sometimes, it can happens that the two members of merge
103
// product have common attributes. In that case, we must mark them
104
// as common. So when we will generate the tuples, we can access
105
// them efficiently.
106
if (childrenOperator == null)
107             return;
108         this.resetPaths();
109         if (this.paths == null || this.paths.isEmpty()) {
110             for (int i = 0; i < childrenOperator.length; i++) {
111                 Operator operatori = childrenOperator[i];
112                 operatori.setParentOperator(this);
113                 if (i != 0 && pivot && operatori.getDepends())
114                     operatori.setPrepared();
115                 addPaths(operatori.getPaths());
116             }
117         }
118         size = childrenOperator[0].getSize(); //.getPaths().size() ;
119
idsize = childrenOperator[0].getIdSize();
120         locations = new int[childrenOperator.length - 1];
121         idlocations = new int[childrenOperator.length - 1];
122         for (int k = 1; k < childrenOperator.length; k++) {
123             locations[k - 1] = size;
124             idlocations[k - 1] = idsize;
125             size += childrenOperator[k].getSize(); //.getPaths().size();
126
idsize += childrenOperator[k].getIdSize();
127         }
128     }
129
130     public int[] getIdLocations() {
131         return idlocations;
132     }
133     public int[] getLocations() {
134         return locations;
135     }
136
137     /**
138      * Return true if the node is of type source.
139      * AlgBinOp is not a source.
140      *
141      * @return false ;
142      */

143     public boolean isSource() {
144         return false;
145     }
146
147     /**
148      * Return all the sources depending of this node.
149      * @return
150      */

151     public ArrayList JavaDoc getSources() {
152         /*
153         if (sources == null) {
154             sources = new ArrayList() ;
155             for (int i=0;i<algebralist.size();i++) {
156                 ArrayList tmplist = ((Algebra)algebralist.get(i)).getSources();
157                 for (int j=0;j<tmplist.size();j++) {
158                     Object sourcej = tmplist.get(j) ;
159                     if (! sources.contains(sourcej)) sources.add(sourcej) ;
160                 }
161             }
162         }
163         return sources ;
164          */

165         return null;
166     }
167
168     protected boolean shareSameSources() {
169         /*
170         for (int i=0;i<algebralist.size();i++)
171             if (((Algebra)algebralist.get(i)).shareSameSources() == false) return false;
172         if (compareSource()) return true ;
173          */

174         return false;
175     }
176
177     // #############################################################################
178
// VISITOR STUFF
179
// #############################################################################
180

181     public void accept(OperatorVisitor visitor) throws MediatorException {
182         visitor.visit(this);
183     }
184
185     // ***********************************************************************
186
// * EXECUTE QUERY
187
// ***********************************************************************
188
/**
189      *
190      */

191     public ResultSet executeQuery(DynamicContext context) throws MediatorException {
192         try {
193             OperatorRunnable[] runnables = new OperatorRunnable[childrenOperator.length];
194             if (ordertype == ORDER_SERIALIZATION) {
195                 for (int i = 0; i < childrenOperator.length; i++) {
196                     OperatorRunnable ori = childrenOperator[i].getOperatorRunnable(context);
197                     runnables[i] = ori;
198                     if (!childrenOperator[i].getDepends() || i == 0 || this.outertype == HintTree.MERGE_TYPE)
199                         ori.run();
200                 }
201             } else if (ordertype == ORDER_PARALLELIZATION) {
202                 Thread JavaDoc[] threads = new Thread JavaDoc[childrenOperator.length];
203                 for (int i = 0; i < childrenOperator.length; i++) {
204                     OperatorRunnable ori = childrenOperator[i].getOperatorRunnable((DynamicContext)context.clone());
205                     runnables[i] = ori;
206                     if (!childrenOperator[i].getDepends() || i == 0 || this.outertype == HintTree.MERGE_TYPE) {
207                         threads[i] = new Thread JavaDoc(ori);
208                         threads[i].start();
209                     }
210                 }
211                 // wait for all threads to exit
212
for (int i = 0; i < threads.length; i++) {
213                     Thread JavaDoc thread = threads[i];
214                     if (thread != null && thread.isAlive())
215                         thread.join();
216                 }
217             }
218             return getResultSet(context, runnables);
219         } catch (InterruptedException JavaDoc e) {
220             throw new MediatorException("AlgMultOp.executeQuery: " + e.getMessage(), e);
221         }
222     }
223
224     protected ResultSet getResultSet(DynamicContext context, OperatorRunnable[] children) throws MediatorException {
225         return new SubQueryNoPivotResultSet(this, context, children, pivot, imbricated);
226     }
227
228     // **********************************************************************
229
// * OPTIMIZE
230
// **********************************************************************
231

232     // **********************************************************************
233
// * DEBUG
234
// **********************************************************************
235
public String JavaDoc toCompleteString(int indent) {
236         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
237         buf.append(Utils.makeIndent(indent) + "<" + getClass().getName() + " hasPivot=\"" + pivot + "\" isImbricated=\"" + this.imbricated + " size=" + size + " id size=" + idsize + " isLet=\"" + islet + "\" valueDepends=\"" + valueDepends + "\" whereDepends=\"" + whereDepends + "\">\n");
238         buf.append(Utils.makeIndent(indent + 1) + "<mergeRestrictions>\n");
239         buf.append(Utils.makeIndent(indent + 2) + mergeRestrictions + "\n");
240         buf.append(Utils.makeIndent(indent + 1) + "</mergeRestrictions>\n");
241         buf.append(Utils.makeIndent(indent + 1) + "<outsideRestrictions>\n");
242         buf.append(Utils.makeIndent(indent + 2) + outsideRestrictions + "\n");
243         buf.append(Utils.makeIndent(indent + 1) + "</outsideRestrictions>\n");
244         /*
245         buf.append(Utils.makeIndent(indent + 1) + "<Expression>\n") ;
246         buf.append(Utils.makeIndent(indent + 2) + expression + "\n") ;
247         buf.append(Utils.makeIndent(indent + 1) + "</Expression>\n") ;
248         if (variables != null) {
249             buf.append(Utils.makeIndent(indent + 1) + "<Variables>\n") ;
250             for (int i = 0 ; i < variables.size() ; i ++) {
251                 buf.append(Utils.makeIndent(indent + 2) + "<Variable>" + variables.get(i) + "</Variable>\n") ;
252             }
253             buf.append(Utils.makeIndent(indent + 1) + "</Variables>\n") ;
254         }
255          */

256         if (paths != null) {
257             buf.append(Utils.makeIndent(indent + 1) + "<Paths>\n");
258             for (int i = 0; i < paths.size(); i++) {
259                 buf.append(Utils.makeIndent(indent + 2) + "<Path>" + paths.get(i) + "</Path>\n");
260             }
261             buf.append(Utils.makeIndent(indent + 1) + "</Paths>\n");
262         }
263         /*
264         if (subelementsrestrict != null && !subelementsrestrict.isEmpty()) {
265             buf.append(Utils.makeIndent(indent + 1) + "<Restrictions>\n") ;
266             for (Iterator it = subelementsrestrict.keySet().iterator();it.hasNext();) {
267                 Integer index = (Integer)it.next();
268                 buf.append(Utils.makeIndent(indent + 2) + "<Index>" + index + "</Index>\n") ;
269                 ArrayList tmplist = (ArrayList)subelementsrestrict.get(index);
270                 if (tmplist == null) continue;
271                 for (int i = 0 ; i < tmplist.size() ; i ++)
272                     buf.append(Utils.makeIndent(indent + 2) + "<Restriction>" + tmplist.get(i) + "</Restriction>\n") ;
273             }
274             buf.append(Utils.makeIndent(indent + 1) + "</Restrictions>\n") ;
275         }
276          */

277         for (int i = 0; i < childrenOperator.length; i++) {
278             buf.append(Utils.makeIndent(indent + 1) + "<Element" + (i + 1) + ">\n");
279             buf.append(childrenOperator[i].toCompleteString(indent + 2));
280             buf.append(Utils.makeIndent(indent + 1) + "</Element" + (i + 1) + ">\n");
281         }
282         buf.append(Utils.makeIndent(indent) + "</" + getClass().getName() + ">\n");
283         return buf.toString();
284     }
285 }
286
287 abstract class SubQueryResultSet extends MultResultSet {
288     // **********************************************************************
289
// * VERSIONING
290
// **********************************************************************
291
private static final String JavaDoc RCSRevision = "$Revision: 1.17 $";
292     private static final String JavaDoc RCSName = "$Name: $";
293     // ***********************************************************************
294
// * VARIABLES
295
// ***********************************************************************
296
protected int[] locations = null;
297     protected int[] idlocations = null;
298     protected boolean pivot;
299     protected boolean imbricated;
300     // ***********************************************************************
301
// * INITIALIZATION
302
// ***********************************************************************
303
/**
304      *
305      */

306     //public SubQueryResultSet(AlgSubQuery algebra, HashMap subelementsrestrict, boolean pivot) throws XMLDBCException {
307
public SubQueryResultSet(OpSubQuery operator, DynamicContext context, OperatorRunnable[] runnables, boolean pivot, boolean imbricated) throws MediatorException {
308         super(operator, context);
309         this.children = runnables;
310 // resultsets = new ResultSet[children.length];
311
// for (int i = 0; i < children.length; i++) {
312
// Operator operatori = operator.getSubElement(i);
313
// if (!operatori.getDepends() || i == 0 || operator.getOuterType() == HintTree.MERGE_TYPE)
314
// resultsets[i] = children[i].getResultSet();
315
// }
316
this.pivot = pivot;
317         this.imbricated = imbricated;
318         // initLocations(algebra);
319
this.locations = operator.getLocations();
320         this.idlocations = operator.getIdLocations();
321     }
322
323     // public int[] getLocations() {
324
// return locations;
325
// }
326
//
327
// public int[] getIdLocations() {
328
// return idlocations;
329
// }
330

331 }
332
333 class SubQueryNoPivotResultSet extends SubQueryResultSet {
334     // **********************************************************************
335
// * VERSIONING
336
// **********************************************************************
337
private static final String JavaDoc RCSRevision = "$Revision: 1.17 $";
338     private static final String JavaDoc RCSName = "$Name: $";
339
340     private int index = 0;
341     //private boolean hasinformation = false;
342
private boolean allLets = true;
343
344     private ResultSet pivotResultSet = null;
345     private ArrayList JavaDoc allTuples = null;
346     private int[] indexes = null;
347     private Tuple tuplepivot = null;
348     private boolean is_used = false;
349     private ArrayList JavaDoc currentTuples = null;
350     private int size = -1;
351     private boolean endindex = false;
352     private int outertype = HintTree.NESTED_TYPE;
353     private SortedKeyMap tm0 = null;
354     private SortedKeyMap tm1 = null;
355     private HashMap JavaDoc pivotindex = null;
356     private int indexindex = 0;
357
358     public SubQueryNoPivotResultSet(OpSubQuery operator, DynamicContext context, OperatorRunnable[] children, boolean pivot, boolean imbricated) throws MediatorException {
359         super(operator, context, children, pivot, imbricated);
360         init();
361     }
362
363     protected void init() throws MediatorException {
364         size = children.length;
365         if (pivot) {
366             pivotResultSet = children[0].getResultSet();
367             if (pivotResultSet == null || !pivotResultSet.hasNext()) {
368                 pivotResultSet.close();
369                 setFinishedWhenEmpty();
370                 for (int i = 1; i < size; i++)
371                     if (!children[i].getOperator().getDepends())
372                         children[i].getResultSet().close();
373                 //this.hasinformation = true;
374
return;
375             }
376             outertype = ((OpSubQuery) operator).getOuterType();
377             if (outertype == HintTree.NESTED_TYPE) {
378                 allTuples = new ArrayList JavaDoc(size - 1);
379                 indexes = new int[size - 1];
380                 for (int i = 1; i < size; i++) {
381                     indexes[i - 1] = -1;
382                     if (allLets && (!children[i].getOperator().isLet() || children[i].getOperator().getWhereDepends()))
383                         allLets = false;
384                 }
385             } else {
386                 allTuples = new ArrayList JavaDoc(size);
387                 indexes = new int[size];
388                 indexes[0] = -1;
389                 for (int i = 1; i < size; i++) {
390                     indexes[i] = -1;
391                     if (allLets && (!children[i].getOperator().isLet() || children[i].getOperator().getWhereDepends()))
392                         allLets = false;
393                 }
394             }
395         }
396     }
397
398     // ***********************************************************************
399
// * EVALUATE IMPLEMENTATION
400
// ***********************************************************************
401
/**
402      *
403      * @param non_blocking if the evaluation is blocking, we must
404      * generate all the results before passing.
405      */

406     protected void evaluate(boolean non_blocking) throws MediatorException {
407
408         //non_blocking = false;
409

410         // DERNIERE VERSION testée
411

412         if (pivot) {
413             if (outertype == HintTree.NESTED_TYPE) {
414                 boolean added = false;
415                 while (!added) {
416                     if (endindex) {
417                         endindex = false;
418                         if (is_used == false && (imbricated || allLets)) {
419                             Tuple newtuple = new Tuple(operator);
420                             newtuple.appendTuple(tuplepivot);
421                             newtuple.fillIdentifiers(0, tuplepivot);
422                             buftuples.add(newtuple);
423                             if (!added && non_blocking)
424                                 added = true;
425                             tuplepivot = null;
426                             continue;
427                         }
428                         tuplepivot = null;
429                     }
430                     if (tuplepivot == null) {
431                         if (pivotResultSet.hasNext()) {
432                             tuplepivot = pivotResultSet.next();
433                             is_used = false;
434                             index = 0;
435                         } else {
436                             pivotResultSet.close();
437                             setFinishedWhenEmpty();
438                             return;
439                         }
440                     }
441                     Tuple tuple = null;
442                     Operator operatori = children[index + 1].getOperator();
443                     if (operatori.getDepends()) {
444                         ResultSet subresultset = children[index + 1].getResultSet();
445                         if (subresultset == null) {
446                             children[index + 1].getContext().addCurrentTuple(tuplepivot);
447                             children[index + 1].run();
448                             subresultset = children[index + 1].getResultSet();
449                         }
450                         if (subresultset != null && subresultset.hasNext()) { // more tuples
451
tuple = subresultset.next();
452                         } else { // no more tuples
453
children[index + 1].getContext().deleteCurrentTuple(tuplepivot);
454                             if (subresultset != null)
455                                 subresultset.close();
456                             allTuples.add(null);
457                             children[index + 1].setResultSet(null);
458                             if (index == size - 2)
459                                 endindex = true;
460                             else
461                                 index++;
462                             continue;
463                         }
464                     } else { // no dependency
465
if (indexes[index] == -1) { // first time round
466
ResultSet subresultset = children[index + 1].getResultSet();
467                             if (subresultset.hasNext()) { // more tuples
468
tuple = subresultset.next();
469                                 if (currentTuples == null)
470                                     currentTuples = new ArrayList JavaDoc();
471                                 currentTuples.add(tuple);
472                             } else { // no more tuples
473
subresultset.close();
474                                 allTuples.add(currentTuples);
475                                 currentTuples = null;
476                                 indexes[index] = 0;
477                                 if (index == size - 2)
478                                     endindex = true;
479                                 else
480                                     index++;
481                                 continue;
482                             }
483                         } else { // not first time round
484
if (indexes[index] == 0)
485                                 currentTuples = (ArrayList JavaDoc) allTuples.get(index);
486                             if (indexes[index] == ((currentTuples == null) ? 0 : currentTuples.size())) {
487                                 indexes[index] = 0;
488                                 if (index == size - 2)
489                                     endindex = true;
490                                 else
491                                     index++;
492                                 continue;
493                             }
494                             if (currentTuples != null)
495                                 tuple = (Tuple) currentTuples.get(indexes[index]);
496                             else
497                                 tuple = null;
498                             indexes[index]++;
499                         }
500                     }
501
502                     Tuple newtuple = new Tuple(operator);
503                     newtuple.appendTuple(tuplepivot);
504                     newtuple.appendTupleAtIndex(locations[index], tuple);
505                     newtuple.fillIdentifiers(0, tuplepivot);
506                     newtuple.fillIdentifiers(idlocations[index], tuple);
507                     if (newtuple != null) {
508                         buftuples.add(newtuple);
509                         if (!added && non_blocking)
510                             added = true;
511                         is_used = true;
512                     }
513                 }
514             } else if (outertype == HintTree.MERGE_TYPE) {
515                 //this is blocking!
516
// get all pivot elements and index them with row id
517
if (allTuples.isEmpty()) {
518                     pivotindex = new HashMap JavaDoc();
519                     ArrayList JavaDoc tmplist = new ArrayList JavaDoc();
520                     int tmpint = 0;
521                     while (pivotResultSet.hasNext()) {
522                         Tuple tmptuple = pivotResultSet.next();
523                         tmplist.add(tmptuple);
524                         pivotindex.put(tmptuple, new Integer JavaDoc(tmpint++));
525                     }
526                     // do not close (it puts the buftuples to null)
527
// pivotResultSet.close();
528
allTuples.add(tmplist);
529                     indexes[0] = 0;
530                     index = 1;
531                     indexindex = 0;
532                 }
533                 // make sort merge if necessary
534
boolean added = false;
535                 while (!added) {
536                     if (endindex) {
537                         endindex = false;
538                         indexes[0]++;
539                         if (is_used == false && (imbricated || allLets)) {
540                             Tuple newtuple = new Tuple(operator);
541                             newtuple.appendTuple(tuplepivot);
542                             newtuple.fillIdentifiers(0, tuplepivot);
543                             buftuples.add(newtuple);
544                             if (!added && non_blocking)
545                                 added = true;
546                             tuplepivot = null;
547                             continue;
548                         }
549                         tuplepivot = null;
550                     }
551                     if (tuplepivot == null) {
552                         if (indexes[0] < ((ArrayList JavaDoc) allTuples.get(0)).size()) {
553                             tuplepivot = (Tuple) ((ArrayList JavaDoc) allTuples.get(0)).get(indexes[0]);
554                             is_used = false;
555                             index = 1;
556                         } else {
557                             setFinishedWhenEmpty();
558                             pivotResultSet.close();
559                             return;
560                         }
561                     }
562                     //Tuple tuple = null;
563
if (children[index].getOperator().getDepends()) {
564                         if (indexes[index] == -1) { // merge not calculate yet
565
// make sort merge
566
// fill alltuples with hashMap
567
ResultSet subalgi = children[index].getResultSet();
568                             if (!subalgi.hasNext()) {
569                                 subalgi.close();
570                                 // add empty resultset
571
allTuples.add(null);
572                                 // set index for no recalculation
573
indexes[index] = 0;
574                                 continue;
575                             }
576                             while (subalgi.hasNext()) {
577                                 Tuple tuplei = subalgi.next();
578                             }
579                             // do not close (it puts the buftuples to null)
580
tm0 = children[0].getResultSet().getIndex(indexindex);
581                             tm0.makeIterator();
582                             tm1 = children[index].getResultSet().getIndex(0);
583                             tm1.makeIterator();
584                             subalgi.close();
585
586                             HashMap JavaDoc tmpmap = new HashMap JavaDoc();
587
588                             // initialize variables
589
ArrayList JavaDoc mergeRestrictions = (ArrayList JavaDoc) ((ArrayList JavaDoc) ((OpSubQuery) operator).getMergeRestrictions()).get(indexindex);
590                             XQueryExpression outsideRestrictions = (XQueryExpression) ((ArrayList JavaDoc) ((OpSubQuery) operator).getOutsideRestrictions()).get(indexindex);
591                             // increment index of index
592
indexindex++;
593                             int size = mergeRestrictions.size();
594                             ArrayList JavaDoc leftstrings = new ArrayList JavaDoc(size);
595                             ArrayList JavaDoc rightstrings = new ArrayList JavaDoc(size);
596                             for (int i = 0; i < size; i++) {
597                                 leftstrings.add(((ListOpCompExpression) mergeRestrictions.get(i)).getExpression1().getStringValue());
598                                 rightstrings.add(((ListOpCompExpression) mergeRestrictions.get(i)).getExpression2().getStringValue());
599                             }
600                             int operatorNEQ = ((ListOpCompExpression) mergeRestrictions.get(size - 1)).getOperator();
601                             ArrayList JavaDoc leftbuffer = new ArrayList JavaDoc();
602                             ArrayList JavaDoc rightbuffer = new ArrayList JavaDoc();
603                             Tuple left = null;
604                             Tuple right = null;
605                             int leftindex = -1;
606                             Tuple leftfront = null;
607                             Tuple rightfront = null;
608                             Tuple nextright = null;
609
610                             if (operatorNEQ == 0) {
611                                 while (true) {
612                                     if (left == null || right == null)
613                                         if (!tm0.hasNext() || !tm1.hasNext()) {
614                                             break;
615                                         }
616                                     // find first equality
617
if (left == null) {
618                                         left = tm0.next();
619                                     }
620                                     if (right == null) {
621                                         right = tm1.next();
622                                     }
623                                     while (left != null && right != null) {
624                                         int num = 0;
625                                         while (num < size) {
626                                             int res = DOMUtils.simpleCompareNodeValue(left.getPath((String JavaDoc) leftstrings.get(num)),right.getPath((String JavaDoc) rightstrings.get(num)));
627                                             if (res == 0)
628                                                 num++;
629                                             else {
630                                                 if (res < 0) {
631                                                     if (tm0.hasNext()) {
632                                                         left = tm0.next();
633                                                     } else
634                                                         left = null;
635                                                 } else if (res > 0) {
636                                                     if (tm1.hasNext()) {
637                                                         right = tm1.next();
638                                                     } else
639                                                         right = null;
640                                                 }
641                                                 break;
642                                             }
643                                         }
644                                         if (num == size)
645                                             break;
646                                     }
647                                     if (left == null || right == null) {
648                                         continue;
649                                     }
650                                     // clean buffers
651
leftbuffer.clear();
652                                     leftbuffer.add(left);
653                                     rightbuffer.clear();
654                                     rightbuffer.add(right);
655                                     // get all left elements equal to another and bufferize them
656
while (true) {
657                                         leftfront = null;
658                                         if (tm0.hasNext()) {
659                                             leftfront = tm0.next();
660                                             int num = 0;
661                                             while (num < size) {
662                                                 int res = DOMUtils.simpleCompareNodeValue(left.getPath((String JavaDoc) leftstrings.get(num)),leftfront.getPath((String JavaDoc) leftstrings.get(num)));
663                                                 if (res == 0)
664                                                     num++;
665                                                 else
666                                                     break;
667                                             }
668                                             if (num == size) {
669                                                 leftbuffer.add(leftfront);
670                                                 left = leftfront;
671                                             } else
672                                                 break;
673                                         } else
674                                             break;
675
676                                     }
677                                     // get all right elements equal to another and bufferize them
678
while (true) {
679                                         rightfront = null;
680                                         if (tm1.hasNext()) {
681                                             rightfront = tm1.next();
682                                             int num = 0;
683                                             while (num < size) {
684                                                 int res = DOMUtils.simpleCompareNodeValue(right.getPath((String JavaDoc) rightstrings.get(num)),rightfront.getPath((String JavaDoc) rightstrings.get(num)));
685                                                 if (res == 0)
686                                                     num++;
687                                                 else
688                                                     break;
689                                             }
690                                             if (num == size) {
691                                                 rightbuffer.add(rightfront);
692                                                 right = rightfront;
693                                             } else {
694                                                 break;
695                                             }
696                                         } else
697                                             break;
698                                     }
699                                     // add all necessary tuples to buffertuple result
700
for (int i = 0; i < leftbuffer.size(); i++) {
701                                         for (int j = 0; j < rightbuffer.size(); j++) {
702                                             // TEST outside restrictions
703
if (outsideRestrictions != null) {
704                                                 ArrayList JavaDoc tmpList = new ArrayList JavaDoc();
705                                                 tmpList.addAll(context.getCurrentTuples());
706                                                 tmpList.add(leftbuffer.get(i));
707                                                 tmpList.add(rightbuffer.get(j));
708                                                 try {
709                                                     EvaluationVisitor visitor = new EvaluationVisitor(operator.getPlan().getSchemaManager(), tmpList);
710                                                     outsideRestrictions.accept(visitor);
711                                                     if (!visitor.getVerdict())
712                                                         continue;
713                                                 } catch (XQueryException e) {
714                                                     throw new MediatorException(e.getMessage(), e);
715                                                 }
716                                             }
717                                             Integer JavaDoc tmpint = (Integer JavaDoc) pivotindex.get(leftbuffer.get(i));
718                                             ArrayList JavaDoc tmplist = (ArrayList JavaDoc) tmpmap.get(tmpint);
719                                             if (tmplist == null) {
720                                                 tmplist = new ArrayList JavaDoc();
721                                                 tmpmap.put(tmpint, tmplist);
722                                             }
723                                             tmplist.add(rightbuffer.get(j));
724                                             //this.buftuples.add(((Tuple) leftbuffer.get(i)).merge((Tuple) rightbuffer.get(j), algebra));
725
}
726                                     }
727                                     left = leftfront;
728                                     right = rightfront;
729                                 }
730                             } else { // with inequality
731

732                                 while (true) {
733                                     if (!tm0.hasNext()) {
734                                         break;
735                                     } else if (right == null && nextright == null && !tm1.hasNext() && rightbuffer.isEmpty()) {
736                                         break;
737                                     }
738                                     int num;
739                                     // find first equality
740
if (left == null) {
741                                         left = tm0.next();
742                                     } else {
743                                         leftfront = tm0.next();
744                                         num = 0;
745                                         while (num < size - 1) {
746                                             int res = DOMUtils.simpleCompareNodeValue(left.getPath((String JavaDoc) leftstrings.get(num)),leftfront.getPath((String JavaDoc) leftstrings.get(num)));
747                                             if (res == 0)
748                                                 num++;
749                                             else
750                                                 break;
751                                         }
752                                         if (num != size - 1) {
753                                             rightbuffer.clear();
754                                         }
755                                         left = leftfront;
756                                     }
757                                     if (right == null) {
758                                         if (nextright != null) {
759                                             right = nextright;
760                                             nextright = null;
761                                         } else if (tm1.hasNext()) {
762                                             right = tm1.next();
763                                         }
764                                     }
765                                     // find first match
766
boolean add = false;
767                                     while (right != null) {
768                                         num = 0;
769                                         while (num < size) {
770                                             int res = DOMUtils.simpleCompareNodeValue(left.getPath((String JavaDoc) leftstrings.get(num)), right.getPath((String JavaDoc) rightstrings.get(num)));
771                                             if (num != size - 1) {
772                                                 if (res == 0)
773                                                     num++;
774                                                 else {
775                                                     if (res < 0) {
776                                                         num = size + 1;
777                                                         break;
778                                                     } else if (res > 0) {
779                                                         if (tm1.hasNext()) {
780                                                             right = tm1.next();
781                                                         } else
782                                                             right = null;
783                                                     }
784                                                     break;
785                                                 }
786                                             } else {
787                                                 if ((res == 0 && (operatorNEQ == Constants.GEQ_COMPOP || operatorNEQ == Constants.LEQ_COMPOP)) || (res < 0 && (operatorNEQ == Constants.LT_COMPOP || operatorNEQ == Constants.LEQ_COMPOP)) || (res > 0 && (operatorNEQ == Constants.GEQ_COMPOP || operatorNEQ == Constants.GT_COMPOP))) {
788                                                     num++;
789                                                 } else {
790                                                     num = size + 1;
791                                                     break;
792                                                 }
793                                             }
794                                         }
795                                         if (num == size) {
796                                             add = true;
797                                             rightbuffer.add(right);
798                                             break;
799                                         }
800                                         if (num > size)
801                                             break;
802                                     }
803
804                                     if (rightbuffer.isEmpty()) {
805                                         continue;
806                                     }
807                                     // get all right elements equal to another and bufferize them
808
if (add) {
809                                         rightfront = nextright;
810                                         while (true) {
811                                             if (rightfront == null)
812                                                 if (tm1.hasNext()) {
813                                                     rightfront = tm1.next();
814                                                 }
815                                             if (rightfront != null) {
816                                                 num = 0;
817                                                 while (num < size) {
818                                                     if (num != size - 1) {
819                                                         int res = DOMUtils.simpleCompareNodeValue(right.getPath((String JavaDoc) rightstrings.get(num)), rightfront.getPath((String JavaDoc) rightstrings.get(num)));
820                                                         if (res == 0)
821                                                             num++;
822                                                         else {
823                                                             nextright = rightfront;
824                                                             rightfront = null;
825                                                             break;
826                                                         }
827                                                     } else {
828                                                         int res = DOMUtils.simpleCompareNodeValue(left.getPath((String JavaDoc) leftstrings.get(num)), rightfront.getPath((String JavaDoc) rightstrings.get(num)));
829                                                         if ((res == 0 && (operatorNEQ == Constants.GEQ_COMPOP || operatorNEQ == Constants.LEQ_COMPOP)) || (res < 0 && (operatorNEQ == Constants.LT_COMPOP || operatorNEQ == Constants.LEQ_COMPOP)) || (res > 0 && (operatorNEQ == Constants.GEQ_COMPOP || operatorNEQ == Constants.GT_COMPOP)))
830                                                             num++;
831                                                         else {
832                                                             nextright = rightfront;
833                                                             rightfront = null;
834                                                             break;
835                                                         }
836                                                     }
837                                                 }
838                                                 if (num == size) {
839                                                     rightbuffer.add(rightfront);
840                                                     right = rightfront;
841                                                     rightfront = null;
842                                                 } else {
843                                                     break;
844                                                 }
845                                             } else
846                                                 break;
847                                         }
848                                         right = rightfront;
849                                     }
850                                     // add all necessary tuples to buffertuple result
851
for (int j = 0; j < rightbuffer.size(); j++) {
852                                         // TEST outside restrictions
853
if (outsideRestrictions != null) {
854                                             ArrayList JavaDoc tmpList = new ArrayList JavaDoc();
855                                             tmpList.addAll(context.getCurrentTuples());
856                                             tmpList.add(left);
857                                             tmpList.add(rightbuffer.get(j));
858                                             try {
859                                                 EvaluationVisitor visitor = new EvaluationVisitor(operator.getPlan().getSchemaManager(), tmpList);
860                                                 outsideRestrictions.accept(visitor);
861                                                 if (!visitor.getVerdict())
862                                                     continue;
863                                             } catch (XQueryException e) {
864                                                 throw new MediatorException(e.getMessage(), e);
865                                             }
866                                         }
867                                         Integer JavaDoc tmpint = (Integer JavaDoc) pivotindex.get(left);
868                                         ArrayList JavaDoc tmplist = (ArrayList JavaDoc) tmpmap.get(tmpint);
869                                         if (tmplist == null) {
870                                             tmplist = new ArrayList JavaDoc();
871                                             tmpmap.put(tmpint, tmplist);
872                                         }
873                                         tmplist.add(rightbuffer.get(j));
874                                         //this.buftuples.add(left.merge((Tuple) rightbuffer.get(j), algebra));
875
}
876                                 }
877                             }
878                             // add hashmap to alltuples
879
if (tmpmap.isEmpty())
880                                 allTuples.add(null);
881                             else
882                                 allTuples.add(tmpmap);
883                             // set index for no recalculation
884
indexes[index] = 0;
885                         }
886                         if (indexes[index] == 0) { // merge already calculated
887
HashMap JavaDoc map = (HashMap JavaDoc) allTuples.get(index);
888                             if (map != null) {
889                                 ArrayList JavaDoc tmplist = (ArrayList JavaDoc) map.get(new Integer JavaDoc(indexes[0]));
890                                 if (tmplist != null) {
891                                     for (int i = 0; i < tmplist.size(); i++) {
892                                         Tuple newtuple = new Tuple(operator);
893                                         newtuple.appendTuple(tuplepivot);
894                                         newtuple.appendTupleAtIndex(locations[index - 1], (Tuple) tmplist.get(i));
895                                         newtuple.fillIdentifiers(0, tuplepivot);
896                                         newtuple.fillIdentifiers(idlocations[index - 1], (Tuple) tmplist.get(i));
897                                         if (newtuple != null) {
898                                             buftuples.add(newtuple);
899                                             if (!added && non_blocking)
900                                                 added = true;
901                                             is_used = true;
902                                         }
903                                     }
904                                 }
905                             }
906                             if (index == size - 1)
907                                 endindex = true;
908                             else
909                                 index++;
910                         }
911                         continue;
912                     } else { // no dependency
913
Tuple tuple = null;
914                         if (indexes[index] == -1) { // first time round
915
ResultSet subresultset = children[index].getResultSet();
916                             if (subresultset.hasNext()) { // more tuples
917
tuple = subresultset.next();
918                                 if (currentTuples == null)
919                                     currentTuples = new ArrayList JavaDoc();
920                                 currentTuples.add(tuple);
921                             } else { // no more tuples
922
subresultset.close();
923                                 allTuples.add(currentTuples);
924                                 currentTuples = null;
925                                 indexes[index] = 0;
926                                 if (index == size - 1)
927                                     endindex = true;
928                                 else
929                                     index++;
930                                 continue;
931                             }
932                         } else { // not first time round
933
if (indexes[index] == 0)
934                                 currentTuples = (ArrayList JavaDoc) allTuples.get(index);
935                             if (indexes[index] == currentTuples.size()) {
936                                 indexes[index] = 0;
937                                 if (index == size - 1)
938                                     endindex = true;
939                                 else
940                                     index++;
941                                 continue;
942                             }
943                             tuple = (Tuple) currentTuples.get(indexes[index]);
944                             indexes[index]++;
945                         }
946                         Tuple newtuple = new Tuple(operator);
947                         newtuple.appendTuple(tuplepivot);
948                         newtuple.appendTupleAtIndex(locations[index - 1], tuple);
949                         newtuple.fillIdentifiers(0, tuplepivot);
950                         newtuple.fillIdentifiers(idlocations[index - 1], tuple);
951                         if (newtuple != null) {
952                             buftuples.add(newtuple);
953                             if (!added && non_blocking)
954                                 added = true;
955                             is_used = true;
956                         }
957                     }
958                 }
959
960             }
961         } else { // NORMAL CODE for this class!!!
962
while (index < children.length && !children[index].getResultSet().hasNext())
963                 index++;
964             while (index < children.length) {
965                 if (children[index].getResultSet().hasNext()) {
966                     Tuple tuple = children[index].getResultSet().next();
967                     Tuple newtuple = new Tuple(operator);
968                     if (index == 0)
969                         newtuple.appendTupleAtIndex(0, tuple);
970                     else
971                         newtuple.appendTupleAtIndex(locations[index - 1], tuple);
972                     if (index == 0)
973                         newtuple.fillIdentifiers(0, tuple);
974                     else
975                         newtuple.fillIdentifiers(idlocations[index - 1], tuple);
976                     buftuples.add(newtuple);
977
978                     if (non_blocking) {
979                         if ((buftuples != null) && (!buftuples.isEmpty()))
980                             break;
981                     }
982                 } else {
983                     children[index].getResultSet().close();
984                     index++;
985                 }
986             }
987             if (index == children.length)
988                 setFinishedWhenEmpty();
989         }
990     }
991 }
992
993 //class SubQueryPivotNoImbResultSet extends SubQueryResultSet {
994
//
995
// public SubQueryPivotNoImbResultSet(OpSubQuery algebra) throws XMLDBCException {
996
// super(algebra, true, false);
997
// }
998
//
999
// protected void evaluate(boolean non_blocking) throws XMLDBCException {
1000
// }
1001
//
1002
//
1003
//}
1004
//
1005
//class SubQueryPivotImbResultSet extends SubQueryPivotNoImbResultSet {
1006
//
1007
// public SubQueryPivotImbResultSet(OpSubQuery algebra) throws XMLDBCException {
1008
// super(algebra);
1009
// this.imbricated = false;
1010
// }
1011
//
1012
// protected void evaluate(boolean non_blocking) throws XMLDBCException {
1013
// }
1014
//}
1015
Popular Tags