/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ import { assert, fail } from "@fluidframework/core-utils/internal"; import { type FieldKey, type ITreeCursorSynchronous, type TreeNodeSchemaIdentifier, forEachField, type Value, } from "../../../core/index.js"; import { brand } from "../../../util/index.js"; import type { Counter, DeduplicationTable } from "./chunkCodecUtilities.js"; import { type BufferFormat, IdentifierToken, Shape } from "./chunkEncodingGeneric.js"; import { type EncoderContext, type FieldEncoder, type KeyedFieldEncoder, type NodeEncoder, encodeValue, } from "./compressedEncode.js"; import { SpecialField, supportsSpecializedNodeShapes, type EncodedChunkShape, type EncodedFieldShape, type EncodedValueShape, type EncodedChunkShapeVTextExperimental, } from "./format/index.js"; /** * Encodes a node with the {@link EncodedNodeShape} shape. * @remarks * The fact this is also a Shape is an implementation detail of the encoder: that allows the shape it uses to be itself, * which is an easy way to keep all the related code together without extra objects. */ export class NodeShapeBasedEncoder extends Shape implements NodeEncoder { /** * Set of keys for fields that are encoded using {@link NodeShapeBasedEncoder.specializedFieldEncoders}. * TODO: Ensure uniform chunks, encoding and identifier generation sort fields the same. */ private readonly specializedFieldKeys: Set; public constructor( public readonly type: undefined | TreeNodeSchemaIdentifier, public readonly value: EncodedValueShape, /** * Encoders for a specific set of fields, by key, in the order they will be encoded. * These are fields for which specialized encoding is provided as an optimization. * Using these for a given field instead of falling back to {@link NodeShapeBasedEncoder.specializedFieldEncoders} is often more efficient: * this avoids the need to explicitly include the key and shape in the encoded data for each node instance. * Instead, this information is here, and thus is encoded only once as part of the node shape. * These encoders will be used, even if the field they apply to is empty (which can add overhead for fields which are usually empty). * * Any fields not included here will be encoded using {@link NodeShapeBasedEncoder.otherFieldsEncoder}. * If {@link NodeShapeBasedEncoder.otherFieldsEncoder} is undefined, then this must handle all non-empty fields. */ public readonly specializedFieldEncoders: readonly KeyedFieldEncoder[], /** * Encoder for all other fields that are not in {@link NodeShapeBasedEncoder.specializedFieldEncoders}. These fields must * be encoded after the specialized fields. */ public readonly otherFieldsEncoder: undefined | FieldEncoder, ) { super(); this.specializedFieldKeys = new Set(this.specializedFieldEncoders.map((f) => f.key)); } private getValueToEncode(cursor: ITreeCursorSynchronous, context: EncoderContext): Value { if (this.value === SpecialField.Identifier) { assert(typeof cursor.value === "string", 0x9aa /* identifier must be type string */); return context.encodePossiblyCompressedId(cursor.value); } return cursor.value; } public encodeNode( cursor: ITreeCursorSynchronous, context: EncoderContext, outputBuffer: BufferFormat, ): void { if (this.type === undefined) { outputBuffer.push(new IdentifierToken(cursor.type)); } else { assert(cursor.type === this.type, 0x741 /* type must match shape */); } encodeValue(this.getValueToEncode(cursor, context), this.value, outputBuffer); for (const fieldEncoder of this.specializedFieldEncoders) { cursor.enterField(brand(fieldEncoder.key)); fieldEncoder.encoder.encodeField(cursor, context, outputBuffer); cursor.exitField(); } const otherFieldsBuffer: BufferFormat = []; forEachField(cursor, () => { const key = cursor.getFieldKey(); if (!this.specializedFieldKeys.has(key)) { assert( this.otherFieldsEncoder !== undefined, 0x742 /* had extra local fields when shape does not support them */, ); otherFieldsBuffer.push(new IdentifierToken(key)); this.otherFieldsEncoder.encodeField(cursor, context, otherFieldsBuffer); } }); if (this.otherFieldsEncoder !== undefined) { outputBuffer.push(otherFieldsBuffer); } } public encodeShape( identifiers: DeduplicationTable, shapes: DeduplicationTable>, ): EncodedChunkShape { return { c: { type: encodeOptionalIdentifier(this.type, identifiers), value: this.value, fields: encodeFieldShapes(this.specializedFieldEncoders, identifiers, shapes), extraFields: encodeOptionalFieldShape(this.otherFieldsEncoder, shapes), }, }; } public countReferencedShapesAndIdentifiers( identifiers: Counter, shapeDiscovered: (shape: Shape) => void, ): void { if (this.type !== undefined) { identifiers.add(this.type); } for (const fieldEncoder of this.specializedFieldEncoders) { identifiers.add(fieldEncoder.key); shapeDiscovered(fieldEncoder.encoder.shape); } if (this.otherFieldsEncoder !== undefined) { shapeDiscovered(this.otherFieldsEncoder.shape); } } public get shape(): Shape { return this; } } /** * Encodes a node with the {@link EncodedSpecializedNodeShape} (`f`) shape. * * @remarks * Wraps a {@link NodeShapeBasedEncoder} and overlays a set of field overrides: the differences * from {@link SpecializedNodeShapeEncoder.base}, in either shapes or values. The encoded shape * refers to the base shape and lists only the overrides, instead of repeating the full node shape. */ export class SpecializedNodeShapeEncoder extends Shape implements NodeEncoder { /** * Encodes the node data. * @remarks * This is the base encoder with the field overrides merged into its fields. A node that uses * this shape has the same data as a node that uses this merged encoder, so this class * delegates {@link SpecializedNodeShapeEncoder.encodeNode} to it. */ private readonly inner: NodeShapeBasedEncoder; /** * @param base - The shape that this shape specializes. The encoded shape refers to it by index. * @param fieldOverrides - The fields that differ from `base`. An override with the key of a * field of `base` replaces that field. Other overrides add fields. Each key must be unique. */ public constructor( private readonly base: NodeShapeBasedEncoder, private readonly fieldOverrides: readonly KeyedFieldEncoder[], ) { super(); const overrideMap = new Map(fieldOverrides.map((override) => [override.key, override])); // Duplicate field keys would produce a wire format the decoder rejects. assert( overrideMap.size === fieldOverrides.length, 0xd7c /* duplicate field key in SpecializedNodeShapeEncoder fieldOverrides */, ); const baseKeys = new Set(base.specializedFieldEncoders.map((field) => field.key)); const mergedFields: KeyedFieldEncoder[] = base.specializedFieldEncoders.map( (baseField) => overrideMap.get(baseField.key) ?? baseField, ); for (const override of fieldOverrides) { if (!baseKeys.has(override.key)) { mergedFields.push(override); } } this.inner = new NodeShapeBasedEncoder( base.type, base.value, mergedFields, base.otherFieldsEncoder, ); } public encodeNode( cursor: ITreeCursorSynchronous, context: EncoderContext, outputBuffer: BufferFormat, ): void { // Older formats cannot represent the `f` shape that this encoder writes. assert( supportsSpecializedNodeShapes(context.version), 0xd7d /* SpecializedNodeShapeEncoder requires a format that supports specialized shapes */, ); this.inner.encodeNode(cursor, context, outputBuffer); } public encodeShape( identifiers: DeduplicationTable, shapes: DeduplicationTable>, ): EncodedChunkShapeVTextExperimental { const baseIndex = shapes.valueToIndex.get(this.base) ?? fail(0xd7e /* SpecializedNodeShapeEncoder: base shape missing from shapes table */); return { f: { base: baseIndex, fields: encodeFieldShapes(this.fieldOverrides, identifiers, shapes), }, }; } public countReferencedShapesAndIdentifiers( identifiers: Counter, shapeDiscovered: (shape: Shape) => void, ): void { shapeDiscovered(this.base); for (const override of this.fieldOverrides) { identifiers.add(override.key); shapeDiscovered(override.encoder.shape); } } public get shape(): this { return this; } } export function encodeFieldShapes( fieldEncoders: readonly KeyedFieldEncoder[], identifiers: DeduplicationTable, shapes: DeduplicationTable>, ): EncodedFieldShape[] | undefined { if (fieldEncoders.length === 0) { return undefined; } return fieldEncoders.map((fieldEncoder) => [ // key encodeIdentifier(fieldEncoder.key, identifiers), // shape shapes.valueToIndex.get(fieldEncoder.encoder.shape) ?? fail(0xb50 /* missing shape */), ]); } function encodeIdentifier( identifier: string, identifiers: DeduplicationTable, ): string | number { return identifiers.valueToIndex.get(identifier) ?? identifier; } function encodeOptionalIdentifier( identifier: string | undefined, identifiers: DeduplicationTable, ): string | number | undefined { return identifier === undefined ? undefined : encodeIdentifier(identifier, identifiers); } function encodeOptionalFieldShape( encoder: FieldEncoder | undefined, shapes: DeduplicationTable>, ): number | undefined { return encoder === undefined ? undefined : dedupShape(encoder.shape, shapes); } function dedupShape( shape: Shape, shapes: DeduplicationTable>, ): number { return shapes.valueToIndex.get(shape) ?? fail(0xb51 /* missing shape */); }