@fluidframework/runtime-utils 2.0.0-dev.2.3.0.115467 → 2.0.0-dev.4.1.0.148229

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/.eslintrc.js +5 -7
  2. package/.mocharc.js +2 -2
  3. package/api-extractor.json +2 -2
  4. package/dist/dataStoreHandleContextUtils.d.ts.map +1 -1
  5. package/dist/dataStoreHandleContextUtils.js +3 -1
  6. package/dist/dataStoreHandleContextUtils.js.map +1 -1
  7. package/dist/dataStoreHelpers.d.ts +3 -1
  8. package/dist/dataStoreHelpers.d.ts.map +1 -1
  9. package/dist/dataStoreHelpers.js +27 -8
  10. package/dist/dataStoreHelpers.js.map +1 -1
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +4 -4
  14. package/dist/index.js.map +1 -1
  15. package/dist/objectstoragepartition.d.ts.map +1 -1
  16. package/dist/objectstoragepartition.js.map +1 -1
  17. package/dist/objectstorageutils.d.ts.map +1 -1
  18. package/dist/objectstorageutils.js.map +1 -1
  19. package/dist/packageVersion.d.ts +1 -1
  20. package/dist/packageVersion.js +1 -1
  21. package/dist/packageVersion.js.map +1 -1
  22. package/dist/requestParser.d.ts.map +1 -1
  23. package/dist/requestParser.js.map +1 -1
  24. package/dist/runtimeFactoryHelper.d.ts +1 -1
  25. package/dist/runtimeFactoryHelper.d.ts.map +1 -1
  26. package/dist/runtimeFactoryHelper.js +7 -6
  27. package/dist/runtimeFactoryHelper.js.map +1 -1
  28. package/dist/summaryUtils.d.ts +33 -1
  29. package/dist/summaryUtils.d.ts.map +1 -1
  30. package/dist/summaryUtils.js +86 -3
  31. package/dist/summaryUtils.js.map +1 -1
  32. package/dist/unpackUsedRoutes.d.ts +11 -0
  33. package/dist/unpackUsedRoutes.d.ts.map +1 -0
  34. package/dist/unpackUsedRoutes.js +33 -0
  35. package/dist/unpackUsedRoutes.js.map +1 -0
  36. package/dist/utils.d.ts.map +1 -1
  37. package/dist/utils.js.map +1 -1
  38. package/lib/dataStoreHandleContextUtils.d.ts.map +1 -1
  39. package/lib/dataStoreHandleContextUtils.js +3 -1
  40. package/lib/dataStoreHandleContextUtils.js.map +1 -1
  41. package/lib/dataStoreHelpers.d.ts +3 -1
  42. package/lib/dataStoreHelpers.d.ts.map +1 -1
  43. package/lib/dataStoreHelpers.js +27 -8
  44. package/lib/dataStoreHelpers.js.map +1 -1
  45. package/lib/index.d.ts +2 -2
  46. package/lib/index.d.ts.map +1 -1
  47. package/lib/index.js +2 -2
  48. package/lib/index.js.map +1 -1
  49. package/lib/objectstoragepartition.d.ts.map +1 -1
  50. package/lib/objectstoragepartition.js.map +1 -1
  51. package/lib/objectstorageutils.d.ts.map +1 -1
  52. package/lib/objectstorageutils.js.map +1 -1
  53. package/lib/packageVersion.d.ts +1 -1
  54. package/lib/packageVersion.js +1 -1
  55. package/lib/packageVersion.js.map +1 -1
  56. package/lib/requestParser.d.ts.map +1 -1
  57. package/lib/requestParser.js.map +1 -1
  58. package/lib/runtimeFactoryHelper.d.ts +1 -1
  59. package/lib/runtimeFactoryHelper.d.ts.map +1 -1
  60. package/lib/runtimeFactoryHelper.js +7 -6
  61. package/lib/runtimeFactoryHelper.js.map +1 -1
  62. package/lib/summaryUtils.d.ts +33 -1
  63. package/lib/summaryUtils.d.ts.map +1 -1
  64. package/lib/summaryUtils.js +84 -2
  65. package/lib/summaryUtils.js.map +1 -1
  66. package/lib/unpackUsedRoutes.d.ts +11 -0
  67. package/lib/unpackUsedRoutes.d.ts.map +1 -0
  68. package/lib/unpackUsedRoutes.js +29 -0
  69. package/lib/unpackUsedRoutes.js.map +1 -0
  70. package/lib/utils.d.ts.map +1 -1
  71. package/lib/utils.js.map +1 -1
  72. package/package.json +54 -55
  73. package/prettier.config.cjs +1 -1
  74. package/src/dataStoreHandleContextUtils.ts +23 -16
  75. package/src/dataStoreHelpers.ts +104 -82
  76. package/src/index.ts +3 -9
  77. package/src/objectstoragepartition.ts +13 -13
  78. package/src/objectstorageutils.ts +31 -28
  79. package/src/packageVersion.ts +1 -1
  80. package/src/requestParser.ts +80 -82
  81. package/src/runtimeFactoryHelper.ts +24 -22
  82. package/src/summaryUtils.ts +393 -291
  83. package/src/unpackUsedRoutes.ts +30 -0
  84. package/src/utils.ts +6 -6
  85. package/tsconfig.esnext.json +6 -6
  86. package/tsconfig.json +8 -12
  87. package/dist/summarizerNode/index.d.ts +0 -8
  88. package/dist/summarizerNode/index.d.ts.map +0 -1
  89. package/dist/summarizerNode/index.js +0 -12
  90. package/dist/summarizerNode/index.js.map +0 -1
  91. package/dist/summarizerNode/summarizerNode.d.ts +0 -140
  92. package/dist/summarizerNode/summarizerNode.d.ts.map +0 -1
  93. package/dist/summarizerNode/summarizerNode.js +0 -439
  94. package/dist/summarizerNode/summarizerNode.js.map +0 -1
  95. package/dist/summarizerNode/summarizerNodeUtils.d.ts +0 -128
  96. package/dist/summarizerNode/summarizerNodeUtils.d.ts.map +0 -1
  97. package/dist/summarizerNode/summarizerNodeUtils.js +0 -132
  98. package/dist/summarizerNode/summarizerNodeUtils.js.map +0 -1
  99. package/dist/summarizerNode/summarizerNodeWithGc.d.ts +0 -131
  100. package/dist/summarizerNode/summarizerNodeWithGc.d.ts.map +0 -1
  101. package/dist/summarizerNode/summarizerNodeWithGc.js +0 -275
  102. package/dist/summarizerNode/summarizerNodeWithGc.js.map +0 -1
  103. package/lib/summarizerNode/index.d.ts +0 -8
  104. package/lib/summarizerNode/index.d.ts.map +0 -1
  105. package/lib/summarizerNode/index.js +0 -7
  106. package/lib/summarizerNode/index.js.map +0 -1
  107. package/lib/summarizerNode/summarizerNode.d.ts +0 -140
  108. package/lib/summarizerNode/summarizerNode.d.ts.map +0 -1
  109. package/lib/summarizerNode/summarizerNode.js +0 -434
  110. package/lib/summarizerNode/summarizerNode.js.map +0 -1
  111. package/lib/summarizerNode/summarizerNodeUtils.d.ts +0 -128
  112. package/lib/summarizerNode/summarizerNodeUtils.d.ts.map +0 -1
  113. package/lib/summarizerNode/summarizerNodeUtils.js +0 -125
  114. package/lib/summarizerNode/summarizerNodeUtils.js.map +0 -1
  115. package/lib/summarizerNode/summarizerNodeWithGc.d.ts +0 -131
  116. package/lib/summarizerNode/summarizerNodeWithGc.d.ts.map +0 -1
  117. package/lib/summarizerNode/summarizerNodeWithGc.js +0 -270
  118. package/lib/summarizerNode/summarizerNodeWithGc.js.map +0 -1
  119. package/src/summarizerNode/index.ts +0 -8
  120. package/src/summarizerNode/summarizerNode.ts +0 -593
  121. package/src/summarizerNode/summarizerNodeUtils.ts +0 -216
  122. package/src/summarizerNode/summarizerNodeWithGc.ts +0 -432
@@ -1,593 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
-
6
- import {
7
- ISummarizerNode,
8
- ISummarizerNodeConfig,
9
- ISummarizeResult,
10
- ISummaryTreeWithStats,
11
- CreateChildSummarizerNodeParam,
12
- CreateSummarizerNodeSource,
13
- SummarizeInternalFn,
14
- ITelemetryContext,
15
- } from "@fluidframework/runtime-definitions";
16
- import {
17
- ISequencedDocumentMessage,
18
- SummaryType,
19
- ISnapshotTree,
20
- SummaryObject,
21
- } from "@fluidframework/protocol-definitions";
22
- import { ITelemetryLogger } from "@fluidframework/common-definitions";
23
- import { assert, unreachableCase } from "@fluidframework/common-utils";
24
- import { mergeStats, convertToSummaryTree, calculateStats } from "../summaryUtils";
25
- import { ReadAndParseBlob } from "../utils";
26
- import {
27
- EscapedPath,
28
- ICreateChildDetails,
29
- IInitialSummary,
30
- ISummarizerNodeRootContract,
31
- parseSummaryForSubtrees,
32
- parseSummaryTreeForSubtrees,
33
- RefreshSummaryResult,
34
- SummaryNode,
35
- } from "./summarizerNodeUtils";
36
-
37
- export interface IRootSummarizerNode extends ISummarizerNode, ISummarizerNodeRootContract {}
38
-
39
- /**
40
- * Encapsulates the summarizing work and state of an individual tree node in the
41
- * summary tree. It tracks changes and allows for optimizations when unchanged, or
42
- * can allow for fallback summaries to be generated when an error is encountered.
43
- * Usage is for the root node to call startSummary first to begin tracking a WIP
44
- * (work in progress) summary. Then all nodes will call summarize to summaries their
45
- * individual parts. Once completed and uploaded to storage, the root node will call
46
- * completeSummary or clearSummary to clear the WIP summary tracking state if something
47
- * went wrong. The SummarizerNodes will track all pending summaries that have been
48
- * recorded by the completeSummary call. When one of them is acked, the root node should
49
- * call refreshLatestSummary to inform the tree of SummarizerNodes of the new baseline
50
- * latest successful summary.
51
- */
52
- export class SummarizerNode implements IRootSummarizerNode {
53
- /**
54
- * The reference sequence number of the most recent acked summary.
55
- * Returns 0 if there is not yet an acked summary.
56
- */
57
- public get referenceSequenceNumber() {
58
- return this._latestSummary?.referenceSequenceNumber ?? 0;
59
- }
60
-
61
- protected readonly children = new Map<string, SummarizerNode>();
62
- protected readonly pendingSummaries = new Map<string, SummaryNode>();
63
- private wipReferenceSequenceNumber: number | undefined;
64
- private wipLocalPaths: { localPath: EscapedPath; additionalPath?: EscapedPath; } | undefined;
65
- private wipSkipRecursion = false;
66
-
67
- public startSummary(referenceSequenceNumber: number, summaryLogger: ITelemetryLogger) {
68
- assert(this.wipSummaryLogger === undefined,
69
- 0x19f /* "wipSummaryLogger should not be set yet in startSummary" */);
70
- assert(this.wipReferenceSequenceNumber === undefined, 0x1a0 /* "Already tracking a summary" */);
71
-
72
- this.wipSummaryLogger = summaryLogger;
73
-
74
- for (const child of this.children.values()) {
75
- child.startSummary(referenceSequenceNumber, this.wipSummaryLogger);
76
- }
77
- this.wipReferenceSequenceNumber = referenceSequenceNumber;
78
- }
79
-
80
- public async summarize(
81
- fullTree: boolean,
82
- trackState: boolean = true,
83
- telemetryContext?: ITelemetryContext,
84
- ): Promise<ISummarizeResult> {
85
- assert(this.isTrackingInProgress(), 0x1a1 /* "summarize should not be called when not tracking the summary" */);
86
- assert(this.wipSummaryLogger !== undefined,
87
- 0x1a2 /* "wipSummaryLogger should have been set in startSummary or ctor" */);
88
-
89
- // Try to reuse the tree if unchanged
90
- if (this.canReuseHandle && !fullTree && !this.hasChanged()) {
91
- const latestSummary = this._latestSummary;
92
- if (latestSummary !== undefined) {
93
- this.wipLocalPaths = {
94
- localPath: latestSummary.localPath,
95
- additionalPath: latestSummary.additionalPath,
96
- };
97
- this.wipSkipRecursion = true;
98
- const stats = mergeStats();
99
- stats.handleNodeCount++;
100
- return {
101
- summary: {
102
- type: SummaryType.Handle,
103
- handle: latestSummary.fullPath.path,
104
- handleType: SummaryType.Tree,
105
- },
106
- stats,
107
- };
108
- }
109
- }
110
-
111
- const result = await this.summarizeInternalFn(fullTree, true, telemetryContext);
112
- this.wipLocalPaths = { localPath: EscapedPath.create(result.id) };
113
- if (result.pathPartsForChildren !== undefined) {
114
- this.wipLocalPaths.additionalPath = EscapedPath.createAndConcat(result.pathPartsForChildren);
115
- }
116
- return { summary: result.summary, stats: result.stats };
117
- }
118
-
119
- /**
120
- * Complete the WIP summary for the given proposalHandle
121
- */
122
- public completeSummary(proposalHandle: string) {
123
- this.completeSummaryCore(proposalHandle, undefined, false);
124
- }
125
-
126
- /**
127
- * Recursive implementation for completeSummary, with additional internal-only parameters
128
- */
129
- protected completeSummaryCore(
130
- proposalHandle: string,
131
- parentPath: EscapedPath | undefined,
132
- parentSkipRecursion: boolean,
133
- ) {
134
- assert(this.wipSummaryLogger !== undefined,
135
- 0x1a3 /* "wipSummaryLogger should have been set in startSummary or ctor" */);
136
- assert(this.wipReferenceSequenceNumber !== undefined, 0x1a4 /* "Not tracking a summary" */);
137
- let localPathsToUse = this.wipLocalPaths;
138
-
139
- if (parentSkipRecursion) {
140
- const latestSummary = this._latestSummary;
141
- if (latestSummary !== undefined) {
142
- // This case the parent node created a failure summary or was reused.
143
- // This node and all children should only try to reference their path
144
- // by its last known good state in the actual summary tree.
145
- // If parent fails or is reused, the child summarize is not called so
146
- // it did not get a chance to change its paths.
147
- // In this case, essentially only propagate the new summary ref seq num.
148
- localPathsToUse = {
149
- localPath: latestSummary.localPath,
150
- additionalPath: latestSummary.additionalPath,
151
- };
152
- } else {
153
- // This case the child is added after the latest non-failure summary.
154
- // This node and all children should consider themselves as still not
155
- // having a successful summary yet.
156
- // We cannot "reuse" this node if unchanged since that summary, because
157
- // handles will be unable to point to that node. It never made it to the
158
- // tree itself, and only exists as an attach op in the _outstandingOps.
159
- this.clearSummary();
160
- return;
161
- }
162
- }
163
-
164
- // This should come from wipLocalPaths in normal cases, or from the latestSummary
165
- // if parentIsFailure or parentIsReused is true.
166
- // If there is no latestSummary, clearSummary and return before reaching this code.
167
- assert(!!localPathsToUse, 0x1a5 /* "Tracked summary local paths not set" */);
168
-
169
- const summary = new SummaryNode({
170
- ...localPathsToUse,
171
- referenceSequenceNumber: this.wipReferenceSequenceNumber,
172
- basePath: parentPath,
173
- });
174
- const fullPathForChildren = summary.fullPathForChildren;
175
- for (const child of this.children.values()) {
176
- child.completeSummaryCore(
177
- proposalHandle,
178
- fullPathForChildren,
179
- this.wipSkipRecursion || parentSkipRecursion,
180
- );
181
- }
182
- // Note that this overwrites existing pending summary with
183
- // the same proposalHandle. If proposalHandle is something like
184
- // a hash or unique identifier, this should be fine. If storage
185
- // can return the same proposalHandle for a different summary,
186
- // this should still be okay, because we should be proposing the
187
- // newer one later which would have to overwrite the previous one.
188
- this.pendingSummaries.set(proposalHandle, summary);
189
- this.clearSummary();
190
- }
191
-
192
- public clearSummary() {
193
- this.wipReferenceSequenceNumber = undefined;
194
- this.wipLocalPaths = undefined;
195
- this.wipSkipRecursion = false;
196
- this.wipSummaryLogger = undefined;
197
- for (const child of this.children.values()) {
198
- child.clearSummary();
199
- }
200
- }
201
-
202
- /**
203
- * Refreshes the latest summary tracked by this node. If we have a pending summary for the given proposal handle,
204
- * it becomes the latest summary. If the current summary is already ahead (e.g., loaded from a service summary),
205
- * we skip the update. Otherwise, we get the snapshot by calling `getSnapshot` and update latest
206
- * summary based off of that.
207
- *
208
- * @returns A RefreshSummaryResult type which returns information based on the following three scenarios:
209
- *
210
- * 1. The latest summary was not udpated.
211
- *
212
- * 2. The latest summary was updated and the summary corresponding to the params was being tracked.
213
- *
214
- * 3. The latest summary was updated but the summary corresponding to the params was not tracked. In this
215
- * case, the latest summary is updated based on the downloaded snapshot which is also returned.
216
- */
217
- public async refreshLatestSummary(
218
- proposalHandle: string | undefined,
219
- summaryRefSeq: number,
220
- getSnapshot: () => Promise<ISnapshotTree>,
221
- readAndParseBlob: ReadAndParseBlob,
222
- correlatedSummaryLogger: ITelemetryLogger,
223
- ): Promise<RefreshSummaryResult> {
224
- if (proposalHandle !== undefined) {
225
- const maybeSummaryNode = this.pendingSummaries.get(proposalHandle);
226
-
227
- if (maybeSummaryNode !== undefined) {
228
- this.refreshLatestSummaryFromPending(proposalHandle, maybeSummaryNode.referenceSequenceNumber);
229
- return { latestSummaryUpdated: true, wasSummaryTracked: true };
230
- }
231
-
232
- const props = {
233
- summaryRefSeq,
234
- pendingSize: this.pendingSummaries.size ?? undefined,
235
- };
236
- this.defaultLogger.sendTelemetryEvent({
237
- eventName: "PendingSummaryNotFound",
238
- proposalHandle,
239
- referenceSequenceNumber: this.referenceSequenceNumber,
240
- details: JSON.stringify(props),
241
- });
242
- }
243
-
244
- // If we have seen a summary same or later as the current one, ignore it.
245
- if (this.referenceSequenceNumber >= summaryRefSeq) {
246
- return { latestSummaryUpdated: false };
247
- }
248
-
249
- const snapshotTree = await getSnapshot();
250
- await this.refreshLatestSummaryFromSnapshot(
251
- summaryRefSeq,
252
- snapshotTree,
253
- undefined,
254
- EscapedPath.create(""),
255
- correlatedSummaryLogger,
256
- readAndParseBlob,
257
- );
258
- return { latestSummaryUpdated: true, wasSummaryTracked: false, snapshot: snapshotTree };
259
- }
260
- /**
261
- * Called when we get an ack from the server for a summary we've just sent. Updates the reference state of this node
262
- * from the state in the pending summary queue.
263
- * @param proposalHandle - Handle for the current proposal.
264
- * @param referenceSequenceNumber - reference sequence number of sent summary.
265
- */
266
- protected refreshLatestSummaryFromPending(
267
- proposalHandle: string,
268
- referenceSequenceNumber: number,
269
- ): void {
270
- const summaryNode = this.pendingSummaries.get(proposalHandle);
271
- if (summaryNode === undefined) {
272
- // This should only happen if parent skipped recursion AND no prior summary existed.
273
- assert(
274
- this._latestSummary === undefined,
275
- 0x1a6 /* "Not found pending summary, but this node has previously completed a summary" */,
276
- );
277
- return;
278
- } else {
279
- assert(
280
- referenceSequenceNumber === summaryNode.referenceSequenceNumber,
281
- 0x1a7 /* Pending summary reference sequence number should be consistent */,
282
- );
283
-
284
- // Clear earlier pending summaries
285
- this.pendingSummaries.delete(proposalHandle);
286
- }
287
-
288
- this.refreshLatestSummaryCore(referenceSequenceNumber);
289
-
290
- this._latestSummary = summaryNode;
291
- // Propagate update to all child nodes
292
- for (const child of this.children.values()) {
293
- child.refreshLatestSummaryFromPending(proposalHandle, referenceSequenceNumber);
294
- }
295
- }
296
-
297
- protected async refreshLatestSummaryFromSnapshot(
298
- referenceSequenceNumber: number,
299
- snapshotTree: ISnapshotTree,
300
- basePath: EscapedPath | undefined,
301
- localPath: EscapedPath,
302
- correlatedSummaryLogger: ITelemetryLogger,
303
- readAndParseBlob: ReadAndParseBlob,
304
- ): Promise<void> {
305
- // Possible re-entrancy. If we have already seen a summary later than this one, ignore it.
306
- if (this.referenceSequenceNumber >= referenceSequenceNumber) {
307
- return;
308
- }
309
-
310
- this.refreshLatestSummaryCore(referenceSequenceNumber);
311
-
312
- this._latestSummary = new SummaryNode({
313
- referenceSequenceNumber,
314
- basePath,
315
- localPath,
316
- });
317
-
318
- const pathParts: string[] = [];
319
- const { childrenTree, childrenPathPart } = parseSummaryForSubtrees(snapshotTree);
320
- if (childrenPathPart !== undefined) {
321
- pathParts.push(childrenPathPart);
322
- }
323
-
324
- if (pathParts.length > 0) {
325
- this._latestSummary.additionalPath = EscapedPath.createAndConcat(pathParts);
326
- }
327
-
328
- // Propagate update to all child nodes
329
- const pathForChildren = this._latestSummary.fullPathForChildren;
330
- await Promise.all(Array.from(this.children)
331
- .filter(([id]) => {
332
- // Assuming subtrees missing from snapshot are newer than the snapshot,
333
- // but might be nice to assert this using earliest seq for node.
334
- return childrenTree.trees[id] !== undefined;
335
- }).map(async ([id, child]) => {
336
- return child.refreshLatestSummaryFromSnapshot(
337
- referenceSequenceNumber,
338
- childrenTree.trees[id],
339
- pathForChildren,
340
- EscapedPath.create(id),
341
- correlatedSummaryLogger,
342
- readAndParseBlob,
343
- );
344
- }));
345
- }
346
-
347
- private refreshLatestSummaryCore(referenceSequenceNumber: number): void {
348
- for (const [key, value] of this.pendingSummaries) {
349
- if (value.referenceSequenceNumber < referenceSequenceNumber) {
350
- this.pendingSummaries.delete(key);
351
- }
352
- }
353
- }
354
-
355
- public updateBaseSummaryState(snapshot: ISnapshotTree) {
356
- // Check base summary to see if it has any additional path parts
357
- // separating child SummarizerNodes. Checks for .channels subtrees.
358
- const { childrenPathPart } = parseSummaryForSubtrees(snapshot);
359
- if (childrenPathPart !== undefined && this._latestSummary !== undefined) {
360
- this._latestSummary.additionalPath = EscapedPath.create(childrenPathPart);
361
- }
362
- }
363
-
364
- public recordChange(op: ISequencedDocumentMessage): void {
365
- this.invalidate(op.sequenceNumber);
366
- }
367
-
368
- public invalidate(sequenceNumber: number): void {
369
- if (sequenceNumber > this._changeSequenceNumber) {
370
- this._changeSequenceNumber = sequenceNumber;
371
- }
372
- }
373
-
374
- /**
375
- * True if a change has been recorded with sequence number exceeding
376
- * the latest successfully acked summary reference sequence number.
377
- * False implies that the previous summary can be reused.
378
- */
379
- protected hasChanged(): boolean {
380
- return this._changeSequenceNumber > this.referenceSequenceNumber;
381
- }
382
-
383
- public get latestSummary(): Readonly<SummaryNode> | undefined {
384
- return this._latestSummary;
385
- }
386
-
387
- private readonly canReuseHandle: boolean;
388
-
389
- /**
390
- * Do not call constructor directly.
391
- * Use createRootSummarizerNode to create root node, or createChild to create child nodes.
392
- */
393
- public constructor(
394
- protected readonly defaultLogger: ITelemetryLogger,
395
- private readonly summarizeInternalFn: SummarizeInternalFn,
396
- config: ISummarizerNodeConfig,
397
- private _changeSequenceNumber: number,
398
- /** Undefined means created without summary */
399
- private _latestSummary?: SummaryNode,
400
- private readonly initialSummary?: IInitialSummary,
401
- protected wipSummaryLogger?: ITelemetryLogger,
402
- ) {
403
- this.canReuseHandle = config.canReuseHandle ?? true;
404
- }
405
-
406
- public createChild(
407
- /** Summarize function */
408
- summarizeInternalFn: SummarizeInternalFn,
409
- /** Initial id or path part of this node */
410
- id: string,
411
- /**
412
- * Information needed to create the node.
413
- * If it is from a base summary, it will assert that a summary has been seen.
414
- * Attach information if it is created from an attach op.
415
- */
416
- createParam: CreateChildSummarizerNodeParam,
417
- config: ISummarizerNodeConfig = {},
418
- ): ISummarizerNode {
419
- assert(!this.children.has(id), 0x1ab /* "Create SummarizerNode child already exists" */);
420
-
421
- const createDetails: ICreateChildDetails = this.getCreateDetailsForChild(id, createParam);
422
- const child = new SummarizerNode(
423
- this.defaultLogger,
424
- summarizeInternalFn,
425
- config,
426
- createDetails.changeSequenceNumber,
427
- createDetails.latestSummary,
428
- createDetails.initialSummary,
429
- this.wipSummaryLogger,
430
- );
431
-
432
- // There may be additional state that has to be updated in this child. For example, if a summary is being
433
- // tracked, the child's summary tracking state needs to be updated too. Same goes for pendingSummaries we might
434
- // have outstanding on the parent in case we realize nodes in between Summary Op and Summary Ack.
435
- this.maybeUpdateChildState(child);
436
-
437
- this.children.set(id, child);
438
- return child;
439
- }
440
-
441
- public getChild(id: string): ISummarizerNode | undefined {
442
- return this.children.get(id);
443
- }
444
-
445
- /**
446
- * Returns the details needed to create a child node.
447
- * @param id - Initial id or path part of the child node.
448
- * @param createParam - Information needed to create the node.
449
- * @returns the details needed to create the child node.
450
- */
451
- protected getCreateDetailsForChild(id: string, createParam: CreateChildSummarizerNodeParam): ICreateChildDetails {
452
- let initialSummary: IInitialSummary | undefined;
453
- let latestSummary: SummaryNode | undefined;
454
- let changeSequenceNumber: number;
455
-
456
- const parentLatestSummary = this._latestSummary;
457
- switch (createParam.type) {
458
- case CreateSummarizerNodeSource.FromAttach: {
459
- if (
460
- parentLatestSummary !== undefined
461
- && createParam.sequenceNumber <= parentLatestSummary.referenceSequenceNumber
462
- ) {
463
- // Prioritize latest summary if it was after this node was attached.
464
- latestSummary = parentLatestSummary.createForChild(id);
465
- } else {
466
- const summary = convertToSummaryTree(createParam.snapshot) as ISummaryTreeWithStats;
467
- initialSummary = {
468
- sequenceNumber: createParam.sequenceNumber,
469
- id,
470
- summary,
471
- };
472
- }
473
- changeSequenceNumber = createParam.sequenceNumber;
474
- break;
475
- }
476
- case CreateSummarizerNodeSource.FromSummary: {
477
- if (this.initialSummary === undefined) {
478
- assert(
479
- !!parentLatestSummary,
480
- 0x1ac /* "Cannot create child from summary if parent does not have latest summary" */);
481
- }
482
- // fallthrough to local
483
- }
484
- case CreateSummarizerNodeSource.Local: {
485
- const parentInitialSummary = this.initialSummary;
486
- if (parentInitialSummary !== undefined) {
487
- let childSummary: SummaryObject | undefined;
488
- if (parentInitialSummary.summary !== undefined) {
489
- const { childrenTree } = parseSummaryTreeForSubtrees(parentInitialSummary.summary.summary);
490
- assert(
491
- childrenTree.type === SummaryType.Tree,
492
- 0x1d6 /* "Parent summary object is not a tree" */,
493
- );
494
- childSummary = childrenTree.tree[id];
495
- }
496
- if (createParam.type === CreateSummarizerNodeSource.FromSummary) {
497
- // Locally created would not have differential subtree.
498
- assert(!!childSummary, 0x1ad /* "Missing child summary tree" */);
499
- }
500
- let childSummaryWithStats: ISummaryTreeWithStats | undefined;
501
- if (childSummary !== undefined) {
502
- assert(
503
- childSummary.type === SummaryType.Tree,
504
- 0x1ae /* "Child summary object is not a tree" */,
505
- );
506
- childSummaryWithStats = {
507
- summary: childSummary,
508
- stats: calculateStats(childSummary),
509
- };
510
- }
511
- initialSummary = {
512
- sequenceNumber: parentInitialSummary.sequenceNumber,
513
- id,
514
- summary: childSummaryWithStats,
515
- };
516
- }
517
- latestSummary = parentLatestSummary?.createForChild(id);
518
- changeSequenceNumber = parentLatestSummary?.referenceSequenceNumber ?? -1;
519
- break;
520
- }
521
- default: {
522
- const type = (createParam as unknown as CreateChildSummarizerNodeParam).type;
523
- unreachableCase(createParam, `Unexpected CreateSummarizerNodeSource: ${type}`);
524
- }
525
- }
526
-
527
- return {
528
- initialSummary,
529
- latestSummary,
530
- changeSequenceNumber,
531
- };
532
- }
533
-
534
- /**
535
- * Updates the state of the child if required. For example, if a summary is currently being tracked, the child's
536
- * summary tracking state needs to be updated too.
537
- * Also, in case a child node gets realized in between Summary Op and Summary Ack, let's initialize the child's
538
- * pending summary as well.
539
- * @param child - The child node whose state is to be updated.
540
- */
541
- protected maybeUpdateChildState(child: SummarizerNode) {
542
- // If we are tracking a summary, this child was created after the tracking started. So, we need to update the
543
- // child's tracking state as well.
544
- if (this.isTrackingInProgress()) {
545
- child.wipReferenceSequenceNumber = this.wipReferenceSequenceNumber;
546
- }
547
- // In case we have pending summaries on the parent, let's initialize it on the child.
548
- if (child._latestSummary !== undefined) {
549
- for (const [key, value] of this.pendingSummaries.entries()) {
550
- const newLatestSummaryNode = new SummaryNode({
551
- referenceSequenceNumber: value.referenceSequenceNumber,
552
- basePath: child._latestSummary.basePath,
553
- localPath: child._latestSummary.localPath,
554
- });
555
-
556
- child.addPendingSummary(key, newLatestSummaryNode);
557
- }
558
- }
559
- }
560
-
561
- protected addPendingSummary(key: string, summary: SummaryNode) {
562
- this.pendingSummaries.set(key, summary);
563
- }
564
- /**
565
- * Tells whether summary tracking is in progress. True if "startSummary" API is called before summarize.
566
- */
567
- protected isTrackingInProgress(): boolean {
568
- return this.wipReferenceSequenceNumber !== undefined;
569
- }
570
- }
571
-
572
- /**
573
- * Creates a root summarizer node.
574
- * @param logger - Logger to use within SummarizerNode
575
- * @param summarizeInternalFn - Function to generate summary
576
- * @param changeSequenceNumber - Sequence number of latest change to new node/subtree
577
- * @param referenceSequenceNumber - Reference sequence number of last acked summary,
578
- * or undefined if not loaded from summary
579
- * @param config - Configure behavior of summarizer node
580
- */
581
- export const createRootSummarizerNode = (
582
- logger: ITelemetryLogger,
583
- summarizeInternalFn: SummarizeInternalFn,
584
- changeSequenceNumber: number,
585
- referenceSequenceNumber: number | undefined,
586
- config: ISummarizerNodeConfig = {},
587
- ): IRootSummarizerNode => new SummarizerNode(
588
- logger,
589
- summarizeInternalFn,
590
- config,
591
- changeSequenceNumber,
592
- referenceSequenceNumber === undefined ? undefined : SummaryNode.createForRoot(referenceSequenceNumber),
593
- );