View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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     * Tests basics
46     * 
47     * @throws Exception
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      // Now let's check if the filter included the right number of rows;
60      // since we're dealing with randomness, we must have a include an epsilon
61      // tolerance.
62      int epsilon = max / 100;
63      assertTrue("Roughly 25% should pass the filter", Math.abs(included - max
64          / 4) < epsilon);
65    }
66  
67    /**
68     * Tests serialization
69     * 
70     * @throws Exception
71     */
72    @Test
73    public void testSerialization() throws Exception {
74      RandomRowFilter newFilter = serializationTest(quarterChanceFilter);
75      // use epsilon float comparison
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      // Decompose filter to bytes.
83      byte[] buffer = filter.toByteArray();
84  
85      // Recompose filter.
86      RandomRowFilter newFilter = RandomRowFilter.parseFrom(buffer);
87  
88      return newFilter;
89    }
90  
91  }
92