/** * FastNode represents a lightweight version of Node used for fast storage. * It stores only the essential data: key, value, and the version when it was last updated. * This is used for the fast storage layer optimization in NodeDB. * * Corresponds to Go's fastnode.Node in fastnode/fast_node.go */ import { decodeBytes, decodeVarintBigInt, encodeBytes, encodedBytesSize, encodeVarint, varintLength, } from "../utils/encoding.js"; /** * FastNode is a lightweight node structure for fast storage. * It contains only the key-value pair and the version when it was last updated. */ export class FastNode { /** The key for this node */ readonly key: Uint8Array; /** The value stored at this key */ readonly value: Uint8Array; /** The version at which this node was last updated */ readonly versionLastUpdatedAt: bigint; /** * Creates a new FastNode instance. * * @param key - The key for this node * @param value - The value to store * @param version - The version when this node was last updated */ constructor(key: Uint8Array, value: Uint8Array, version: bigint) { this.key = key; this.value = value; this.versionLastUpdatedAt = version; // Make the fast node immutable Object.freeze(this); } /** * Calculates the encoded size of this FastNode in bytes. * This is the size that would be written by toBytes(). * * Format: varint(version) + length-prefixed-bytes(value) * Note: Key is stored separately in the database, not in the encoded data. * * @returns The number of bytes needed to encode this FastNode */ encodedSize(): number { return varintLength(this.versionLastUpdatedAt) + encodedBytesSize(this.value); } /** * Serializes this FastNode to bytes. * * Format: varint(version) + length-prefixed-bytes(value) * Note: Key is stored separately in the database, not in the encoded data. * * @returns Encoded byte array */ toBytes(): Uint8Array { const versionBytes = encodeVarint(this.versionLastUpdatedAt); const valueBytes = encodeBytes(this.value); const result = new Uint8Array(versionBytes.length + valueBytes.length); result.set(versionBytes, 0); result.set(valueBytes, versionBytes.length); return result; } /** * Deserializes a FastNode from bytes. * * Format: varint(version) + length-prefixed-bytes(value) * Note: Key must be provided separately as it's stored separately in the database. * * @param key - The key for this node (stored separately) * @param bytes - The encoded byte array * @returns A new FastNode instance * @throws Error if the bytes are malformed */ static fromBytes(key: Uint8Array, bytes: Uint8Array): FastNode { let offset = 0; // Decode version const { value: version, bytesRead: versionBytes, } = decodeVarintBigInt(bytes, offset); offset += versionBytes; // Decode value const { bytes: value, bytesRead: valueBytes, } = decodeBytes(bytes, offset); offset += valueBytes; // Verify we consumed all bytes if (offset !== bytes.length) { throw new Error( `FastNode.fromBytes: unexpected trailing bytes (consumed ${offset}, have ${bytes.length})`, ); } return new FastNode(key, value, version); } /** * Returns the key as a hex string for debugging. * * @returns Hex string representation of the key */ keyString(): string { return Buffer.from(this.key).toString("hex"); } /** * Returns the value as a hex string for debugging. * * @returns Hex string representation of the value */ valueString(): string { return Buffer.from(this.value).toString("hex"); } }