001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.filter; 019 020import static org.junit.Assert.*; 021 022import org.apache.hadoop.hbase.HBaseClassTestRule; 023import org.apache.hadoop.hbase.KeyValueUtil; 024import org.apache.hadoop.hbase.testclassification.FilterTests; 025import org.apache.hadoop.hbase.testclassification.SmallTests; 026import org.apache.hadoop.hbase.util.Bytes; 027import org.junit.Before; 028import org.junit.ClassRule; 029import org.junit.Test; 030import org.junit.experimental.categories.Category; 031 032@Category({ FilterTests.class, SmallTests.class }) 033public class TestRandomRowFilter { 034 035 @ClassRule 036 public static final HBaseClassTestRule CLASS_RULE = 037 HBaseClassTestRule.forClass(TestRandomRowFilter.class); 038 039 protected RandomRowFilter quarterChanceFilter; 040 041 @Before 042 public void setUp() throws Exception { 043 quarterChanceFilter = new RandomRowFilter(0.25f); 044 } 045 046 /** 047 * Tests basics 048 */ 049 @Test 050 public void testBasics() throws Exception { 051 int included = 0; 052 int max = 1000000; 053 for (int i = 0; i < max; i++) { 054 if (!quarterChanceFilter.filterRowKey(KeyValueUtil.createFirstOnRow(Bytes.toBytes("row")))) { 055 included++; 056 } 057 } 058 // Now let's check if the filter included the right number of rows; 059 // since we're dealing with randomness, we must have a include an epsilon 060 // tolerance. 061 int epsilon = max / 100; 062 assertTrue("Roughly 25% should pass the filter", Math.abs(included - max / 4) < epsilon); 063 } 064 065 /** 066 * Tests serialization 067 */ 068 @Test 069 public void testSerialization() throws Exception { 070 RandomRowFilter newFilter = serializationTest(quarterChanceFilter); 071 // use epsilon float comparison 072 assertTrue("float should be equal", 073 Math.abs(newFilter.getChance() - quarterChanceFilter.getChance()) < 0.000001f); 074 } 075 076 private RandomRowFilter serializationTest(RandomRowFilter filter) throws Exception { 077 // Decompose filter to bytes. 078 byte[] buffer = filter.toByteArray(); 079 080 // Recompose filter. 081 RandomRowFilter newFilter = RandomRowFilter.parseFrom(buffer); 082 083 return newFilter; 084 } 085 086}