Search in sources :

Example 1 with FilterBase

use of org.apache.hadoop.hbase.filter.FilterBase in project hbase by apache.

the class TestHRegion method testScanner_JoinedScannersWithLimits.

/**
   * HBASE-5416
   *
   * Test case when scan limits amount of KVs returned on each next() call.
   */
@Test
public void testScanner_JoinedScannersWithLimits() throws IOException {
    final byte[] cf_first = Bytes.toBytes("first");
    final byte[] cf_second = Bytes.toBytes("second");
    this.region = initHRegion(tableName, method, CONF, cf_first, cf_second);
    try {
        final byte[] col_a = Bytes.toBytes("a");
        final byte[] col_b = Bytes.toBytes("b");
        Put put;
        for (int i = 0; i < 10; i++) {
            put = new Put(Bytes.toBytes("r" + Integer.toString(i)));
            put.addColumn(cf_first, col_a, Bytes.toBytes(i));
            if (i < 5) {
                put.addColumn(cf_first, col_b, Bytes.toBytes(i));
                put.addColumn(cf_second, col_a, Bytes.toBytes(i));
                put.addColumn(cf_second, col_b, Bytes.toBytes(i));
            }
            region.put(put);
        }
        Scan scan = new Scan();
        scan.setLoadColumnFamiliesOnDemand(true);
        Filter bogusFilter = new FilterBase() {

            @Override
            public ReturnCode filterKeyValue(Cell ignored) throws IOException {
                return ReturnCode.INCLUDE;
            }

            @Override
            public boolean isFamilyEssential(byte[] name) {
                return Bytes.equals(name, cf_first);
            }
        };
        scan.setFilter(bogusFilter);
        InternalScanner s = region.getScanner(scan);
        // Our data looks like this:
        // r0: first:a, first:b, second:a, second:b
        // r1: first:a, first:b, second:a, second:b
        // r2: first:a, first:b, second:a, second:b
        // r3: first:a, first:b, second:a, second:b
        // r4: first:a, first:b, second:a, second:b
        // r5: first:a
        // r6: first:a
        // r7: first:a
        // r8: first:a
        // r9: first:a
        // But due to next's limit set to 3, we should get this:
        // r0: first:a, first:b, second:a
        // r0: second:b
        // r1: first:a, first:b, second:a
        // r1: second:b
        // r2: first:a, first:b, second:a
        // r2: second:b
        // r3: first:a, first:b, second:a
        // r3: second:b
        // r4: first:a, first:b, second:a
        // r4: second:b
        // r5: first:a
        // r6: first:a
        // r7: first:a
        // r8: first:a
        // r9: first:a
        List<Cell> results = new ArrayList<>();
        int index = 0;
        ScannerContext scannerContext = ScannerContext.newBuilder().setBatchLimit(3).build();
        while (true) {
            boolean more = s.next(results, scannerContext);
            if ((index >> 1) < 5) {
                if (index % 2 == 0)
                    assertEquals(results.size(), 3);
                else
                    assertEquals(results.size(), 1);
            } else
                assertEquals(results.size(), 1);
            results.clear();
            index++;
            if (!more)
                break;
        }
    } finally {
        HBaseTestingUtility.closeRegionAndWAL(this.region);
        this.region = null;
    }
}
Also used : PrefixFilter(org.apache.hadoop.hbase.filter.PrefixFilter) ColumnCountGetFilter(org.apache.hadoop.hbase.filter.ColumnCountGetFilter) SingleColumnValueExcludeFilter(org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter) Filter(org.apache.hadoop.hbase.filter.Filter) SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) ArrayList(java.util.ArrayList) Scan(org.apache.hadoop.hbase.client.Scan) Cell(org.apache.hadoop.hbase.Cell) Put(org.apache.hadoop.hbase.client.Put) FilterBase(org.apache.hadoop.hbase.filter.FilterBase) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Cell (org.apache.hadoop.hbase.Cell)1 Put (org.apache.hadoop.hbase.client.Put)1 Scan (org.apache.hadoop.hbase.client.Scan)1 ColumnCountGetFilter (org.apache.hadoop.hbase.filter.ColumnCountGetFilter)1 Filter (org.apache.hadoop.hbase.filter.Filter)1 FilterBase (org.apache.hadoop.hbase.filter.FilterBase)1 PrefixFilter (org.apache.hadoop.hbase.filter.PrefixFilter)1 SingleColumnValueExcludeFilter (org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter)1 SingleColumnValueFilter (org.apache.hadoop.hbase.filter.SingleColumnValueFilter)1 Test (org.junit.Test)1