View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.regionserver;
22  
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.util.Bytes;
25  
26  /**
27   * This class is responsible for the tracking and enforcement of Deletes
28   * during the course of a Scan operation.
29   *
30   * It only has to enforce Delete and DeleteColumn, since the
31   * DeleteFamily is handled at a higher level.
32   *
33   * <p>
34   * This class is utilized through three methods:
35   * <ul><li>{@link #add} when encountering a Delete or DeleteColumn
36   * <li>{@link #isDeleted} when checking if a Put KeyValue has been deleted
37   * <li>{@link #update} when reaching the end of a StoreFile or row for scans
38   * <p>
39   * This class is NOT thread-safe as queries are never multi-threaded
40   */
41  public class ScanDeleteTracker implements DeleteTracker {
42  
43    private boolean hasFamilyStamp = false;
44    private long familyStamp = 0L;
45    private byte [] deleteBuffer = null;
46    private int deleteOffset = 0;
47    private int deleteLength = 0;
48    private byte deleteType = 0;
49    private long deleteTimestamp = 0L;
50  
51    /**
52     * Constructor for ScanDeleteTracker
53     */
54    public ScanDeleteTracker() {
55      super();
56    }
57  
58    /**
59     * Add the specified KeyValue to the list of deletes to check against for
60     * this row operation.
61     * <p>
62     * This is called when a Delete is encountered in a StoreFile.
63     * @param buffer KeyValue buffer
64     * @param qualifierOffset column qualifier offset
65     * @param qualifierLength column qualifier length
66     * @param timestamp timestamp
67     * @param type delete type as byte
68     */
69    @Override
70    public void add(byte[] buffer, int qualifierOffset, int qualifierLength,
71        long timestamp, byte type) {
72      if (!hasFamilyStamp || timestamp > familyStamp) {
73        if (type == KeyValue.Type.DeleteFamily.getCode()) {
74          hasFamilyStamp = true;
75          familyStamp = timestamp;
76          return;
77        }
78  
79        if (deleteBuffer != null && type < deleteType) {
80          // same column, so ignore less specific delete
81          if (Bytes.equals(deleteBuffer, deleteOffset, deleteLength,
82              buffer, qualifierOffset, qualifierLength)){
83            return;
84          }
85        }
86        // new column, or more general delete type
87        deleteBuffer = buffer;
88        deleteOffset = qualifierOffset;
89        deleteLength = qualifierLength;
90        deleteType = type;
91        deleteTimestamp = timestamp;
92      }
93      // missing else is never called.
94    }
95  
96    /**
97     * Check if the specified KeyValue buffer has been deleted by a previously
98     * seen delete.
99     *
100    * @param buffer KeyValue buffer
101    * @param qualifierOffset column qualifier offset
102    * @param qualifierLength column qualifier length
103    * @param timestamp timestamp
104    * @return deleteResult
105    */
106   @Override
107   public DeleteResult isDeleted(byte [] buffer, int qualifierOffset,
108       int qualifierLength, long timestamp) {
109     if (hasFamilyStamp && timestamp <= familyStamp) {
110       return DeleteResult.FAMILY_DELETED;
111     }
112 
113     if (deleteBuffer != null) {
114       int ret = Bytes.compareTo(deleteBuffer, deleteOffset, deleteLength,
115           buffer, qualifierOffset, qualifierLength);
116 
117       if (ret == 0) {
118         if (deleteType == KeyValue.Type.DeleteColumn.getCode()) {
119           return DeleteResult.COLUMN_DELETED;
120         }
121         // Delete (aka DeleteVersion)
122         // If the timestamp is the same, keep this one
123         if (timestamp == deleteTimestamp) {
124           return DeleteResult.VERSION_DELETED;
125         }
126         // use assert or not?
127         assert timestamp < deleteTimestamp;
128 
129         // different timestamp, let's clear the buffer.
130         deleteBuffer = null;
131       } else if(ret < 0){
132         // Next column case.
133         deleteBuffer = null;
134       } else {
135         throw new IllegalStateException("isDelete failed: deleteBuffer="
136             + Bytes.toStringBinary(deleteBuffer, deleteOffset, deleteLength)
137             + ", qualifier="
138             + Bytes.toStringBinary(buffer, qualifierOffset, qualifierLength)
139             + ", timestamp=" + timestamp + ", comparison result: " + ret);
140       }
141     }
142 
143     return DeleteResult.NOT_DELETED;
144   }
145 
146   @Override
147   public boolean isEmpty() {
148     return deleteBuffer == null && !hasFamilyStamp;
149   }
150 
151   @Override
152   // called between every row.
153   public void reset() {
154     hasFamilyStamp = false;
155     familyStamp = 0L;
156     deleteBuffer = null;
157   }
158 
159   @Override
160   // should not be called at all even (!)
161   public void update() {
162     this.reset();
163   }
164 }