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.Assert.assertArrayEquals;
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.CellUtil;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.PrivateCellUtil;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.client.Result;
034import org.apache.hadoop.hbase.client.ResultScanner;
035import org.apache.hadoop.hbase.client.Scan;
036import org.apache.hadoop.hbase.client.Table;
037import org.apache.hadoop.hbase.exceptions.DeserializationException;
038import org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy;
039import org.apache.hadoop.hbase.testclassification.FilterTests;
040import org.apache.hadoop.hbase.testclassification.MediumTests;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.junit.AfterClass;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Rule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048import org.junit.rules.TestName;
049
050@Category({ FilterTests.class, MediumTests.class }) public class TestSeekHints {
051
052  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
053  private static String cf = "f";
054  private static String cq = "q";
055  private static String table = "t";
056  private static Table ht;
057
058  @ClassRule public static final HBaseClassTestRule CLASS_RULE =
059    HBaseClassTestRule.forClass(TestSeekHints.class);
060
061  @Rule public TestName name = new TestName();
062
063  @BeforeClass public static void setUpBeforeClass() throws Exception {
064    Configuration conf = TEST_UTIL.getConfiguration();
065    conf.setInt("hbase.client.scanner.caching", 1000);
066    conf.set(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
067      ConstantSizeRegionSplitPolicy.class.getName());
068    // set no splits
069    conf.setLong(HConstants.HREGION_MAX_FILESIZE, (1024L) * 1024 * 1024 * 10);
070
071    TEST_UTIL.startMiniCluster();
072
073    // load the mini cluster with a single table with 20 rows, with rowkeys of a single byte, 0-19.
074    ht = TEST_UTIL.createTable(TableName.valueOf(table), Bytes.toBytes(cf),
075      Integer.MAX_VALUE);
076    for (byte b = 0; b < 20; b++) {
077      Put put = new Put(new byte[] { b }).addColumn(Bytes.toBytes(cf), Bytes.toBytes(cq),
078        Bytes.toBytes("value"));
079      ht.put(put);
080    }
081    TEST_UTIL.flush();
082  }
083
084  @AfterClass public static void tearDownAfterClass() throws Exception {
085    TEST_UTIL.shutdownMiniCluster();
086  }
087
088  @Test
089  public void testNormalScanForwardsSeek() throws IOException {
090
091    KeepAllButSeekFilter filter = new KeepAllButSeekFilter((byte) 10, (byte) 15);
092
093    Scan scan = new Scan();
094    scan.setFilter(filter);
095
096    ResultScanner scanner = ht.getScanner(scan);
097    List<byte[]> actualRowsList = new ArrayList<>();
098    for (Result result : scanner) {
099      actualRowsList.add(result.getRow());
100    }
101    byte[][] actualRows = actualRowsList.toArray(new byte[][] {});
102
103    List<byte[]> expectedRowsList = new ArrayList<>();
104    // Start counting up from 0, as that's the first of our test rows
105    for (byte i = 0; i < 10; i++) {
106      expectedRowsList.add(new byte[] { i });
107    }
108    // Skip rows starting from 10 and ending before 15, as the filter should remove them
109    for (byte i = 15; i < 20; i++) {
110      expectedRowsList.add(new byte[] { i });
111    }
112    byte[][] expectedRows = expectedRowsList.toArray(new byte[][] {});
113
114    assertArrayEquals(expectedRows, actualRows);
115  }
116
117  @Test
118  public void testReversedScanBackwardsSeek() throws IOException {
119
120    KeepAllButSeekFilter filter = new KeepAllButSeekFilter((byte) 10, (byte) 5);
121
122    Scan scan = new Scan();
123    scan.setFilter(filter);
124    scan.setReversed(true);
125
126    ResultScanner scanner = ht.getScanner(scan);
127    List<byte[]> actualRowsList = new ArrayList<>();
128    for (Result result : scanner) {
129      actualRowsList.add(result.getRow());
130    }
131    byte[][] actualRows = actualRowsList.toArray(new byte[][] {});
132
133    List<byte[]> expectedRowsList = new ArrayList<>();
134    // Start counting down from 19, as that's the last of our test rows
135    for (byte i = 19; i > 10; i--) {
136      expectedRowsList.add(new byte[] { i });
137    }
138    // Skip rows starting from 10 and ending before 5, as the filter should remove them
139    for (byte i = 5; i >= 0; i--) {
140      expectedRowsList.add(new byte[] { i });
141    }
142    byte[][] expectedRows = expectedRowsList.toArray(new byte[][] {});
143
144    assertArrayEquals(expectedRows, actualRows);
145  }
146
147  public static class KeepAllButSeekFilter extends FilterBase {
148
149    private byte seekStartRow;
150    private byte seekTargetRow;
151
152    public KeepAllButSeekFilter(byte seekStartRow, byte seekTargetRow) {
153      this.seekStartRow = seekStartRow;
154      this.seekTargetRow = seekTargetRow;
155    }
156
157    /* We return SEEK_NEXT_USING_HINT when we hit the specified row, but we return INCLUDE for all
158     * other rows. This will wind up including the rows between our "seek" row and our "hint" row
159     * only if we don't seek past them.
160     */
161    @Override public ReturnCode filterCell(final Cell c) throws IOException {
162      byte rowKeyPrefix = CellUtil.cloneRow(c)[0];
163      if (rowKeyPrefix == seekStartRow) {
164        return ReturnCode.SEEK_NEXT_USING_HINT;
165      }
166      return ReturnCode.INCLUDE;
167    }
168
169    @Override public Cell getNextCellHint(Cell currentCell) {
170      return PrivateCellUtil.createFirstOnRow(new byte[] { seekTargetRow });
171    }
172
173    @Override public byte[] toByteArray() {
174      return new byte[] { seekStartRow, seekTargetRow };
175    }
176
177    public static KeepAllButSeekFilter parseFrom(final byte[] pbBytes)
178      throws DeserializationException {
179
180      return new KeepAllButSeekFilter(pbBytes[0], pbBytes[1]);
181    }
182  }
183
184}