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.regionserver.querymatcher;
019
020import static org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode.INCLUDE;
021import static org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode.SKIP;
022import static org.junit.Assert.assertEquals;
023
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.List;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.KeepDeletedCells;
030import org.apache.hadoop.hbase.KeyValue;
031import org.apache.hadoop.hbase.KeyValue.Type;
032import org.apache.hadoop.hbase.KeyValueUtil;
033import org.apache.hadoop.hbase.PrivateConstants;
034import org.apache.hadoop.hbase.regionserver.ScanInfo;
035import org.apache.hadoop.hbase.regionserver.ScanType;
036import org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode;
037import org.apache.hadoop.hbase.testclassification.RegionServerTests;
038import org.apache.hadoop.hbase.testclassification.SmallTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047@Category({ RegionServerTests.class, SmallTests.class })
048public class TestCompactionScanQueryMatcher extends AbstractTestScanQueryMatcher {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052    HBaseClassTestRule.forClass(TestCompactionScanQueryMatcher.class);
053
054  private static final Logger LOG = LoggerFactory.getLogger(TestCompactionScanQueryMatcher.class);
055
056  @Test
057  public void testMatch_PartialRangeDropDeletes() throws Exception {
058    // Some ranges.
059    testDropDeletes(row2, row3, new byte[][] { row1, row2, row2, row3 }, INCLUDE, SKIP, SKIP,
060      INCLUDE);
061    testDropDeletes(row2, row3, new byte[][] { row1, row1, row2 }, INCLUDE, INCLUDE, SKIP);
062    testDropDeletes(row2, row3, new byte[][] { row2, row3, row3 }, SKIP, INCLUDE, INCLUDE);
063    testDropDeletes(row1, row3, new byte[][] { row1, row2, row3 }, SKIP, SKIP, INCLUDE);
064    // Open ranges.
065    testDropDeletes(HConstants.EMPTY_START_ROW, row3, new byte[][] { row1, row2, row3 }, SKIP, SKIP,
066      INCLUDE);
067    testDropDeletes(row2, HConstants.EMPTY_END_ROW, new byte[][] { row1, row2, row3 }, INCLUDE,
068      SKIP, SKIP);
069    testDropDeletes(HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW,
070      new byte[][] { row1, row2, row3, row3 }, SKIP, SKIP, SKIP, SKIP);
071
072    // No KVs in range.
073    testDropDeletes(row2, row3, new byte[][] { row1, row1, row3 }, INCLUDE, INCLUDE, INCLUDE);
074    testDropDeletes(row2, row3, new byte[][] { row3, row3 }, INCLUDE, INCLUDE);
075    testDropDeletes(row2, row3, new byte[][] { row1, row1 }, INCLUDE, INCLUDE);
076  }
077
078  private void testDropDeletes(byte[] from, byte[] to, byte[][] rows, MatchCode... expected)
079    throws IOException {
080    long now = EnvironmentEdgeManager.currentTime();
081    // Set time to purge deletes to negative value to avoid it ever happening.
082    ScanInfo scanInfo = new ScanInfo(this.conf, fam2, 0, 1, ttl, KeepDeletedCells.FALSE,
083      HConstants.DEFAULT_BLOCKSIZE, -1L, rowComparator, false);
084
085    CompactionScanQueryMatcher qm =
086      CompactionScanQueryMatcher.create(scanInfo, ScanType.COMPACT_RETAIN_DELETES, Long.MAX_VALUE,
087        PrivateConstants.OLDEST_TIMESTAMP, PrivateConstants.OLDEST_TIMESTAMP, now, from, to, null);
088    List<ScanQueryMatcher.MatchCode> actual = new ArrayList<>(rows.length);
089    byte[] prevRow = null;
090    for (byte[] row : rows) {
091      if (prevRow == null || !Bytes.equals(prevRow, row)) {
092        qm.setToNewRow(KeyValueUtil.createFirstOnRow(row));
093        prevRow = row;
094      }
095      actual.add(qm.match(new KeyValue(row, fam2, null, now, Type.Delete)));
096    }
097
098    assertEquals(expected.length, actual.size());
099    for (int i = 0; i < expected.length; i++) {
100      LOG.debug("expected " + expected[i] + ", actual " + actual.get(i));
101      assertEquals(expected[i], actual.get(i));
102    }
103  }
104}