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.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.CellComparator;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * The MemStoreMergerSegmentsIterator extends MemStoreSegmentsIterator and performs the scan for
029 * simple merge operation meaning it is NOT based on SQM
030 */
031@InterfaceAudience.Private
032public class MemStoreMergerSegmentsIterator extends MemStoreSegmentsIterator {
033
034  // heap of scanners, lazily initialized
035  private KeyValueHeap heap = null;
036  // remember the initial version of the scanners list
037  List<KeyValueScanner> scanners = new ArrayList<KeyValueScanner>();
038
039  private boolean closed = false;
040
041  // C-tor
042  public MemStoreMergerSegmentsIterator(List<ImmutableSegment> segments, CellComparator comparator,
043    int compactionKVMax) throws IOException {
044    super(compactionKVMax);
045    // create the list of scanners to traverse over all the data
046    // no dirty reads here as these are immutable segments
047    AbstractMemStore.addToScanners(segments, Long.MAX_VALUE, scanners);
048    heap = new KeyValueHeap(scanners, comparator);
049  }
050
051  @Override
052  public boolean hasNext() {
053    if (closed) {
054      return false;
055    }
056    if (this.heap != null) {
057      return (this.heap.peek() != null);
058    }
059    // Doing this way in case some test cases tries to peek directly
060    return false;
061  }
062
063  @Override
064  public Cell next() {
065    try { // try to get next
066      if (!closed && heap != null) {
067        return heap.next();
068      }
069    } catch (IOException ie) {
070      throw new IllegalStateException(ie);
071    }
072    return null;
073  }
074
075  @Override
076  public void close() {
077    if (closed) {
078      return;
079    }
080    // Ensuring that all the segment scanners are closed
081    if (heap != null) {
082      heap.close();
083      // It is safe to do close as no new calls will be made to this scanner.
084      heap = null;
085    } else {
086      for (KeyValueScanner scanner : scanners) {
087        scanner.close();
088      }
089    }
090    closed = true;
091  }
092
093  @Override
094  public void remove() {
095    throw new UnsupportedOperationException();
096  }
097}