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

com.swirlds.common.utility.RuntimeObjectRegistry Maven / Gradle / Ivy

Go to download

Swirlds is a software platform designed to build fully-distributed applications that harness the power of the cloud without servers. Now you can develop applications with fairness in decision making, speed, trust and reliability, at a fraction of the cost of traditional server-based platforms.

There is a newer version: 0.56.6
Show newest version
/*
 * Copyright (C) 2022-2024 Hedera Hashgraph, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 com.swirlds.common.utility;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Keeps track of the {@link com.swirlds.common.constructable.RuntimeConstructable} objects currently in memory.
 * One registry per process.
 */
public final class RuntimeObjectRegistry {

    /**
     * Contains records of runtime objects, per class, that are currently in memory or that were recently in memory.
     */
    private static final Map, List> RECORDS = new ConcurrentHashMap<>();

    private RuntimeObjectRegistry() {}

    /**
     * Create a new record for a runtime object with the specified class and add it to the records list.
     * When the record is released, it gets deleted from this registry.
     *
     * @param cls
     * 		the object class
     * @return a new {@link RuntimeObjectRecord}. Should be saved by the runtime object and released
     * 		when the object is released.
     */
    public static  RuntimeObjectRecord createRecord(final Class cls) {
        return createRecord(cls, null);
    }

    /**
     * Create a new record for a runtime object with the specified class and add it to the records list.
     * When the record is released, it gets deleted from this registry.
     *
     * @param cls
     * 		the object class
     * @param metadata
     * 		optional metadata associated with the object
     * @return a new {@link RuntimeObjectRecord}. Should be saved by the runtime object and released
     * 		when the object is released.
     */
    public static  RuntimeObjectRecord createRecord(final Class cls, final Object metadata) {
        final Instant now = Instant.now();
        final List classRecords =
                RECORDS.computeIfAbsent(cls, clsid -> Collections.synchronizedList(new ArrayList<>()));
        final RuntimeObjectRecord objectRecord = new RuntimeObjectRecord(now, classRecords::remove, metadata);
        classRecords.add(objectRecord);

        return objectRecord;
    }

    /**
     * Get the current number of runtime objects of the specified class tracked in this registry.
     *
     * @param cls
     * 		the objwct class
     */
    public static  int getActiveObjectsCount(final Class cls) {
        final List classRecords = RECORDS.get(cls);
        return classRecords != null ? classRecords.size() : 0;
    }

    /**
     * Get the age of the oldest runtime object of the specified class tracked in this registry.
     *
     * @param cls
     * 		the object class
     * @param now
     * 		the current time
     */
    public static  Duration getOldestActiveObjectAge(final Class cls, final Instant now) {
        final List classRecords = RECORDS.get(cls);
        if (classRecords == null) {
            return Duration.ZERO;
        }
        try {
            // It doesn't make sense to check if the list is empty, as it may become empty at any moment, as
            // the method isn't synchronized. Instead, just catch IOOBE
            final RuntimeObjectRecord oldestRecord = classRecords.get(0);
            return oldestRecord.getAge(now);
        } catch (final IndexOutOfBoundsException e) {
            return Duration.ZERO;
        }
    }

    /**
     * Get the record associated with the oldest runtime object of the specified class tracked in this registry.
     *
     * @param cls
     * 		the object class
     * @return the oldest record, or null if there are no records available for this class
     */
    public static  RuntimeObjectRecord getOldestActiveObjectRecord(final Class cls) {
        final List classRecords = RECORDS.get(cls);
        if (classRecords == null) {
            return null;
        }
        try {
            // It doesn't make sense to check if the list is empty, as it may become empty at any moment, as
            // the method isn't synchronized. Instead, just catch IOOBE
            return classRecords.get(0);
        } catch (final IndexOutOfBoundsException e) {
            return null;
        }
    }

    /**
     * Get object classes tracked in this registry. The set of classes is immutable.
     *
     * @return the list of tracked classes
     */
    public static Set> getTrackedClasses() {
        return Collections.unmodifiableSet(RECORDS.keySet());
    }

    /**
     * Drop all records of active runtime constructable objects. Allows this class to be unit tested
     * without interference from prior unit tests.
     */
    public static void reset() {
        RECORDS.clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy