View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.testclassification.ClientTests;
30  import org.apache.hadoop.hbase.testclassification.MediumTests;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.junit.AfterClass;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  /**
38   * With filter we may stop at a middle of row and think that we still have more cells for the
39   * current row but actually all the remaining cells will be filtered out by the filter. So it will
40   * lead to a Result that mayHaveMoreCellsInRow is true but actually there are no cells for the same
41   * row. Here we want to test if our limited scan still works.
42   */
43  @Category({ MediumTests.class, ClientTests.class })
44  public class TestLimitedScanWithFilter {
45  
46    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
47  
48    private static final TableName TABLE_NAME = TableName.valueOf("TestRegionScanner");
49  
50    private static final byte[] FAMILY = Bytes.toBytes("cf");
51  
52    private static final byte[][] CQS =
53        { Bytes.toBytes("cq1"), Bytes.toBytes("cq2"), Bytes.toBytes("cq3"), Bytes.toBytes("cq4") };
54  
55    private static int ROW_COUNT = 10;
56  
57    @BeforeClass
58    public static void setUp() throws Exception {
59      UTIL.startMiniCluster(1);
60      try (Table table = UTIL.createTable(TABLE_NAME, FAMILY)) {
61        for (int i = 0; i < ROW_COUNT; i++) {
62          Put put = new Put(Bytes.toBytes(i));
63          for (int j = 0; j < CQS.length; j++) {
64            put.addColumn(FAMILY, CQS[j], Bytes.toBytes((j + 1) * i));
65          }
66          table.put(put);
67        }
68      }
69    }
70  
71    @AfterClass
72    public static void tearDown() throws Exception {
73      UTIL.shutdownMiniCluster();
74    }
75  
76    @Test
77    public void testCompleteResult() throws IOException {
78      int limit = 5;
79      Scan scan =
80          new Scan().setFilter(new ColumnCountOnRowFilter(2)).setMaxResultSize(1).setLimit(limit);
81      try (Table table = UTIL.getConnection().getTable(TABLE_NAME);
82          ResultScanner scanner = table.getScanner(scan)) {
83        for (int i = 0; i < limit; i++) {
84          Result result = scanner.next();
85          assertEquals(i, Bytes.toInt(result.getRow()));
86          assertEquals(2, result.size());
87          assertFalse(result.mayHaveMoreCellsInRow());
88          assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQS[0])));
89          assertEquals(2 * i, Bytes.toInt(result.getValue(FAMILY, CQS[1])));
90        }
91        assertNull(scanner.next());
92      }
93    }
94  
95    @Test
96    public void testAllowPartial() throws IOException {
97      int limit = 5;
98      Scan scan = new Scan().setFilter(new ColumnCountOnRowFilter(2)).setMaxResultSize(1)
99          .setAllowPartialResults(true).setLimit(limit);
100     try (Table table = UTIL.getConnection().getTable(TABLE_NAME);
101         ResultScanner scanner = table.getScanner(scan)) {
102       for (int i = 0; i < 2 * limit; i++) {
103         int key = i / 2;
104         Result result = scanner.next();
105         assertEquals(key, Bytes.toInt(result.getRow()));
106         assertEquals(1, result.size());
107         assertTrue(result.mayHaveMoreCellsInRow());
108         int cqIndex = i % 2;
109         assertEquals(key * (cqIndex + 1), Bytes.toInt(result.getValue(FAMILY, CQS[cqIndex])));
110       }
111       assertNull(scanner.next());
112     }
113   }
114 
115   @Test
116   public void testBatchAllowPartial() throws IOException {
117     int limit = 5;
118     Scan scan = new Scan().setFilter(new ColumnCountOnRowFilter(3)).setBatch(2).setMaxResultSize(1)
119         .setAllowPartialResults(true).setLimit(limit);
120     try (Table table = UTIL.getConnection().getTable(TABLE_NAME);
121         ResultScanner scanner = table.getScanner(scan)) {
122       for (int i = 0; i < 3 * limit; i++) {
123         int key = i / 3;
124         Result result = scanner.next();
125         assertEquals(key, Bytes.toInt(result.getRow()));
126         assertEquals(1, result.size());
127         assertTrue(result.mayHaveMoreCellsInRow());
128         int cqIndex = i % 3;
129         assertEquals(key * (cqIndex + 1), Bytes.toInt(result.getValue(FAMILY, CQS[cqIndex])));
130       }
131       assertNull(scanner.next());
132     }
133   }
134 
135   @Test
136   public void testBatch() throws IOException {
137     int limit = 5;
138     Scan scan = new Scan().setFilter(new ColumnCountOnRowFilter(2)).setBatch(2).setMaxResultSize(1)
139         .setLimit(limit);
140     try (Table table = UTIL.getConnection().getTable(TABLE_NAME);
141         ResultScanner scanner = table.getScanner(scan)) {
142       for (int i = 0; i < limit; i++) {
143         Result result = scanner.next();
144         assertEquals(i, Bytes.toInt(result.getRow()));
145         assertEquals(2, result.size());
146         assertTrue(result.mayHaveMoreCellsInRow());
147         assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQS[0])));
148         assertEquals(2 * i, Bytes.toInt(result.getValue(FAMILY, CQS[1])));
149       }
150       assertNull(scanner.next());
151     }
152   }
153 
154   @Test
155   public void testBatchAndFilterDiffer() throws IOException {
156     int limit = 5;
157     Scan scan = new Scan().setFilter(new ColumnCountOnRowFilter(3)).setBatch(2).setMaxResultSize(1)
158         .setLimit(limit);
159     try (Table table = UTIL.getConnection().getTable(TABLE_NAME);
160         ResultScanner scanner = table.getScanner(scan)) {
161       for (int i = 0; i < limit; i++) {
162         Result result = scanner.next();
163         assertEquals(i, Bytes.toInt(result.getRow()));
164         assertEquals(2, result.size());
165         assertTrue(result.mayHaveMoreCellsInRow());
166         assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQS[0])));
167         assertEquals(2 * i, Bytes.toInt(result.getValue(FAMILY, CQS[1])));
168         result = scanner.next();
169         assertEquals(i, Bytes.toInt(result.getRow()));
170         assertEquals(1, result.size());
171         assertFalse(result.mayHaveMoreCellsInRow());
172         assertEquals(3 * i, Bytes.toInt(result.getValue(FAMILY, CQS[2])));
173       }
174       assertNull(scanner.next());
175     }
176   }
177 }