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