1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.filter;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.DataInputStream;
26 import java.io.DataOutputStream;
27
28 import junit.framework.TestCase;
29
30 import org.apache.hadoop.hbase.util.Bytes;
31
32 public class TestRandomRowFilter extends TestCase {
33 protected RandomRowFilter quarterChanceFilter;
34
35 @Override
36 protected void setUp() throws Exception {
37 super.setUp();
38 quarterChanceFilter = new RandomRowFilter(0.25f);
39 }
40
41
42
43
44
45
46 public void testBasics() throws Exception {
47 int included = 0;
48 int max = 1000000;
49 for (int i = 0; i < max; i++) {
50 if (!quarterChanceFilter.filterRowKey(Bytes.toBytes("row"), 0, Bytes
51 .toBytes("row").length)) {
52 included++;
53 }
54 }
55
56
57
58 int epsilon = max / 100;
59 assertTrue("Roughly 25% should pass the filter", Math.abs(included - max
60 / 4) < epsilon);
61 }
62
63
64
65
66
67
68 public void testSerialization() throws Exception {
69 RandomRowFilter newFilter = serializationTest(quarterChanceFilter);
70
71 assertTrue("float should be equal", Math.abs(newFilter.getChance()
72 - quarterChanceFilter.getChance()) < 0.000001f);
73 }
74
75 private RandomRowFilter serializationTest(RandomRowFilter filter)
76 throws Exception {
77
78 ByteArrayOutputStream stream = new ByteArrayOutputStream();
79 DataOutputStream out = new DataOutputStream(stream);
80 filter.write(out);
81 out.close();
82 byte[] buffer = stream.toByteArray();
83
84
85 DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
86 RandomRowFilter newFilter = new RandomRowFilter();
87 newFilter.readFields(in);
88
89 return newFilter;
90 }
91 }