View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import java.io.IOException;
23  import java.util.NavigableSet;
24  
25  import org.apache.hadoop.hbase.KeyValue.Type;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.Cell;
28  import org.apache.hadoop.hbase.CellUtil;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.KeepDeletedCells;
31  import org.apache.hadoop.hbase.KeyValue;
32  import org.apache.hadoop.hbase.KeyValueUtil;
33  import org.apache.hadoop.hbase.client.Scan;
34  import org.apache.hadoop.hbase.filter.Filter;
35  import org.apache.hadoop.hbase.filter.Filter.ReturnCode;
36  import org.apache.hadoop.hbase.io.TimeRange;
37  import org.apache.hadoop.hbase.regionserver.DeleteTracker.DeleteResult;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
40  
41  import com.google.common.base.Preconditions;
42  
43  /**
44   * A query matcher that is specifically designed for the scan case.
45   */
46  @InterfaceAudience.Private
47  public class ScanQueryMatcher {
48    // Optimization so we can skip lots of compares when we decide to skip
49    // to the next row.
50    private boolean stickyNextRow;
51    private final byte[] stopRow;
52  
53    private final TimeRange tr;
54  
55    private final Filter filter;
56  
57    /** Keeps track of deletes */
58    private final DeleteTracker deletes;
59  
60    /*
61     * The following three booleans define how we deal with deletes.
62     * There are three different aspects:
63     * 1. Whether to keep delete markers. This is used in compactions.
64     *    Minor compactions always keep delete markers.
65     * 2. Whether to keep deleted rows. This is also used in compactions,
66     *    if the store is set to keep deleted rows. This implies keeping
67     *    the delete markers as well.
68     *    In this case deleted rows are subject to the normal max version
69     *    and TTL/min version rules just like "normal" rows.
70     * 3. Whether a scan can do time travel queries even before deleted
71     *    marker to reach deleted rows.
72     */
73    /** whether to retain delete markers */
74    private boolean retainDeletesInOutput;
75  
76    /** whether to return deleted rows */
77    private final KeepDeletedCells keepDeletedCells;
78    /** whether time range queries can see rows "behind" a delete */
79    private final boolean seePastDeleteMarkers;
80  
81  
82    /** Keeps track of columns and versions */
83    private final ColumnTracker columns;
84  
85    /** Key to seek to in memstore and StoreFiles */
86    private final Cell startKey;
87  
88    /** Row comparator for the region this query is for */
89    private final KeyValue.KVComparator rowComparator;
90  
91    /* row is not private for tests */
92    /** Row the query is on */
93    byte [] row;
94    int rowOffset;
95    short rowLength;
96    
97    /**
98     * Oldest put in any of the involved store files
99     * Used to decide whether it is ok to delete
100    * family delete marker of this store keeps
101    * deleted KVs.
102    */
103   private final long earliestPutTs;
104   private final long ttl;
105 
106   /** The oldest timestamp we are interested in, based on TTL */
107   private final long oldestUnexpiredTS;
108   private final long now;
109 
110   /** readPoint over which the KVs are unconditionally included */
111   protected long maxReadPointToTrackVersions;
112 
113   private byte[] dropDeletesFromRow = null, dropDeletesToRow = null;
114 
115   /**
116    * This variable shows whether there is an null column in the query. There
117    * always exists a null column in the wildcard column query.
118    * There maybe exists a null column in the explicit column query based on the
119    * first column.
120    * */
121   private boolean hasNullColumn = true;
122   
123   private RegionCoprocessorHost regionCoprocessorHost= null;
124 
125   // By default, when hbase.hstore.time.to.purge.deletes is 0ms, a delete
126   // marker is always removed during a major compaction. If set to non-zero
127   // value then major compaction will try to keep a delete marker around for
128   // the given number of milliseconds. We want to keep the delete markers
129   // around a bit longer because old puts might appear out-of-order. For
130   // example, during log replication between two clusters.
131   //
132   // If the delete marker has lived longer than its column-family's TTL then
133   // the delete marker will be removed even if time.to.purge.deletes has not
134   // passed. This is because all the Puts that this delete marker can influence
135   // would have also expired. (Removing of delete markers on col family TTL will
136   // not happen if min-versions is set to non-zero)
137   //
138   // But, if time.to.purge.deletes has not expired then a delete
139   // marker will not be removed just because there are no Puts that it is
140   // currently influencing. This is because Puts, that this delete can
141   // influence.  may appear out of order.
142   private final long timeToPurgeDeletes;
143   
144   private final boolean isUserScan;
145 
146   private final boolean isReversed;
147 
148   /**
149    * Construct a QueryMatcher for a scan
150    * @param scan
151    * @param scanInfo The store's immutable scan info
152    * @param columns
153    * @param scanType Type of the scan
154    * @param earliestPutTs Earliest put seen in any of the store files.
155    * @param oldestUnexpiredTS the oldest timestamp we are interested in,
156    *  based on TTL
157    * @param regionCoprocessorHost 
158    * @throws IOException 
159    */
160   public ScanQueryMatcher(Scan scan, ScanInfo scanInfo, NavigableSet<byte[]> columns,
161       ScanType scanType, long readPointToUse, long earliestPutTs, long oldestUnexpiredTS,
162       long now, RegionCoprocessorHost regionCoprocessorHost) throws IOException {
163     this.tr = scan.getTimeRange();
164     this.rowComparator = scanInfo.getComparator();
165     this.regionCoprocessorHost = regionCoprocessorHost;
166     this.deletes =  instantiateDeleteTracker();
167     this.stopRow = scan.getStopRow();
168     this.startKey = KeyValueUtil.createFirstDeleteFamilyOnRow(scan.getStartRow(),
169         scanInfo.getFamily());
170     this.filter = scan.getFilter();
171     this.earliestPutTs = earliestPutTs;
172     this.oldestUnexpiredTS = oldestUnexpiredTS;
173     this.now = now;
174 
175     this.maxReadPointToTrackVersions = readPointToUse;
176     this.timeToPurgeDeletes = scanInfo.getTimeToPurgeDeletes();
177     this.ttl = oldestUnexpiredTS;
178 
179     /* how to deal with deletes */
180     this.isUserScan = scanType == ScanType.USER_SCAN;
181     // keep deleted cells: if compaction or raw scan
182     this.keepDeletedCells = scan.isRaw() ? KeepDeletedCells.TRUE :
183       isUserScan ? KeepDeletedCells.FALSE : scanInfo.getKeepDeletedCells();
184     // retain deletes: if minor compaction or raw scanisDone
185     this.retainDeletesInOutput = scanType == ScanType.COMPACT_RETAIN_DELETES || scan.isRaw();
186     // seePastDeleteMarker: user initiated scans
187     this.seePastDeleteMarkers =
188         scanInfo.getKeepDeletedCells() != KeepDeletedCells.FALSE && isUserScan;
189 
190     int maxVersions =
191         scan.isRaw() ? scan.getMaxVersions() : Math.min(scan.getMaxVersions(),
192           scanInfo.getMaxVersions());
193 
194     // Single branch to deal with two types of reads (columns vs all in family)
195     if (columns == null || columns.size() == 0) {
196       // there is always a null column in the wildcard column query.
197       hasNullColumn = true;
198 
199       // use a specialized scan for wildcard column tracker.
200       this.columns = new ScanWildcardColumnTracker(
201           scanInfo.getMinVersions(), maxVersions, oldestUnexpiredTS);
202     } else {
203       // whether there is null column in the explicit column query
204       hasNullColumn = (columns.first().length == 0);
205 
206       // We can share the ExplicitColumnTracker, diff is we reset
207       // between rows, not between storefiles.
208       this.columns = new ExplicitColumnTracker(columns, scanInfo.getMinVersions(), maxVersions,
209           oldestUnexpiredTS);
210     }
211     this.isReversed = scan.isReversed();
212   }
213 
214   private DeleteTracker instantiateDeleteTracker() throws IOException {
215     DeleteTracker tracker = new ScanDeleteTracker();
216     if (regionCoprocessorHost != null) {
217       tracker = regionCoprocessorHost.postInstantiateDeleteTracker(tracker);
218     }
219     return tracker;
220   }
221 
222   /**
223    * Construct a QueryMatcher for a scan that drop deletes from a limited range of rows.
224    * @param scan
225    * @param scanInfo The store's immutable scan info
226    * @param columns
227    * @param earliestPutTs Earliest put seen in any of the store files.
228    * @param oldestUnexpiredTS the oldest timestamp we are interested in, based on TTL
229    * @param now the current server time
230    * @param dropDeletesFromRow The inclusive left bound of the range; can be EMPTY_START_ROW.
231    * @param dropDeletesToRow The exclusive right bound of the range; can be EMPTY_END_ROW.
232    * @param regionCoprocessorHost 
233    * @throws IOException 
234    */
235   public ScanQueryMatcher(Scan scan, ScanInfo scanInfo, NavigableSet<byte[]> columns,
236       long readPointToUse, long earliestPutTs, long oldestUnexpiredTS, long now,
237       byte[] dropDeletesFromRow, byte[] dropDeletesToRow,
238       RegionCoprocessorHost regionCoprocessorHost) throws IOException {
239     this(scan, scanInfo, columns, ScanType.COMPACT_RETAIN_DELETES, readPointToUse, earliestPutTs,
240         oldestUnexpiredTS, now, regionCoprocessorHost);
241     Preconditions.checkArgument((dropDeletesFromRow != null) && (dropDeletesToRow != null));
242     this.dropDeletesFromRow = dropDeletesFromRow;
243     this.dropDeletesToRow = dropDeletesToRow;
244   }
245 
246   /*
247    * Constructor for tests
248    */
249   ScanQueryMatcher(Scan scan, ScanInfo scanInfo,
250       NavigableSet<byte[]> columns, long oldestUnexpiredTS, long now) throws IOException {
251     this(scan, scanInfo, columns, ScanType.USER_SCAN,
252           Long.MAX_VALUE, /* max Readpoint to track versions */
253         HConstants.LATEST_TIMESTAMP, oldestUnexpiredTS, now, null);
254   }
255 
256   /**
257    *
258    * @return  whether there is an null column in the query
259    */
260   public boolean hasNullColumnInQuery() {
261     return hasNullColumn;
262   }
263 
264   /**
265    * Determines if the caller should do one of several things:
266    * - seek/skip to the next row (MatchCode.SEEK_NEXT_ROW)
267    * - seek/skip to the next column (MatchCode.SEEK_NEXT_COL)
268    * - include the current KeyValue (MatchCode.INCLUDE)
269    * - ignore the current KeyValue (MatchCode.SKIP)
270    * - got to the next row (MatchCode.DONE)
271    *
272    * @param cell KeyValue to check
273    * @return The match code instance.
274    * @throws IOException in case there is an internal consistency problem
275    *      caused by a data corruption.
276    */
277   public MatchCode match(Cell cell) throws IOException {
278     if (filter != null && filter.filterAllRemaining()) {
279       return MatchCode.DONE_SCAN;
280     }
281     int ret = this.rowComparator.compareRows(row, this.rowOffset, this.rowLength,
282         cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
283     if (!this.isReversed) {
284       if (ret <= -1) {
285         return MatchCode.DONE;
286       } else if (ret >= 1) {
287         // could optimize this, if necessary?
288         // Could also be called SEEK_TO_CURRENT_ROW, but this
289         // should be rare/never happens.
290         return MatchCode.SEEK_NEXT_ROW;
291       }
292     } else {
293       if (ret <= -1) {
294         return MatchCode.SEEK_NEXT_ROW;
295       } else if (ret >= 1) {
296         return MatchCode.DONE;
297       }
298     }
299 
300     // optimize case.
301     if (this.stickyNextRow)
302       return MatchCode.SEEK_NEXT_ROW;
303 
304     if (this.columns.done()) {
305       stickyNextRow = true;
306       return MatchCode.SEEK_NEXT_ROW;
307     }
308 
309     int qualifierOffset = cell.getQualifierOffset();
310     int qualifierLength = cell.getQualifierLength();
311 
312     long timestamp = cell.getTimestamp();
313     // check for early out based on timestamp alone
314     if (columns.isDone(timestamp)) {
315       return columns.getNextRowOrNextColumn(cell.getQualifierArray(), qualifierOffset,
316           qualifierLength);
317     }
318     // check if the cell is expired by cell TTL
319     if (HStore.isCellTTLExpired(cell, this.oldestUnexpiredTS, this.now)) {
320       return MatchCode.SKIP;
321     }    
322 
323     /*
324      * The delete logic is pretty complicated now.
325      * This is corroborated by the following:
326      * 1. The store might be instructed to keep deleted rows around.
327      * 2. A scan can optionally see past a delete marker now.
328      * 3. If deleted rows are kept, we have to find out when we can
329      *    remove the delete markers.
330      * 4. Family delete markers are always first (regardless of their TS)
331      * 5. Delete markers should not be counted as version
332      * 6. Delete markers affect puts of the *same* TS
333      * 7. Delete marker need to be version counted together with puts
334      *    they affect
335      */
336     byte typeByte = cell.getTypeByte();
337     long mvccVersion = cell.getMvccVersion();
338     if (CellUtil.isDelete(cell)) {
339       if (keepDeletedCells == KeepDeletedCells.FALSE
340           || (keepDeletedCells == KeepDeletedCells.TTL && timestamp < ttl)) {
341         // first ignore delete markers if the scanner can do so, and the
342         // range does not include the marker
343         //
344         // during flushes and compactions also ignore delete markers newer
345         // than the readpoint of any open scanner, this prevents deleted
346         // rows that could still be seen by a scanner from being collected
347         boolean includeDeleteMarker = seePastDeleteMarkers ?
348             tr.withinTimeRange(timestamp) :
349             tr.withinOrAfterTimeRange(timestamp);
350         if (includeDeleteMarker
351             && mvccVersion <= maxReadPointToTrackVersions) {
352           this.deletes.add(cell);
353         }
354         // Can't early out now, because DelFam come before any other keys
355       }
356      
357       if ((!isUserScan)
358           && timeToPurgeDeletes > 0
359           && (EnvironmentEdgeManager.currentTime() - timestamp) 
360             <= timeToPurgeDeletes) {
361         return MatchCode.INCLUDE;
362       } else if (retainDeletesInOutput || mvccVersion > maxReadPointToTrackVersions) {
363         // always include or it is not time yet to check whether it is OK
364         // to purge deltes or not
365         if (!isUserScan) {
366           // if this is not a user scan (compaction), we can filter this deletemarker right here
367           // otherwise (i.e. a "raw" scan) we fall through to normal version and timerange checking
368           return MatchCode.INCLUDE;
369         }
370       } else if (keepDeletedCells == KeepDeletedCells.TRUE
371           || (keepDeletedCells == KeepDeletedCells.TTL && timestamp >= ttl)) {
372         if (timestamp < earliestPutTs) {
373           // keeping delete rows, but there are no puts older than
374           // this delete in the store files.
375           return columns.getNextRowOrNextColumn(cell.getQualifierArray(),
376               qualifierOffset, qualifierLength);
377         }
378         // else: fall through and do version counting on the
379         // delete markers
380       } else {
381         return MatchCode.SKIP;
382       }
383       // note the following next else if...
384       // delete marker are not subject to other delete markers
385     } else if (!this.deletes.isEmpty()) {
386       DeleteResult deleteResult = deletes.isDeleted(cell);
387       switch (deleteResult) {
388         case FAMILY_DELETED:
389         case COLUMN_DELETED:
390           return columns.getNextRowOrNextColumn(cell.getQualifierArray(),
391               qualifierOffset, qualifierLength);
392         case VERSION_DELETED:
393         case FAMILY_VERSION_DELETED:
394           return MatchCode.SKIP;
395         case NOT_DELETED:
396           break;
397         default:
398           throw new RuntimeException("UNEXPECTED");
399         }
400     }
401 
402     int timestampComparison = tr.compare(timestamp);
403     if (timestampComparison >= 1) {
404       return MatchCode.SKIP;
405     } else if (timestampComparison <= -1) {
406       return columns.getNextRowOrNextColumn(cell.getQualifierArray(), qualifierOffset,
407           qualifierLength);
408     }
409 
410     // STEP 1: Check if the column is part of the requested columns
411     MatchCode colChecker = columns.checkColumn(cell.getQualifierArray(), 
412         qualifierOffset, qualifierLength, typeByte);
413     if (colChecker == MatchCode.INCLUDE) {
414       ReturnCode filterResponse = ReturnCode.SKIP;
415       // STEP 2: Yes, the column is part of the requested columns. Check if filter is present
416       if (filter != null) {
417         // STEP 3: Filter the key value and return if it filters out
418         filterResponse = filter.filterKeyValue(cell);
419         switch (filterResponse) {
420         case SKIP:
421           return MatchCode.SKIP;
422         case NEXT_COL:
423           return columns.getNextRowOrNextColumn(cell.getQualifierArray(), 
424               qualifierOffset, qualifierLength);
425         case NEXT_ROW:
426           stickyNextRow = true;
427           return MatchCode.SEEK_NEXT_ROW;
428         case SEEK_NEXT_USING_HINT:
429           return MatchCode.SEEK_NEXT_USING_HINT;
430         default:
431           //It means it is either include or include and seek next
432           break;
433         }
434       }
435       /*
436        * STEP 4: Reaching this step means the column is part of the requested columns and either
437        * the filter is null or the filter has returned INCLUDE or INCLUDE_AND_NEXT_COL response.
438        * Now check the number of versions needed. This method call returns SKIP, INCLUDE,
439        * INCLUDE_AND_SEEK_NEXT_ROW, INCLUDE_AND_SEEK_NEXT_COL.
440        *
441        * FilterResponse            ColumnChecker               Desired behavior
442        * INCLUDE                   SKIP                        row has already been included, SKIP.
443        * INCLUDE                   INCLUDE                     INCLUDE
444        * INCLUDE                   INCLUDE_AND_SEEK_NEXT_COL   INCLUDE_AND_SEEK_NEXT_COL
445        * INCLUDE                   INCLUDE_AND_SEEK_NEXT_ROW   INCLUDE_AND_SEEK_NEXT_ROW
446        * INCLUDE_AND_SEEK_NEXT_COL SKIP                        row has already been included, SKIP.
447        * INCLUDE_AND_SEEK_NEXT_COL INCLUDE                     INCLUDE_AND_SEEK_NEXT_COL
448        * INCLUDE_AND_SEEK_NEXT_COL INCLUDE_AND_SEEK_NEXT_COL   INCLUDE_AND_SEEK_NEXT_COL
449        * INCLUDE_AND_SEEK_NEXT_COL INCLUDE_AND_SEEK_NEXT_ROW   INCLUDE_AND_SEEK_NEXT_ROW
450        *
451        * In all the above scenarios, we return the column checker return value except for
452        * FilterResponse (INCLUDE_AND_SEEK_NEXT_COL) and ColumnChecker(INCLUDE)
453        */
454       colChecker =
455           columns.checkVersions(cell.getQualifierArray(), qualifierOffset,
456               qualifierLength, timestamp, typeByte,
457             mvccVersion > maxReadPointToTrackVersions);
458       //Optimize with stickyNextRow
459       stickyNextRow = colChecker == MatchCode.INCLUDE_AND_SEEK_NEXT_ROW ? true : stickyNextRow;
460       return (filterResponse == ReturnCode.INCLUDE_AND_NEXT_COL &&
461           colChecker == MatchCode.INCLUDE) ? MatchCode.INCLUDE_AND_SEEK_NEXT_COL
462           : colChecker;
463     }
464     stickyNextRow = (colChecker == MatchCode.SEEK_NEXT_ROW) ? true
465         : stickyNextRow;
466     return colChecker;
467   }
468 
469   /** Handle partial-drop-deletes. As we match keys in order, when we have a range from which
470    * we can drop deletes, we can set retainDeletesInOutput to false for the duration of this
471    * range only, and maintain consistency. */
472   private void checkPartialDropDeleteRange(byte [] row, int offset, short length) {
473     // If partial-drop-deletes are used, initially, dropDeletesFromRow and dropDeletesToRow
474     // are both set, and the matcher is set to retain deletes. We assume ordered keys. When
475     // dropDeletesFromRow is leq current kv, we start dropping deletes and reset
476     // dropDeletesFromRow; thus the 2nd "if" starts to apply.
477     if ((dropDeletesFromRow != null)
478         && ((dropDeletesFromRow == HConstants.EMPTY_START_ROW)
479           || (Bytes.compareTo(row, offset, length,
480               dropDeletesFromRow, 0, dropDeletesFromRow.length) >= 0))) {
481       retainDeletesInOutput = false;
482       dropDeletesFromRow = null;
483     }
484     // If dropDeletesFromRow is null and dropDeletesToRow is set, we are inside the partial-
485     // drop-deletes range. When dropDeletesToRow is leq current kv, we stop dropping deletes,
486     // and reset dropDeletesToRow so that we don't do any more compares.
487     if ((dropDeletesFromRow == null)
488         && (dropDeletesToRow != null) && (dropDeletesToRow != HConstants.EMPTY_END_ROW)
489         && (Bytes.compareTo(row, offset, length,
490             dropDeletesToRow, 0, dropDeletesToRow.length) >= 0)) {
491       retainDeletesInOutput = true;
492       dropDeletesToRow = null;
493     }
494   }
495 
496   public boolean moreRowsMayExistAfter(Cell kv) {
497     if (this.isReversed) {
498       if (rowComparator.compareRows(kv.getRowArray(), kv.getRowOffset(),
499           kv.getRowLength(), stopRow, 0, stopRow.length) <= 0) {
500         return false;
501       } else {
502         return true;
503       }
504     }
505     if (!Bytes.equals(stopRow , HConstants.EMPTY_END_ROW) &&
506         rowComparator.compareRows(kv.getRowArray(),kv.getRowOffset(),
507             kv.getRowLength(), stopRow, 0, stopRow.length) >= 0) {
508       // KV >= STOPROW
509       // then NO there is nothing left.
510       return false;
511     } else {
512       return true;
513     }
514   }
515 
516   /**
517    * Set current row
518    * @param row
519    */
520   public void setRow(byte [] row, int offset, short length) {
521     checkPartialDropDeleteRange(row, offset, length);
522     this.row = row;
523     this.rowOffset = offset;
524     this.rowLength = length;
525     reset();
526   }
527 
528   public void reset() {
529     this.deletes.reset();
530     this.columns.reset();
531 
532     stickyNextRow = false;
533   }
534 
535   /**
536    *
537    * @return the start key
538    */
539   public Cell getStartKey() {
540     return this.startKey;
541   }
542 
543   /**
544    *
545    * @return the Filter
546    */
547   Filter getFilter() {
548     return this.filter;
549   }
550 
551   public Cell getNextKeyHint(Cell kv) throws IOException {
552     if (filter == null) {
553       return null;
554     } else {
555       return filter.getNextCellHint(kv);
556     }
557   }
558 
559   public Cell getKeyForNextColumn(Cell kv) {
560     ColumnCount nextColumn = columns.getColumnHint();
561     if (nextColumn == null) {
562       return KeyValueUtil.createLastOnRow(
563           kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
564           kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(),
565           kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength());
566     } else {
567       return KeyValueUtil.createFirstOnRow(
568           kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
569           kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(),
570           nextColumn.getBuffer(), nextColumn.getOffset(), nextColumn.getLength());
571     }
572   }
573 
574   public Cell getKeyForNextRow(Cell kv) {
575     return KeyValueUtil.createLastOnRow(
576         kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
577         null, 0, 0,
578         null, 0, 0);
579   }
580 
581   /**
582    * @param nextIndexed the key of the next entry in the block index (if any)
583    * @param kv The Cell we're using to calculate the seek key
584    * @return result of the compare between the indexed key and the key portion of the passed cell
585    */
586   public int compareKeyForNextRow(Cell nextIndexed, Cell kv) {
587     return rowComparator.compareKey(nextIndexed,
588       kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
589       null, 0, 0,
590       null, 0, 0,
591       HConstants.OLDEST_TIMESTAMP, Type.Minimum.getCode());
592   }
593 
594   /**
595    * @param nextIndexed the key of the next entry in the block index (if any)
596    * @param kv The Cell we're using to calculate the seek key
597    * @return result of the compare between the indexed key and the key portion of the passed cell
598    */
599   public int compareKeyForNextColumn(Cell nextIndexed, Cell kv) {
600     ColumnCount nextColumn = columns.getColumnHint();
601     if (nextColumn == null) {
602       return rowComparator.compareKey(nextIndexed,
603         kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
604         kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(),
605         kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(),
606         HConstants.OLDEST_TIMESTAMP, Type.Minimum.getCode());
607     } else {
608       return rowComparator.compareKey(nextIndexed,
609         kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
610         kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(),
611         nextColumn.getBuffer(), nextColumn.getOffset(), nextColumn.getLength(),
612         HConstants.LATEST_TIMESTAMP, Type.Maximum.getCode());
613     }
614   }
615 
616   //Used only for testing purposes
617   static MatchCode checkColumn(ColumnTracker columnTracker, byte[] bytes, int offset,
618       int length, long ttl, byte type, boolean ignoreCount) throws IOException {
619     MatchCode matchCode = columnTracker.checkColumn(bytes, offset, length, type);
620     if (matchCode == MatchCode.INCLUDE) {
621       return columnTracker.checkVersions(bytes, offset, length, ttl, type, ignoreCount);
622     }
623     return matchCode;
624   }
625 
626   /**
627    * {@link #match} return codes.  These instruct the scanner moving through
628    * memstores and StoreFiles what to do with the current KeyValue.
629    * <p>
630    * Additionally, this contains "early-out" language to tell the scanner to
631    * move on to the next File (memstore or Storefile), or to return immediately.
632    */
633   public static enum MatchCode {
634     /**
635      * Include KeyValue in the returned result
636      */
637     INCLUDE,
638 
639     /**
640      * Do not include KeyValue in the returned result
641      */
642     SKIP,
643 
644     /**
645      * Do not include, jump to next StoreFile or memstore (in time order)
646      */
647     NEXT,
648 
649     /**
650      * Do not include, return current result
651      */
652     DONE,
653 
654     /**
655      * These codes are used by the ScanQueryMatcher
656      */
657 
658     /**
659      * Done with the row, seek there.
660      */
661     SEEK_NEXT_ROW,
662     /**
663      * Done with column, seek to next.
664      */
665     SEEK_NEXT_COL,
666 
667     /**
668      * Done with scan, thanks to the row filter.
669      */
670     DONE_SCAN,
671 
672     /*
673      * Seek to next key which is given as hint.
674      */
675     SEEK_NEXT_USING_HINT,
676 
677     /**
678      * Include KeyValue and done with column, seek to next.
679      */
680     INCLUDE_AND_SEEK_NEXT_COL,
681 
682     /**
683      * Include KeyValue and done with row, seek to next.
684      */
685     INCLUDE_AND_SEEK_NEXT_ROW,
686   }
687 }