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