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.List;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * A list of segment managers coupled with the version of the memstore (version at the time it was
026 * created).
027 * This structure helps to guarantee that the compaction pipeline updates after the compaction is
028 * updated in a consistent (atomic) way.
029 * Specifically, swapping some of the elements in a compaction pipeline with a new compacted
030 * element is permitted only if the pipeline version is the same as the version attached to the
031 * elements.
032 *
033 */
034@InterfaceAudience.Private
035public class VersionedSegmentsList {
036
037  private final List<ImmutableSegment> storeSegments;
038  private final long version;
039
040  public VersionedSegmentsList(List<ImmutableSegment> storeSegments, long version) {
041    this.storeSegments = storeSegments;
042    this.version = version;
043  }
044
045  public List<ImmutableSegment> getStoreSegments() {
046    return storeSegments;
047  }
048
049  public long getVersion() {
050    return version;
051  }
052
053  public int getNumOfCells() {
054    int totalCells = 0;
055    for (ImmutableSegment s : storeSegments) {
056      totalCells += s.getCellsCount();
057    }
058    return totalCells;
059  }
060
061  public int getNumOfSegments() {
062    return storeSegments.size();
063  }
064
065  // Estimates fraction of unique keys
066  double getEstimatedUniquesFrac() {
067    int segmentCells = 0;
068    int maxCells = 0;
069    double est = 0;
070
071    for (ImmutableSegment s : storeSegments) {
072      double segmentUniques = s.getNumUniqueKeys();
073      if(segmentUniques != CellSet.UNKNOWN_NUM_UNIQUES) {
074        segmentCells = s.getCellsCount();
075        if(segmentCells > maxCells) {
076          maxCells = segmentCells;
077          est = segmentUniques / segmentCells;
078        }
079      }
080      // else ignore this segment specifically since if the unique number is unknown counting
081      // cells can be expensive
082    }
083    if(maxCells == 0) {
084      return 1.0;
085    }
086    return est;
087  }
088}