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.io.IOException;
23 import java.util.ArrayList;
24
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26 import org.apache.hadoop.hbase.classification.InterfaceStability;
27 import org.apache.hadoop.hbase.Cell;
28 import org.apache.hadoop.hbase.KeyValue;
29 import org.apache.hadoop.hbase.KeyValueUtil;
30 import org.apache.hadoop.hbase.exceptions.DeserializationException;
31 import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
32 import org.apache.hadoop.hbase.util.Bytes;
33
34 import com.google.common.base.Preconditions;
35 import com.google.protobuf.InvalidProtocolBufferException;
36
37
38
39
40
41
42
43
44 @InterfaceAudience.Public
45 @InterfaceStability.Stable
46 public class KeyOnlyFilter extends FilterBase {
47
48 boolean lenAsVal;
49 public KeyOnlyFilter() { this(false); }
50 public KeyOnlyFilter(boolean lenAsVal) { this.lenAsVal = lenAsVal; }
51
52 @Override
53 public Cell transformCell(Cell cell) {
54 return createKeyOnlyCell(cell);
55 }
56
57 private Cell createKeyOnlyCell(Cell c) {
58
59
60 int dataLen = lenAsVal ? Bytes.SIZEOF_INT : 0;
61 int keyOffset = (2 * Bytes.SIZEOF_INT);
62 int keyLen = KeyValueUtil.keyLength(c);
63 byte[] newBuffer = new byte[keyLen + keyOffset + dataLen];
64 Bytes.putInt(newBuffer, 0, keyLen);
65 Bytes.putInt(newBuffer, Bytes.SIZEOF_INT, dataLen);
66 KeyValueUtil.appendKeyTo(c, newBuffer, keyOffset);
67 if (lenAsVal) {
68 Bytes.putInt(newBuffer, newBuffer.length - dataLen, c.getValueLength());
69 }
70 return new KeyValue(newBuffer);
71 }
72
73 @Override
74 public ReturnCode filterKeyValue(Cell ignored) throws IOException {
75 return ReturnCode.INCLUDE;
76 }
77
78 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
79 Preconditions.checkArgument((filterArguments.size() == 0 || filterArguments.size() == 1),
80 "Expected: 0 or 1 but got: %s", filterArguments.size());
81 KeyOnlyFilter filter = new KeyOnlyFilter();
82 if (filterArguments.size() == 1) {
83 filter.lenAsVal = ParseFilter.convertByteArrayToBoolean(filterArguments.get(0));
84 }
85 return filter;
86 }
87
88
89
90
91 public byte [] toByteArray() {
92 FilterProtos.KeyOnlyFilter.Builder builder =
93 FilterProtos.KeyOnlyFilter.newBuilder();
94 builder.setLenAsVal(this.lenAsVal);
95 return builder.build().toByteArray();
96 }
97
98
99
100
101
102
103
104 public static KeyOnlyFilter parseFrom(final byte [] pbBytes)
105 throws DeserializationException {
106 FilterProtos.KeyOnlyFilter proto;
107 try {
108 proto = FilterProtos.KeyOnlyFilter.parseFrom(pbBytes);
109 } catch (InvalidProtocolBufferException e) {
110 throw new DeserializationException(e);
111 }
112 return new KeyOnlyFilter(proto.getLenAsVal());
113 }
114
115
116
117
118
119
120 boolean areSerializedFieldsEqual(Filter o) {
121 if (o == this) return true;
122 if (!(o instanceof KeyOnlyFilter)) return false;
123
124 KeyOnlyFilter other = (KeyOnlyFilter)o;
125 return this.lenAsVal == other.lenAsVal;
126 }
127 }