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.Arrays;
24  import java.util.NavigableSet;
25  
26  import org.apache.hadoop.hbase.KeyValue.Type;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.CellComparator;
30  import org.apache.hadoop.hbase.CellUtil;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.KeepDeletedCells;
33  import org.apache.hadoop.hbase.KeyValue;
34  import org.apache.hadoop.hbase.KeyValueUtil;
35  import org.apache.hadoop.hbase.client.Scan;
36  import org.apache.hadoop.hbase.filter.Filter;
37  import org.apache.hadoop.hbase.filter.Filter.ReturnCode;
38  import org.apache.hadoop.hbase.io.TimeRange;
39  import org.apache.hadoop.hbase.regionserver.DeleteTracker.DeleteResult;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
42  
43  import com.google.common.base.Preconditions;
44  
45  /**
46   * A query matcher that is specifically designed for the scan case.
47   */
48  @InterfaceAudience.Private
49  public class ScanQueryMatcher {
50    // Optimization so we can skip lots of compares when we decide to skip
51    // to the next row.
52    private boolean stickyNextRow;
53    private final byte[] stopRow;
54  
55    private final TimeRange tr;
56  
57    private final Filter filter;
58  
59    /** Keeps track of deletes */
60    private final DeleteTracker deletes;
61  
62    /*
63     * The following three booleans define how we deal with deletes.
64     * There are three different aspects:
65     * 1. Whether to keep delete markers. This is used in compactions.
66     *    Minor compactions always keep delete markers.
67     * 2. Whether to keep deleted rows. This is also used in compactions,
68     *    if the store is set to keep deleted rows. This implies keeping
69     *    the delete markers as well.
70     *    In this case deleted rows are subject to the normal max version
71     *    and TTL/min version rules just like "normal" rows.
72     * 3. Whether a scan can do time travel queries even before deleted
73     *    marker to reach deleted rows.
74     */
75    /** whether to retain delete markers */
76    private boolean retainDeletesInOutput;
77  
78    /** whether to return deleted rows */
79    private final KeepDeletedCells keepDeletedCells;
80    /** whether time range queries can see rows "behind" a delete */
81    private final boolean seePastDeleteMarkers;
82  
83  
84    /** Keeps track of columns and versions */
85    private final ColumnTracker columns;
86  
87    /** Key to seek to in memstore and StoreFiles */
88    private final Cell startKey;
89  
90    /** Row comparator for the region this query is for */
91    private final CellComparator rowComparator;
92  
93    /* row is not private for tests */
94    /** Row the query is on */
95    Cell curCell;
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(curCell, cell);
282     if (!this.isReversed) {
283       if (ret <= -1) {
284         return MatchCode.DONE;
285       } else if (ret >= 1) {
286         // could optimize this, if necessary?
287         // Could also be called SEEK_TO_CURRENT_ROW, but this
288         // should be rare/never happens.
289         return MatchCode.SEEK_NEXT_ROW;
290       }
291     } else {
292       if (ret <= -1) {
293         return MatchCode.SEEK_NEXT_ROW;
294       } else if (ret >= 1) {
295         return MatchCode.DONE;
296       }
297     }
298 
299     // optimize case.
300     if (this.stickyNextRow)
301       return MatchCode.SEEK_NEXT_ROW;
302 
303     if (this.columns.done()) {
304       stickyNextRow = true;
305       return MatchCode.SEEK_NEXT_ROW;
306     }
307 
308     long timestamp = cell.getTimestamp();
309     // check for early out based on timestamp alone
310     if (columns.isDone(timestamp)) {
311       return columns.getNextRowOrNextColumn(cell);
312     }
313     // check if the cell is expired by cell TTL
314     if (HStore.isCellTTLExpired(cell, this.oldestUnexpiredTS, this.now)) {
315       return MatchCode.SKIP;
316     }    
317 
318     /*
319      * The delete logic is pretty complicated now.
320      * This is corroborated by the following:
321      * 1. The store might be instructed to keep deleted rows around.
322      * 2. A scan can optionally see past a delete marker now.
323      * 3. If deleted rows are kept, we have to find out when we can
324      *    remove the delete markers.
325      * 4. Family delete markers are always first (regardless of their TS)
326      * 5. Delete markers should not be counted as version
327      * 6. Delete markers affect puts of the *same* TS
328      * 7. Delete marker need to be version counted together with puts
329      *    they affect
330      */
331     byte typeByte = cell.getTypeByte();
332     long mvccVersion = cell.getMvccVersion();
333     if (CellUtil.isDelete(cell)) {
334       if (keepDeletedCells == KeepDeletedCells.FALSE
335           || (keepDeletedCells == KeepDeletedCells.TTL && timestamp < ttl)) {
336         // first ignore delete markers if the scanner can do so, and the
337         // range does not include the marker
338         //
339         // during flushes and compactions also ignore delete markers newer
340         // than the readpoint of any open scanner, this prevents deleted
341         // rows that could still be seen by a scanner from being collected
342         boolean includeDeleteMarker = seePastDeleteMarkers ?
343             tr.withinTimeRange(timestamp) :
344             tr.withinOrAfterTimeRange(timestamp);
345         if (includeDeleteMarker
346             && mvccVersion <= maxReadPointToTrackVersions) {
347           this.deletes.add(cell);
348         }
349         // Can't early out now, because DelFam come before any other keys
350       }
351      
352       if ((!isUserScan)
353           && timeToPurgeDeletes > 0
354           && (EnvironmentEdgeManager.currentTime() - timestamp) 
355             <= timeToPurgeDeletes) {
356         return MatchCode.INCLUDE;
357       } else if (retainDeletesInOutput || mvccVersion > maxReadPointToTrackVersions) {
358         // always include or it is not time yet to check whether it is OK
359         // to purge deltes or not
360         if (!isUserScan) {
361           // if this is not a user scan (compaction), we can filter this deletemarker right here
362           // otherwise (i.e. a "raw" scan) we fall through to normal version and timerange checking
363           return MatchCode.INCLUDE;
364         }
365       } else if (keepDeletedCells == KeepDeletedCells.TRUE
366           || (keepDeletedCells == KeepDeletedCells.TTL && timestamp >= ttl)) {
367         if (timestamp < earliestPutTs) {
368           // keeping delete rows, but there are no puts older than
369           // this delete in the store files.
370           return columns.getNextRowOrNextColumn(cell);
371         }
372         // else: fall through and do version counting on the
373         // delete markers
374       } else {
375         return MatchCode.SKIP;
376       }
377       // note the following next else if...
378       // delete marker are not subject to other delete markers
379     } else if (!this.deletes.isEmpty()) {
380       DeleteResult deleteResult = deletes.isDeleted(cell);
381       switch (deleteResult) {
382         case FAMILY_DELETED:
383         case COLUMN_DELETED:
384           return columns.getNextRowOrNextColumn(cell);
385         case VERSION_DELETED:
386         case FAMILY_VERSION_DELETED:
387           return MatchCode.SKIP;
388         case NOT_DELETED:
389           break;
390         default:
391           throw new RuntimeException("UNEXPECTED");
392         }
393     }
394 
395     int timestampComparison = tr.compare(timestamp);
396     if (timestampComparison >= 1) {
397       return MatchCode.SKIP;
398     } else if (timestampComparison <= -1) {
399       return columns.getNextRowOrNextColumn(cell);
400     }
401 
402     // STEP 1: Check if the column is part of the requested columns
403     MatchCode colChecker = columns.checkColumn(cell, typeByte);
404     if (colChecker == MatchCode.INCLUDE) {
405       ReturnCode filterResponse = ReturnCode.SKIP;
406       // STEP 2: Yes, the column is part of the requested columns. Check if filter is present
407       if (filter != null) {
408         // STEP 3: Filter the key value and return if it filters out
409         filterResponse = filter.filterKeyValue(cell);
410         switch (filterResponse) {
411         case SKIP:
412           return MatchCode.SKIP;
413         case NEXT_COL:
414           return columns.getNextRowOrNextColumn(cell);
415         case NEXT_ROW:
416           stickyNextRow = true;
417           return MatchCode.SEEK_NEXT_ROW;
418         case SEEK_NEXT_USING_HINT:
419           return MatchCode.SEEK_NEXT_USING_HINT;
420         default:
421           //It means it is either include or include and seek next
422           break;
423         }
424       }
425       /*
426        * STEP 4: Reaching this step means the column is part of the requested columns and either
427        * the filter is null or the filter has returned INCLUDE or INCLUDE_AND_NEXT_COL response.
428        * Now check the number of versions needed. This method call returns SKIP, INCLUDE,
429        * INCLUDE_AND_SEEK_NEXT_ROW, INCLUDE_AND_SEEK_NEXT_COL.
430        *
431        * FilterResponse            ColumnChecker               Desired behavior
432        * INCLUDE                   SKIP                        row has already been included, SKIP.
433        * INCLUDE                   INCLUDE                     INCLUDE
434        * INCLUDE                   INCLUDE_AND_SEEK_NEXT_COL   INCLUDE_AND_SEEK_NEXT_COL
435        * INCLUDE                   INCLUDE_AND_SEEK_NEXT_ROW   INCLUDE_AND_SEEK_NEXT_ROW
436        * INCLUDE_AND_SEEK_NEXT_COL SKIP                        row has already been included, SKIP.
437        * INCLUDE_AND_SEEK_NEXT_COL INCLUDE                     INCLUDE_AND_SEEK_NEXT_COL
438        * INCLUDE_AND_SEEK_NEXT_COL INCLUDE_AND_SEEK_NEXT_COL   INCLUDE_AND_SEEK_NEXT_COL
439        * INCLUDE_AND_SEEK_NEXT_COL INCLUDE_AND_SEEK_NEXT_ROW   INCLUDE_AND_SEEK_NEXT_ROW
440        *
441        * In all the above scenarios, we return the column checker return value except for
442        * FilterResponse (INCLUDE_AND_SEEK_NEXT_COL) and ColumnChecker(INCLUDE)
443        */
444       colChecker = columns.checkVersions(cell, timestamp, typeByte,
445           mvccVersion > maxReadPointToTrackVersions);
446       //Optimize with stickyNextRow
447       stickyNextRow = colChecker == MatchCode.INCLUDE_AND_SEEK_NEXT_ROW ? true : stickyNextRow;
448       return (filterResponse == ReturnCode.INCLUDE_AND_NEXT_COL &&
449           colChecker == MatchCode.INCLUDE) ? MatchCode.INCLUDE_AND_SEEK_NEXT_COL
450           : colChecker;
451     }
452     stickyNextRow = (colChecker == MatchCode.SEEK_NEXT_ROW) ? true
453         : stickyNextRow;
454     return colChecker;
455   }
456 
457   /** Handle partial-drop-deletes. As we match keys in order, when we have a range from which
458    * we can drop deletes, we can set retainDeletesInOutput to false for the duration of this
459    * range only, and maintain consistency. */
460   private void checkPartialDropDeleteRange(Cell curCell) {
461     // If partial-drop-deletes are used, initially, dropDeletesFromRow and dropDeletesToRow
462     // are both set, and the matcher is set to retain deletes. We assume ordered keys. When
463     // dropDeletesFromRow is leq current kv, we start dropping deletes and reset
464     // dropDeletesFromRow; thus the 2nd "if" starts to apply.
465     if ((dropDeletesFromRow != null)
466         && (Arrays.equals(dropDeletesFromRow, HConstants.EMPTY_START_ROW) ||
467             (CellComparator.COMPARATOR.compareRows(curCell, dropDeletesFromRow, 0,
468                 dropDeletesFromRow.length) >= 0))) {
469       retainDeletesInOutput = false;
470       dropDeletesFromRow = null;
471     }
472     // If dropDeletesFromRow is null and dropDeletesToRow is set, we are inside the partial-
473     // drop-deletes range. When dropDeletesToRow is leq current kv, we stop dropping deletes,
474     // and reset dropDeletesToRow so that we don't do any more compares.
475     if ((dropDeletesFromRow == null)
476         && (dropDeletesToRow != null)
477         && !Arrays.equals(dropDeletesToRow, HConstants.EMPTY_END_ROW)
478         && (CellComparator.COMPARATOR
479             .compareRows(curCell, dropDeletesToRow, 0, dropDeletesToRow.length) >= 0)) {
480       retainDeletesInOutput = true;
481       dropDeletesToRow = null;
482     }
483   }
484 
485   public boolean moreRowsMayExistAfter(Cell kv) {
486     if (this.isReversed) {
487       if (rowComparator.compareRows(kv, stopRow, 0, stopRow.length) <= 0) {
488         return false;
489       } else {
490         return true;
491       }
492     }
493     if (!Bytes.equals(stopRow , HConstants.EMPTY_END_ROW) &&
494         rowComparator.compareRows(kv, stopRow, 0, stopRow.length) >= 0) {
495       // KV >= STOPROW
496       // then NO there is nothing left.
497       return false;
498     } else {
499       return true;
500     }
501   }
502 
503   /**
504    * Set the row when there is change in row
505    * @param curCell
506    */
507   public void setToNewRow(Cell curCell) {
508     checkPartialDropDeleteRange(curCell);
509     this.curCell = curCell;
510     reset();
511   }
512 
513   public void reset() {
514     this.deletes.reset();
515     this.columns.reset();
516 
517     stickyNextRow = false;
518   }
519 
520   /**
521    *
522    * @return the start key
523    */
524   public Cell getStartKey() {
525     return this.startKey;
526   }
527 
528   /**
529    *
530    * @return the Filter
531    */
532   Filter getFilter() {
533     return this.filter;
534   }
535 
536   public Cell getNextKeyHint(Cell kv) throws IOException {
537     if (filter == null) {
538       return null;
539     } else {
540       return filter.getNextCellHint(kv);
541     }
542   }
543 
544   public Cell getKeyForNextColumn(Cell kv) {
545     ColumnCount nextColumn = columns.getColumnHint();
546     if (nextColumn == null) {
547       return KeyValueUtil.createLastOnRow(
548           kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
549           kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(),
550           kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength());
551     } else {
552       return KeyValueUtil.createFirstOnRow(
553           kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
554           kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(),
555           nextColumn.getBuffer(), nextColumn.getOffset(), nextColumn.getLength());
556     }
557   }
558 
559   public Cell getKeyForNextRow(Cell kv) {
560     return KeyValueUtil.createLastOnRow(
561         kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
562         null, 0, 0,
563         null, 0, 0);
564   }
565 
566   /**
567    * @param nextIndexed the key of the next entry in the block index (if any)
568    * @param kv The Cell we're using to calculate the seek key
569    * @return result of the compare between the indexed key and the key portion of the passed cell
570    */
571   public int compareKeyForNextRow(Cell nextIndexed, Cell kv) {
572     return rowComparator.compareKeyBasedOnColHint(nextIndexed, kv, 0, 0, null, 0, 0,
573         HConstants.OLDEST_TIMESTAMP, Type.Minimum.getCode());
574   }
575 
576   /**
577    * @param nextIndexed the key of the next entry in the block index (if any)
578    * @param currentCell The Cell we're using to calculate the seek key
579    * @return result of the compare between the indexed key and the key portion of the passed cell
580    */
581   public int compareKeyForNextColumn(Cell nextIndexed, Cell currentCell) {
582     ColumnCount nextColumn = columns.getColumnHint();
583     if (nextColumn == null) {
584       return rowComparator.compareKeyBasedOnColHint(nextIndexed, currentCell, 0, 0, null, 0, 0,
585           HConstants.OLDEST_TIMESTAMP, Type.Minimum.getCode());
586     } else {
587       return rowComparator.compareKeyBasedOnColHint(nextIndexed, currentCell,
588           currentCell.getFamilyOffset(), currentCell.getFamilyLength(), nextColumn.getBuffer(),
589           nextColumn.getOffset(), nextColumn.getLength(), HConstants.LATEST_TIMESTAMP,
590           Type.Maximum.getCode());
591     }
592   }
593 
594   //Used only for testing purposes
595   static MatchCode checkColumn(ColumnTracker columnTracker, byte[] bytes, int offset,
596       int length, long ttl, byte type, boolean ignoreCount) throws IOException {
597     KeyValue kv = KeyValueUtil.createFirstOnRow(HConstants.EMPTY_BYTE_ARRAY, 0, 0,
598         HConstants.EMPTY_BYTE_ARRAY, 0, 0, bytes, offset, length);
599     MatchCode matchCode = columnTracker.checkColumn(kv, type);
600     if (matchCode == MatchCode.INCLUDE) {
601       return columnTracker.checkVersions(kv, ttl, type, ignoreCount);
602     }
603     return matchCode;
604   }
605 
606   /**
607    * {@link #match} return codes.  These instruct the scanner moving through
608    * memstores and StoreFiles what to do with the current KeyValue.
609    * <p>
610    * Additionally, this contains "early-out" language to tell the scanner to
611    * move on to the next File (memstore or Storefile), or to return immediately.
612    */
613   public static enum MatchCode {
614     /**
615      * Include KeyValue in the returned result
616      */
617     INCLUDE,
618 
619     /**
620      * Do not include KeyValue in the returned result
621      */
622     SKIP,
623 
624     /**
625      * Do not include, jump to next StoreFile or memstore (in time order)
626      */
627     NEXT,
628 
629     /**
630      * Do not include, return current result
631      */
632     DONE,
633 
634     /**
635      * These codes are used by the ScanQueryMatcher
636      */
637 
638     /**
639      * Done with the row, seek there.
640      */
641     SEEK_NEXT_ROW,
642     /**
643      * Done with column, seek to next.
644      */
645     SEEK_NEXT_COL,
646 
647     /**
648      * Done with scan, thanks to the row filter.
649      */
650     DONE_SCAN,
651 
652     /*
653      * Seek to next key which is given as hint.
654      */
655     SEEK_NEXT_USING_HINT,
656 
657     /**
658      * Include KeyValue and done with column, seek to next.
659      */
660     INCLUDE_AND_SEEK_NEXT_COL,
661 
662     /**
663      * Include KeyValue and done with row, seek to next.
664      */
665     INCLUDE_AND_SEEK_NEXT_ROW,
666   }
667 }