1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.filter;
21
22 import java.io.IOException;
23 import java.util.Objects;
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.exceptions.DeserializationException;
29 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
30 import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
31
32 import com.google.protobuf.InvalidProtocolBufferException;
33
34
35
36
37
38
39
40
41
42 @InterfaceAudience.Public
43 @InterfaceStability.Stable
44 public class WhileMatchFilter extends FilterBase {
45 private boolean filterAllRemaining = false;
46 private Filter filter;
47
48 public WhileMatchFilter(Filter filter) {
49 this.filter = filter;
50 }
51
52 public Filter getFilter() {
53 return filter;
54 }
55
56 @Override
57 public void reset() throws IOException {
58 this.filter.reset();
59 }
60
61 private void changeFAR(boolean value) {
62 filterAllRemaining = filterAllRemaining || value;
63 }
64
65 @Override
66 public boolean filterAllRemaining() throws IOException {
67 return this.filterAllRemaining || this.filter.filterAllRemaining();
68 }
69
70 @Override
71 public boolean filterRowKey(byte[] buffer, int offset, int length) throws IOException {
72 boolean value = filter.filterRowKey(buffer, offset, length);
73 changeFAR(value);
74 return value;
75 }
76
77 @Override
78 public ReturnCode filterKeyValue(Cell v) throws IOException {
79 ReturnCode c = filter.filterKeyValue(v);
80 changeFAR(c != ReturnCode.INCLUDE);
81 return c;
82 }
83
84 @Override
85 public Cell transformCell(Cell v) throws IOException {
86 return filter.transformCell(v);
87 }
88
89 @Override
90 public boolean filterRow() throws IOException {
91 boolean filterRow = this.filter.filterRow();
92 changeFAR(filterRow);
93 return filterRow;
94 }
95
96 @Override
97 public boolean hasFilterRow() {
98 return true;
99 }
100
101
102
103
104 @Override
105 public byte[] toByteArray() throws IOException {
106 FilterProtos.WhileMatchFilter.Builder builder =
107 FilterProtos.WhileMatchFilter.newBuilder();
108 builder.setFilter(ProtobufUtil.toFilter(this.filter));
109 return builder.build().toByteArray();
110 }
111
112
113
114
115
116
117
118 public static WhileMatchFilter parseFrom(final byte [] pbBytes)
119 throws DeserializationException {
120 FilterProtos.WhileMatchFilter proto;
121 try {
122 proto = FilterProtos.WhileMatchFilter.parseFrom(pbBytes);
123 } catch (InvalidProtocolBufferException e) {
124 throw new DeserializationException(e);
125 }
126 try {
127 return new WhileMatchFilter(ProtobufUtil.toFilter(proto.getFilter()));
128 } catch (IOException ioe) {
129 throw new DeserializationException(ioe);
130 }
131 }
132
133
134
135
136
137
138 @Override
139 boolean areSerializedFieldsEqual(Filter o) {
140 if (o == this) return true;
141 if (!(o instanceof WhileMatchFilter)) return false;
142
143 WhileMatchFilter other = (WhileMatchFilter)o;
144 return getFilter().areSerializedFieldsEqual(other.getFilter());
145 }
146
147 @Override
148 public boolean isFamilyEssential(byte[] name) throws IOException {
149 return filter.isFamilyEssential(name);
150 }
151
152 @Override
153 public String toString() {
154 return this.getClass().getSimpleName() + " " + this.filter.toString();
155 }
156
157 @Override
158 public boolean equals(Object obj) {
159 return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
160 }
161
162 @Override
163 public int hashCode() {
164 return Objects.hash(this.filter);
165 }
166 }