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