1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.filter;
20
21
22 import java.util.ArrayList;
23
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.classification.InterfaceStability;
26 import org.apache.hadoop.hbase.Cell;
27 import org.apache.hadoop.hbase.KeyValue;
28 import org.apache.hadoop.hbase.KeyValueUtil;
29 import org.apache.hadoop.hbase.exceptions.DeserializationException;
30 import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
31
32 import com.google.common.base.Preconditions;
33 import com.google.protobuf.InvalidProtocolBufferException;
34
35
36
37
38
39
40
41
42 @InterfaceAudience.Public
43 @InterfaceStability.Stable
44 public class KeyOnlyFilter extends FilterBase {
45
46 boolean lenAsVal;
47 public KeyOnlyFilter() { this(false); }
48 public KeyOnlyFilter(boolean lenAsVal) { this.lenAsVal = lenAsVal; }
49
50 @Override
51 public Cell transformCell(Cell kv) {
52
53
54
55 KeyValue v = KeyValueUtil.ensureKeyValue(kv);
56
57 return v.createKeyOnly(this.lenAsVal);
58 }
59
60 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
61 Preconditions.checkArgument((filterArguments.size() == 0 || filterArguments.size() == 1),
62 "Expected: 0 or 1 but got: %s", filterArguments.size());
63 KeyOnlyFilter filter = new KeyOnlyFilter();
64 if (filterArguments.size() == 1) {
65 filter.lenAsVal = ParseFilter.convertByteArrayToBoolean(filterArguments.get(0));
66 }
67 return filter;
68 }
69
70
71
72
73 public byte [] toByteArray() {
74 FilterProtos.KeyOnlyFilter.Builder builder =
75 FilterProtos.KeyOnlyFilter.newBuilder();
76 builder.setLenAsVal(this.lenAsVal);
77 return builder.build().toByteArray();
78 }
79
80
81
82
83
84
85
86 public static KeyOnlyFilter parseFrom(final byte [] pbBytes)
87 throws DeserializationException {
88 FilterProtos.KeyOnlyFilter proto;
89 try {
90 proto = FilterProtos.KeyOnlyFilter.parseFrom(pbBytes);
91 } catch (InvalidProtocolBufferException e) {
92 throw new DeserializationException(e);
93 }
94 return new KeyOnlyFilter(proto.getLenAsVal());
95 }
96
97
98
99
100
101
102 boolean areSerializedFieldsEqual(Filter o) {
103 if (o == this) return true;
104 if (!(o instanceof KeyOnlyFilter)) return false;
105
106 KeyOnlyFilter other = (KeyOnlyFilter)o;
107 return this.lenAsVal == other.lenAsVal;
108 }
109 }