@fluidframework/id-compressor 2.0.0-dev-rc.1.0.0.224419

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 (157) hide show
  1. package/.eslintrc.js +27 -0
  2. package/.mocharc.js +12 -0
  3. package/CHANGELOG.md +7 -0
  4. package/LICENSE +21 -0
  5. package/README.md +170 -0
  6. package/api-extractor-lint.json +4 -0
  7. package/api-extractor.json +4 -0
  8. package/api-report/id-compressor.api.md +139 -0
  9. package/dist/appendOnlySortedMap.cjs +318 -0
  10. package/dist/appendOnlySortedMap.cjs.map +1 -0
  11. package/dist/appendOnlySortedMap.d.ts +124 -0
  12. package/dist/appendOnlySortedMap.d.ts.map +1 -0
  13. package/dist/finalSpace.cjs +62 -0
  14. package/dist/finalSpace.cjs.map +1 -0
  15. package/dist/finalSpace.d.ts +29 -0
  16. package/dist/finalSpace.d.ts.map +1 -0
  17. package/dist/id-compressor-alpha.d.ts +320 -0
  18. package/dist/id-compressor-beta.d.ts +172 -0
  19. package/dist/id-compressor-public.d.ts +172 -0
  20. package/dist/id-compressor-untrimmed.d.ts +333 -0
  21. package/dist/idCompressor.cjs +502 -0
  22. package/dist/idCompressor.cjs.map +1 -0
  23. package/dist/idCompressor.d.ts +84 -0
  24. package/dist/idCompressor.d.ts.map +1 -0
  25. package/dist/identifiers.cjs +15 -0
  26. package/dist/identifiers.cjs.map +1 -0
  27. package/dist/identifiers.d.ts +32 -0
  28. package/dist/identifiers.d.ts.map +1 -0
  29. package/dist/index.cjs +20 -0
  30. package/dist/index.cjs.map +1 -0
  31. package/dist/index.d.ts +11 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/packageVersion.cjs +12 -0
  34. package/dist/packageVersion.cjs.map +1 -0
  35. package/dist/packageVersion.d.ts +9 -0
  36. package/dist/packageVersion.d.ts.map +1 -0
  37. package/dist/persistanceUtilities.cjs +43 -0
  38. package/dist/persistanceUtilities.cjs.map +1 -0
  39. package/dist/persistanceUtilities.d.ts +22 -0
  40. package/dist/persistanceUtilities.d.ts.map +1 -0
  41. package/dist/sessionSpaceNormalizer.cjs +80 -0
  42. package/dist/sessionSpaceNormalizer.cjs.map +1 -0
  43. package/dist/sessionSpaceNormalizer.d.ts +46 -0
  44. package/dist/sessionSpaceNormalizer.d.ts.map +1 -0
  45. package/dist/sessions.cjs +305 -0
  46. package/dist/sessions.cjs.map +1 -0
  47. package/dist/sessions.d.ts +115 -0
  48. package/dist/sessions.d.ts.map +1 -0
  49. package/dist/tsdoc-metadata.json +11 -0
  50. package/dist/types/idCompressor.cjs +7 -0
  51. package/dist/types/idCompressor.cjs.map +1 -0
  52. package/dist/types/idCompressor.d.ts +139 -0
  53. package/dist/types/idCompressor.d.ts.map +1 -0
  54. package/dist/types/identifiers.cjs +7 -0
  55. package/dist/types/identifiers.cjs.map +1 -0
  56. package/dist/types/identifiers.d.ts +40 -0
  57. package/dist/types/identifiers.d.ts.map +1 -0
  58. package/dist/types/index.cjs +7 -0
  59. package/dist/types/index.cjs.map +1 -0
  60. package/dist/types/index.d.ts +8 -0
  61. package/dist/types/index.d.ts.map +1 -0
  62. package/dist/types/persisted-types/0.0.1.cjs +7 -0
  63. package/dist/types/persisted-types/0.0.1.cjs.map +1 -0
  64. package/dist/types/persisted-types/0.0.1.d.ts +52 -0
  65. package/dist/types/persisted-types/0.0.1.d.ts.map +1 -0
  66. package/dist/types/persisted-types/index.cjs +7 -0
  67. package/dist/types/persisted-types/index.cjs.map +1 -0
  68. package/dist/types/persisted-types/index.d.ts +6 -0
  69. package/dist/types/persisted-types/index.d.ts.map +1 -0
  70. package/dist/utilities.cjs +170 -0
  71. package/dist/utilities.cjs.map +1 -0
  72. package/dist/utilities.d.ts +53 -0
  73. package/dist/utilities.d.ts.map +1 -0
  74. package/lib/appendOnlySortedMap.d.mts +124 -0
  75. package/lib/appendOnlySortedMap.d.mts.map +1 -0
  76. package/lib/appendOnlySortedMap.mjs +312 -0
  77. package/lib/appendOnlySortedMap.mjs.map +1 -0
  78. package/lib/finalSpace.d.mts +29 -0
  79. package/lib/finalSpace.d.mts.map +1 -0
  80. package/lib/finalSpace.mjs +58 -0
  81. package/lib/finalSpace.mjs.map +1 -0
  82. package/lib/id-compressor-alpha.d.mts +320 -0
  83. package/lib/id-compressor-beta.d.mts +172 -0
  84. package/lib/id-compressor-public.d.mts +172 -0
  85. package/lib/id-compressor-untrimmed.d.mts +333 -0
  86. package/lib/idCompressor.d.mts +84 -0
  87. package/lib/idCompressor.d.mts.map +1 -0
  88. package/lib/idCompressor.mjs +496 -0
  89. package/lib/idCompressor.mjs.map +1 -0
  90. package/lib/identifiers.d.mts +32 -0
  91. package/lib/identifiers.d.mts.map +1 -0
  92. package/lib/identifiers.mjs +11 -0
  93. package/lib/identifiers.mjs.map +1 -0
  94. package/lib/index.d.mts +8 -0
  95. package/lib/index.d.mts.map +1 -0
  96. package/lib/index.mjs +7 -0
  97. package/lib/index.mjs.map +1 -0
  98. package/lib/packageVersion.d.mts +9 -0
  99. package/lib/packageVersion.d.mts.map +1 -0
  100. package/lib/packageVersion.mjs +9 -0
  101. package/lib/packageVersion.mjs.map +1 -0
  102. package/lib/persistanceUtilities.d.mts +22 -0
  103. package/lib/persistanceUtilities.d.mts.map +1 -0
  104. package/lib/persistanceUtilities.mjs +34 -0
  105. package/lib/persistanceUtilities.mjs.map +1 -0
  106. package/lib/sessionSpaceNormalizer.d.mts +46 -0
  107. package/lib/sessionSpaceNormalizer.d.mts.map +1 -0
  108. package/lib/sessionSpaceNormalizer.mjs +76 -0
  109. package/lib/sessionSpaceNormalizer.mjs.map +1 -0
  110. package/lib/sessions.d.mts +115 -0
  111. package/lib/sessions.d.mts.map +1 -0
  112. package/lib/sessions.mjs +290 -0
  113. package/lib/sessions.mjs.map +1 -0
  114. package/lib/types/idCompressor.d.mts +139 -0
  115. package/lib/types/idCompressor.d.mts.map +1 -0
  116. package/lib/types/idCompressor.mjs +6 -0
  117. package/lib/types/idCompressor.mjs.map +1 -0
  118. package/lib/types/identifiers.d.mts +40 -0
  119. package/lib/types/identifiers.d.mts.map +1 -0
  120. package/lib/types/identifiers.mjs +6 -0
  121. package/lib/types/identifiers.mjs.map +1 -0
  122. package/lib/types/index.d.mts +8 -0
  123. package/lib/types/index.d.mts.map +1 -0
  124. package/lib/types/index.mjs +6 -0
  125. package/lib/types/index.mjs.map +1 -0
  126. package/lib/types/persisted-types/0.0.1.d.mts +52 -0
  127. package/lib/types/persisted-types/0.0.1.d.mts.map +1 -0
  128. package/lib/types/persisted-types/0.0.1.mjs +6 -0
  129. package/lib/types/persisted-types/0.0.1.mjs.map +1 -0
  130. package/lib/types/persisted-types/index.d.mts +6 -0
  131. package/lib/types/persisted-types/index.d.mts.map +1 -0
  132. package/lib/types/persisted-types/index.mjs +6 -0
  133. package/lib/types/persisted-types/index.mjs.map +1 -0
  134. package/lib/utilities.d.mts +53 -0
  135. package/lib/utilities.d.mts.map +1 -0
  136. package/lib/utilities.mjs +151 -0
  137. package/lib/utilities.mjs.map +1 -0
  138. package/package.json +114 -0
  139. package/prettier.config.cjs +8 -0
  140. package/src/appendOnlySortedMap.ts +366 -0
  141. package/src/finalSpace.ts +67 -0
  142. package/src/idCompressor.ts +685 -0
  143. package/src/identifiers.ts +42 -0
  144. package/src/index.ts +23 -0
  145. package/src/packageVersion.ts +9 -0
  146. package/src/persistanceUtilities.ts +58 -0
  147. package/src/sessionSpaceNormalizer.ts +83 -0
  148. package/src/sessions.ts +405 -0
  149. package/src/types/idCompressor.ts +155 -0
  150. package/src/types/identifiers.ts +39 -0
  151. package/src/types/index.ts +15 -0
  152. package/src/types/persisted-types/0.0.1.ts +58 -0
  153. package/src/types/persisted-types/README.md +3 -0
  154. package/src/types/persisted-types/index.ts +11 -0
  155. package/src/utilities.ts +191 -0
  156. package/tsc-multi.test.json +4 -0
  157. package/tsconfig.json +12 -0
@@ -0,0 +1,685 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { assert } from "@fluidframework/core-utils";
7
+ import { bufferToString, stringToBuffer } from "@fluid-internal/client-utils";
8
+ import { ITelemetryBaseLogger } from "@fluidframework/core-interfaces";
9
+ import { ITelemetryLoggerExt, createChildLogger } from "@fluidframework/telemetry-utils";
10
+ import {
11
+ IdCreationRange,
12
+ IIdCompressor,
13
+ IIdCompressorCore,
14
+ OpSpaceCompressedId,
15
+ SerializedIdCompressor,
16
+ SerializedIdCompressorWithNoSession,
17
+ SerializedIdCompressorWithOngoingSession,
18
+ SessionId,
19
+ SessionSpaceCompressedId,
20
+ StableId,
21
+ } from "./types";
22
+ import { FinalCompressedId, isFinalId, LocalCompressedId, NumericUuid } from "./identifiers";
23
+ import {
24
+ createSessionId,
25
+ localIdFromGenCount,
26
+ genCountFromLocalId,
27
+ numericUuidFromStableId,
28
+ offsetNumericUuid,
29
+ stableIdFromNumericUuid,
30
+ subtractNumericUuids,
31
+ } from "./utilities";
32
+ import {
33
+ Index,
34
+ readBoolean,
35
+ readNumber,
36
+ readNumericUuid,
37
+ writeBoolean,
38
+ writeNumber,
39
+ writeNumericUuid,
40
+ } from "./persistanceUtilities";
41
+ import {
42
+ getAlignedLocal,
43
+ getAlignedFinal,
44
+ IdCluster,
45
+ lastFinalizedLocal,
46
+ Session,
47
+ Sessions,
48
+ } from "./sessions";
49
+ import { SessionSpaceNormalizer } from "./sessionSpaceNormalizer";
50
+ import { FinalSpace } from "./finalSpace";
51
+
52
+ /**
53
+ * The version of IdCompressor that is currently persisted.
54
+ * This should not be changed without careful consideration to compatibility.
55
+ */
56
+ const currentWrittenVersion = 1;
57
+
58
+ /**
59
+ * See {@link IIdCompressor} and {@link IIdCompressorCore}
60
+ *
61
+ * @alpha
62
+ *
63
+ * @deprecated Not intended for public use and will be removed from the public API in the future.
64
+ * Use `createIdCompressor` instead.
65
+ */
66
+ export class IdCompressor implements IIdCompressor, IIdCompressorCore {
67
+ /**
68
+ * Max allowed initial cluster size.
69
+ */
70
+ public static readonly maxClusterSize = 2 ** 20;
71
+
72
+ // #region Local state
73
+
74
+ public readonly localSessionId: SessionId;
75
+ private readonly localSession: Session;
76
+ private readonly normalizer = new SessionSpaceNormalizer();
77
+ // The number of IDs generated by the local session
78
+ private localGenCount = 0;
79
+
80
+ // #endregion
81
+
82
+ // #region Final state
83
+
84
+ // The gen count to be annotated on the range returned by the next call to `takeNextCreationRange`.
85
+ // This is updated to be equal to `generatedIdCount` + 1 each time it is called.
86
+ private nextRangeBaseGenCount = 1;
87
+ private readonly sessions = new Sessions();
88
+ private readonly finalSpace = new FinalSpace();
89
+
90
+ // #endregion
91
+
92
+ // #region Ephemeral state
93
+
94
+ /**
95
+ * Roughly equates to a minimum of 1M sessions before we start allocating 64 bit IDs.
96
+ * Eventually, this can be adjusted dynamically to have cluster reservation policies that
97
+ * optimize the number of eager finals.
98
+ * It is not readonly as it is accessed by tests for clear-box testing.
99
+ */
100
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly
101
+ private nextRequestedClusterSize: number = 512;
102
+ // The number of local IDs generated since the last telemetry was sent.
103
+ private telemetryLocalIdCount = 0;
104
+ // The number of eager final IDs generated since the last telemetry was sent.
105
+ private telemetryEagerFinalIdCount = 0;
106
+
107
+ // #endregion
108
+
109
+ private constructor(
110
+ localSessionIdOrDeserialized: SessionId | Sessions,
111
+ private readonly logger?: ITelemetryLoggerExt,
112
+ ) {
113
+ if (typeof localSessionIdOrDeserialized === "string") {
114
+ this.localSessionId = localSessionIdOrDeserialized;
115
+ this.localSession = this.sessions.getOrCreate(localSessionIdOrDeserialized);
116
+ } else {
117
+ // Deserialize case
118
+ this.sessions = localSessionIdOrDeserialized;
119
+ // As policy, the first session is always the local session. Preserve this invariant
120
+ // during deserialization.
121
+ const firstSession = localSessionIdOrDeserialized.sessions().next();
122
+ assert(!firstSession.done, 0x754 /* First session must be present. */);
123
+ this.localSession = firstSession.value;
124
+ this.localSessionId = stableIdFromNumericUuid(
125
+ this.localSession.sessionUuid,
126
+ ) as SessionId;
127
+ }
128
+ }
129
+
130
+ /**
131
+ * @deprecated Use `createIdCompressor` instead.
132
+ */
133
+ public static create(logger?: ITelemetryBaseLogger): IIdCompressor & IIdCompressorCore;
134
+ /**
135
+ * @deprecated Use `createIdCompressor` instead.
136
+ */
137
+ public static create(
138
+ sessionId: SessionId,
139
+ logger?: ITelemetryBaseLogger,
140
+ ): IIdCompressor & IIdCompressorCore;
141
+ public static create(
142
+ sessionIdOrLogger?: SessionId | ITelemetryBaseLogger,
143
+ loggerOrUndefined?: ITelemetryBaseLogger,
144
+ ): IIdCompressor & IIdCompressorCore {
145
+ let localSessionId: SessionId;
146
+ let logger: ITelemetryBaseLogger | undefined;
147
+ if (sessionIdOrLogger === undefined) {
148
+ localSessionId = createSessionId();
149
+ } else {
150
+ if (typeof sessionIdOrLogger === "string") {
151
+ localSessionId = sessionIdOrLogger;
152
+ logger = loggerOrUndefined;
153
+ } else {
154
+ localSessionId = createSessionId();
155
+ logger = loggerOrUndefined;
156
+ }
157
+ }
158
+ const compressor = new IdCompressor(
159
+ localSessionId,
160
+ logger === undefined ? undefined : createChildLogger({ logger }),
161
+ );
162
+ return compressor;
163
+ }
164
+
165
+ public generateCompressedId(): SessionSpaceCompressedId {
166
+ this.localGenCount++;
167
+ const lastCluster = this.localSession.getLastCluster();
168
+ if (lastCluster === undefined) {
169
+ this.telemetryLocalIdCount++;
170
+ return this.generateNextLocalId();
171
+ }
172
+
173
+ // If there exists a cluster of final IDs already claimed by the local session that still has room in it,
174
+ // it is known prior to range sequencing what a local ID's corresponding final ID will be.
175
+ // In this case, it is safe to return the final ID immediately. This is guaranteed to be safe because
176
+ // any op that the local session sends that contains one of those final IDs are guaranteed to arrive to
177
+ // collaborators *after* the one containing the creation range.
178
+ const clusterOffset = this.localGenCount - genCountFromLocalId(lastCluster.baseLocalId);
179
+ if (lastCluster.capacity > clusterOffset) {
180
+ this.telemetryEagerFinalIdCount++;
181
+ // Space in the cluster: eager final
182
+ return ((lastCluster.baseFinalId as number) +
183
+ clusterOffset) as SessionSpaceCompressedId;
184
+ }
185
+ // No space in the cluster, return next local
186
+ this.telemetryLocalIdCount++;
187
+ return this.generateNextLocalId();
188
+ }
189
+
190
+ private generateNextLocalId(): LocalCompressedId {
191
+ // Must tell the normalizer that we generated a local ID
192
+ this.normalizer.addLocalRange(this.localGenCount, 1);
193
+ return localIdFromGenCount(this.localGenCount);
194
+ }
195
+
196
+ public takeNextCreationRange(): IdCreationRange {
197
+ const count = this.localGenCount - (this.nextRangeBaseGenCount - 1);
198
+ if (count === 0) {
199
+ return {
200
+ sessionId: this.localSessionId,
201
+ };
202
+ }
203
+ const range: IdCreationRange = {
204
+ sessionId: this.localSessionId,
205
+ ids: {
206
+ firstGenCount: this.nextRangeBaseGenCount,
207
+ count,
208
+ requestedClusterSize: this.nextRequestedClusterSize,
209
+ },
210
+ };
211
+ this.nextRangeBaseGenCount = this.localGenCount + 1;
212
+ IdCompressor.assertValidRange(range);
213
+ return range;
214
+ }
215
+
216
+ private static assertValidRange(range: IdCreationRange): void {
217
+ if (range.ids === undefined) {
218
+ return;
219
+ }
220
+ const { count, requestedClusterSize } = range.ids;
221
+ assert(count > 0, 0x755 /* Malformed ID Range. */);
222
+ assert(requestedClusterSize > 0, 0x876 /* Clusters must have a positive capacity. */);
223
+ assert(
224
+ requestedClusterSize <= IdCompressor.maxClusterSize,
225
+ 0x877 /* Clusters must not exceed max cluster size. */,
226
+ );
227
+ }
228
+
229
+ public finalizeCreationRange(range: IdCreationRange): void {
230
+ // Check if the range has IDs
231
+ if (range.ids === undefined) {
232
+ return;
233
+ }
234
+
235
+ IdCompressor.assertValidRange(range);
236
+ const { sessionId, ids } = range;
237
+ const { count, firstGenCount, requestedClusterSize } = ids;
238
+ const session = this.sessions.getOrCreate(sessionId);
239
+ const isLocal = session === this.localSession;
240
+ const rangeBaseLocal = localIdFromGenCount(firstGenCount);
241
+ let lastCluster = session.getLastCluster();
242
+ if (lastCluster === undefined) {
243
+ // This is the first cluster in the session space
244
+ if (rangeBaseLocal !== -1) {
245
+ throw new Error("Ranges finalized out of order.");
246
+ }
247
+ lastCluster = this.addEmptyCluster(session, requestedClusterSize + count);
248
+ if (isLocal) {
249
+ this.logger?.sendTelemetryEvent({
250
+ eventName: "RuntimeIdCompressor:FirstCluster",
251
+ sessionId: this.localSessionId,
252
+ });
253
+ }
254
+ }
255
+
256
+ const remainingCapacity = lastCluster.capacity - lastCluster.count;
257
+ if (lastCluster.baseLocalId - lastCluster.count !== rangeBaseLocal) {
258
+ throw new Error("Ranges finalized out of order.");
259
+ }
260
+
261
+ if (remainingCapacity >= count) {
262
+ // The current range fits in the existing cluster
263
+ lastCluster.count += count;
264
+ } else {
265
+ const overflow = count - remainingCapacity;
266
+ const newClaimedFinalCount = overflow + requestedClusterSize;
267
+ if (lastCluster === this.finalSpace.getLastCluster()) {
268
+ // The last cluster in the sessions chain is the last cluster globally, so it can be expanded.
269
+ lastCluster.capacity += newClaimedFinalCount;
270
+ lastCluster.count += count;
271
+ assert(
272
+ !this.sessions.clusterCollides(lastCluster),
273
+ 0x756 /* Cluster collision detected. */,
274
+ );
275
+ if (isLocal) {
276
+ this.logger?.sendTelemetryEvent({
277
+ eventName: "RuntimeIdCompressor:ClusterExpansion",
278
+ sessionId: this.localSessionId,
279
+ previousCapacity: lastCluster.capacity - newClaimedFinalCount,
280
+ newCapacity: lastCluster.capacity,
281
+ overflow,
282
+ });
283
+ }
284
+ } else {
285
+ // The last cluster in the sessions chain is *not* the last cluster globally. Fill and overflow to new.
286
+ lastCluster.count = lastCluster.capacity;
287
+ const newCluster = this.addEmptyCluster(session, newClaimedFinalCount);
288
+ newCluster.count += overflow;
289
+ if (isLocal) {
290
+ this.logger?.sendTelemetryEvent({
291
+ eventName: "RuntimeIdCompressor:NewCluster",
292
+ sessionId: this.localSessionId,
293
+ });
294
+ }
295
+ }
296
+ }
297
+
298
+ if (isLocal) {
299
+ this.logger?.sendTelemetryEvent({
300
+ eventName: "RuntimeIdCompressor:IdCompressorStatus",
301
+ eagerFinalIdCount: this.telemetryEagerFinalIdCount,
302
+ localIdCount: this.telemetryLocalIdCount,
303
+ sessionId: this.localSessionId,
304
+ });
305
+ this.telemetryEagerFinalIdCount = 0;
306
+ this.telemetryLocalIdCount = 0;
307
+ }
308
+
309
+ assert(!session.isEmpty(), 0x757 /* Empty sessions should not be created. */);
310
+ }
311
+
312
+ private addEmptyCluster(session: Session, capacity: number): IdCluster {
313
+ const newCluster = session.addNewCluster(
314
+ this.finalSpace.getAllocatedIdLimit(),
315
+ capacity,
316
+ 0,
317
+ );
318
+ assert(!this.sessions.clusterCollides(newCluster), 0x758 /* Cluster collision detected. */);
319
+ this.finalSpace.addCluster(newCluster);
320
+ return newCluster;
321
+ }
322
+
323
+ public normalizeToOpSpace(id: SessionSpaceCompressedId): OpSpaceCompressedId {
324
+ if (isFinalId(id)) {
325
+ return id;
326
+ } else {
327
+ const local = id as unknown as LocalCompressedId;
328
+ if (!this.normalizer.contains(local)) {
329
+ throw new Error("Invalid ID to normalize.");
330
+ }
331
+ const finalForm = this.localSession.tryConvertToFinal(local, true);
332
+ return finalForm === undefined
333
+ ? (local as unknown as OpSpaceCompressedId)
334
+ : (finalForm as OpSpaceCompressedId);
335
+ }
336
+ }
337
+
338
+ public normalizeToSessionSpace(
339
+ id: OpSpaceCompressedId,
340
+ originSessionId: SessionId,
341
+ ): SessionSpaceCompressedId {
342
+ if (isFinalId(id)) {
343
+ const containingCluster = this.localSession.getClusterByAllocatedFinal(id);
344
+ if (containingCluster === undefined) {
345
+ // Does not exist in local cluster chain
346
+ if (id >= this.finalSpace.getFinalizedIdLimit()) {
347
+ throw new Error("Unknown op space ID.");
348
+ }
349
+ return id as unknown as SessionSpaceCompressedId;
350
+ } else {
351
+ const alignedLocal = getAlignedLocal(containingCluster, id);
352
+ if (this.normalizer.contains(alignedLocal)) {
353
+ return alignedLocal;
354
+ } else {
355
+ if (genCountFromLocalId(alignedLocal) > this.localGenCount) {
356
+ throw new Error("Unknown op space ID.");
357
+ }
358
+ return id as unknown as SessionSpaceCompressedId;
359
+ }
360
+ }
361
+ } else {
362
+ const localToNormalize = id as unknown as LocalCompressedId;
363
+ if (originSessionId === this.localSessionId) {
364
+ if (this.normalizer.contains(localToNormalize)) {
365
+ return localToNormalize;
366
+ } else {
367
+ // We never generated this local ID, so fail
368
+ throw new Error("Unknown op space ID.");
369
+ }
370
+ } else {
371
+ // LocalId from a remote session
372
+ const remoteSession = this.sessions.get(originSessionId);
373
+ if (remoteSession === undefined) {
374
+ throw new Error("No IDs have ever been finalized by the supplied session.");
375
+ }
376
+ const correspondingFinal = remoteSession.tryConvertToFinal(localToNormalize, false);
377
+ if (correspondingFinal === undefined) {
378
+ throw new Error("Unknown op space ID.");
379
+ }
380
+ return correspondingFinal as unknown as SessionSpaceCompressedId;
381
+ }
382
+ }
383
+ }
384
+
385
+ public decompress(id: SessionSpaceCompressedId): StableId {
386
+ if (isFinalId(id)) {
387
+ const containingCluster = Session.getContainingCluster(id, this.finalSpace.clusters);
388
+ if (containingCluster === undefined) {
389
+ throw new Error("Unknown ID");
390
+ }
391
+ const alignedLocal = getAlignedLocal(containingCluster, id);
392
+ const alignedGenCount = genCountFromLocalId(alignedLocal);
393
+ const lastFinalizedGenCount = genCountFromLocalId(
394
+ lastFinalizedLocal(containingCluster),
395
+ );
396
+ if (alignedGenCount > lastFinalizedGenCount) {
397
+ // should be an eager final id generated by the local session
398
+ if (containingCluster.session === this.localSession) {
399
+ assert(
400
+ !this.normalizer.contains(alignedLocal),
401
+ 0x759 /* Normalizer out of sync. */,
402
+ );
403
+ } else {
404
+ throw new Error("Unknown ID");
405
+ }
406
+ }
407
+
408
+ return stableIdFromNumericUuid(
409
+ offsetNumericUuid(containingCluster.session.sessionUuid, alignedGenCount - 1),
410
+ );
411
+ } else {
412
+ const localToDecompress = id as unknown as LocalCompressedId;
413
+ if (!this.normalizer.contains(localToDecompress)) {
414
+ throw new Error("Unknown ID");
415
+ }
416
+ return stableIdFromNumericUuid(
417
+ offsetNumericUuid(
418
+ this.localSession.sessionUuid,
419
+ genCountFromLocalId(localToDecompress) - 1,
420
+ ),
421
+ );
422
+ }
423
+ }
424
+
425
+ public recompress(uncompressed: StableId): SessionSpaceCompressedId {
426
+ const recompressed = this.tryRecompress(uncompressed);
427
+ if (recompressed === undefined) {
428
+ throw new Error("Could not recompress.");
429
+ }
430
+ return recompressed;
431
+ }
432
+
433
+ public tryRecompress(uncompressed: StableId): SessionSpaceCompressedId | undefined {
434
+ const match = this.sessions.getContainingCluster(uncompressed);
435
+ if (match === undefined) {
436
+ const numericUncompressed = numericUuidFromStableId(uncompressed);
437
+ const offset = subtractNumericUuids(numericUncompressed, this.localSession.sessionUuid);
438
+ if (offset < Number.MAX_SAFE_INTEGER) {
439
+ const genCountEquivalent = Number(offset) + 1;
440
+ const localEquivalent = localIdFromGenCount(genCountEquivalent);
441
+ if (this.normalizer.contains(localEquivalent)) {
442
+ return localEquivalent;
443
+ }
444
+ }
445
+ return undefined;
446
+ } else {
447
+ const [containingCluster, alignedLocal] = match;
448
+ if (containingCluster.session === this.localSession) {
449
+ // Local session
450
+ if (this.normalizer.contains(alignedLocal)) {
451
+ return alignedLocal;
452
+ } else {
453
+ assert(
454
+ genCountFromLocalId(alignedLocal) <= this.localGenCount,
455
+ 0x75a /* Clusters out of sync. */,
456
+ );
457
+ // Id is an eager final
458
+ return getAlignedFinal(containingCluster, alignedLocal) as
459
+ | SessionSpaceCompressedId
460
+ | undefined;
461
+ }
462
+ } else {
463
+ // Not the local session
464
+ return genCountFromLocalId(alignedLocal) >= lastFinalizedLocal(containingCluster)
465
+ ? (getAlignedFinal(containingCluster, alignedLocal) as
466
+ | SessionSpaceCompressedId
467
+ | undefined)
468
+ : undefined;
469
+ }
470
+ }
471
+ }
472
+
473
+ public serialize(withSession: true): SerializedIdCompressorWithOngoingSession;
474
+ public serialize(withSession: false): SerializedIdCompressorWithNoSession;
475
+ public serialize(hasLocalState: boolean): SerializedIdCompressor {
476
+ const { normalizer, finalSpace, sessions } = this;
477
+ const sessionIndexMap = new Map<Session, number>();
478
+ let sessionIndex = 0;
479
+ for (const session of sessions.sessions()) {
480
+ // Filter empty sessions to prevent them accumulating in the serialized state
481
+ if (!session.isEmpty() || hasLocalState) {
482
+ sessionIndexMap.set(session, sessionIndex);
483
+ sessionIndex++;
484
+ }
485
+ }
486
+ const localStateSize = hasLocalState
487
+ ? 1 + // generated ID count
488
+ 1 + // next range base genCount
489
+ 1 + // count of normalizer pairs
490
+ this.normalizer.idRanges.size * 2 // pairs
491
+ : 0;
492
+ // Layout size, in 8 byte increments
493
+ const totalSize =
494
+ 1 + // version
495
+ 1 + // hasLocalState
496
+ 1 + // session count
497
+ 1 + // cluster count
498
+ sessionIndexMap.size * 2 + // session IDs
499
+ finalSpace.clusters.length * 3 + // clusters: (sessionIndex, capacity, count)[]
500
+ localStateSize; // local state, if present
501
+
502
+ const serializedFloat = new Float64Array(totalSize);
503
+ const serializedUint = new BigUint64Array(serializedFloat.buffer);
504
+ let index = 0;
505
+ index = writeNumber(serializedFloat, index, currentWrittenVersion);
506
+ index = writeBoolean(serializedFloat, index, hasLocalState);
507
+ index = writeNumber(serializedFloat, index, sessionIndexMap.size);
508
+ index = writeNumber(serializedFloat, index, finalSpace.clusters.length);
509
+
510
+ for (const [session] of sessionIndexMap.entries()) {
511
+ index = writeNumericUuid(serializedUint, index, session.sessionUuid);
512
+ }
513
+
514
+ finalSpace.clusters.forEach((cluster) => {
515
+ index = writeNumber(
516
+ serializedFloat,
517
+ index,
518
+ sessionIndexMap.get(cluster.session) as number,
519
+ );
520
+ index = writeNumber(serializedFloat, index, cluster.capacity);
521
+ index = writeNumber(serializedFloat, index, cluster.count);
522
+ });
523
+
524
+ if (hasLocalState) {
525
+ index = writeNumber(serializedFloat, index, this.localGenCount);
526
+ index = writeNumber(serializedFloat, index, this.nextRangeBaseGenCount);
527
+ index = writeNumber(serializedFloat, index, normalizer.idRanges.size);
528
+ for (const [leadingGenCount, count] of normalizer.idRanges.entries()) {
529
+ index = writeNumber(serializedFloat, index, leadingGenCount);
530
+ index = writeNumber(serializedFloat, index, count);
531
+ }
532
+ }
533
+
534
+ assert(index === totalSize, 0x75b /* Serialized size was incorrectly calculated. */);
535
+ this.logger?.sendTelemetryEvent({
536
+ eventName: "RuntimeIdCompressor:SerializedIdCompressorSize",
537
+ size: serializedFloat.byteLength,
538
+ clusterCount: finalSpace.clusters.length,
539
+ sessionCount: sessionIndexMap.size,
540
+ });
541
+
542
+ return bufferToString(serializedFloat.buffer, "base64") as SerializedIdCompressor;
543
+ }
544
+
545
+ public static deserialize(serialized: SerializedIdCompressorWithOngoingSession): IdCompressor;
546
+ public static deserialize(
547
+ serialized: SerializedIdCompressorWithNoSession,
548
+ newSessionId: SessionId,
549
+ ): IdCompressor;
550
+ public static deserialize(
551
+ serialized: SerializedIdCompressor,
552
+ sessionId?: SessionId,
553
+ ): IdCompressor {
554
+ const buffer = stringToBuffer(serialized, "base64");
555
+ const index: Index = {
556
+ index: 0,
557
+ bufferFloat: new Float64Array(buffer),
558
+ bufferUint: new BigUint64Array(buffer),
559
+ };
560
+ const version = readNumber(index);
561
+ assert(version === currentWrittenVersion, 0x75c /* Unknown serialized version. */);
562
+ const hasLocalState = readBoolean(index);
563
+ const sessionCount = readNumber(index);
564
+ const clusterCount = readNumber(index);
565
+
566
+ // Sessions
567
+ let sessionOffset = 0;
568
+ const sessions: [NumericUuid, Session][] = [];
569
+ if (!hasLocalState) {
570
+ // If !hasLocalState, there won't be a serialized local session ID so insert one at the beginning
571
+ assert(sessionId !== undefined, 0x75d /* Local session ID is undefined. */);
572
+ const localSessionNumeric = numericUuidFromStableId(sessionId);
573
+ sessions.push([localSessionNumeric, new Session(localSessionNumeric)]);
574
+ sessionOffset = 1;
575
+ } else {
576
+ assert(
577
+ sessionId === undefined,
578
+ 0x75e /* Local state should not exist in serialized form. */,
579
+ );
580
+ }
581
+
582
+ for (let i = 0; i < sessionCount; i++) {
583
+ const numeric = readNumericUuid(index);
584
+ sessions.push([numeric, new Session(numeric)]);
585
+ }
586
+
587
+ const compressor = new IdCompressor(new Sessions(sessions));
588
+
589
+ // Clusters
590
+ let baseFinalId = 0;
591
+ for (let i = 0; i < clusterCount; i++) {
592
+ const sessionIndex = readNumber(index);
593
+ const session = sessions[sessionIndex + sessionOffset][1];
594
+ const capacity = readNumber(index);
595
+ const count = readNumber(index);
596
+ const cluster = session.addNewCluster(
597
+ baseFinalId as FinalCompressedId,
598
+ capacity,
599
+ count,
600
+ );
601
+ compressor.finalSpace.addCluster(cluster);
602
+ baseFinalId += capacity;
603
+ }
604
+
605
+ // Local state
606
+ if (hasLocalState) {
607
+ compressor.localGenCount = readNumber(index);
608
+ compressor.nextRangeBaseGenCount = readNumber(index);
609
+ const normalizerCount = readNumber(index);
610
+ for (let i = 0; i < normalizerCount; i++) {
611
+ compressor.normalizer.addLocalRange(readNumber(index), readNumber(index));
612
+ }
613
+ }
614
+
615
+ assert(
616
+ index.index === index.bufferFloat.length,
617
+ 0x75f /* Failed to read entire serialized compressor. */,
618
+ );
619
+ return compressor;
620
+ }
621
+
622
+ public equals(other: IdCompressor, includeLocalState: boolean): boolean {
623
+ if (
624
+ includeLocalState &&
625
+ (this.localSessionId !== other.localSessionId ||
626
+ !this.localSession.equals(other.localSession) ||
627
+ !this.normalizer.equals(other.normalizer) ||
628
+ this.nextRangeBaseGenCount !== other.nextRangeBaseGenCount ||
629
+ this.localGenCount !== other.localGenCount)
630
+ ) {
631
+ return false;
632
+ }
633
+ return (
634
+ this.sessions.equals(other.sessions, includeLocalState) &&
635
+ this.finalSpace.equals(other.finalSpace)
636
+ );
637
+ }
638
+ }
639
+
640
+ /**
641
+ * Create a new {@link IIdCompressor}.
642
+ * @alpha
643
+ */
644
+ export function createIdCompressor(
645
+ logger?: ITelemetryBaseLogger,
646
+ ): IIdCompressor & IIdCompressorCore;
647
+ /**
648
+ * Create a new {@link IIdCompressor}.
649
+ * @param sessionId - The seed ID for the compressor.
650
+ * @alpha
651
+ */
652
+ export function createIdCompressor(
653
+ sessionId: SessionId,
654
+ logger?: ITelemetryBaseLogger,
655
+ ): IIdCompressor & IIdCompressorCore;
656
+ export function createIdCompressor(
657
+ sessionIdOrLogger?: SessionId | ITelemetryBaseLogger,
658
+ loggerOrUndefined?: ITelemetryBaseLogger,
659
+ ): IIdCompressor & IIdCompressorCore {
660
+ return IdCompressor.create(sessionIdOrLogger as SessionId, loggerOrUndefined);
661
+ }
662
+
663
+ /**
664
+ * Deserializes the supplied state into an ID compressor.
665
+ * @alpha
666
+ */
667
+ export function deserializeIdCompressor(
668
+ serialized: SerializedIdCompressorWithOngoingSession,
669
+ ): IIdCompressor & IIdCompressorCore;
670
+ /**
671
+ * Deserializes the supplied state into an ID compressor.
672
+ * @alpha
673
+ */
674
+ export function deserializeIdCompressor(
675
+ serialized: SerializedIdCompressorWithNoSession,
676
+ newSessionId: SessionId,
677
+ ): IIdCompressor & IIdCompressorCore;
678
+ export function deserializeIdCompressor(
679
+ serialized: SerializedIdCompressor | SerializedIdCompressorWithNoSession,
680
+ sessionId?: SessionId,
681
+ ): IIdCompressor & IIdCompressorCore {
682
+ return sessionId === undefined
683
+ ? IdCompressor.deserialize(serialized as SerializedIdCompressorWithOngoingSession)
684
+ : IdCompressor.deserialize(serialized as SerializedIdCompressorWithNoSession, sessionId);
685
+ }