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.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023 024import java.io.IOException; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.testclassification.FilterTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.junit.ClassRule; 029import org.junit.Test; 030import org.junit.experimental.categories.Category; 031 032/** 033 * Tests for the page filter 034 */ 035@Category({FilterTests.class, SmallTests.class}) 036public class TestPageFilter { 037 038 @ClassRule 039 public static final HBaseClassTestRule CLASS_RULE = 040 HBaseClassTestRule.forClass(TestPageFilter.class); 041 042 static final int ROW_LIMIT = 3; 043 044 /** 045 * test page size filter 046 * @throws Exception 047 */ 048 @Test 049 public void testPageSize() throws Exception { 050 Filter f = new PageFilter(ROW_LIMIT); 051 pageSizeTests(f); 052 } 053 054 /** 055 * Test filter serialization 056 * @throws Exception 057 */ 058 @Test 059 public void testSerialization() throws Exception { 060 Filter f = new PageFilter(ROW_LIMIT); 061 // Decompose mainFilter to bytes. 062 byte[] buffer = f.toByteArray(); 063 // Recompose mainFilter. 064 Filter newFilter = PageFilter.parseFrom(buffer); 065 066 // Ensure the serialization preserved the filter by running a full test. 067 pageSizeTests(newFilter); 068 } 069 070 private void pageSizeTests(Filter f) throws Exception { 071 testFiltersBeyondPageSize(f, ROW_LIMIT); 072 } 073 074 private void testFiltersBeyondPageSize(final Filter f, final int pageSize) throws IOException { 075 int count = 0; 076 for (int i = 0; i < (pageSize * 2); i++) { 077 boolean filterOut = f.filterRow(); 078 079 if(filterOut) { 080 break; 081 } else { 082 count++; 083 } 084 085 // If at last row, should tell us to skip all remaining 086 if(count == pageSize) { 087 assertTrue(f.filterAllRemaining()); 088 } else { 089 assertFalse(f.filterAllRemaining()); 090 } 091 092 } 093 assertEquals(pageSize, count); 094 } 095 096} 097