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.compare;
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.Operator;
28 import org.apache.expreval.expr.node.BooleanValue;
29 import org.apache.expreval.expr.node.GenericValue;
30 import org.apache.expreval.expr.var.DelegateColumn;
31 import org.apache.expreval.expr.var.GenericColumn;
32 import org.apache.hadoop.hbase.filter.CompareFilter;
33 import org.apache.hadoop.hbase.filter.Filter;
34 import org.apache.hadoop.hbase.hbql.client.HBqlException;
35 import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
36 import org.apache.hadoop.hbase.hbql.impl.Utils;
37 import org.apache.hadoop.hbase.hbql.io.IO;
38 import org.apache.hadoop.hbase.hbql.mapping.FieldType;
39
40 import java.io.DataInput;
41 import java.io.DataOutput;
42 import java.io.IOException;
43
44 public class ByteCompare extends GenericCompare implements BooleanValue {
45
46 private static final Log LOG = LogFactory.getLog(ByteCompare.class);
47
48 public ByteCompare(final GenericValue arg0, final Operator operator, final GenericValue arg1) {
49 super(arg0, operator, arg1);
50 }
51
52 public Class<? extends GenericValue> validateTypes(final GenericValue parentExpr,
53 final boolean allowCollections) throws HBqlException {
54 return this.validateType(BooleanValue.class);
55 }
56
57 public Boolean getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
58 ResultMissingColumnException,
59 NullColumnValueException {
60 final byte val0 = (Byte)this.getValue(0, conn, object);
61 final byte val1 = (Byte)this.getValue(1, conn, object);
62
63 switch (this.getOperator()) {
64 case EQ:
65 return val0 == val1;
66 case GT:
67 return val0 > val1;
68 case GTEQ:
69 return val0 >= val1;
70 case LT:
71 return val0 < val1;
72 case LTEQ:
73 return val0 <= val1;
74 case NOTEQ:
75 return val0 != val1;
76 default:
77 throw new HBqlException("Invalid operator: " + this.getOperator());
78 }
79 }
80
81 public Filter getFilter() throws HBqlException {
82
83 this.validateArgsForCompareFilter();
84
85 final GenericColumn<? extends GenericValue> column;
86 final Object constant;
87 final CompareFilter.CompareOp compareOp;
88
89 if (this.getExprArg(0).isAColumnReference()) {
90 column = ((DelegateColumn)this.getExprArg(0)).getTypedColumn();
91 constant = this.getConstantValue(1);
92 compareOp = this.getOperator().getCompareOpLeft();
93 }
94 else {
95 column = ((DelegateColumn)this.getExprArg(1)).getTypedColumn();
96 constant = this.getConstantValue(0);
97 compareOp = this.getOperator().getCompareOpRight();
98 }
99
100 return this.newSingleColumnValueFilter(column.getColumnAttrib(),
101 compareOp,
102 new ByteComparable((Byte)constant));
103 }
104
105 private static class ByteComparable extends GenericComparable<Byte> {
106
107 public ByteComparable() {
108 }
109
110 public ByteComparable(final Byte value) {
111 this.setTypedValue(value);
112 }
113
114 public int compareTo(final byte[] bytes) {
115
116 if (this.equalValues(bytes))
117 return 0;
118
119 try {
120 final Byte columnValue = IO.getSerialization().getByteFromBytes(bytes);
121 return (this.getTypedValue().compareTo(columnValue));
122 }
123 catch (HBqlException e) {
124 e.printStackTrace();
125 Utils.logException(LOG, e);
126 return 1;
127 }
128 }
129
130 public void write(final DataOutput dataOutput) throws IOException {
131 dataOutput.writeByte(this.getTypedValue());
132 }
133
134 public void readFields(final DataInput dataInput) throws IOException {
135 this.setTypedValue(dataInput.readByte());
136
137 this.setValueInBytes(FieldType.ByteType, this.getTypedValue());
138 }
139 }
140 }