@fluidframework/id-compressor 2.0.0-dev.7.4.0.221926

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