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