{"version":3,"file":"fast-node.cjs","names":["varintLength","encodedBytesSize","encodeVarint","encodeBytes","decodeVarintBigInt","decodeBytes"],"sources":["../../src/storage/fast-node.ts"],"sourcesContent":["/**\n * FastNode represents a lightweight version of Node used for fast storage.\n * It stores only the essential data: key, value, and the version when it was last updated.\n * This is used for the fast storage layer optimization in NodeDB.\n *\n * Corresponds to Go's fastnode.Node in fastnode/fast_node.go\n */\n\nimport {\n  decodeBytes,\n  decodeVarintBigInt,\n  encodeBytes,\n  encodedBytesSize,\n  encodeVarint,\n  varintLength,\n} from \"../utils/encoding.js\";\n\n/**\n * FastNode is a lightweight node structure for fast storage.\n * It contains only the key-value pair and the version when it was last updated.\n */\nexport class FastNode {\n  /** The key for this node */\n  readonly key: Uint8Array;\n\n  /** The value stored at this key */\n  readonly value: Uint8Array;\n\n  /** The version at which this node was last updated */\n  readonly versionLastUpdatedAt: bigint;\n\n  /**\n   * Creates a new FastNode instance.\n   *\n   * @param key - The key for this node\n   * @param value - The value to store\n   * @param version - The version when this node was last updated\n   */\n  constructor(key: Uint8Array, value: Uint8Array, version: bigint) {\n    this.key = key;\n    this.value = value;\n    this.versionLastUpdatedAt = version;\n\n    // Make the fast node immutable\n    Object.freeze(this);\n  }\n\n  /**\n   * Calculates the encoded size of this FastNode in bytes.\n   * This is the size that would be written by toBytes().\n   *\n   * Format: varint(version) + length-prefixed-bytes(value)\n   * Note: Key is stored separately in the database, not in the encoded data.\n   *\n   * @returns The number of bytes needed to encode this FastNode\n   */\n  encodedSize(): number {\n    return varintLength(this.versionLastUpdatedAt) + encodedBytesSize(this.value);\n  }\n\n  /**\n   * Serializes this FastNode to bytes.\n   *\n   * Format: varint(version) + length-prefixed-bytes(value)\n   * Note: Key is stored separately in the database, not in the encoded data.\n   *\n   * @returns Encoded byte array\n   */\n  toBytes(): Uint8Array {\n    const versionBytes = encodeVarint(this.versionLastUpdatedAt);\n    const valueBytes = encodeBytes(this.value);\n\n    const result = new Uint8Array(versionBytes.length + valueBytes.length);\n    result.set(versionBytes, 0);\n    result.set(valueBytes, versionBytes.length);\n\n    return result;\n  }\n\n  /**\n   * Deserializes a FastNode from bytes.\n   *\n   * Format: varint(version) + length-prefixed-bytes(value)\n   * Note: Key must be provided separately as it's stored separately in the database.\n   *\n   * @param key - The key for this node (stored separately)\n   * @param bytes - The encoded byte array\n   * @returns A new FastNode instance\n   * @throws Error if the bytes are malformed\n   */\n  static fromBytes(key: Uint8Array, bytes: Uint8Array): FastNode {\n    let offset = 0;\n\n    // Decode version\n    const {\n      value: version, bytesRead: versionBytes,\n    } = decodeVarintBigInt(bytes, offset);\n    offset += versionBytes;\n\n    // Decode value\n    const {\n      bytes: value, bytesRead: valueBytes,\n    } = decodeBytes(bytes, offset);\n    offset += valueBytes;\n\n    // Verify we consumed all bytes\n    if (offset !== bytes.length) {\n      throw new Error(\n        `FastNode.fromBytes: unexpected trailing bytes (consumed ${offset}, have ${bytes.length})`,\n      );\n    }\n\n    return new FastNode(key, value, version);\n  }\n\n  /**\n   * Returns the key as a hex string for debugging.\n   *\n   * @returns Hex string representation of the key\n   */\n  keyString(): string {\n    return Buffer.from(this.key).toString(\"hex\");\n  }\n\n  /**\n   * Returns the value as a hex string for debugging.\n   *\n   * @returns Hex string representation of the value\n   */\n  valueString(): string {\n    return Buffer.from(this.value).toString(\"hex\");\n  }\n}\n"],"mappings":";;;;;;;AAqBA,IAAa,WAAb,MAAa,SAAS;;CAEpB,AAAS;;CAGT,AAAS;;CAGT,AAAS;;;;;;;;CAST,YAAY,KAAiB,OAAmB,SAAiB;AAC/D,OAAK,MAAM;AACX,OAAK,QAAQ;AACb,OAAK,uBAAuB;AAG5B,SAAO,OAAO,KAAK;;;;;;;;;;;CAYrB,cAAsB;AACpB,SAAOA,8BAAa,KAAK,qBAAqB,GAAGC,kCAAiB,KAAK,MAAM;;;;;;;;;;CAW/E,UAAsB;EACpB,MAAM,eAAeC,8BAAa,KAAK,qBAAqB;EAC5D,MAAM,aAAaC,6BAAY,KAAK,MAAM;EAE1C,MAAM,SAAS,IAAI,WAAW,aAAa,SAAS,WAAW,OAAO;AACtE,SAAO,IAAI,cAAc,EAAE;AAC3B,SAAO,IAAI,YAAY,aAAa,OAAO;AAE3C,SAAO;;;;;;;;;;;;;CAcT,OAAO,UAAU,KAAiB,OAA6B;EAC7D,IAAI,SAAS;EAGb,MAAM,EACJ,OAAO,SAAS,WAAW,iBACzBC,oCAAmB,OAAO,OAAO;AACrC,YAAU;EAGV,MAAM,EACJ,OAAO,OAAO,WAAW,eACvBC,6BAAY,OAAO,OAAO;AAC9B,YAAU;AAGV,MAAI,WAAW,MAAM,OACnB,OAAM,IAAI,MACR,2DAA2D,OAAO,SAAS,MAAM,OAAO,GACzF;AAGH,SAAO,IAAI,SAAS,KAAK,OAAO,QAAQ;;;;;;;CAQ1C,YAAoB;AAClB,SAAO,OAAO,KAAK,KAAK,IAAI,CAAC,SAAS,MAAM;;;;;;;CAQ9C,cAAsB;AACpB,SAAO,OAAO,KAAK,KAAK,MAAM,CAAC,SAAS,MAAM"}