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.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26
27 import com.google.protobuf.InvalidProtocolBufferException;
28 import org.apache.hadoop.hbase.Cell;
29 import org.apache.hadoop.hbase.KeyValue;
30 import org.apache.hadoop.hbase.KeyValueUtil;
31 import org.apache.hadoop.hbase.classification.InterfaceAudience;
32 import org.apache.hadoop.hbase.classification.InterfaceStability;
33 import org.apache.hadoop.hbase.exceptions.DeserializationException;
34 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
35 import org.apache.hadoop.hbase.protobuf.generated.FilterProtos;
36
37
38
39
40
41
42
43
44
45
46
47 @InterfaceAudience.Public
48 @InterfaceStability.Stable
49 final public class FilterList extends Filter {
50
51
52 @InterfaceAudience.Public
53 @InterfaceStability.Stable
54 public enum Operator {
55
56 MUST_PASS_ALL,
57
58 MUST_PASS_ONE
59 }
60
61 private Operator operator = Operator.MUST_PASS_ALL;
62 private FilterListBase filterListBase;
63
64
65
66
67
68
69 public FilterList(final Operator operator, final List<Filter> filters) {
70 if (operator == Operator.MUST_PASS_ALL) {
71 filterListBase = new FilterListWithAND(filters);
72 } else if (operator == Operator.MUST_PASS_ONE) {
73 filterListBase = new FilterListWithOR(filters);
74 } else {
75 throw new IllegalArgumentException("Invalid operator: " + operator);
76 }
77 this.operator = operator;
78 }
79
80
81
82
83
84
85 public FilterList(final List<Filter> filters) {
86 this(Operator.MUST_PASS_ALL, filters);
87 }
88
89
90
91
92
93
94 public FilterList(final Filter... filters) {
95 this(Operator.MUST_PASS_ALL, Arrays.asList(filters));
96 }
97
98
99
100
101
102 public FilterList(final Operator operator) {
103 this(operator, new ArrayList<Filter>());
104 }
105
106
107
108
109
110
111 public FilterList(final Operator operator, final Filter... filters) {
112 this(operator, Arrays.asList(filters));
113 }
114
115
116
117
118
119 public Operator getOperator() {
120 return operator;
121 }
122
123
124
125
126
127 public List<Filter> getFilters() {
128 return filterListBase.getFilters();
129 }
130
131 public int size() {
132 return filterListBase.size();
133 }
134
135 public void addFilter(List<Filter> filters) {
136 filterListBase.addFilterLists(filters);
137 }
138
139
140
141
142
143 public void addFilter(Filter filter) {
144 addFilter(Collections.singletonList(filter));
145 }
146
147 @Override
148 public void reset() throws IOException {
149 filterListBase.reset();
150 }
151
152 @Override
153 public boolean filterRowKey(byte[] rowKey, int offset, int length) throws IOException {
154 return filterListBase.filterRowKey(rowKey, offset, length);
155 }
156
157 @Override
158 public boolean filterAllRemaining() throws IOException {
159 return filterListBase.filterAllRemaining();
160 }
161
162 @Override
163 public Cell transformCell(Cell c) throws IOException {
164 return filterListBase.transformCell(c);
165 }
166
167
168
169
170
171
172 @Deprecated
173 @Override
174 public KeyValue transform(KeyValue v) throws IOException {
175 return KeyValueUtil.ensureKeyValue(transformCell((Cell) v));
176 }
177
178 @Override
179 public ReturnCode filterKeyValue(Cell c) throws IOException {
180 return filterListBase.filterKeyValue(c);
181 }
182
183
184
185
186
187 @Override
188 public void filterRowCells(List<Cell> cells) throws IOException {
189 filterListBase.filterRowCells(cells);
190 }
191
192 @Override
193 public boolean hasFilterRow() {
194 return filterListBase.hasFilterRow();
195 }
196
197 @Override
198 public boolean filterRow() throws IOException {
199 return filterListBase.filterRow();
200 }
201
202
203
204
205 @Override
206 public byte[] toByteArray() throws IOException {
207 FilterProtos.FilterList.Builder builder = FilterProtos.FilterList.newBuilder();
208 builder.setOperator(FilterProtos.FilterList.Operator.valueOf(operator.name()));
209 ArrayList<Filter> filters = filterListBase.getFilters();
210 for (int i = 0, n = filters.size(); i < n; i++) {
211 builder.addFilters(ProtobufUtil.toFilter(filters.get(i)));
212 }
213 return builder.build().toByteArray();
214 }
215
216
217
218
219
220
221
222 public static FilterList parseFrom(final byte[] pbBytes) throws DeserializationException {
223 FilterProtos.FilterList proto;
224 try {
225 proto = FilterProtos.FilterList.parseFrom(pbBytes);
226 } catch (InvalidProtocolBufferException e) {
227 throw new DeserializationException(e);
228 }
229
230 List<Filter> rowFilters = new ArrayList<Filter>(proto.getFiltersCount());
231 try {
232 List<FilterProtos.Filter> filtersList = proto.getFiltersList();
233 for (int i = 0, n = filtersList.size(); i < n; i++) {
234 rowFilters.add(ProtobufUtil.toFilter(filtersList.get(i)));
235 }
236 } catch (IOException ioe) {
237 throw new DeserializationException(ioe);
238 }
239 return new FilterList(Operator.valueOf(proto.getOperator().name()), rowFilters);
240 }
241
242
243
244
245
246
247 @Override
248 boolean areSerializedFieldsEqual(Filter other) {
249 if (other == this) return true;
250 if (!(other instanceof FilterList)) return false;
251
252 FilterList o = (FilterList) other;
253 return this.getOperator().equals(o.getOperator())
254 && ((this.getFilters() == o.getFilters()) || this.getFilters().equals(o.getFilters()));
255 }
256
257 @Override
258 @Deprecated
259 public KeyValue getNextKeyHint(KeyValue currentKV) throws IOException {
260 return KeyValueUtil.ensureKeyValue(getNextCellHint((Cell) currentKV));
261 }
262
263 @Override
264 public Cell getNextCellHint(Cell currentCell) throws IOException {
265 return this.filterListBase.getNextCellHint(currentCell);
266 }
267
268 @Override
269 public boolean isFamilyEssential(byte[] name) throws IOException {
270 return this.filterListBase.isFamilyEssential(name);
271 }
272
273 @Override
274 public void setReversed(boolean reversed) {
275 this.reversed = reversed;
276 this.filterListBase.setReversed(reversed);
277 }
278
279 @Override
280 public boolean isReversed() {
281 assert this.reversed == this.filterListBase.isReversed();
282 return this.reversed;
283 }
284
285
286
287
288
289 @Deprecated
290 public String toString(int maxFilters) {
291 return this.filterListBase.toString();
292 }
293
294 @Override
295 public String toString() {
296 return this.filterListBase.toString();
297 }
298 }