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 * {@link MemStoreSnapshot} is a Context Object to hold details of the snapshot taken on a MemStore.
025 * Details include the snapshot's identifier, count of cells in it and total memory size occupied by
026 * all the cells, timestamp information of all the cells and the snapshot immutableSegment.
027 * <p>
028 * NOTE:Every time when {@link MemStoreSnapshot#getScanners} is called, we create new
029 * {@link SnapshotSegmentScanner}s on the {@link MemStoreSnapshot#snapshotImmutableSegment},and
030 * {@link Segment#incScannerCount} is invoked in the {@link SnapshotSegmentScanner} ctor to increase
031 * the reference count of {@link MemStoreLAB} which used by
032 * {@link MemStoreSnapshot#snapshotImmutableSegment}, so after we finish using these scanners, we
033 * must call their close method to invoke {@link Segment#decScannerCount}.
034 */
035@InterfaceAudience.Private
036public class MemStoreSnapshot {
037  private final long id;
038  private final int cellsCount;
039  private final MemStoreSize memStoreSize;
040  private final TimeRangeTracker timeRangeTracker;
041  private final boolean tagsPresent;
042  private final ImmutableSegment snapshotImmutableSegment;
043
044  public MemStoreSnapshot(long id, ImmutableSegment snapshot) {
045    this.id = id;
046    this.cellsCount = snapshot.getCellsCount();
047    this.memStoreSize = snapshot.getMemStoreSize();
048    this.timeRangeTracker = snapshot.getTimeRangeTracker();
049    this.tagsPresent = snapshot.isTagsPresent();
050    this.snapshotImmutableSegment = snapshot;
051  }
052
053  /** Returns snapshot's identifier. */
054  public long getId() {
055    return id;
056  }
057
058  /** Returns Number of Cells in this snapshot. */
059  public int getCellsCount() {
060    return cellsCount;
061  }
062
063  public long getDataSize() {
064    return memStoreSize.getDataSize();
065  }
066
067  public MemStoreSize getMemStoreSize() {
068    return memStoreSize;
069  }
070
071  /** Returns {@link TimeRangeTracker} for all the Cells in the snapshot. */
072  public TimeRangeTracker getTimeRangeTracker() {
073    return timeRangeTracker;
074  }
075
076  /**
077   * Create new {@link SnapshotSegmentScanner}s for iterating over the snapshot. <br/>
078   * NOTE:Here when create new {@link SnapshotSegmentScanner}s, {@link Segment#incScannerCount} is
079   * invoked in the {@link SnapshotSegmentScanner} ctor,so after we use these
080   * {@link SnapshotSegmentScanner}s, we must call {@link SnapshotSegmentScanner#close} to invoke
081   * {@link Segment#decScannerCount}.
082   * @return {@link KeyValueScanner}s(Which type is {@link SnapshotSegmentScanner}) for iterating
083   *         over the snapshot.
084   */
085  public List<KeyValueScanner> getScanners() {
086    return snapshotImmutableSegment.getSnapshotScanners();
087  }
088
089  /** Returns true if tags are present in this snapshot */
090  public boolean isTagsPresent() {
091    return this.tagsPresent;
092  }
093}