View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.filter;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   * Tests for the page filter
33   */
34  @Category(SmallTests.class)
35  public class TestPageFilter {
36    static final int ROW_LIMIT = 3;
37  
38    /**
39     * test page size filter
40     * @throws Exception
41     */
42    @Test
43    public void testPageSize() throws Exception {
44      Filter f = new PageFilter(ROW_LIMIT);
45      pageSizeTests(f);
46    }
47  
48    /**
49     * Test filter serialization
50     * @throws Exception
51     */
52    @Test
53    public void testSerialization() throws Exception {
54      Filter f = new PageFilter(ROW_LIMIT);
55      // Decompose mainFilter to bytes.
56      byte[] buffer = f.toByteArray();
57      // Recompose mainFilter.
58      Filter newFilter = PageFilter.parseFrom(buffer);
59  
60      // Ensure the serialization preserved the filter by running a full test.
61      pageSizeTests(newFilter);
62    }
63  
64    private void pageSizeTests(Filter f) throws Exception {
65      testFiltersBeyondPageSize(f, ROW_LIMIT);
66    }
67  
68    private void testFiltersBeyondPageSize(final Filter f, final int pageSize) throws IOException {
69      int count = 0;
70      for (int i = 0; i < (pageSize * 2); i++) {
71        boolean filterOut = f.filterRow();
72  
73        if(filterOut) {
74          break;
75        } else {
76          count++;
77        }
78  
79        // If at last row, should tell us to skip all remaining
80        if(count == pageSize) {
81          assertTrue(f.filterAllRemaining());
82        } else {
83          assertFalse(f.filterAllRemaining());
84        }
85  
86      }
87      assertEquals(pageSize, count);
88    }
89  
90  }
91