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

com.swirlds.state.merkle.queue.QueueNode 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) 2023-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.state.merkle.queue;

import static com.swirlds.state.merkle.logging.StateLogger.logQueueAdd;
import static com.swirlds.state.merkle.logging.StateLogger.logQueueIterate;
import static com.swirlds.state.merkle.logging.StateLogger.logQueuePeek;
import static com.swirlds.state.merkle.logging.StateLogger.logQueueRemove;
import static java.util.Objects.requireNonNull;

import com.hedera.pbj.runtime.Codec;
import com.swirlds.common.merkle.MerkleInternal;
import com.swirlds.common.merkle.impl.PartialBinaryMerkleInternal;
import com.swirlds.common.merkle.utility.DebugIterationEndpoint;
import com.swirlds.common.utility.Labeled;
import com.swirlds.fcqueue.FCQueue;
import com.swirlds.state.merkle.StateUtils;
import com.swirlds.state.merkle.singleton.StringLeaf;
import com.swirlds.state.merkle.singleton.ValueLeaf;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Iterator;

/**
 * A merkle node with a string (the label) as the left child, and the merkle node value as the right
 * child. We actually support a raw type (any type!) as the value, and we serialize it and put it
 * into a simple merkle node.
 *
 * @param  The element type
 */
@DebugIterationEndpoint
public class QueueNode extends PartialBinaryMerkleInternal implements Labeled, MerkleInternal {
    private static final long CLASS_ID = 0x990FF87AD2691DCL;
    public static final int CLASS_VERSION = 1;

    /** Key codec. */
    private final Codec codec;

    /** Key class id. */
    private final Long queueNodeClassId;

    private final Long leafClassId;

    /**
     * @deprecated Only exists for constructable registry as it works today. Remove ASAP!
     */
    @Deprecated(forRemoval = true)
    public QueueNode() {
        setLeft(new StringLeaf());
        setRight(null);
        this.codec = null;
        this.queueNodeClassId = CLASS_ID;
        this.leafClassId = ValueLeaf.CLASS_ID;
    }

    /**
     * Create a new instance.
     *
     *
     */
    public QueueNode(
            @NonNull String serviceName,
            @NonNull String stateKey,
            final long queueNodeClassId,
            final long leafClassId,
            @NonNull final Codec codec) {
        setLeft(new StringLeaf(StateUtils.computeLabel(serviceName, stateKey)));
        setRight(new FCQueue>());
        this.codec = requireNonNull(codec);
        this.queueNodeClassId = queueNodeClassId;
        this.leafClassId = leafClassId;
    }

    /** Copy constructor */
    private QueueNode(@NonNull final QueueNode other) {
        this.setLeft(other.getLeft().copy());
        this.setRight(other.getRight().copy());
        this.codec = other.codec;
        this.queueNodeClassId = other.queueNodeClassId;
        this.leafClassId = other.leafClassId;
    }

    @Override
    public QueueNode copy() {
        return new QueueNode<>(this);
    }

    @Override
    public long getClassId() {
        return queueNodeClassId;
    }

    @Override
    public int getVersion() {
        return CLASS_VERSION;
    }

    @Override
    public String getLabel() {
        final StringLeaf left = getLeft();
        return left.getLabel();
    }

    /** Adds an element to this queue. */
    public void add(E element) {
        getQueue().add(new ValueLeaf<>(leafClassId, codec, element));
        // Log to transaction state log, what was added
        logQueueAdd(getLabel(), element);
    }

    /** Peek an element */
    public E peek() {
        final var valueLeaf = getQueue().peek();
        // Log to transaction state log, what was peeked
        logQueuePeek(getLabel(), valueLeaf);
        return valueLeaf == null ? null : valueLeaf.getValue();
    }

    /** Retrieve and remove an element */
    public E remove() {
        final var valueLeaf = getQueue().remove();
        // Log to transaction state log, what was added
        logQueueRemove(getLabel(), valueLeaf);
        return valueLeaf == null ? null : valueLeaf.getValue();
    }

    /** Iterate over all elements */
    public Iterator iterator() {
        // Log to transaction state log, what was iterated
        logQueueIterate(getLabel(), getRight());
        final var itr = getQueue().stream().iterator();
        return new Iterator<>() {
            @Override
            public boolean hasNext() {
                return itr.hasNext();
            }

            @Override
            public E next() {
                final var valueLeaf = itr.next();
                return valueLeaf == null ? null : valueLeaf.getValue();
            }
        };
    }

    /** Utility shorthand to get the queue */
    private FCQueue> getQueue() {
        return getRight();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy