1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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.SmallTests;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.junit.experimental.categories.Category;
33  
34  @Category(SmallTests.class)
35  public class TestRandomRowFilter extends TestCase {
36    protected RandomRowFilter quarterChanceFilter;
37  
38    @Override
39    protected void setUp() throws Exception {
40      super.setUp();
41      quarterChanceFilter = new RandomRowFilter(0.25f);
42    }
43  
44    /**
45     * Tests basics
46     * 
47     * @throws Exception
48     */
49    public void testBasics() throws Exception {
50      int included = 0;
51      int max = 1000000;
52      for (int i = 0; i < max; i++) {
53        if (!quarterChanceFilter.filterRowKey(Bytes.toBytes("row"), 0, Bytes
54            .toBytes("row").length)) {
55          included++;
56        }
57      }
58      // Now let's check if the filter included the right number of rows;
59      // since we're dealing with randomness, we must have a include an epsilon
60      // tolerance.
61      int epsilon = max / 100;
62      assertTrue("Roughly 25% should pass the filter", Math.abs(included - max
63          / 4) < epsilon);
64    }
65  
66    /**
67     * Tests serialization
68     * 
69     * @throws Exception
70     */
71    public void testSerialization() throws Exception {
72      RandomRowFilter newFilter = serializationTest(quarterChanceFilter);
73      // use epsilon float comparison
74      assertTrue("float should be equal", Math.abs(newFilter.getChance()
75          - quarterChanceFilter.getChance()) < 0.000001f);
76    }
77  
78    private RandomRowFilter serializationTest(RandomRowFilter filter)
79        throws Exception {
80      // Decompose filter to bytes.
81      ByteArrayOutputStream stream = new ByteArrayOutputStream();
82      DataOutputStream out = new DataOutputStream(stream);
83      filter.write(out);
84      out.close();
85      byte[] buffer = stream.toByteArray();
86  
87      // Recompose filter.
88      DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
89      RandomRowFilter newFilter = new RandomRowFilter();
90      newFilter.readFields(in);
91  
92      return newFilter;
93    }
94  
95    @org.junit.Rule
96    public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
97      new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
98  }
99