1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }