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