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   */
047  @Test
048  public void testPageSize() throws Exception {
049    Filter f = new PageFilter(ROW_LIMIT);
050    pageSizeTests(f);
051  }
052
053  /**
054   * Test filter serialization
055   */
056  @Test
057  public void testSerialization() throws Exception {
058    Filter f = new PageFilter(ROW_LIMIT);
059    // Decompose mainFilter to bytes.
060    byte[] buffer = f.toByteArray();
061    // Recompose mainFilter.
062    Filter newFilter = PageFilter.parseFrom(buffer);
063
064    // Ensure the serialization preserved the filter by running a full test.
065    pageSizeTests(newFilter);
066  }
067
068  private void pageSizeTests(Filter f) throws Exception {
069    testFiltersBeyondPageSize(f, ROW_LIMIT);
070  }
071
072  private void testFiltersBeyondPageSize(final Filter f, final int pageSize) throws IOException {
073    int count = 0;
074    for (int i = 0; i < (pageSize * 2); i++) {
075      boolean filterOut = f.filterRow();
076
077      if (filterOut) {
078        break;
079      } else {
080        count++;
081      }
082
083      // If at last row, should tell us to skip all remaining
084      if (count == pageSize) {
085        assertTrue(f.filterAllRemaining());
086      } else {
087        assertFalse(f.filterAllRemaining());
088      }
089
090    }
091    assertEquals(pageSize, count);
092  }
093
094}