All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nstream.reflect.model.StoreStats Maven / Gradle / Ivy

The newest version!
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nstream.reflect.model;

import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Kind;
import swim.structure.Record;
import swim.structure.Value;

@SuppressWarnings("checkstyle:VisibilityModifier")
public class StoreStats {
  public volatile long dataSize;
  public volatile long storeSize;
  public volatile long treeCount;
  public volatile long leafCount;
  public volatile long commitCount;
  public volatile long compactCount;

  public StoreStats() {
    // nop
  }

  public void didCommit(long commitSize) {
    COMMIT_COUNT.incrementAndGet(this);
  }

  public void didCompact() {
    COMPACT_COUNT.incrementAndGet(this);
  }

  public void accumulate(StoreStats stats) {
    DATA_SIZE.addAndGet(this, stats.dataSize);
    STORE_SIZE.addAndGet(this, stats.storeSize);
    TREE_COUNT.addAndGet(this, stats.treeCount);
    LEAF_COUNT.addAndGet(this, stats.leafCount);
    COMMIT_COUNT.addAndGet(this, stats.commitCount);
    COMPACT_COUNT.addAndGet(this, stats.compactCount);
  }

  public void supersedeLocal(StoreStats total, StoreStats delta) {
    final long newDataSize = this.dataSize;
    final long newStoreSize = this.storeSize;
    final long newTreeCount = this.treeCount;
    final long newLeafCount = this.leafCount;

    final long oldDataSize = DATA_SIZE.getAndSet(total, newDataSize);
    final long oldStoreSize = STORE_SIZE.getAndSet(total, newStoreSize);
    final long oldTreeCount = TREE_COUNT.getAndSet(total, newTreeCount);
    final long oldLeafCount = LEAF_COUNT.getAndSet(total, newLeafCount);

    DATA_SIZE.addAndGet(delta, newDataSize - oldDataSize);
    STORE_SIZE.addAndGet(delta, newStoreSize - oldStoreSize);
    TREE_COUNT.addAndGet(delta, newTreeCount - oldTreeCount);
    LEAF_COUNT.addAndGet(delta, newLeafCount - oldLeafCount);
  }

  public void supersede(StoreStats total, StoreStats delta) {
    supersedeLocal(total, delta);

    final long newCommitCount = this.commitCount;
    final long newCompactCount = this.compactCount;

    final long oldCommitCount = COMMIT_COUNT.getAndSet(total, newCommitCount);
    final long oldCompactCount = COMPACT_COUNT.getAndSet(total, newCompactCount);

    COMMIT_COUNT.addAndGet(delta, newCommitCount - oldCommitCount);
    COMPACT_COUNT.addAndGet(delta, newCompactCount - oldCompactCount);
  }

  public StoreStats getAndReset() {
    final StoreStats stats = new StoreStats();
    stats.dataSize = DATA_SIZE.getAndSet(this, 0L);
    stats.storeSize = STORE_SIZE.getAndSet(this, 0L);
    stats.treeCount = TREE_COUNT.getAndSet(this, 0L);
    stats.leafCount = LEAF_COUNT.getAndSet(this, 0L);
    stats.commitCount = COMMIT_COUNT.getAndSet(this, 0L);
    stats.compactCount = COMPACT_COUNT.getAndSet(this, 0L);
    return stats;
  }

  public StoreStats get() {
    final StoreStats stats = new StoreStats();
    stats.dataSize = this.dataSize;
    stats.storeSize = this.storeSize;
    stats.treeCount = this.treeCount;
    stats.leafCount = this.leafCount;
    stats.commitCount = this.commitCount;
    stats.compactCount = this.compactCount;
    return stats;
  }

  public Value toValue() {
    return form().mold(this).toValue();
  }

  static final AtomicLongFieldUpdater DATA_SIZE =
      AtomicLongFieldUpdater.newUpdater(StoreStats.class, "dataSize");
  static final AtomicLongFieldUpdater STORE_SIZE =
      AtomicLongFieldUpdater.newUpdater(StoreStats.class, "storeSize");
  static final AtomicLongFieldUpdater TREE_COUNT =
      AtomicLongFieldUpdater.newUpdater(StoreStats.class, "treeCount");
  static final AtomicLongFieldUpdater LEAF_COUNT =
      AtomicLongFieldUpdater.newUpdater(StoreStats.class, "leafCount");
  static final AtomicLongFieldUpdater COMMIT_COUNT =
      AtomicLongFieldUpdater.newUpdater(StoreStats.class, "commitCount");
  static final AtomicLongFieldUpdater COMPACT_COUNT =
      AtomicLongFieldUpdater.newUpdater(StoreStats.class, "compactCount");

  private static Form form;

  @Kind
  public static Form form() {
    if (form == null) {
      form = new StoreStatsForm();
    }
    return form;
  }
}

final class StoreStatsForm extends Form {
  @Override
  public Class type() {
    return StoreStats.class;
  }

  @Override
  public Item mold(StoreStats stats) {
    if (stats != null) {
      final Record record = Record.create(6);
      if (stats.dataSize > 0L) {
        record.slot("dataSize", stats.dataSize);
      }
      if (stats.storeSize > 0L) {
        record.slot("storeSize", stats.storeSize);
      }
      if (stats.treeCount > 0L) {
        record.slot("treeCount", stats.treeCount);
      }
      if (stats.leafCount > 0L) {
        record.slot("leafCount", stats.leafCount);
      }
      if (stats.commitCount > 0L) {
        record.slot("commitCount", stats.commitCount);
      }
      if (stats.compactCount > 0L) {
        record.slot("compactCount", stats.compactCount);
      }
      return record;
    } else {
      return Item.extant();
    }
  }

  @Override
  public StoreStats cast(Item item) {
    final Value value = item.toValue();
    final StoreStats stats = new StoreStats();
    stats.dataSize = value.get("dataSize").longValue(0L);
    stats.storeSize = value.get("storeSize").longValue(0L);
    stats.treeCount = value.get("treeCount").longValue(0L);
    stats.leafCount = value.get("leafCount").longValue(0L);
    stats.commitCount = value.get("commitCount").longValue(0L);
    stats.compactCount = value.get("compactCount").longValue(0L);
    return stats;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy