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.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     * Tests basics
43     * 
44     * @throws Exception
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      // Now let's check if the filter included the right number of rows;
56      // since we're dealing with randomness, we must have a include an epsilon
57      // tolerance.
58      int epsilon = max / 100;
59      assertTrue("Roughly 25% should pass the filter", Math.abs(included - max
60          / 4) < epsilon);
61    }
62  
63    /**
64     * Tests serialization
65     * 
66     * @throws Exception
67     */
68    public void testSerialization() throws Exception {
69      RandomRowFilter newFilter = serializationTest(quarterChanceFilter);
70      // use epsilon float comparison
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      // Decompose filter to bytes.
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      // Recompose filter.
85      DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
86      RandomRowFilter newFilter = new RandomRowFilter();
87      newFilter.readFields(in);
88  
89      return newFilter;
90    }
91  }