com.swirlds.state.merkle.disk.OnDiskReadableKVState 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.disk;
import static com.swirlds.state.merkle.logging.StateLogger.logMapGet;
import static com.swirlds.state.merkle.logging.StateLogger.logMapGetSize;
import static com.swirlds.state.merkle.logging.StateLogger.logMapIterate;
import static java.util.Objects.requireNonNull;
import com.hedera.pbj.runtime.Codec;
import com.swirlds.state.spi.ReadableKVState;
import com.swirlds.state.spi.ReadableKVStateBase;
import com.swirlds.virtualmap.VirtualMap;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Iterator;
/**
* An implementation of {@link ReadableKVState} backed by a {@link VirtualMap}, resulting in a state
* that is stored on disk.
*
* @param The type of key for the state
* @param The type of value for the state
*/
public final class OnDiskReadableKVState extends ReadableKVStateBase {
/** The backing merkle data structure to use */
private final VirtualMap, OnDiskValue> virtualMap;
private final long keyClassId;
private final Codec keyCodec;
/**
* Create a new instance
*
* @param stateKey
* @param keyClassId
* @param keyCodec
* @param virtualMap the backing merkle structure to use
*/
public OnDiskReadableKVState(
String stateKey,
final long keyClassId,
@Nullable final Codec keyCodec,
@NonNull final VirtualMap, OnDiskValue> virtualMap) {
super(stateKey);
this.keyClassId = keyClassId;
this.keyCodec = keyCodec;
this.virtualMap = requireNonNull(virtualMap);
}
/** {@inheritDoc} */
@Override
protected V readFromDataSource(@NonNull K key) {
final var k = new OnDiskKey<>(keyClassId, keyCodec, key);
final var v = virtualMap.get(k);
final var value = v == null ? null : v.getValue();
// Log to transaction state log, what was read
logMapGet(getStateKey(), key, value);
return value;
}
/** {@inheritDoc} */
@NonNull
@Override
protected Iterator iterateFromDataSource() {
// Log to transaction state log, what was iterated
logMapIterate(getStateKey(), virtualMap);
return new OnDiskIterator<>(virtualMap);
}
/** {@inheritDoc} */
@Override
public long size() {
final var size = virtualMap.size();
// Log to transaction state log, size of map
logMapGetSize(getStateKey(), size);
return size;
}
@Override
public void warm(@NonNull final K key) {
final var k = new OnDiskKey<>(keyClassId, keyCodec, key);
virtualMap.warm(k);
}
}