nstream.reflect.model.DataStats Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nstream-reflect Show documentation
Show all versions of nstream-reflect Show documentation
Web Agent introspection runtime
// 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 DataStats {
public volatile long dataSize;
public volatile long treeCount;
public volatile long leafCount;
public volatile long pageLoadSize;
public volatile long pageLoadCount;
public DataStats() {
// nop
}
public void didLoadPage(long pageSize) {
PAGE_LOAD_SIZE.addAndGet(this, pageSize);
PAGE_LOAD_COUNT.incrementAndGet(this);
}
public void accumulate(DataStats stats) {
DATA_SIZE.addAndGet(this, stats.dataSize);
TREE_COUNT.addAndGet(this, stats.treeCount);
LEAF_COUNT.addAndGet(this, stats.leafCount);
PAGE_LOAD_SIZE.addAndGet(this, stats.pageLoadSize);
PAGE_LOAD_COUNT.addAndGet(this, stats.pageLoadCount);
}
public void supersedeLocal(DataStats total, DataStats delta) {
final long newDataSize = this.dataSize;
final long newTreeCount = this.treeCount;
final long newLeafCount = this.leafCount;
final long oldDataSize = DATA_SIZE.getAndSet(total, newDataSize);
final long oldTreeCount = TREE_COUNT.getAndSet(total, newTreeCount);
final long oldLeafCount = LEAF_COUNT.getAndSet(total, newLeafCount);
DATA_SIZE.addAndGet(delta, newDataSize - oldDataSize);
TREE_COUNT.addAndGet(delta, newTreeCount - oldTreeCount);
LEAF_COUNT.addAndGet(delta, newLeafCount - oldLeafCount);
}
public void supersede(DataStats total, DataStats delta) {
supersedeLocal(total, delta);
final long newPageLoadSize = this.pageLoadSize;
final long newPageLoadCount = this.pageLoadCount;
final long oldPageLoadSize = PAGE_LOAD_SIZE.getAndSet(total, newPageLoadSize);
final long oldPageLoadCount = PAGE_LOAD_COUNT.getAndSet(total, newPageLoadCount);
PAGE_LOAD_SIZE.addAndGet(delta, newPageLoadSize - oldPageLoadSize);
PAGE_LOAD_COUNT.addAndGet(delta, newPageLoadCount - oldPageLoadCount);
}
public DataStats getAndReset() {
final DataStats stats = new DataStats();
stats.dataSize = DATA_SIZE.getAndSet(this, 0L);
stats.treeCount = TREE_COUNT.getAndSet(this, 0L);
stats.leafCount = LEAF_COUNT.getAndSet(this, 0L);
stats.pageLoadSize = PAGE_LOAD_SIZE.getAndSet(this, 0L);
stats.pageLoadCount = PAGE_LOAD_COUNT.getAndSet(this, 0L);
return stats;
}
public DataStats get() {
final DataStats stats = new DataStats();
stats.dataSize = this.dataSize;
stats.treeCount = this.treeCount;
stats.leafCount = this.leafCount;
stats.pageLoadSize = this.pageLoadSize;
stats.pageLoadCount = this.pageLoadCount;
return stats;
}
public Value toValue() {
return form().mold(this).toValue();
}
static final AtomicLongFieldUpdater DATA_SIZE =
AtomicLongFieldUpdater.newUpdater(DataStats.class, "dataSize");
static final AtomicLongFieldUpdater TREE_COUNT =
AtomicLongFieldUpdater.newUpdater(DataStats.class, "treeCount");
static final AtomicLongFieldUpdater LEAF_COUNT =
AtomicLongFieldUpdater.newUpdater(DataStats.class, "leafCount");
static final AtomicLongFieldUpdater PAGE_LOAD_SIZE =
AtomicLongFieldUpdater.newUpdater(DataStats.class, "pageLoadSize");
static final AtomicLongFieldUpdater PAGE_LOAD_COUNT =
AtomicLongFieldUpdater.newUpdater(DataStats.class, "pageLoadCount");
private static Form form;
@Kind
public static Form form() {
if (form == null) {
form = new DataStatsForm();
}
return form;
}
}
final class DataStatsForm extends Form {
@Override
public Class> type() {
return DataStats.class;
}
@Override
public Item mold(DataStats stats) {
if (stats != null) {
final Record record = Record.create(5);
if (stats.dataSize > 0L) {
record.slot("dataSize", stats.dataSize);
}
if (stats.treeCount > 0L) {
record.slot("treeCount", stats.treeCount);
}
if (stats.leafCount > 0L) {
record.slot("leafCount", stats.leafCount);
}
if (stats.pageLoadSize > 0L) {
record.slot("pageLoadSize", stats.pageLoadSize);
}
if (stats.pageLoadCount > 0L) {
record.slot("pageLoadCount", stats.pageLoadCount);
}
return record;
} else {
return Item.extant();
}
}
@Override
public DataStats cast(Item item) {
final Value value = item.toValue();
final DataStats stats = new DataStats();
stats.dataSize = value.get("dataSize").longValue(0L);
stats.treeCount = value.get("treeCount").longValue(0L);
stats.leafCount = value.get("leafCount").longValue(0L);
stats.pageLoadSize = value.get("pageLoadSize").longValue(0L);
stats.pageLoadCount = value.get("pageLoadCount").longValue(0L);
return stats;
}
}