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