KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.xquark.mediator.DOMUtils.Tuple;
26 import org.xquark.mediator.runtime.MediatorException;
27 import org.xquark.xml.xdbc.XMLDBCException;
28
29 public class OpUnion extends OpMult {
30     // **********************************************************************
31
// * VERSIONING
32
// **********************************************************************
33
private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
34     private static final String JavaDoc RCSName = "$Name: $"; // * INITIALIZATION
35
// ***********************************************************************
36
/**
37      *
38      */

39
40     public OpUnion(ExecutionPlan plan, Operator[] operators) throws MediatorException {
41         super(plan, null, operators) ;
42         init();
43     }
44
45     private void init() {
46         size = childrenOperator[0].getSize();
47         idsize = childrenOperator[0].getIdSize();
48         ordertype = ORDER_PARALLELIZATION ;
49         //ordertype = this.ORDER_SERIALIZATION ;
50
}
51
52     // #############################################################################
53
// VISITOR STUFF
54
// #############################################################################
55

56     public void accept(OperatorVisitor visitor) throws MediatorException {
57         visitor.visit(this);
58     }
59
60     // ***********************************************************************
61
// * EXECUTE QUERY
62
// ***********************************************************************
63
/**
64      *
65      */

66     protected ResultSet getResultSet(DynamicContext context, OperatorRunnable[] children) throws MediatorException {
67         try {
68             return new UnionResultSet(this, context, children) ;
69         }
70         catch (XMLDBCException e) {
71             throw new MediatorException("Can't construct " + getClass() + ": " + e.getMessage(), e) ;
72         }
73     }
74
75     // **********************************************************************
76
// * OPTIMIZE
77
// **********************************************************************
78
/**
79      * Merge *this* node with the subnode(s). As it should be called
80      * only from source, the returned node must be of type AlgSource.
81      * The return node is a AlgSource where the XQueryExpression is the merging
82      * of all child(ren) nodes' expression.
83      */

84     // protected Algebra mergeWithSub() throws MediatorException { return this ; }
85

86     // **********************************************************************
87
// * DEBUG
88
// **********************************************************************
89
}
90
91 class UnionResultSet extends MultResultSet {
92     // **********************************************************************
93
// * VERSIONING
94
// **********************************************************************
95
private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
96     private static final String JavaDoc RCSName = "$Name: $";
97
98     private int index = 0;
99     // ***********************************************************************
100
// * INITIALIZATION
101
// ***********************************************************************
102
/**
103      *
104      */

105     public UnionResultSet(OpUnion operator, DynamicContext context, OperatorRunnable[] children) throws XMLDBCException {
106         super(operator, context, children) ;
107     }
108
109     // ***********************************************************************
110
// * EVALUATE IMPLEMENTATION
111
// ***********************************************************************
112
/**
113      *
114      * @param non_blocking if the evaluation is blocking, we must
115      * generate all the results before passing.
116      * A cartesian product is necessary blocking.
117      */

118     protected void evaluate(boolean non_blocking) throws MediatorException {
119         
120         //non_blocking = false;
121

122         boolean islet = operator.isLet();
123         Tuple restuple = null;
124         while (true) {
125             while (index < children.length && (children[index].getResultSet() == null || !children[index].getResultSet().hasNext())) {
126                 if (children[index].getResultSet() != null)
127                     children[index].getResultSet().close();
128                 index++;
129             }
130             if (index == children.length) {
131                 setFinishedWhenEmpty() ;
132                 break;
133             }
134                 
135             Tuple tuple = children[index].getResultSet().next() ;
136             if (islet) {
137                 if (restuple == null) restuple = tuple;
138                 else restuple.appendTuple(tuple);
139             }
140             else buftuples.add(tuple) ;
141             if (non_blocking) {
142                 if (!islet && buftuples != null && ! buftuples.isEmpty()) break ;
143             }
144         }
145         if (islet && restuple != null) buftuples.add(restuple);
146     }
147 }
148
Popular Tags