]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
88201e4410ee8b0b9393156f5a7ad8e55a0a9f04
[packages/trusty/mysql-wsrep-5.6.git] /
1 /*
2    Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
16 */
17
18 package com.mysql.clusterj.core.query;
19
20 import com.mysql.clusterj.core.spi.QueryExecutionContext;
21 import com.mysql.clusterj.core.store.Operation;
22 import com.mysql.clusterj.core.store.ScanFilter;
23 import com.mysql.clusterj.core.store.ScanOperation;
24 import com.mysql.clusterj.ClusterJException;
25 import com.mysql.clusterj.ClusterJFatalInternalException;
26 import com.mysql.clusterj.query.Predicate;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 public class AndPredicateImpl extends PredicateImpl {
31
32     List<PredicateImpl> predicates = new ArrayList<PredicateImpl>();
33
34     public AndPredicateImpl(QueryDomainTypeImpl<?> dobj,
35             PredicateImpl left, PredicateImpl right) {
36         super(dobj);
37         predicates.add(left);
38         predicates.add(right);
39     }
40
41     @Override
42     public Predicate and(Predicate predicate) {
43         if (predicate instanceof ComparativePredicateImpl) {
44             predicates.add((PredicateImpl)predicate);
45             return this;
46         } else if (predicate instanceof AndPredicateImpl) {
47             predicates.addAll(((AndPredicateImpl)predicate).predicates);
48             return this;
49         } else if (predicate instanceof OrPredicateImpl) {
50             predicates.add((PredicateImpl)predicate);
51             return this;
52         } else if (predicate instanceof InPredicateImpl) {
53             predicates.add((PredicateImpl)predicate);
54             return this;
55         } else if (predicate instanceof NotPredicateImpl) {
56             predicates.add((PredicateImpl)predicate);
57             return this;
58         } else {
59             throw new UnsupportedOperationException(
60                     local.message("ERR_NotImplemented"));
61         }
62     }
63
64     public Predicate or(Predicate predicate) {
65         throw new UnsupportedOperationException(
66                 local.message("ERR_NotImplemented"));
67     }
68
69     public Predicate not() {
70         throw new UnsupportedOperationException(
71                 local.message("ERR_NotImplemented"));
72     }
73
74     @Override
75     public void markParameters() {
76         for (PredicateImpl predicateImpl: predicates) {
77             predicateImpl.markParameters();
78         }
79     }
80
81     @Override
82     public void unmarkParameters() {
83         for (PredicateImpl predicateImpl: predicates) {
84             predicateImpl.unmarkParameters();
85         }
86     }
87
88     /** Create a filter for the operation. Set the conditions into the
89      * new filter, one for each predicate.
90      * @param context the query execution context with the parameter values
91      * @param op the operation
92      */
93     @Override
94     public void filterCmpValue(QueryExecutionContext context,
95             ScanOperation op) {
96         try {
97             ScanFilter filter = op.getScanFilter(context);
98             filter.begin(ScanFilter.Group.GROUP_AND);
99             for (PredicateImpl predicate: predicates) {
100                 predicate.filterCmpValue(context, op, filter);
101             }
102             filter.end();
103         } catch (Exception ex) {
104             throw new ClusterJException(
105                     local.message("ERR_Get_NdbFilter"), ex);
106         }
107     }
108
109     /** Set the keys into the operation for each predicate.
110      * Each predicate must be an equal predicate for a primary or unique key.
111      */
112     public void operationEqual(QueryExecutionContext context,
113             Operation op) {
114         for (PredicateImpl predicate: predicates) {
115             if (!(predicate instanceof EqualPredicateImpl)) {
116                 throw new ClusterJFatalInternalException(
117                         local.message("ERR_Implementation_Should_Not_Occur"));
118             }
119             predicate.operationEqual(context, op);
120         }
121     }
122
123     /** Get the best index for the operation. Delegate to the method
124      * in the superclass, passing the array of predicates.
125      *
126      * @return the best index
127      */
128     @Override
129     public CandidateIndexImpl getBestCandidateIndex(QueryExecutionContext context) {
130         return getBestCandidateIndexFor(context, predicates.toArray(
131                 new PredicateImpl[predicates.size()]));
132     }
133
134     /** Get the number of conditions in the top level predicate.
135      * This is used to determine whether a hash index can be used. If there
136      * are exactly the number of conditions as index columns, then the
137      * hash index might be used.
138      * For AND predicates, there is one condition for each predicate included
139      * in the AND.
140      * AndPredicateImpl overrides this method.
141      * @return the number of conditions
142      */
143     @Override
144     protected int getNumberOfConditionsInPredicate() {
145         return predicates.size();
146     }
147 }