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 java.io.IOException;
021import org.apache.hadoop.hbase.Cell;
022import org.apache.hadoop.hbase.PrivateCellUtil;
023import org.apache.hadoop.hbase.regionserver.ScanInfo;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * Query matcher for stripe compaction if range drop deletes is used.
028 */
029@InterfaceAudience.Private
030public class StripeCompactionScanQueryMatcher extends DropDeletesCompactionScanQueryMatcher {
031
032  private final byte[] dropDeletesFromRow;
033
034  private final byte[] dropDeletesToRow;
035
036  private enum DropDeletesInOutput {
037    BEFORE,
038    IN,
039    AFTER
040  }
041
042  private DropDeletesInOutput dropDeletesInOutput = DropDeletesInOutput.BEFORE;
043
044  public StripeCompactionScanQueryMatcher(ScanInfo scanInfo, DeleteTracker deletes,
045    ColumnTracker columns, long readPointToUse, long earliestPutTs, long oldestUnexpiredTS,
046    long now, byte[] dropDeletesFromRow, byte[] dropDeletesToRow) {
047    super(scanInfo, deletes, columns, readPointToUse, earliestPutTs, oldestUnexpiredTS, now);
048    this.dropDeletesFromRow = dropDeletesFromRow;
049    this.dropDeletesToRow = dropDeletesToRow;
050  }
051
052  @Override
053  public MatchCode match(Cell cell) throws IOException {
054    MatchCode returnCode = preCheck(cell);
055    if (returnCode != null) {
056      return returnCode;
057    }
058    long mvccVersion = cell.getSequenceId();
059    byte typeByte = cell.getTypeByte();
060    if (PrivateCellUtil.isDelete(typeByte)) {
061      if (mvccVersion > maxReadPointToTrackVersions) {
062        return MatchCode.INCLUDE;
063      }
064      trackDelete(cell);
065      if (dropDeletesInOutput == DropDeletesInOutput.IN) {
066        // here we are running like major compaction
067        trackDelete(cell);
068        returnCode = tryDropDelete(cell);
069        if (returnCode != null) {
070          return returnCode;
071        }
072      } else {
073        return MatchCode.INCLUDE;
074      }
075    } else {
076      returnCode = checkDeleted(deletes, cell);
077      if (returnCode != null) {
078        return returnCode;
079      }
080    }
081    // Skip checking column since we do not remove column during compaction.
082    return columns.checkVersions(cell, cell.getTimestamp(), typeByte,
083      mvccVersion > maxReadPointToTrackVersions);
084  }
085
086  private boolean entered() {
087    return dropDeletesFromRow.length == 0
088      || rowComparator.compareRows(currentRow, dropDeletesFromRow, 0, dropDeletesFromRow.length)
089          >= 0;
090  }
091
092  private boolean left() {
093    return dropDeletesToRow.length > 0
094      && rowComparator.compareRows(currentRow, dropDeletesToRow, 0, dropDeletesToRow.length) >= 0;
095  }
096
097  @Override
098  protected void reset() {
099    super.reset();
100    // Check if we are about to enter or leave the drop deletes range.
101    switch (dropDeletesInOutput) {
102      case BEFORE:
103        if (entered()) {
104          if (left()) {
105            // Already out of range, which means there are no rows within the range.
106            dropDeletesInOutput = DropDeletesInOutput.AFTER;
107          } else {
108            dropDeletesInOutput = DropDeletesInOutput.IN;
109          }
110        }
111        break;
112      case IN:
113        if (left()) {
114          dropDeletesInOutput = DropDeletesInOutput.AFTER;
115        }
116        break;
117      default:
118        break;
119    }
120  }
121}