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.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.DataInputStream;
25 import java.io.DataOutputStream;
26
27 import org.apache.hadoop.hbase.SmallTests;
28 import org.apache.hadoop.hbase.util.Bytes;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.experimental.categories.Category;
32
33 import static org.junit.Assert.*;
34
35 @Category(SmallTests.class)
36 public class TestRandomRowFilter {
37 protected RandomRowFilter quarterChanceFilter;
38
39 @Before
40 public void setUp() throws Exception {
41 quarterChanceFilter = new RandomRowFilter(0.25f);
42 }
43
44
45
46
47
48
49 @Test
50 public void testBasics() throws Exception {
51 int included = 0;
52 int max = 1000000;
53 for (int i = 0; i < max; i++) {
54 if (!quarterChanceFilter.filterRowKey(Bytes.toBytes("row"), 0, Bytes
55 .toBytes("row").length)) {
56 included++;
57 }
58 }
59
60
61
62 int epsilon = max / 100;
63 assertTrue("Roughly 25% should pass the filter", Math.abs(included - max
64 / 4) < epsilon);
65 }
66
67
68
69
70
71
72 @Test
73 public void testSerialization() throws Exception {
74 RandomRowFilter newFilter = serializationTest(quarterChanceFilter);
75
76 assertTrue("float should be equal", Math.abs(newFilter.getChance()
77 - quarterChanceFilter.getChance()) < 0.000001f);
78 }
79
80 private RandomRowFilter serializationTest(RandomRowFilter filter)
81 throws Exception {
82
83 byte[] buffer = filter.toByteArray();
84
85
86 RandomRowFilter newFilter = RandomRowFilter.parseFrom(buffer);
87
88 return newFilter;
89 }
90
91 }
92