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.client;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertNull;
023import static org.junit.Assert.assertTrue;
024
025import java.io.IOException;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.testclassification.ClientTests;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.AfterClass;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038/**
039 * With filter we may stop at a middle of row and think that we still have more cells for the
040 * current row but actually all the remaining cells will be filtered out by the filter. So it will
041 * lead to a Result that mayHaveMoreCellsInRow is true but actually there are no cells for the same
042 * row. Here we want to test if our limited scan still works.
043 */
044@Category({ MediumTests.class, ClientTests.class })
045public class TestLimitedScanWithFilter {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestLimitedScanWithFilter.class);
050
051  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
052
053  private static final TableName TABLE_NAME = TableName.valueOf("TestRegionScanner");
054
055  private static final byte[] FAMILY = Bytes.toBytes("cf");
056
057  private static final byte[][] CQS =
058    { Bytes.toBytes("cq1"), Bytes.toBytes("cq2"), Bytes.toBytes("cq3"), Bytes.toBytes("cq4") };
059
060  private static int ROW_COUNT = 10;
061
062  @BeforeClass
063  public static void setUp() throws Exception {
064    UTIL.startMiniCluster(1);
065    try (Table table = UTIL.createTable(TABLE_NAME, FAMILY)) {
066      for (int i = 0; i < ROW_COUNT; i++) {
067        Put put = new Put(Bytes.toBytes(i));
068        for (int j = 0; j < CQS.length; j++) {
069          put.addColumn(FAMILY, CQS[j], Bytes.toBytes((j + 1) * i));
070        }
071        table.put(put);
072      }
073    }
074  }
075
076  @AfterClass
077  public static void tearDown() throws Exception {
078    UTIL.shutdownMiniCluster();
079  }
080
081  @Test
082  public void testCompleteResult() throws IOException {
083    int limit = 5;
084    Scan scan =
085      new Scan().setFilter(new ColumnCountOnRowFilter(2)).setMaxResultSize(1).setLimit(limit);
086    try (Table table = UTIL.getConnection().getTable(TABLE_NAME);
087      ResultScanner scanner = table.getScanner(scan)) {
088      for (int i = 0; i < limit; i++) {
089        Result result = scanner.next();
090        assertEquals(i, Bytes.toInt(result.getRow()));
091        assertEquals(2, result.size());
092        assertFalse(result.mayHaveMoreCellsInRow());
093        assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQS[0])));
094        assertEquals(2 * i, Bytes.toInt(result.getValue(FAMILY, CQS[1])));
095      }
096      assertNull(scanner.next());
097    }
098  }
099
100  @Test
101  public void testAllowPartial() throws IOException {
102    int limit = 5;
103    Scan scan = new Scan().setFilter(new ColumnCountOnRowFilter(2)).setMaxResultSize(1)
104      .setAllowPartialResults(true).setLimit(limit);
105    try (Table table = UTIL.getConnection().getTable(TABLE_NAME);
106      ResultScanner scanner = table.getScanner(scan)) {
107      for (int i = 0; i < 2 * limit; i++) {
108        int key = i / 2;
109        Result result = scanner.next();
110        assertEquals(key, Bytes.toInt(result.getRow()));
111        assertEquals(1, result.size());
112        assertTrue(result.mayHaveMoreCellsInRow());
113        int cqIndex = i % 2;
114        assertEquals(key * (cqIndex + 1), Bytes.toInt(result.getValue(FAMILY, CQS[cqIndex])));
115      }
116      assertNull(scanner.next());
117    }
118  }
119
120  @Test
121  public void testBatchAllowPartial() throws IOException {
122    int limit = 5;
123    Scan scan = new Scan().setFilter(new ColumnCountOnRowFilter(3)).setBatch(2).setMaxResultSize(1)
124      .setAllowPartialResults(true).setLimit(limit);
125    try (Table table = UTIL.getConnection().getTable(TABLE_NAME);
126      ResultScanner scanner = table.getScanner(scan)) {
127      for (int i = 0; i < 3 * limit; i++) {
128        int key = i / 3;
129        Result result = scanner.next();
130        assertEquals(key, Bytes.toInt(result.getRow()));
131        assertEquals(1, result.size());
132        assertTrue(result.mayHaveMoreCellsInRow());
133        int cqIndex = i % 3;
134        assertEquals(key * (cqIndex + 1), Bytes.toInt(result.getValue(FAMILY, CQS[cqIndex])));
135      }
136      assertNull(scanner.next());
137    }
138  }
139
140  @Test
141  public void testBatch() throws IOException {
142    int limit = 5;
143    Scan scan = new Scan().setFilter(new ColumnCountOnRowFilter(2)).setBatch(2).setMaxResultSize(1)
144      .setLimit(limit);
145    try (Table table = UTIL.getConnection().getTable(TABLE_NAME);
146      ResultScanner scanner = table.getScanner(scan)) {
147      for (int i = 0; i < limit; i++) {
148        Result result = scanner.next();
149        assertEquals(i, Bytes.toInt(result.getRow()));
150        assertEquals(2, result.size());
151        assertTrue(result.mayHaveMoreCellsInRow());
152        assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQS[0])));
153        assertEquals(2 * i, Bytes.toInt(result.getValue(FAMILY, CQS[1])));
154      }
155      assertNull(scanner.next());
156    }
157  }
158
159  @Test
160  public void testBatchAndFilterDiffer() throws IOException {
161    int limit = 5;
162    Scan scan = new Scan().setFilter(new ColumnCountOnRowFilter(3)).setBatch(2).setMaxResultSize(1)
163      .setLimit(limit);
164    try (Table table = UTIL.getConnection().getTable(TABLE_NAME);
165      ResultScanner scanner = table.getScanner(scan)) {
166      for (int i = 0; i < limit; i++) {
167        Result result = scanner.next();
168        assertEquals(i, Bytes.toInt(result.getRow()));
169        assertEquals(2, result.size());
170        assertTrue(result.mayHaveMoreCellsInRow());
171        assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQS[0])));
172        assertEquals(2 * i, Bytes.toInt(result.getValue(FAMILY, CQS[1])));
173        result = scanner.next();
174        assertEquals(i, Bytes.toInt(result.getRow()));
175        assertEquals(1, result.size());
176        assertFalse(result.mayHaveMoreCellsInRow());
177        assertEquals(3 * i, Bytes.toInt(result.getValue(FAMILY, CQS[2])));
178      }
179      assertNull(scanner.next());
180    }
181  }
182}