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  import org.apache.hadoop.hbase.hbql.mapping.FieldType;
38  
39  import java.io.DataInput;
40  import java.io.DataOutput;
41  import java.io.IOException;
42  
43  public class ByteBetweenStmt extends GenericBetweenStmt {
44  
45      private static final Log LOG = LogFactory.getLog(ByteBetweenStmt.class);
46  
47      public ByteBetweenStmt(final GenericValue expr,
48                             final boolean not,
49                             final GenericValue lower,
50                             final GenericValue upper) {
51          super(ExpressionType.BYTEBETWEEN, not, expr, lower, upper);
52      }
53  
54      public Boolean getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
55                                                                                      ResultMissingColumnException,
56                                                                                      NullColumnValueException {
57          final byte val = (Byte)this.getExprArg(0).getValue(conn, object);
58          final boolean retval = val >= (Byte)this.getExprArg(1).getValue(conn, object)
59                                 && val <= (Byte)this.getExprArg(2).getValue(conn, object);
60  
61          return (this.isNot()) ? !retval : retval;
62      }
63  
64      public Filter getFilter() throws HBqlException {
65  
66          this.validateArgsForBetweenFilter();
67  
68          final GenericColumn<? extends GenericValue> column = ((DelegateColumn)this.getExprArg(0)).getTypedColumn();
69          final byte lowerVal = (Byte)this.getConstantValue(1);
70          final byte upperVal = (Byte)this.getConstantValue(2);
71  
72          return this.newSingleColumnValueFilter(column.getColumnAttrib(),
73                                                 CompareFilter.CompareOp.EQUAL,
74                                                 new ByteBetweenComparable(lowerVal, upperVal));
75      }
76  
77      private static class ByteBetweenComparable extends GenericBetweenComparable<Byte> {
78  
79          public ByteBetweenComparable() {
80          }
81  
82          public ByteBetweenComparable(final byte lowerValue, final byte upperValue) {
83              this.setLowerValue(lowerValue);
84              this.setUpperValue(upperValue);
85          }
86  
87          public int compareTo(final byte[] bytes) {
88              try {
89                  byte val = (Byte)IO.getSerialization().getScalarFromBytes(FieldType.ByteType, bytes);
90                  final byte lowerVal = this.getLowerValue();
91                  final byte upperVal = this.getUpperValue();
92                  return (val >= lowerVal && val <= upperVal) ? 0 : 1;
93              }
94              catch (HBqlException e) {
95                  e.printStackTrace();
96                  Utils.logException(LOG, e);
97                  return 1;
98              }
99          }
100 
101         public void write(final DataOutput dataOutput) throws IOException {
102             dataOutput.writeByte(this.getLowerValue());
103             dataOutput.writeByte(this.getUpperValue());
104         }
105 
106         public void readFields(final DataInput dataInput) throws IOException {
107             this.setLowerValue(dataInput.readByte());
108             this.setUpperValue(dataInput.readByte());
109         }
110     }
111 }