001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.regionserver;
020
021import java.util.Iterator;
022
023import org.apache.hadoop.hbase.Cell;
024import org.apache.yetus.audience.InterfaceAudience;
025
026
027/**
028 * A basic SegmentScanner used against an ImmutableScanner snapshot
029 * Used flushing where we do a single pass, no reverse scanning or
030 * inserts happening. Its a dumbed-down Scanner that can go fast.
031 * Like {@link org.apache.hadoop.hbase.util.CollectionBackedScanner}
032 * (but making it know about Segments was onerous).
033 */
034@InterfaceAudience.Private
035public class SnapshotSegmentScanner extends NonReversedNonLazyKeyValueScanner {
036  private final ImmutableSegment segment;
037  private Iterator<Cell> iter;
038  private Cell current;
039
040  public SnapshotSegmentScanner(ImmutableSegment segment) {
041    this.segment = segment;
042    this.segment.incScannerCount();
043    this.iter = createIterator(this.segment);
044    if (this.iter.hasNext()){
045      this.current = this.iter.next();
046    }
047  }
048
049  private static Iterator<Cell> createIterator(Segment segment) {
050    return segment.getCellSet().iterator();
051  }
052
053  @Override
054  public Cell peek() {
055    return current;
056  }
057
058  @Override
059  public Cell next() {
060    Cell oldCurrent = current;
061    if(iter.hasNext()){
062      current = iter.next();
063    } else {
064      current = null;
065    }
066    return oldCurrent;
067  }
068
069  @Override
070  public boolean seek(Cell seekCell) {
071    // restart iterator
072    this.iter = createIterator(this.segment);
073    return reseek(seekCell);
074  }
075
076  @Override
077  public boolean reseek(Cell seekCell) {
078    while (this.iter.hasNext()){
079      Cell next = this.iter.next();
080      int ret = this.segment.getComparator().compare(next, seekCell);
081      if (ret >= 0) {
082        this.current = next;
083        return true;
084      }
085    }
086    return false;
087  }
088
089  /**
090   * @see KeyValueScanner#getScannerOrder()
091   */
092  @Override
093  public long getScannerOrder() {
094    return 0;
095  }
096
097  @Override
098  public void close() {
099    this.segment.decScannerCount();
100  }
101}