com.swirlds.state.merkle.memory.InMemoryValue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swirlds-state-api Show documentation
Show all versions of swirlds-state-api Show documentation
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.
/*
* 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.memory;
import static com.swirlds.state.merkle.StateUtils.readFromStream;
import static com.swirlds.state.merkle.StateUtils.writeToStream;
import com.hedera.pbj.runtime.Codec;
import com.swirlds.common.io.SelfSerializable;
import com.swirlds.common.io.streams.SerializableDataInputStream;
import com.swirlds.common.io.streams.SerializableDataOutputStream;
import com.swirlds.common.merkle.MerkleLeaf;
import com.swirlds.common.merkle.MerkleNode;
import com.swirlds.common.merkle.impl.PartialMerkleLeaf;
import com.swirlds.common.merkle.utility.Keyed;
import com.swirlds.merkle.map.MerkleMap;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.IOException;
import java.util.Objects;
/** The value stored in a {@link MerkleMap} for in memory states */
public final class InMemoryValue extends PartialMerkleLeaf
implements MerkleNode, Keyed>, SelfSerializable, MerkleLeaf {
@Deprecated(forRemoval = true)
private static final long CLASS_ID = 0x657483284563728L;
/** The key associated with this value. {@link MerkleMap} requires we do this. */
private InMemoryKey key;
private final Codec keyCodec;
private final Codec valueCodec;
private final long classId;
/** The actual value. For example, it could be an Account or SmartContract. */
private V val;
// Default constructor provided for ConstructableRegistry, TO BE REMOVED ASAP
@Deprecated(forRemoval = true)
public InMemoryValue() {
classId = CLASS_ID;
keyCodec = null;
valueCodec = null;
}
/**
* Used by the deserialization system to create an {@link InMemoryValue} that does not yet have
* a value. Normally this should not be used.
*
* @param classId
* @param keyCodec
* @param valueCodec
*/
public InMemoryValue(final long classId, @Nullable Codec keyCodec, @NonNull Codec valueCodec) {
this.classId = classId;
this.keyCodec = keyCodec;
this.valueCodec = valueCodec;
}
/**
* Create a new instance with the given value.
*
* @param classId value class ID
* @param keyCodec key codec
* @param valueCodec value codec
* @param key The associated key.
* @param value The value.
*/
public InMemoryValue(
final long classId,
@Nullable Codec keyCodec,
@NonNull Codec valueCodec,
@NonNull final InMemoryKey key,
@NonNull final V value) {
this(classId, keyCodec, valueCodec);
this.key = Objects.requireNonNull(key);
this.val = Objects.requireNonNull(value);
}
/** {@inheritDoc} */
@Override
public InMemoryValue copy() {
throwIfImmutable();
throwIfDestroyed();
final var cp = new InMemoryValue<>(classId, keyCodec, valueCodec, key, val);
setImmutable(true);
return cp;
}
/** {@inheritDoc} */
@Override
public long getClassId() {
return classId;
}
/** {@inheritDoc} */
@Override
public int getVersion() {
return 1;
}
/** {@inheritDoc} */
@Override
public InMemoryKey getKey() {
return key;
}
/** {@inheritDoc} */
@Override
public void setKey(@NonNull final InMemoryKey inMemoryKey) {
throwIfImmutable();
this.key = Objects.requireNonNull(inMemoryKey);
}
/**
* Gets the value.
*
* @return The value.
*/
@Nullable
public V getValue() {
return val;
}
/**
* Sets the value. Cannot be called if the leaf is immutable.
*
* @param value the new value
*/
public void setValue(@Nullable final V value) {
throwIfImmutable();
this.val = value;
}
/** {@inheritDoc} */
@Override
public void deserialize(@NonNull final SerializableDataInputStream in, final int ignored) throws IOException {
assert keyCodec != null;
final var k = readFromStream(in, keyCodec);
this.key = new InMemoryKey<>(k);
this.val = readFromStream(in, valueCodec);
}
/** {@inheritDoc} */
@Override
public void serialize(@NonNull final SerializableDataOutputStream out) throws IOException {
writeToStream(out, keyCodec, key.key());
writeToStream(out, valueCodec, val);
}
}