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 java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.Cell;
025import org.apache.hadoop.hbase.CellUtil;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.HTestConst;
029import org.apache.hadoop.hbase.KeyValue;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.filter.Filter;
032import org.apache.hadoop.hbase.filter.FilterBase;
033import org.apache.hadoop.hbase.regionserver.StoreScanner;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.Threads;
036import org.junit.AfterClass;
037import org.junit.BeforeClass;
038
039public abstract class AbstractTestScanCursor {
040
041  protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
042
043  /**
044   * Table configuration
045   */
046  protected static TableName TABLE_NAME = TableName.valueOf("TestScanCursor");
047
048  protected static int NUM_ROWS = 5;
049  protected static byte[] ROW = Bytes.toBytes("testRow");
050  protected static byte[][] ROWS = HTestConst.makeNAscii(ROW, NUM_ROWS);
051
052  protected static int NUM_FAMILIES = 2;
053  protected static byte[] FAMILY = Bytes.toBytes("testFamily");
054  protected static byte[][] FAMILIES = HTestConst.makeNAscii(FAMILY, NUM_FAMILIES);
055
056  protected static int NUM_QUALIFIERS = 2;
057  protected static byte[] QUALIFIER = Bytes.toBytes("testQualifier");
058  protected static byte[][] QUALIFIERS = HTestConst.makeNAscii(QUALIFIER, NUM_QUALIFIERS);
059
060  protected static int VALUE_SIZE = 10;
061  protected static byte[] VALUE = Bytes.createMaxByteArray(VALUE_SIZE);
062
063  protected static final int TIMEOUT = 4000;
064
065  @BeforeClass
066  public static void setUpBeforeClass() throws Exception {
067    Configuration conf = TEST_UTIL.getConfiguration();
068
069    conf.setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, TIMEOUT);
070    conf.setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, TIMEOUT);
071
072    // Check the timeout condition after every cell
073    conf.setLong(StoreScanner.HBASE_CELLS_SCANNED_PER_HEARTBEAT_CHECK, 1);
074    TEST_UTIL.startMiniCluster(1);
075
076    createTestTable(TABLE_NAME, ROWS, FAMILIES, QUALIFIERS, VALUE);
077  }
078
079  private static void createTestTable(TableName name, byte[][] rows, byte[][] families,
080    byte[][] qualifiers, byte[] cellValue) throws IOException {
081    TEST_UTIL.createTable(name, families).put(createPuts(rows, families, qualifiers, cellValue));
082  }
083
084  private static List<Put> createPuts(byte[][] rows, byte[][] families, byte[][] qualifiers,
085    byte[] value) throws IOException {
086    List<Put> puts = new ArrayList<>();
087    for (int row = 0; row < rows.length; row++) {
088      Put put = new Put(rows[row]);
089      for (int fam = 0; fam < families.length; fam++) {
090        for (int qual = 0; qual < qualifiers.length; qual++) {
091          KeyValue kv = new KeyValue(rows[row], families[fam], qualifiers[qual], qual, value);
092          put.add(kv);
093        }
094      }
095      puts.add(put);
096    }
097    return puts;
098  }
099
100  @AfterClass
101  public static void tearDownAfterClass() throws Exception {
102    TEST_UTIL.shutdownMiniCluster();
103  }
104
105  public static final class SparseFilter extends FilterBase {
106
107    private final boolean reversed;
108
109    public SparseFilter(boolean reversed) {
110      this.reversed = reversed;
111    }
112
113    @Override
114    public ReturnCode filterCell(final Cell c) throws IOException {
115      Threads.sleep(TIMEOUT / 2 + 100);
116      return Bytes.equals(CellUtil.cloneRow(c), ROWS[reversed ? 0 : NUM_ROWS - 1])
117        ? ReturnCode.INCLUDE
118        : ReturnCode.SKIP;
119    }
120
121    @Override
122    public byte[] toByteArray() throws IOException {
123      return reversed ? new byte[] { 1 } : new byte[] { 0 };
124    }
125
126    public static Filter parseFrom(final byte[] pbBytes) {
127      return new SparseFilter(pbBytes[0] != 0);
128    }
129  }
130
131  protected Scan createScanWithSparseFilter() {
132    return new Scan().setMaxResultSize(Long.MAX_VALUE).setCaching(Integer.MAX_VALUE)
133      .setNeedCursorResult(true).setAllowPartialResults(true).setFilter(new SparseFilter(false));
134  }
135
136  protected Scan createReversedScanWithSparseFilter() {
137    return new Scan().setMaxResultSize(Long.MAX_VALUE).setCaching(Integer.MAX_VALUE)
138      .setReversed(true).setNeedCursorResult(true).setAllowPartialResults(true)
139      .setFilter(new SparseFilter(true));
140  }
141
142  protected Scan createScanWithSizeLimit() {
143    return new Scan().setMaxResultSize(1).setCaching(Integer.MAX_VALUE).setNeedCursorResult(true);
144  }
145}