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