Skip to content

Commit 5cf2bff

Browse files
committed
PR comments 3
1 parent fb79930 commit 5cf2bff

File tree

9 files changed

+187
-172
lines changed

9 files changed

+187
-172
lines changed

packages/dds/tree/src/feature-libraries/chunked-forest/chunkTree.ts

Lines changed: 50 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
type ITreeCursorSynchronous,
1313
LeafNodeStoredSchema,
1414
ObjectNodeStoredSchema,
15-
type StoredSchemaCollection,
1615
type TreeFieldStoredSchema,
1716
type TreeNodeSchemaIdentifier,
1817
type TreeStoredSchemaSubscription,
@@ -23,6 +22,7 @@ import {
2322
ValueSchema,
2423
type TreeChunk,
2524
tryGetChunk,
25+
type SchemaAndPolicy,
2626
} from "../../core/index.js";
2727
import { getOrCreate } from "../../util/index.js";
2828
import type { FullSchemaPolicy } from "../modular-schema/index.js";
@@ -54,13 +54,15 @@ export function makeTreeChunker(
5454
defaultChunkPolicy.sequenceChunkInlineThreshold,
5555
defaultChunkPolicy.uniformChunkNodeCount,
5656
(type: TreeNodeSchemaIdentifier, shapes: Map<TreeNodeSchemaIdentifier, ShapeInfo>) =>
57-
tryShapeFromNodeSchema({
58-
schema,
59-
policy,
60-
shouldEncodeIncrementally,
61-
shapes,
62-
nodeSchema: type,
63-
}),
57+
tryShapeFromNodeSchema(
58+
{
59+
schema,
60+
policy,
61+
shouldEncodeIncrementally,
62+
shapes,
63+
},
64+
type,
65+
),
6466
);
6567
}
6668

@@ -231,15 +233,7 @@ export function makePolicy(policy?: Partial<ChunkPolicy>): ChunkPolicy {
231233
return withDefaults;
232234
}
233235

234-
export interface ShapeFromSchemaParameters {
235-
/**
236-
* The collection of stored schemas containing node and field definitions.
237-
*/
238-
readonly schema: StoredSchemaCollection;
239-
/**
240-
* Policy from the app for interpreting the stored schema.
241-
*/
242-
readonly policy: FullSchemaPolicy;
236+
export interface ShapeFromSchemaParameters extends SchemaAndPolicy {
243237
/**
244238
* Policy function to determine if a field should be encoded incrementally.
245239
* Incrementally encoding requires the subtree to not start in the middle of a larger uniform chunk.
@@ -248,29 +242,26 @@ export interface ShapeFromSchemaParameters {
248242
*/
249243
readonly shouldEncodeIncrementally: IncrementalEncodingPolicy;
250244
/**
251-
* A cache for shapes which may be read and/or updated. A new (or cleared) cache must be provided
252-
* if recalling this function after any of the parameters except in `ShapeFromSchemaParameters` have changed.
245+
* A cache for shapes which may be read and/or updated.
246+
* As the shape is a function of the other members of `ShapeFromSchemaParameters`,
247+
* it must be replaced or cleared if any of the properties other than this cache are modified.
253248
*/
254249
readonly shapes: Map<TreeNodeSchemaIdentifier, ShapeInfo>;
255250
}
256251

257-
export interface ShapeFromNodeSchemaParameters extends ShapeFromSchemaParameters {
258-
/**
259-
* The identifier of the specific node schema to analyze for shape uniformity.
260-
*/
261-
readonly nodeSchema: TreeNodeSchemaIdentifier;
262-
}
263-
264-
export interface ShapeFromFieldSchemaParameters extends ShapeFromSchemaParameters {
252+
/**
253+
* A TreeFieldStoredSchema with some additional context about where it is in the tree.
254+
*/
255+
export interface FieldSchemaWithContext {
265256
/**
266257
* The identifier of the specific field schema to analyze for shape uniformity.
267258
*/
268259
readonly fieldSchema: TreeFieldStoredSchema;
269-
270260
/**
271261
* The identifier of the parent node schema containing this field.
262+
* If undefined, this is a root field.
272263
*/
273-
readonly parentNodeSchema: TreeNodeSchemaIdentifier;
264+
readonly parentNodeSchema?: TreeNodeSchemaIdentifier;
274265
/**
275266
* The field key/name used to identify this field within the parent node.
276267
*/
@@ -283,7 +274,8 @@ export interface ShapeFromFieldSchemaParameters extends ShapeFromSchemaParameter
283274
* single child types), returns a TreeShape that can be used for efficient uniform chunking. Otherwise,
284275
* returns Polymorphic to indicate the shape varies and should use basic chunking.
285276
*
286-
* @param params - {@link ShapeFromNodeSchemaParameters}.
277+
* @param params - {@link ShapeFromSchemaParameters}.
278+
* @param nodeSchema - The identifier of the specific node schema to analyze for shape uniformity.
287279
* @returns TreeShape if the schema has a uniform shape, or Polymorphic if shape varies.
288280
*
289281
* @remarks
@@ -292,8 +284,11 @@ export interface ShapeFromFieldSchemaParameters extends ShapeFromSchemaParameter
292284
* optimize for patterns of specific values.
293285
*
294286
*/
295-
export function tryShapeFromNodeSchema(params: ShapeFromNodeSchemaParameters): ShapeInfo {
296-
const { schema, policy, shouldEncodeIncrementally, shapes, nodeSchema } = params;
287+
export function tryShapeFromNodeSchema(
288+
params: ShapeFromSchemaParameters,
289+
nodeSchema: TreeNodeSchemaIdentifier,
290+
): ShapeInfo {
291+
const { schema, policy, shouldEncodeIncrementally, shapes } = params;
297292
return getOrCreate(shapes, nodeSchema, () => {
298293
const treeSchema = schema.nodeSchema.get(nodeSchema) ?? fail(0xaf9 /* missing schema */);
299294
if (treeSchema instanceof LeafNodeStoredSchema) {
@@ -308,15 +303,15 @@ export function tryShapeFromNodeSchema(params: ShapeFromNodeSchemaParameters): S
308303
if (treeSchema instanceof ObjectNodeStoredSchema) {
309304
const fieldsArray: FieldShape[] = [];
310305
for (const [key, fieldSchema] of treeSchema.objectNodeFields) {
311-
const fieldShape = tryShapeFromFieldSchema({
312-
schema,
313-
policy,
314-
shouldEncodeIncrementally,
315-
shapes,
316-
parentNodeSchema: nodeSchema,
317-
fieldSchema,
318-
key,
319-
});
306+
const fieldShape = tryShapeFromFieldSchema(
307+
{
308+
schema,
309+
policy,
310+
shouldEncodeIncrementally,
311+
shapes,
312+
},
313+
{ fieldSchema, parentNodeSchema: nodeSchema, key },
314+
);
320315
if (fieldShape === undefined) {
321316
return polymorphic;
322317
}
@@ -335,6 +330,7 @@ export function tryShapeFromNodeSchema(params: ShapeFromNodeSchemaParameters): S
335330
* or should be encoded incrementally, which requires separate chunking.
336331
*
337332
* @param params - {@link ShapeFromFieldSchemaParameters}.
333+
* @param fieldSchemaWithContext - {@link FieldSchemaWithContext}.
338334
* @returns FieldShape if the field has a uniform shape, or undefined if the field is polymorphic.
339335
*
340336
* @remarks
@@ -343,17 +339,11 @@ export function tryShapeFromNodeSchema(params: ShapeFromNodeSchemaParameters): S
343339
* optimize for patterns of specific values.
344340
*/
345341
export function tryShapeFromFieldSchema(
346-
params: ShapeFromFieldSchemaParameters,
342+
params: ShapeFromSchemaParameters,
343+
fieldSchemaWithContext: FieldSchemaWithContext,
347344
): FieldShape | undefined {
348-
const {
349-
schema,
350-
policy,
351-
shouldEncodeIncrementally,
352-
shapes,
353-
parentNodeSchema,
354-
fieldSchema,
355-
key,
356-
} = params;
345+
const { schema, policy, shouldEncodeIncrementally, shapes } = params;
346+
const { fieldSchema, parentNodeSchema, key } = fieldSchemaWithContext;
357347
// If this field should be encoded incrementally, use polymorphic shape so that they
358348
// are chunked separately and can be re-used across encodings if they do not change.
359349
if (shouldEncodeIncrementally(parentNodeSchema, key)) {
@@ -367,13 +357,15 @@ export function tryShapeFromFieldSchema(
367357
return undefined;
368358
}
369359
const childType = [...fieldSchema.types][0] ?? oob();
370-
const childShape = tryShapeFromNodeSchema({
371-
schema,
372-
policy,
373-
shouldEncodeIncrementally,
374-
shapes,
375-
nodeSchema: childType,
376-
});
360+
const childShape = tryShapeFromNodeSchema(
361+
{
362+
schema,
363+
policy,
364+
shouldEncodeIncrementally,
365+
shapes,
366+
},
367+
childType,
368+
);
377369
if (childShape instanceof Polymorphic) {
378370
return undefined;
379371
}

packages/dds/tree/src/feature-libraries/chunked-forest/codec/incrementalEncodingPolicy.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { FieldKey, TreeNodeSchemaIdentifier } from "../../../core/index.js"
88
/**
99
* Policy to determine whether a node / field should be incrementally encoded.
1010
* @param nodeIdentifier - The identifier of the node containing the field.
11+
* If undefined, the field is a root field.
1112
* @param fieldKey - The key of the field to check.
1213
* @returns whether the node / field should be incrementally encoded.
1314
* @remarks
@@ -17,15 +18,15 @@ import type { FieldKey, TreeNodeSchemaIdentifier } from "../../../core/index.js"
1718
* TODO: AB#9068: Measure the actual overhead.
1819
*/
1920
export type IncrementalEncodingPolicy = (
20-
nodeIdentifier: TreeNodeSchemaIdentifier,
21+
nodeIdentifier: TreeNodeSchemaIdentifier | undefined,
2122
fieldKey: FieldKey,
2223
) => boolean;
2324

2425
/**
2526
* Default policy for incremental encoding is to not encode incrementally.
2627
*/
2728
export const defaultIncrementalEncodingPolicy: IncrementalEncodingPolicy = (
28-
nodeIdentifier: TreeNodeSchemaIdentifier,
29+
nodeIdentifier: TreeNodeSchemaIdentifier | undefined,
2930
fieldKey: FieldKey,
3031
): boolean => {
3132
return false;

packages/dds/tree/src/shared-tree/sharedTree.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535
SchemaVersion,
3636
type TaggedChange,
3737
type TreeFieldStoredSchema,
38-
type TreeNodeSchemaIdentifier,
3938
type TreeNodeStoredSchema,
4039
type TreeStoredSchema,
4140
TreeStoredSchemaRepository,
@@ -51,6 +50,7 @@ import {
5150
TreeCompressionStrategy,
5251
buildChunkedForest,
5352
buildForest,
53+
defaultIncrementalEncodingPolicy,
5454
defaultSchemaPolicy,
5555
jsonableTreeFromFieldCursor,
5656
makeFieldBatchCodec,
@@ -737,12 +737,7 @@ export const defaultSharedTreeOptions: Required<SharedTreeOptionsInternal> = {
737737
treeEncodeType: TreeCompressionStrategy.Compressed,
738738
formatVersion: SharedTreeFormatVersion.v3,
739739
disposeForksAfterTransaction: true,
740-
shouldEncodeIncrementally: (
741-
nodeIdentifier: TreeNodeSchemaIdentifier,
742-
fieldKey: FieldKey,
743-
): boolean => {
744-
return false;
745-
},
740+
shouldEncodeIncrementally: defaultIncrementalEncodingPolicy,
746741
};
747742

748743
function exportSimpleFieldSchemaStored(schema: TreeFieldStoredSchema): SimpleFieldSchema {

packages/dds/tree/src/test/feature-libraries/chunked-forest/chunkEncodingEndToEnd.spec.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,15 @@ describe("End to end chunked encoding", () => {
139139
Number.POSITIVE_INFINITY,
140140
defaultChunkPolicy.uniformChunkNodeCount,
141141
(type: TreeNodeSchemaIdentifier, shapes: Map<TreeNodeSchemaIdentifier, ShapeInfo>) =>
142-
tryShapeFromNodeSchema({
143-
schema: treeSchema,
144-
policy: defaultSchemaPolicy,
145-
shouldEncodeIncrementally: defaultIncrementalEncodingPolicy,
146-
shapes,
147-
nodeSchema: type,
148-
}),
142+
tryShapeFromNodeSchema(
143+
{
144+
schema: treeSchema,
145+
policy: defaultSchemaPolicy,
146+
shouldEncodeIncrementally: defaultIncrementalEncodingPolicy,
147+
shapes,
148+
},
149+
type,
150+
),
149151
);
150152

151153
const forest = buildChunkedForest(chunker);

0 commit comments

Comments
 (0)