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