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 * @throws Exception 050 */ 051 @Test 052 public void testBasics() throws Exception { 053 int included = 0; 054 int max = 1000000; 055 for (int i = 0; i < max; i++) { 056 if (!quarterChanceFilter.filterRowKey(KeyValueUtil.createFirstOnRow(Bytes.toBytes("row")))) { 057 included++; 058 } 059 } 060 // Now let's check if the filter included the right number of rows; 061 // since we're dealing with randomness, we must have a include an epsilon 062 // tolerance. 063 int epsilon = max / 100; 064 assertTrue("Roughly 25% should pass the filter", Math.abs(included - max 065 / 4) < epsilon); 066 } 067 068 /** 069 * Tests serialization 070 * 071 * @throws Exception 072 */ 073 @Test 074 public void testSerialization() throws Exception { 075 RandomRowFilter newFilter = serializationTest(quarterChanceFilter); 076 // use epsilon float comparison 077 assertTrue("float should be equal", Math.abs(newFilter.getChance() 078 - quarterChanceFilter.getChance()) < 0.000001f); 079 } 080 081 private RandomRowFilter serializationTest(RandomRowFilter filter) 082 throws Exception { 083 // Decompose filter to bytes. 084 byte[] buffer = filter.toByteArray(); 085 086 // Recompose filter. 087 RandomRowFilter newFilter = RandomRowFilter.parseFrom(buffer); 088 089 return newFilter; 090 } 091 092} 093