-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfilters.py
498 lines (363 loc) · 15.2 KB
/
filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
"""
Copyright 2015 Samuel Curley
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import traceback
import pb.Filter_pb2 as pbFilter
import pb.Comparator_pb2 as pbComparator
from pb.HBase_pb2 import BytesBytesPair as pbBytesBytesPair
# You're brave to venture into this file.
filter_path = "org.apache.hadoop.hbase.filter."
comparator_path = "org.apache.hadoop.hbase.filter."
# Operators
MUST_PASS_ALL = 1
MUST_PASS_ONE = 2
# BitwiseOps
AND = 1
OR = 2
XOR = 3
# CompareTypes
LESS = 0
LESS_OR_EQUAL = 1
EQUAL = 2
NOT_EQUAL = 3
GREATER_OR_EQUAL = 4
GREATER = 5
NO_OP = 6
# A FilterList is also a Filter. But it's also a list of Filters with an
# operator. This allows you to build up complicated boolean expressions by
# chaining FilterLists.
class FilterList:
def __init__(self, operator, *arg):
self.filter_type = pbFilter.FilterList
self.name = filter_path + "FilterList"
self.operator = operator
self.filters = []
try:
for incoming_filter in arg:
self.filters.append(_to_filter(incoming_filter))
except TypeError:
# They passed a single filter and not a sequence of filters.
self.filters.append(_to_filter(filters))
def add_filters(self, *arg):
for new_filter in arg:
self.filters.append(_to_filter(new_filter))
class ColumnCountGetFilter:
def __init__(self, limit):
self.filter_type = pbFilter.ColumnCountGetFilter
self.name = filter_path + "ColumnCountGetFilter"
self.limit = limit
class ColumnPaginationFilter:
def __init__(self, limit, offset, column_offset):
self.filter_type = pbFilter.ColumnPaginationFilter
self.name = filter_path + "ColumnPaginationFilter"
self.limit = limit
self.offset = offset
self.column_offset = column_offset
class ColumnPrefixFilter:
def __init__(self, prefix):
self.filter_type = pbFilter.ColumnPrefixFilter
self.name = filter_path + "ColumnPrefixFilter"
self.prefix = prefix
class ColumnRangeFilter:
def __init__(self, min_column, min_column_inclusive, max_column, max_column_inclusive):
self.filter_type = pbFilter.ColumnRangeFilter
self.name = filter_path + "ColumnRangeFilter"
self.min_column = min_column
self.min_column_inclusive = min_column_inclusive
self.max_column = max_column
self.max_column_inclusive = max_column_inclusive
class CompareFilter:
def __init__(self, compare_op, comparator):
self.filter_type = pbFilter.CompareFilter
self.name = filter_path + "CompareFilter"
self.compare_op = compare_op
self.comparator = _to_comparator(comparator)
class DependentColumnFilter:
def __init__(self, compare_filter, column_family, column_qualifier, drop_dependent_column):
self.filter_type = pbFilter.DependentColumnFilter
self.name = filter_path + "DependentColumnFilter"
self.compare_filter = _to_filter(compare_filter)
self.column_family = column_family
self.column_qualifier = column_qualifier
self.drop_dependent_column = drop_dependent_column
class FamilyFilter:
def __init__(self, compare_filter):
self.filter_type = pbFilter.FamilyFilter
self.name = filter_path + "FamilyFilter"
self.compare_filter = _to_filter(compare_filter)
class FilterWrapper:
def __init__(self, new_filter):
self.filter_type = pbFilter.FilterWrapper
self.name = filter_path + "FilterWrapper"
self.filter = _to_filter(new_filter)
class FirstKeyOnlyFilter:
def __init__(self):
self.filter_type = pbFilter.FirstKeyOnlyFilter
self.name = filter_path + "FirstKeyOnlyFilter"
class FirstKeyValueMatchingQualifiersFilter:
def __init__(self, qualifiers):
self.filter_type = pbFilter.FirstKeyValueMatchingQualifiersFilter
self.name = filter_path + "FirstKeyValueMatchingQualifiersFilter"
self.qualifiers = qualifiers
class FuzzyRowFilter:
def __init__(self, fuzzy_keys_data):
self.filter_type = pbFilter.FuzzyRowFilter
self.name = filter_path + "FuzzyRowFilter"
self.fuzzy_keys_data = []
try:
for fuzz in fuzzy_keys_data:
self.fuzzy_keys_data.append(_to_bytes_bytes_pair(fuzz))
except TypeError:
# They passed a single element and not a sequence of elements.
self.fuzzy_keys_data.append(_to_bytes_bytes_pair(fuzzy_keys_data))
class InclusiveStopFilter:
def __init__(self, stop_row_key):
self.filter_type = pbFilter.InclusiveStopFilter
self.name = filter_path + "InclusiveStopFilter"
self.stop_row_key = stop_row_key
class KeyOnlyFilter:
def __init__(self, len_as_val):
self.filter_type = pbFilter.KeyOnlyFilter
self.name = filter_path + "KeyOnlyFilter"
self.len_as_val = len_as_val
class MultipleColumnPrefixFilter:
def __init__(self, sorted_prefixes):
self.filter_type = pbFilter.MultipleColumnPrefixFilter
self.name = filter_path + "MultipleColumnPrefixFilter"
if isinstance(sorted_prefixes, list):
self.sorted_prefixes = sorted_prefixes
else:
self.sorted_prefixes = [sorted_prefixes]
class PageFilter:
def __init__(self, page_size):
self.filter_type = pbFilter.PageFilter
self.name = filter_path + "PageFilter"
self.page_size = page_size
class PrefixFilter:
def __init__(self, prefix):
self.filter_type = pbFilter.PrefixFilter
self.name = filter_path + "PrefixFilter"
self.prefix = prefix
class QualifierFilter:
def __init__(self, compare_filter):
self.filter_type = pbFilter.QualifierFilter
self.name = filter_path + "QualifierFilter"
self.compare_filter = _to_pb_filter(compare_filter)
class RandomRowFilter:
def __init__(self, chance):
self.filter_type = pbFilter.RandomRowFilter
self.name = filter_path + "RandomRowFilter"
self.chance = chance
class RowFilter:
def __init__(self, compare_filter):
self.filter_type = pbFilter.RowFilter
self.name = filter_path + "RowFilter"
self.compare_filter = _to_filter(compare_filter)
class SkipColumnValueExcludeFilter:
def __init__(self, single_column_value_filter):
self.filter_type = pbFilter.SkipColumnValueExcludeFilter
self.name = filter_path + "SkipColumnValueExcludeFilter"
self.single_column_value_filter = _to_filter(
single_column_value_filter)
class SkipColumnValueFilter:
def __init__(self, compare_op, comparator, column_family, column_qualifier, filter_if_missing, latest_version_only):
self.filter_type = pbFilter.SkipColumnValueFilter
self.name = filter_path + "SkipColumnValueFilter"
self.compare_op = compare_op
self.comparator = _to_comparator(comparator)
self.column_family = column_family
self.column_qualifier = column_qualifier
self.filter_if_missing = filter_if_missing
self.latest_version_only = latest_version_only
class SkipFilter:
def __init__(self, orig_filter):
self.filter_type = pbFilter.SkipFilter
self.name = filter_path + "SkipFilter"
self.filter = orig_filter
class TimestampsFilter:
def __init__(self, timestamps):
self.filter_type = pbFilter.TimestampsFilter
self.name = filter_path + "TimestampsFilter"
if isinstance(timestamps, list):
self.timestamps = timestamps
else:
self.timestamps = [timestamps]
class ValueFilter:
def __init__(self, compare_filter):
self.filter_type = pbFilter.ValueFilter
self.name = filter_path + "ValueFilter"
self.compare_filter = _to_filter(compare_filter)
class WhileMatchFilter:
def __init__(self, origFilter):
self.filter_type = pbFilter.WhileMatchFilter
self.name = filter_path + "WhileMatchFilter"
self.filter = _to_filter(origFilter)
class FilterAllFilter:
def __init__(self):
self.filter_type = pbFilter.FilterAllFilter
self.name = filter_path + "FilterAllFilter"
class MultiRowRangeFilter:
def __init__(self, row_range_list):
self.filter_type = pbFilter.MultiRowRangeFilter
self.name = filter_path + "MultiRowRangeFilter"
self.row_range_list = []
try:
for row in row_range_list:
self.row_range_list.append(_to_row_range(row))
except TypeError:
# They passed a single element and not a sequence of elements.
self.row_range_list.append(_to_row_range(row_range_list))
# Instead of having to define a _to_filter method for every filter I
# instead opted to be hard core and define it once and support every
# filter.
#
# _to_filter will take any of the above classes, create the associated pb
# type, iterate over any special variables and set them accordingly,
# serialize the special pb filter type into a standard pb Filter object
# and return that.
def _to_filter(orig_filter):
if orig_filter is None:
return None
ft = pbFilter.Filter()
ft.name = orig_filter.name
ft.serialized_filter = _to_pb_filter(orig_filter).SerializeToString()
return ft
def _to_pb_filter(orig_filter):
try:
ft2 = orig_filter.filter_type()
members = [attr for attr in dir(orig_filter) if not callable(
attr) and not attr.startswith("__") and attr not in ["name", "filter_type", "add_filters"]]
for member in members:
try:
val = getattr(orig_filter, member)
if val is not None:
# skip none value that should be optional
setattr(ft2, member, val)
except AttributeError:
# It's a repeated element and we need to 'extend' it.
el = getattr(ft2, member)
try:
el.extend(getattr(orig_filter, member))
except AttributeError:
# Just kidding. It's a composite field.
el.CopyFrom(getattr(orig_filter, member))
return ft2
except Exception as ex:
raise ValueError("Malformed Filter provided, %s %s" % (ex, traceback.format_exc()))
class ByteArrayComparable:
def __init__(self, value):
self.comparable_type = pbComparator.ByteArrayComparable
self.value = value
# Just like _to_filter, but for comparables.
def _to_comparable(orig_cmp):
try:
new_cmp = orig_cmp.comparable_type()
members = [attr for attr in dir(orig_cmp) if not callable(
attr) and not attr.startswith("__") and attr not in ["name", "comparable_type"]]
for member in members:
val = getattr(orig_cmp, member)
# skip none value that should be optional
if val is not None:
setattr(new_cmp, member, val)
return new_cmp
except Exception as ex:
raise ValueError("Malformed Comparable provided %s %s" % (ex, traceback.format_exc()))
class BinaryComparator:
def __init__(self, comparable):
self.comparator_type = pbComparator.BinaryComparator
self.name = comparator_path + "BinaryComparator"
self.comparable = _to_comparable(comparable)
class LongComparator:
def __init__(self, comparable):
self.comparator_type = pbComparator.LongComparator
self.name = comparator_path + "LongComparator"
self.comparable = _to_comparable(comparable)
class BinaryPrefixComparator:
def __init__(self, comparable):
self.comparator_type = pbComparator.BinaryPrefixComparator
self.name = comparator_path + "BinaryPrefixComparator"
self.comparable = _to_comparable(comparable)
class BitComparator:
def __init__(self, comparable, bitwise_op):
self.comparator_type = pbComparator.BitComparator
self.name = comparator_path + "BitComparator"
self.comparable = _to_comparable(comparable)
self.bitwise_op = bitwise_op
class NullComparator:
def __init__(self):
self.comparator_type = pbComparator.NullComparator
self.name = comparator_path + "NullComparator"
class RegexStringComparator:
def __init__(self, pattern, pattern_flags, charset, engine):
self.comparator_type = pbComparator.RegexStringComparator
self.name = comparator_path + "RegexStringComparator"
self.pattern = pattern
self.pattern_flags = pattern_flags
self.charset = charset
self.engine = engine
class StringComparator:
def __init__(self, substr):
self.comparator_type = pbComparator.BinaryPrefixComparator
self.name = comparator_path + "BinaryPrefixComparator"
self.substr = substr
# Just like _to_filter, but for comparators.
def _to_comparator(orig_cmp):
try:
new_cmp = pbComparator.Comparator()
new_cmp.name = orig_cmp.name
new_cmp2 = orig_cmp.comparator_type()
members = [attr for attr in dir(orig_cmp) if not callable(
attr) and not attr.startswith("__") and attr not in ["name", "comparator_type"]]
for member in members:
try:
val = getattr(orig_cmp, member)
if val is not None:
# skip none value that should be optional
setattr(new_cmp2, member, val)
except AttributeError:
# It's a composite element and we need to copy it in.
el = getattr(new_cmp2, member)
el.CopyFrom(getattr(orig_cmp, member))
new_cmp.serialized_comparator = new_cmp2.SerializeToString()
return new_cmp
except Exception as ex:
raise ValueError("Malformed Comparator provided %s %s" % (ex, traceback.format_exc()))
class BytesBytesPair:
def __init__(self, first, second):
self.first = first
self.second = second
def _to_bytes_bytes_pair(bbp):
try:
new_bbp = pbBytesBytesPair()
new_bbp.first = bbp.first
new_bbp.second = bbp.second
return new_bbp
except Exception:
raise ValueError("Malformed BytesBytesPair provided")
class RowRange:
def __init__(self, start_row, start_row_inclusive, stop_row, stop_row_inclusive):
self.filter_type = pbFilter.RowRange
self.name = filter_path + "RowRange"
self.start_row = start_row
self.start_row_inclusive = start_row_inclusive
self.stop_row = stop_row
self.stop_row_inclusive = stop_row_inclusive
def _to_row_range(rr):
try:
new = pbFilter.RowRange()
new.start_row = rr.start_row
new.start_row_inclusive = rr.start_row_inclusive
new.stop_row = rr.stop_row
new.stop_row_inclusive = rr.stop_row_inclusive
return new
except Exception:
raise ValueError("Malformed RowRange provided")