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