View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver.querymatcher;
19  
20  import java.io.IOException;
21  
22  import org.apache.hadoop.hbase.Cell;
23  import org.apache.hadoop.hbase.CellUtil;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.regionserver.DeleteTracker;
26  import org.apache.hadoop.hbase.regionserver.ScanInfo;
27  
28  /**
29   * Query matcher for stripe compaction if range drop deletes is used.
30   */
31  @InterfaceAudience.Private
32  public class StripeCompactionScanQueryMatcher extends DropDeletesCompactionScanQueryMatcher {
33  
34    private final byte[] dropDeletesFromRow;
35  
36    private final byte[] dropDeletesToRow;
37  
38    private enum DropDeletesInOutput {
39      BEFORE, IN, AFTER
40    }
41  
42    private DropDeletesInOutput dropDeletesInOutput = DropDeletesInOutput.BEFORE;
43  
44    public StripeCompactionScanQueryMatcher(ScanInfo scanInfo, DeleteTracker deletes,
45        long readPointToUse, long earliestPutTs, long oldestUnexpiredTS, long now,
46        byte[] dropDeletesFromRow, byte[] dropDeletesToRow) {
47      super(scanInfo, deletes, readPointToUse, earliestPutTs, oldestUnexpiredTS, now);
48      this.dropDeletesFromRow = dropDeletesFromRow;
49      this.dropDeletesToRow = dropDeletesToRow;
50    }
51  
52    @Override
53    public MatchCode match(Cell cell) throws IOException {
54      MatchCode returnCode = preCheck(cell);
55      if (returnCode != null) {
56        return returnCode;
57      }
58      long mvccVersion = cell.getSequenceId();
59      byte typeByte = cell.getTypeByte();
60      if (CellUtil.isDelete(typeByte)) {
61        if (mvccVersion > maxReadPointToTrackVersions) {
62          return MatchCode.INCLUDE;
63        }
64        trackDelete(cell);
65        if (dropDeletesInOutput == DropDeletesInOutput.IN) {
66          // here we are running like major compaction
67          trackDelete(cell);
68          returnCode = tryDropDelete(cell);
69          if (returnCode != null) {
70            return returnCode;
71          }
72        } else {
73          return MatchCode.INCLUDE;
74        }
75      } else {
76        returnCode = checkDeleted(deletes, cell);
77        if (returnCode != null) {
78          return returnCode;
79        }
80      }
81      // Skip checking column since we do not remove column during compaction.
82      return columns.checkVersions(cell.getQualifierArray(), cell.getQualifierOffset(),
83        cell.getQualifierLength(), cell.getTimestamp(), typeByte,
84        mvccVersion > maxReadPointToTrackVersions);
85    }
86  
87    private boolean entered() {
88      return dropDeletesFromRow.length == 0 || rowComparator.compareRows(currentRow,
89        dropDeletesFromRow, 0, dropDeletesFromRow.length) >= 0;
90    }
91  
92    private boolean left() {
93      return dropDeletesToRow.length > 0
94          && rowComparator.compareRows(currentRow, dropDeletesToRow, 0, dropDeletesToRow.length) >= 0;
95    }
96  
97    @Override
98    protected void reset() {
99      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 }