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, long offHeapSizeDelta,
056      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 boolean compareAndSetDataSize(long expected, long updated) {
066    if (dataSize == expected) {
067      dataSize = updated;
068      return true;
069    }
070    return false;
071  }
072
073  @Override
074  public long getDataSize() {
075    return dataSize;
076  }
077
078  @Override
079  public long getHeapSize() {
080    return heapSize;
081  }
082
083  @Override
084  public long getOffHeapSize() {
085    return offHeapSize;
086  }
087
088  @Override
089  public int getCellsCount() {
090    return cellsCount;
091  }
092
093  @Override
094  public String toString() {
095    return getMemStoreSize().toString();
096  }
097}