View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.expreval.expr.betweenstmt;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.expreval.client.NullColumnValueException;
26  import org.apache.expreval.client.ResultMissingColumnException;
27  import org.apache.expreval.expr.ExpressionType;
28  import org.apache.expreval.expr.node.GenericValue;
29  import org.apache.expreval.expr.var.DelegateColumn;
30  import org.apache.expreval.expr.var.GenericColumn;
31  import org.apache.hadoop.hbase.filter.CompareFilter;
32  import org.apache.hadoop.hbase.filter.Filter;
33  import org.apache.hadoop.hbase.hbql.client.HBqlException;
34  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
35  import org.apache.hadoop.hbase.hbql.impl.Utils;
36  import org.apache.hadoop.hbase.hbql.io.IO;
37  
38  import java.io.DataInput;
39  import java.io.DataOutput;
40  import java.io.IOException;
41  
42  public class StringBetweenStmt extends GenericBetweenStmt {
43  
44      private static final Log LOG = LogFactory.getLog(StringBetweenStmt.class);
45  
46      public StringBetweenStmt(final GenericValue arg0,
47                               final boolean not,
48                               final GenericValue arg1,
49                               final GenericValue arg2) {
50          super(ExpressionType.STRINGBETWEEN, not, arg0, arg1, arg2);
51      }
52  
53      public Boolean getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
54                                                                                      ResultMissingColumnException,
55                                                                                      NullColumnValueException {
56          final String val = (String)this.getExprArg(0).getValue(conn, object);
57          final String lowerVal = (String)this.getExprArg(1).getValue(conn, object);
58          final String upperVal = (String)this.getExprArg(2).getValue(conn, object);
59  
60          final boolean retval = val.compareTo(lowerVal) >= 0 && val.compareTo(upperVal) <= 0;
61  
62          return (this.isNot()) ? !retval : retval;
63      }
64  
65      public Filter getFilter() throws HBqlException {
66  
67          this.validateArgsForBetweenFilter();
68  
69          final GenericColumn<? extends GenericValue> column = ((DelegateColumn)this.getExprArg(0)).getTypedColumn();
70          final String lowerVal = (String)this.getConstantValue(1);
71          final String upperVal = (String)this.getConstantValue(2);
72  
73          return this.newSingleColumnValueFilter(column.getColumnAttrib(),
74                                                 CompareFilter.CompareOp.EQUAL,
75                                                 new StringBetweenComparable(lowerVal, upperVal));
76      }
77  
78      private static class StringBetweenComparable extends GenericBetweenComparable<String> {
79  
80          public StringBetweenComparable() {
81          }
82  
83          public StringBetweenComparable(final String lowerValue, final String upperValue) {
84              this.setLowerValue(lowerValue);
85              this.setUpperValue(upperValue);
86          }
87  
88          public int compareTo(final byte[] bytes) {
89  
90              try {
91                  final String val = IO.getSerialization().getStringFromBytes(bytes);
92                  final String lowerVal = this.getLowerValue();
93                  final String upperVal = this.getUpperValue();
94                  return (val.compareTo(lowerVal) >= 0 && val.compareTo(upperVal) <= 0) ? 0 : 1;
95              }
96              catch (HBqlException e) {
97                  e.printStackTrace();
98                  Utils.logException(LOG, e);
99                  return 1;
100             }
101         }
102 
103         public void write(final DataOutput dataOutput) throws IOException {
104             dataOutput.writeUTF(this.getLowerValue());
105             dataOutput.writeUTF(this.getUpperValue());
106         }
107 
108         public void readFields(final DataInput dataInput) throws IOException {
109             this.setLowerValue(dataInput.readUTF());
110             this.setUpperValue(dataInput.readUTF());
111         }
112     }
113 }