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;
021
022import org.apache.hadoop.hbase.Cell;
023import org.apache.hadoop.hbase.CellUtil;
024import org.apache.hadoop.hbase.PrivateCellUtil;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.apache.hadoop.hbase.regionserver.ScanInfo;
027
028/**
029 * Query matcher for stripe compaction if range drop deletes is used.
030 */
031@InterfaceAudience.Private
032public class StripeCompactionScanQueryMatcher extends DropDeletesCompactionScanQueryMatcher {
033
034  private final byte[] dropDeletesFromRow;
035
036  private final byte[] dropDeletesToRow;
037
038  private enum DropDeletesInOutput {
039    BEFORE, IN, 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 || rowComparator.compareRows(currentRow,
088      dropDeletesFromRow, 0, dropDeletesFromRow.length) >= 0;
089  }
090
091  private boolean left() {
092    return dropDeletesToRow.length > 0
093        && rowComparator.compareRows(currentRow, dropDeletesToRow, 0, dropDeletesToRow.length) >= 0;
094  }
095
096  @Override
097  protected void reset() {
098    super.reset();
099    // Check if we are about to enter or leave the drop deletes range.
100    switch (dropDeletesInOutput) {
101      case BEFORE:
102        if (entered()) {
103          if (left()) {
104            // Already out of range, which means there are no rows within the range.
105            dropDeletesInOutput = DropDeletesInOutput.AFTER;
106          } else {
107            dropDeletesInOutput = DropDeletesInOutput.IN;
108          }
109        }
110        break;
111      case IN:
112        if (left()) {
113          dropDeletesInOutput = DropDeletesInOutput.AFTER;
114        }
115        break;
116      default:
117        break;
118    }
119  }
120}