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