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 import java.util.ArrayList;
22 import java.util.Objects;
23
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.classification.InterfaceStability;
26 import org.apache.hadoop.hbase.Cell;
27 import org.apache.hadoop.hbase.exceptions.DeserializationException;
28 import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
29
30 import com.google.common.base.Preconditions;
31 import com.google.protobuf.InvalidProtocolBufferException;
32
33
34
35
36
37
38 @InterfaceAudience.Public
39 @InterfaceStability.Stable
40 public class FirstKeyOnlyFilter extends FilterBase {
41 private boolean foundKV = false;
42
43 public FirstKeyOnlyFilter() {
44 }
45
46 @Override
47 public void reset() {
48 foundKV = false;
49 }
50
51 @Override
52 public ReturnCode filterKeyValue(Cell v) {
53 if(foundKV) return ReturnCode.NEXT_ROW;
54 foundKV = true;
55 return ReturnCode.INCLUDE;
56 }
57
58
59
60 @Override
61 public Cell transformCell(Cell v) {
62 return v;
63 }
64
65 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
66 Preconditions.checkArgument(filterArguments.size() == 0,
67 "Expected 0 but got: %s", filterArguments.size());
68 return new FirstKeyOnlyFilter();
69 }
70
71
72
73
74 protected boolean hasFoundKV() {
75 return this.foundKV;
76 }
77
78
79
80
81
82 protected void setFoundKV(boolean value) {
83 this.foundKV = value;
84 }
85
86
87
88
89 @Override
90 public byte [] toByteArray() {
91 FilterProtos.FirstKeyOnlyFilter.Builder builder =
92 FilterProtos.FirstKeyOnlyFilter.newBuilder();
93 return builder.build().toByteArray();
94 }
95
96
97
98
99
100
101
102 public static FirstKeyOnlyFilter parseFrom(final byte [] pbBytes)
103 throws DeserializationException {
104
105 try {
106 FilterProtos.FirstKeyOnlyFilter.parseFrom(pbBytes);
107 } catch (InvalidProtocolBufferException e) {
108 throw new DeserializationException(e);
109 }
110
111 return new FirstKeyOnlyFilter();
112 }
113
114
115
116
117
118
119 @Override
120 boolean areSerializedFieldsEqual(Filter o) {
121 if (o == this) return true;
122 if (!(o instanceof FirstKeyOnlyFilter)) return false;
123
124 return true;
125 }
126
127 @Override
128 public boolean equals(Object obj) {
129 return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
130 }
131
132 @Override
133 public int hashCode() {
134 return Objects.hashCode(foundKV);
135 }
136 }