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.instmt;
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.TypeSupport;
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.InvalidServerFilterException;
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.util.Lists;
38 import org.apache.hadoop.hbase.util.Bytes;
39
40 import java.io.DataInput;
41 import java.io.DataOutput;
42 import java.io.IOException;
43 import java.util.Collection;
44 import java.util.List;
45
46 public class StringInStmt extends GenericInStmt {
47
48 private static final Log LOG = LogFactory.getLog(StringInStmt.class);
49
50 public StringInStmt(final GenericValue arg0, final boolean not, final List<GenericValue> inList) {
51 super(arg0, not, inList);
52 }
53
54 protected boolean evaluateInList(final Object object) throws HBqlException,
55 ResultMissingColumnException,
56 NullColumnValueException {
57 final String attribVal = (String)this.getExprArg(0).getValue(null, object);
58
59 for (final GenericValue obj : this.getInList()) {
60
61
62 final Object objval = obj.getValue(null, object);
63 if (TypeSupport.isACollection(objval)) {
64 for (final GenericValue val : (Collection<GenericValue>)objval) {
65 if (attribVal.equals(val.getValue(null, object)))
66 return true;
67 }
68 }
69 else {
70 if (attribVal.equals(objval))
71 return true;
72 }
73 }
74 return false;
75 }
76
77 public Filter getFilter() throws HBqlException {
78
79 this.validateArgsForInFilter();
80
81 final GenericColumn<? extends GenericValue> column = ((DelegateColumn)this.getExprArg(0)).getTypedColumn();
82 final List<byte[]> inValues = Lists.newArrayList();
83 try {
84 for (final GenericValue obj : this.getInList()) {
85 final byte[] val = Bytes.toBytes((String)obj.getValue(null, null));
86 inValues.add(val);
87 }
88 }
89 catch (ResultMissingColumnException e) {
90 throw new InvalidServerFilterException();
91 }
92 catch (NullColumnValueException e) {
93 throw new InvalidServerFilterException();
94 }
95
96 return this.newSingleColumnValueFilter(column.getColumnAttrib(),
97 CompareFilter.CompareOp.EQUAL,
98 new StringInComparable(inValues));
99 }
100
101 private static class StringInComparable extends GenericInComparable<byte[]> {
102
103 public StringInComparable() {
104 }
105
106 public StringInComparable(final List<byte[]> inValues) {
107 this.setInValue(inValues);
108 }
109
110 public int compareTo(final byte[] val) {
111 for (final byte[] inVal : this.getInValues()) {
112 if (Bytes.equals(val, inVal))
113 return 0;
114 }
115 return 1;
116 }
117
118 public void write(final DataOutput dataOutput) throws IOException {
119 try {
120 final byte[] b = IO.getSerialization().getObjectAsBytes(this.getInValues());
121 Bytes.writeByteArray(dataOutput, b);
122 }
123 catch (HBqlException e) {
124 e.printStackTrace();
125 Utils.logException(LOG, e);
126 throw new IOException("HBqlException: " + e.getCause());
127 }
128 }
129
130 public void readFields(final DataInput dataInput) throws IOException {
131 try {
132 final byte[] b = Bytes.readByteArray(dataInput);
133 final List<byte[]> inValues = (List<byte[]>)IO.getSerialization().getObjectFromBytes(b);
134 this.setInValue(inValues);
135 }
136 catch (HBqlException e) {
137 e.printStackTrace();
138 Utils.logException(LOG, e);
139 throw new IOException("HBqlException: " + e.getCause());
140 }
141 }
142 }
143 }