KickJava   Java API By Example, From Geeks To Geeks.

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


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.EvaluationVisitor;
26 import org.xquark.mediator.DOMUtils.Tuple;
27 import org.xquark.mediator.runtime.MediatorException;
28 import org.xquark.xquery.parser.XQueryException;
29 import org.xquark.xquery.parser.XQueryExpression;
30
31
32 public class OpRestrict extends OpUn {
33     // **********************************************************************
34
// * VERSIONING
35
// **********************************************************************
36
private static final String JavaDoc RCSRevision = "$Revision: 1.12 $";
37     private static final String JavaDoc RCSName = "$Name: $"; // * INITIALIZATION
38
// ***********************************************************************
39
/**
40      *
41      */

42     public OpRestrict(ExecutionPlan plan, XQueryExpression expression,Operator childexp) throws MediatorException {
43         super(plan, expression, childexp) ;
44         size = childexp.getSize();
45         idsize = childexp.getIdSize();
46     }
47
48     // #############################################################################
49
// VISITOR STUFF
50
// #############################################################################
51

52     public void accept(OperatorVisitor visitor) throws MediatorException {
53         visitor.visit(this);
54     }
55
56     // ***********************************************************************
57
// * EXECUTE QUERY
58
// ***********************************************************************
59
/**
60      *
61      */

62     protected ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException {
63         return new RestrictResultSet(this, context, child) ;
64     }
65
66     // **********************************************************************
67
// * OPTIMIZE
68
// **********************************************************************
69

70     // **********************************************************************
71
// * DEBUG
72
// **********************************************************************
73
}
74
75
76
77 class RestrictResultSet extends UnResultSet {
78     // **********************************************************************
79
// * VERSIONING
80
// **********************************************************************
81
private static final String JavaDoc RCSRevision = "$Revision: 1.12 $";
82     private static final String JavaDoc RCSName = "$Name: $"; // * INITIALIZATION
83
// ***********************************************************************
84

85     private EvaluationVisitor evalvisitor = null;
86
87     /**
88      *
89      */

90     public RestrictResultSet(OpRestrict operator, DynamicContext context, OperatorRunnable child) throws MediatorException {
91         super(operator, context, child) ;
92     }
93
94     // ***********************************************************************
95
// * EVALUATE IMPLEMENTATION
96
// ***********************************************************************
97
/**
98      * @param non_blocking if the evaluation is blocking, we must
99      * generate all the results before passing.
100      */

101     protected void evaluate(boolean non_blocking) throws MediatorException {
102         
103         while (true) {
104             if (resultset.hasNext()) {
105                 Tuple tuple = resultset.next() ;
106                 try {
107                     if (expression == null) buftuples.add(tuple) ;
108                     else {
109                         if (evalvisitor == null)
110                             evalvisitor = new EvaluationVisitor(this.operator.getPlan().getSchemaManager()) ;
111                         context.addCurrentTuple(tuple);
112                         //evalvisitor.reset(tuple);
113
evalvisitor.reset(context.getCurrentTuples());
114                         evalvisitor.setReturnType(EvaluationVisitor.BOOLEAN_TYPE);
115                         expression.accept(evalvisitor) ;
116                         context.deleteCurrentTuple(tuple);
117                         if (evalvisitor.getVerdict()) {
118                             buftuples.add(tuple) ;
119                         }
120                         else if (! resultset.hasNext()) setFinishedWhenEmpty() ;
121                     }
122                 }
123                 catch (XQueryException e) {
124                     throw new MediatorException("AlgRestrict.evaluate: " + e.getMessage(), e) ;
125                 }
126
127                 if (non_blocking) {
128                     // can make higher buffering by modifying this test
129
if ((buftuples != null) && (! buftuples.isEmpty()))
130                         break ;
131                 }
132             }
133             else {
134                 setFinishedWhenEmpty() ;
135                 resultset.close();
136                 return ;
137             }
138         }
139         if (! resultset.hasNext()) {
140             setFinishedWhenEmpty() ;
141             resultset.close();
142         }
143         return;
144     }
145 }
146
Popular Tags