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.util.ArrayList;
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
39 @InterfaceAudience.Public
40 @InterfaceStability.Stable
41 public class ColumnCountGetFilter extends FilterBase {
42 private int limit = 0;
43 private int count = 0;
44
45 public ColumnCountGetFilter(final int n) {
46 Preconditions.checkArgument(n >= 0, "limit be positive %s", n);
47 this.limit = n;
48 }
49
50 public int getLimit() {
51 return limit;
52 }
53
54 @Override
55 public boolean filterAllRemaining() {
56 return this.count > this.limit;
57 }
58
59 @Override
60 public ReturnCode filterKeyValue(Cell v) {
61 this.count++;
62 return filterAllRemaining() ? ReturnCode.NEXT_COL : ReturnCode.INCLUDE_AND_NEXT_COL;
63 }
64
65
66
67 @Override
68 public Cell transformCell(Cell v) {
69 return v;
70 }
71
72 @Override
73 public void reset() {
74 this.count = 0;
75 }
76
77 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
78 Preconditions.checkArgument(filterArguments.size() == 1,
79 "Expected 1 but got: %s", filterArguments.size());
80 int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0));
81 return new ColumnCountGetFilter(limit);
82 }
83
84
85
86
87 public byte [] toByteArray() {
88 FilterProtos.ColumnCountGetFilter.Builder builder =
89 FilterProtos.ColumnCountGetFilter.newBuilder();
90 builder.setLimit(this.limit);
91 return builder.build().toByteArray();
92 }
93
94
95
96
97
98
99
100 public static ColumnCountGetFilter parseFrom(final byte [] pbBytes)
101 throws DeserializationException {
102 FilterProtos.ColumnCountGetFilter proto;
103 try {
104 proto = FilterProtos.ColumnCountGetFilter.parseFrom(pbBytes);
105 } catch (InvalidProtocolBufferException e) {
106 throw new DeserializationException(e);
107 }
108 return new ColumnCountGetFilter(proto.getLimit());
109 }
110
111
112
113
114
115
116 boolean areSerializedFieldsEqual(Filter o) {
117 if (o == this) return true;
118 if (!(o instanceof ColumnCountGetFilter)) return false;
119
120 ColumnCountGetFilter other = (ColumnCountGetFilter)o;
121 return this.getLimit() == other.getLimit();
122 }
123
124 @Override
125 public String toString() {
126 return this.getClass().getSimpleName() + " " + this.limit;
127 }
128 }