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