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.Objects;
23 import java.util.Random;
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.generated.FilterProtos;
30
31 import com.google.protobuf.InvalidProtocolBufferException;
32
33
34
35
36
37 @InterfaceAudience.Public
38 @InterfaceStability.Stable
39 public class RandomRowFilter extends FilterBase {
40 protected static final Random random = new Random();
41
42 protected float chance;
43 protected boolean filterOutRow;
44
45
46
47
48
49
50 public RandomRowFilter(float chance) {
51 this.chance = chance;
52 }
53
54
55
56
57 public float getChance() {
58 return chance;
59 }
60
61
62
63
64
65
66 public void setChance(float chance) {
67 this.chance = chance;
68 }
69
70 @Override
71 public boolean filterAllRemaining() {
72 return false;
73 }
74
75 @Override
76 public ReturnCode filterKeyValue(Cell v) {
77 if (filterOutRow) {
78 return ReturnCode.NEXT_ROW;
79 }
80 return ReturnCode.INCLUDE;
81 }
82
83
84
85 @Override
86 public Cell transformCell(Cell v) {
87 return v;
88 }
89
90 @Override
91 public boolean filterRow() {
92 return filterOutRow;
93 }
94
95 @Override
96 public boolean hasFilterRow() {
97 return true;
98 }
99
100 @Override
101 public boolean filterRowKey(byte[] buffer, int offset, int length) {
102 if (chance < 0) {
103
104 filterOutRow = true;
105 } else if (chance > 1) {
106
107 filterOutRow = false;
108 } else {
109
110 filterOutRow = !(random.nextFloat() < chance);
111 }
112 return filterOutRow;
113 }
114
115 @Override
116 public void reset() {
117 filterOutRow = false;
118 }
119
120
121
122
123 @Override
124 public byte [] toByteArray() {
125 FilterProtos.RandomRowFilter.Builder builder =
126 FilterProtos.RandomRowFilter.newBuilder();
127 builder.setChance(this.chance);
128 return builder.build().toByteArray();
129 }
130
131
132
133
134
135
136
137 public static RandomRowFilter parseFrom(final byte [] pbBytes)
138 throws DeserializationException {
139 FilterProtos.RandomRowFilter proto;
140 try {
141 proto = FilterProtos.RandomRowFilter.parseFrom(pbBytes);
142 } catch (InvalidProtocolBufferException e) {
143 throw new DeserializationException(e);
144 }
145 return new RandomRowFilter(proto.getChance());
146 }
147
148
149
150
151
152
153 @Override
154 boolean areSerializedFieldsEqual(Filter o) {
155 if (o == this) return true;
156 if (!(o instanceof RandomRowFilter)) return false;
157
158 RandomRowFilter other = (RandomRowFilter)o;
159 return this.getChance() == other.getChance();
160 }
161
162 @Override
163 public boolean equals(Object obj) {
164 return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
165 }
166
167 @Override
168 public int hashCode() {
169 return Objects.hash(this.getChance());
170 }
171 }