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 org.apache.yetus.audience.InterfaceAudience;
021
022/**
023 * Accounting of current heap and data sizes.
024 * <em>NOT THREAD SAFE</em>.
025 * Use in a 'local' context only where just a single-thread is updating. No concurrency!
026 * Used, for example, when summing all Cells in a single batch where result is then applied to the
027 * Store.
028 * @see ThreadSafeMemStoreSizing
029 */
030@InterfaceAudience.Private
031class NonThreadSafeMemStoreSizing implements MemStoreSizing {
032  private long dataSize = 0;
033  private long heapSize = 0;
034  private long offHeapSize = 0;
035  private int cellsCount = 0;
036
037  NonThreadSafeMemStoreSizing() {
038    this(0, 0, 0, 0);
039  }
040
041  NonThreadSafeMemStoreSizing(MemStoreSize mss) {
042    this(mss.getDataSize(), mss.getHeapSize(), mss.getOffHeapSize(), mss.getCellsCount());
043  }
044
045  NonThreadSafeMemStoreSizing(long dataSize, long heapSize, long offHeapSize, int cellsCount) {
046    incMemStoreSize(dataSize, heapSize, offHeapSize, cellsCount);
047  }
048
049  @Override
050  public MemStoreSize getMemStoreSize() {
051    return new MemStoreSize(this.dataSize, this.heapSize, this.offHeapSize, this.cellsCount);
052  }
053
054  @Override
055  public long incMemStoreSize(long dataSizeDelta, long heapSizeDelta,
056      long offHeapSizeDelta, int cellsCountDelta) {
057    this.offHeapSize += offHeapSizeDelta;
058    this.heapSize += heapSizeDelta;
059    this.dataSize += dataSizeDelta;
060    this.cellsCount += cellsCountDelta;
061    return this.dataSize;
062  }
063
064  @Override
065  public long getDataSize() {
066    return dataSize;
067  }
068
069  @Override
070  public long getHeapSize() {
071    return heapSize;
072  }
073
074  @Override
075  public long getOffHeapSize() {
076    return offHeapSize;
077  }
078
079  @Override
080  public int getCellsCount() {
081    return cellsCount;
082  }
083
084  @Override
085  public String toString() {
086    return getMemStoreSize().toString();
087  }
088}