@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,502 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.deserializeIdCompressor = exports.createIdCompressor = exports.IdCompressor = void 0;
8
+ const core_utils_1 = require("@fluidframework/core-utils");
9
+ const client_utils_1 = require("@fluid-internal/client-utils");
10
+ const telemetry_utils_1 = require("@fluidframework/telemetry-utils");
11
+ const identifiers_1 = require("./identifiers.cjs");
12
+ const utilities_1 = require("./utilities.cjs");
13
+ const persistanceUtilities_1 = require("./persistanceUtilities.cjs");
14
+ const sessions_1 = require("./sessions.cjs");
15
+ const sessionSpaceNormalizer_1 = require("./sessionSpaceNormalizer.cjs");
16
+ const finalSpace_1 = require("./finalSpace.cjs");
17
+ /**
18
+ * The version of IdCompressor that is currently persisted.
19
+ * This should not be changed without careful consideration to compatibility.
20
+ */
21
+ const currentWrittenVersion = 1;
22
+ /**
23
+ * See {@link IIdCompressor} and {@link IIdCompressorCore}
24
+ *
25
+ * @alpha
26
+ *
27
+ * @deprecated Not intended for public use and will be removed from the public API in the future.
28
+ * Use `createIdCompressor` instead.
29
+ */
30
+ class IdCompressor {
31
+ // #endregion
32
+ constructor(localSessionIdOrDeserialized, logger) {
33
+ this.logger = logger;
34
+ this.normalizer = new sessionSpaceNormalizer_1.SessionSpaceNormalizer();
35
+ // The number of IDs generated by the local session
36
+ this.localGenCount = 0;
37
+ // #endregion
38
+ // #region Final state
39
+ // The gen count to be annotated on the range returned by the next call to `takeNextCreationRange`.
40
+ // This is updated to be equal to `generatedIdCount` + 1 each time it is called.
41
+ this.nextRangeBaseGenCount = 1;
42
+ this.sessions = new sessions_1.Sessions();
43
+ this.finalSpace = new finalSpace_1.FinalSpace();
44
+ // #endregion
45
+ // #region Ephemeral state
46
+ /**
47
+ * Roughly equates to a minimum of 1M sessions before we start allocating 64 bit IDs.
48
+ * Eventually, this can be adjusted dynamically to have cluster reservation policies that
49
+ * optimize the number of eager finals.
50
+ * It is not readonly as it is accessed by tests for clear-box testing.
51
+ */
52
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly
53
+ this.nextRequestedClusterSize = 512;
54
+ // The number of local IDs generated since the last telemetry was sent.
55
+ this.telemetryLocalIdCount = 0;
56
+ // The number of eager final IDs generated since the last telemetry was sent.
57
+ this.telemetryEagerFinalIdCount = 0;
58
+ if (typeof localSessionIdOrDeserialized === "string") {
59
+ this.localSessionId = localSessionIdOrDeserialized;
60
+ this.localSession = this.sessions.getOrCreate(localSessionIdOrDeserialized);
61
+ }
62
+ else {
63
+ // Deserialize case
64
+ this.sessions = localSessionIdOrDeserialized;
65
+ // As policy, the first session is always the local session. Preserve this invariant
66
+ // during deserialization.
67
+ const firstSession = localSessionIdOrDeserialized.sessions().next();
68
+ (0, core_utils_1.assert)(!firstSession.done, 0x754 /* First session must be present. */);
69
+ this.localSession = firstSession.value;
70
+ this.localSessionId = (0, utilities_1.stableIdFromNumericUuid)(this.localSession.sessionUuid);
71
+ }
72
+ }
73
+ static create(sessionIdOrLogger, loggerOrUndefined) {
74
+ let localSessionId;
75
+ let logger;
76
+ if (sessionIdOrLogger === undefined) {
77
+ localSessionId = (0, utilities_1.createSessionId)();
78
+ }
79
+ else {
80
+ if (typeof sessionIdOrLogger === "string") {
81
+ localSessionId = sessionIdOrLogger;
82
+ logger = loggerOrUndefined;
83
+ }
84
+ else {
85
+ localSessionId = (0, utilities_1.createSessionId)();
86
+ logger = loggerOrUndefined;
87
+ }
88
+ }
89
+ const compressor = new IdCompressor(localSessionId, logger === undefined ? undefined : (0, telemetry_utils_1.createChildLogger)({ logger }));
90
+ return compressor;
91
+ }
92
+ generateCompressedId() {
93
+ this.localGenCount++;
94
+ const lastCluster = this.localSession.getLastCluster();
95
+ if (lastCluster === undefined) {
96
+ this.telemetryLocalIdCount++;
97
+ return this.generateNextLocalId();
98
+ }
99
+ // If there exists a cluster of final IDs already claimed by the local session that still has room in it,
100
+ // it is known prior to range sequencing what a local ID's corresponding final ID will be.
101
+ // In this case, it is safe to return the final ID immediately. This is guaranteed to be safe because
102
+ // any op that the local session sends that contains one of those final IDs are guaranteed to arrive to
103
+ // collaborators *after* the one containing the creation range.
104
+ const clusterOffset = this.localGenCount - (0, utilities_1.genCountFromLocalId)(lastCluster.baseLocalId);
105
+ if (lastCluster.capacity > clusterOffset) {
106
+ this.telemetryEagerFinalIdCount++;
107
+ // Space in the cluster: eager final
108
+ return (lastCluster.baseFinalId +
109
+ clusterOffset);
110
+ }
111
+ // No space in the cluster, return next local
112
+ this.telemetryLocalIdCount++;
113
+ return this.generateNextLocalId();
114
+ }
115
+ generateNextLocalId() {
116
+ // Must tell the normalizer that we generated a local ID
117
+ this.normalizer.addLocalRange(this.localGenCount, 1);
118
+ return (0, utilities_1.localIdFromGenCount)(this.localGenCount);
119
+ }
120
+ takeNextCreationRange() {
121
+ const count = this.localGenCount - (this.nextRangeBaseGenCount - 1);
122
+ if (count === 0) {
123
+ return {
124
+ sessionId: this.localSessionId,
125
+ };
126
+ }
127
+ const range = {
128
+ sessionId: this.localSessionId,
129
+ ids: {
130
+ firstGenCount: this.nextRangeBaseGenCount,
131
+ count,
132
+ requestedClusterSize: this.nextRequestedClusterSize,
133
+ },
134
+ };
135
+ this.nextRangeBaseGenCount = this.localGenCount + 1;
136
+ IdCompressor.assertValidRange(range);
137
+ return range;
138
+ }
139
+ static assertValidRange(range) {
140
+ if (range.ids === undefined) {
141
+ return;
142
+ }
143
+ const { count, requestedClusterSize } = range.ids;
144
+ (0, core_utils_1.assert)(count > 0, 0x755 /* Malformed ID Range. */);
145
+ (0, core_utils_1.assert)(requestedClusterSize > 0, 0x876 /* Clusters must have a positive capacity. */);
146
+ (0, core_utils_1.assert)(requestedClusterSize <= IdCompressor.maxClusterSize, 0x877 /* Clusters must not exceed max cluster size. */);
147
+ }
148
+ finalizeCreationRange(range) {
149
+ // Check if the range has IDs
150
+ if (range.ids === undefined) {
151
+ return;
152
+ }
153
+ IdCompressor.assertValidRange(range);
154
+ const { sessionId, ids } = range;
155
+ const { count, firstGenCount, requestedClusterSize } = ids;
156
+ const session = this.sessions.getOrCreate(sessionId);
157
+ const isLocal = session === this.localSession;
158
+ const rangeBaseLocal = (0, utilities_1.localIdFromGenCount)(firstGenCount);
159
+ let lastCluster = session.getLastCluster();
160
+ if (lastCluster === undefined) {
161
+ // This is the first cluster in the session space
162
+ if (rangeBaseLocal !== -1) {
163
+ throw new Error("Ranges finalized out of order.");
164
+ }
165
+ lastCluster = this.addEmptyCluster(session, requestedClusterSize + count);
166
+ if (isLocal) {
167
+ this.logger?.sendTelemetryEvent({
168
+ eventName: "RuntimeIdCompressor:FirstCluster",
169
+ sessionId: this.localSessionId,
170
+ });
171
+ }
172
+ }
173
+ const remainingCapacity = lastCluster.capacity - lastCluster.count;
174
+ if (lastCluster.baseLocalId - lastCluster.count !== rangeBaseLocal) {
175
+ throw new Error("Ranges finalized out of order.");
176
+ }
177
+ if (remainingCapacity >= count) {
178
+ // The current range fits in the existing cluster
179
+ lastCluster.count += count;
180
+ }
181
+ else {
182
+ const overflow = count - remainingCapacity;
183
+ const newClaimedFinalCount = overflow + requestedClusterSize;
184
+ if (lastCluster === this.finalSpace.getLastCluster()) {
185
+ // The last cluster in the sessions chain is the last cluster globally, so it can be expanded.
186
+ lastCluster.capacity += newClaimedFinalCount;
187
+ lastCluster.count += count;
188
+ (0, core_utils_1.assert)(!this.sessions.clusterCollides(lastCluster), 0x756 /* Cluster collision detected. */);
189
+ if (isLocal) {
190
+ this.logger?.sendTelemetryEvent({
191
+ eventName: "RuntimeIdCompressor:ClusterExpansion",
192
+ sessionId: this.localSessionId,
193
+ previousCapacity: lastCluster.capacity - newClaimedFinalCount,
194
+ newCapacity: lastCluster.capacity,
195
+ overflow,
196
+ });
197
+ }
198
+ }
199
+ else {
200
+ // The last cluster in the sessions chain is *not* the last cluster globally. Fill and overflow to new.
201
+ lastCluster.count = lastCluster.capacity;
202
+ const newCluster = this.addEmptyCluster(session, newClaimedFinalCount);
203
+ newCluster.count += overflow;
204
+ if (isLocal) {
205
+ this.logger?.sendTelemetryEvent({
206
+ eventName: "RuntimeIdCompressor:NewCluster",
207
+ sessionId: this.localSessionId,
208
+ });
209
+ }
210
+ }
211
+ }
212
+ if (isLocal) {
213
+ this.logger?.sendTelemetryEvent({
214
+ eventName: "RuntimeIdCompressor:IdCompressorStatus",
215
+ eagerFinalIdCount: this.telemetryEagerFinalIdCount,
216
+ localIdCount: this.telemetryLocalIdCount,
217
+ sessionId: this.localSessionId,
218
+ });
219
+ this.telemetryEagerFinalIdCount = 0;
220
+ this.telemetryLocalIdCount = 0;
221
+ }
222
+ (0, core_utils_1.assert)(!session.isEmpty(), 0x757 /* Empty sessions should not be created. */);
223
+ }
224
+ addEmptyCluster(session, capacity) {
225
+ const newCluster = session.addNewCluster(this.finalSpace.getAllocatedIdLimit(), capacity, 0);
226
+ (0, core_utils_1.assert)(!this.sessions.clusterCollides(newCluster), 0x758 /* Cluster collision detected. */);
227
+ this.finalSpace.addCluster(newCluster);
228
+ return newCluster;
229
+ }
230
+ normalizeToOpSpace(id) {
231
+ if ((0, identifiers_1.isFinalId)(id)) {
232
+ return id;
233
+ }
234
+ else {
235
+ const local = id;
236
+ if (!this.normalizer.contains(local)) {
237
+ throw new Error("Invalid ID to normalize.");
238
+ }
239
+ const finalForm = this.localSession.tryConvertToFinal(local, true);
240
+ return finalForm === undefined
241
+ ? local
242
+ : finalForm;
243
+ }
244
+ }
245
+ normalizeToSessionSpace(id, originSessionId) {
246
+ if ((0, identifiers_1.isFinalId)(id)) {
247
+ const containingCluster = this.localSession.getClusterByAllocatedFinal(id);
248
+ if (containingCluster === undefined) {
249
+ // Does not exist in local cluster chain
250
+ if (id >= this.finalSpace.getFinalizedIdLimit()) {
251
+ throw new Error("Unknown op space ID.");
252
+ }
253
+ return id;
254
+ }
255
+ else {
256
+ const alignedLocal = (0, sessions_1.getAlignedLocal)(containingCluster, id);
257
+ if (this.normalizer.contains(alignedLocal)) {
258
+ return alignedLocal;
259
+ }
260
+ else {
261
+ if ((0, utilities_1.genCountFromLocalId)(alignedLocal) > this.localGenCount) {
262
+ throw new Error("Unknown op space ID.");
263
+ }
264
+ return id;
265
+ }
266
+ }
267
+ }
268
+ else {
269
+ const localToNormalize = id;
270
+ if (originSessionId === this.localSessionId) {
271
+ if (this.normalizer.contains(localToNormalize)) {
272
+ return localToNormalize;
273
+ }
274
+ else {
275
+ // We never generated this local ID, so fail
276
+ throw new Error("Unknown op space ID.");
277
+ }
278
+ }
279
+ else {
280
+ // LocalId from a remote session
281
+ const remoteSession = this.sessions.get(originSessionId);
282
+ if (remoteSession === undefined) {
283
+ throw new Error("No IDs have ever been finalized by the supplied session.");
284
+ }
285
+ const correspondingFinal = remoteSession.tryConvertToFinal(localToNormalize, false);
286
+ if (correspondingFinal === undefined) {
287
+ throw new Error("Unknown op space ID.");
288
+ }
289
+ return correspondingFinal;
290
+ }
291
+ }
292
+ }
293
+ decompress(id) {
294
+ if ((0, identifiers_1.isFinalId)(id)) {
295
+ const containingCluster = sessions_1.Session.getContainingCluster(id, this.finalSpace.clusters);
296
+ if (containingCluster === undefined) {
297
+ throw new Error("Unknown ID");
298
+ }
299
+ const alignedLocal = (0, sessions_1.getAlignedLocal)(containingCluster, id);
300
+ const alignedGenCount = (0, utilities_1.genCountFromLocalId)(alignedLocal);
301
+ const lastFinalizedGenCount = (0, utilities_1.genCountFromLocalId)((0, sessions_1.lastFinalizedLocal)(containingCluster));
302
+ if (alignedGenCount > lastFinalizedGenCount) {
303
+ // should be an eager final id generated by the local session
304
+ if (containingCluster.session === this.localSession) {
305
+ (0, core_utils_1.assert)(!this.normalizer.contains(alignedLocal), 0x759 /* Normalizer out of sync. */);
306
+ }
307
+ else {
308
+ throw new Error("Unknown ID");
309
+ }
310
+ }
311
+ return (0, utilities_1.stableIdFromNumericUuid)((0, utilities_1.offsetNumericUuid)(containingCluster.session.sessionUuid, alignedGenCount - 1));
312
+ }
313
+ else {
314
+ const localToDecompress = id;
315
+ if (!this.normalizer.contains(localToDecompress)) {
316
+ throw new Error("Unknown ID");
317
+ }
318
+ return (0, utilities_1.stableIdFromNumericUuid)((0, utilities_1.offsetNumericUuid)(this.localSession.sessionUuid, (0, utilities_1.genCountFromLocalId)(localToDecompress) - 1));
319
+ }
320
+ }
321
+ recompress(uncompressed) {
322
+ const recompressed = this.tryRecompress(uncompressed);
323
+ if (recompressed === undefined) {
324
+ throw new Error("Could not recompress.");
325
+ }
326
+ return recompressed;
327
+ }
328
+ tryRecompress(uncompressed) {
329
+ const match = this.sessions.getContainingCluster(uncompressed);
330
+ if (match === undefined) {
331
+ const numericUncompressed = (0, utilities_1.numericUuidFromStableId)(uncompressed);
332
+ const offset = (0, utilities_1.subtractNumericUuids)(numericUncompressed, this.localSession.sessionUuid);
333
+ if (offset < Number.MAX_SAFE_INTEGER) {
334
+ const genCountEquivalent = Number(offset) + 1;
335
+ const localEquivalent = (0, utilities_1.localIdFromGenCount)(genCountEquivalent);
336
+ if (this.normalizer.contains(localEquivalent)) {
337
+ return localEquivalent;
338
+ }
339
+ }
340
+ return undefined;
341
+ }
342
+ else {
343
+ const [containingCluster, alignedLocal] = match;
344
+ if (containingCluster.session === this.localSession) {
345
+ // Local session
346
+ if (this.normalizer.contains(alignedLocal)) {
347
+ return alignedLocal;
348
+ }
349
+ else {
350
+ (0, core_utils_1.assert)((0, utilities_1.genCountFromLocalId)(alignedLocal) <= this.localGenCount, 0x75a /* Clusters out of sync. */);
351
+ // Id is an eager final
352
+ return (0, sessions_1.getAlignedFinal)(containingCluster, alignedLocal);
353
+ }
354
+ }
355
+ else {
356
+ // Not the local session
357
+ return (0, utilities_1.genCountFromLocalId)(alignedLocal) >= (0, sessions_1.lastFinalizedLocal)(containingCluster)
358
+ ? (0, sessions_1.getAlignedFinal)(containingCluster, alignedLocal)
359
+ : undefined;
360
+ }
361
+ }
362
+ }
363
+ serialize(hasLocalState) {
364
+ const { normalizer, finalSpace, sessions } = this;
365
+ const sessionIndexMap = new Map();
366
+ let sessionIndex = 0;
367
+ for (const session of sessions.sessions()) {
368
+ // Filter empty sessions to prevent them accumulating in the serialized state
369
+ if (!session.isEmpty() || hasLocalState) {
370
+ sessionIndexMap.set(session, sessionIndex);
371
+ sessionIndex++;
372
+ }
373
+ }
374
+ const localStateSize = hasLocalState
375
+ ? 1 + // generated ID count
376
+ 1 + // next range base genCount
377
+ 1 + // count of normalizer pairs
378
+ this.normalizer.idRanges.size * 2 // pairs
379
+ : 0;
380
+ // Layout size, in 8 byte increments
381
+ const totalSize = 1 + // version
382
+ 1 + // hasLocalState
383
+ 1 + // session count
384
+ 1 + // cluster count
385
+ sessionIndexMap.size * 2 + // session IDs
386
+ finalSpace.clusters.length * 3 + // clusters: (sessionIndex, capacity, count)[]
387
+ localStateSize; // local state, if present
388
+ const serializedFloat = new Float64Array(totalSize);
389
+ const serializedUint = new BigUint64Array(serializedFloat.buffer);
390
+ let index = 0;
391
+ index = (0, persistanceUtilities_1.writeNumber)(serializedFloat, index, currentWrittenVersion);
392
+ index = (0, persistanceUtilities_1.writeBoolean)(serializedFloat, index, hasLocalState);
393
+ index = (0, persistanceUtilities_1.writeNumber)(serializedFloat, index, sessionIndexMap.size);
394
+ index = (0, persistanceUtilities_1.writeNumber)(serializedFloat, index, finalSpace.clusters.length);
395
+ for (const [session] of sessionIndexMap.entries()) {
396
+ index = (0, persistanceUtilities_1.writeNumericUuid)(serializedUint, index, session.sessionUuid);
397
+ }
398
+ finalSpace.clusters.forEach((cluster) => {
399
+ index = (0, persistanceUtilities_1.writeNumber)(serializedFloat, index, sessionIndexMap.get(cluster.session));
400
+ index = (0, persistanceUtilities_1.writeNumber)(serializedFloat, index, cluster.capacity);
401
+ index = (0, persistanceUtilities_1.writeNumber)(serializedFloat, index, cluster.count);
402
+ });
403
+ if (hasLocalState) {
404
+ index = (0, persistanceUtilities_1.writeNumber)(serializedFloat, index, this.localGenCount);
405
+ index = (0, persistanceUtilities_1.writeNumber)(serializedFloat, index, this.nextRangeBaseGenCount);
406
+ index = (0, persistanceUtilities_1.writeNumber)(serializedFloat, index, normalizer.idRanges.size);
407
+ for (const [leadingGenCount, count] of normalizer.idRanges.entries()) {
408
+ index = (0, persistanceUtilities_1.writeNumber)(serializedFloat, index, leadingGenCount);
409
+ index = (0, persistanceUtilities_1.writeNumber)(serializedFloat, index, count);
410
+ }
411
+ }
412
+ (0, core_utils_1.assert)(index === totalSize, 0x75b /* Serialized size was incorrectly calculated. */);
413
+ this.logger?.sendTelemetryEvent({
414
+ eventName: "RuntimeIdCompressor:SerializedIdCompressorSize",
415
+ size: serializedFloat.byteLength,
416
+ clusterCount: finalSpace.clusters.length,
417
+ sessionCount: sessionIndexMap.size,
418
+ });
419
+ return (0, client_utils_1.bufferToString)(serializedFloat.buffer, "base64");
420
+ }
421
+ static deserialize(serialized, sessionId) {
422
+ const buffer = (0, client_utils_1.stringToBuffer)(serialized, "base64");
423
+ const index = {
424
+ index: 0,
425
+ bufferFloat: new Float64Array(buffer),
426
+ bufferUint: new BigUint64Array(buffer),
427
+ };
428
+ const version = (0, persistanceUtilities_1.readNumber)(index);
429
+ (0, core_utils_1.assert)(version === currentWrittenVersion, 0x75c /* Unknown serialized version. */);
430
+ const hasLocalState = (0, persistanceUtilities_1.readBoolean)(index);
431
+ const sessionCount = (0, persistanceUtilities_1.readNumber)(index);
432
+ const clusterCount = (0, persistanceUtilities_1.readNumber)(index);
433
+ // Sessions
434
+ let sessionOffset = 0;
435
+ const sessions = [];
436
+ if (!hasLocalState) {
437
+ // If !hasLocalState, there won't be a serialized local session ID so insert one at the beginning
438
+ (0, core_utils_1.assert)(sessionId !== undefined, 0x75d /* Local session ID is undefined. */);
439
+ const localSessionNumeric = (0, utilities_1.numericUuidFromStableId)(sessionId);
440
+ sessions.push([localSessionNumeric, new sessions_1.Session(localSessionNumeric)]);
441
+ sessionOffset = 1;
442
+ }
443
+ else {
444
+ (0, core_utils_1.assert)(sessionId === undefined, 0x75e /* Local state should not exist in serialized form. */);
445
+ }
446
+ for (let i = 0; i < sessionCount; i++) {
447
+ const numeric = (0, persistanceUtilities_1.readNumericUuid)(index);
448
+ sessions.push([numeric, new sessions_1.Session(numeric)]);
449
+ }
450
+ const compressor = new IdCompressor(new sessions_1.Sessions(sessions));
451
+ // Clusters
452
+ let baseFinalId = 0;
453
+ for (let i = 0; i < clusterCount; i++) {
454
+ const sessionIndex = (0, persistanceUtilities_1.readNumber)(index);
455
+ const session = sessions[sessionIndex + sessionOffset][1];
456
+ const capacity = (0, persistanceUtilities_1.readNumber)(index);
457
+ const count = (0, persistanceUtilities_1.readNumber)(index);
458
+ const cluster = session.addNewCluster(baseFinalId, capacity, count);
459
+ compressor.finalSpace.addCluster(cluster);
460
+ baseFinalId += capacity;
461
+ }
462
+ // Local state
463
+ if (hasLocalState) {
464
+ compressor.localGenCount = (0, persistanceUtilities_1.readNumber)(index);
465
+ compressor.nextRangeBaseGenCount = (0, persistanceUtilities_1.readNumber)(index);
466
+ const normalizerCount = (0, persistanceUtilities_1.readNumber)(index);
467
+ for (let i = 0; i < normalizerCount; i++) {
468
+ compressor.normalizer.addLocalRange((0, persistanceUtilities_1.readNumber)(index), (0, persistanceUtilities_1.readNumber)(index));
469
+ }
470
+ }
471
+ (0, core_utils_1.assert)(index.index === index.bufferFloat.length, 0x75f /* Failed to read entire serialized compressor. */);
472
+ return compressor;
473
+ }
474
+ equals(other, includeLocalState) {
475
+ if (includeLocalState &&
476
+ (this.localSessionId !== other.localSessionId ||
477
+ !this.localSession.equals(other.localSession) ||
478
+ !this.normalizer.equals(other.normalizer) ||
479
+ this.nextRangeBaseGenCount !== other.nextRangeBaseGenCount ||
480
+ this.localGenCount !== other.localGenCount)) {
481
+ return false;
482
+ }
483
+ return (this.sessions.equals(other.sessions, includeLocalState) &&
484
+ this.finalSpace.equals(other.finalSpace));
485
+ }
486
+ }
487
+ exports.IdCompressor = IdCompressor;
488
+ /**
489
+ * Max allowed initial cluster size.
490
+ */
491
+ IdCompressor.maxClusterSize = 2 ** 20;
492
+ function createIdCompressor(sessionIdOrLogger, loggerOrUndefined) {
493
+ return IdCompressor.create(sessionIdOrLogger, loggerOrUndefined);
494
+ }
495
+ exports.createIdCompressor = createIdCompressor;
496
+ function deserializeIdCompressor(serialized, sessionId) {
497
+ return sessionId === undefined
498
+ ? IdCompressor.deserialize(serialized)
499
+ : IdCompressor.deserialize(serialized, sessionId);
500
+ }
501
+ exports.deserializeIdCompressor = deserializeIdCompressor;
502
+ //# sourceMappingURL=idCompressor.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idCompressor.cjs","sourceRoot":"","sources":["../src/idCompressor.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,2DAAoD;AACpD,+DAA8E;AAE9E,qEAAyF;AAazF,mDAA6F;AAC7F,+CAQqB;AACrB,qEAQgC;AAChC,6CAOoB;AACpB,yEAAkE;AAClE,iDAA0C;AAE1C;;;GAGG;AACH,MAAM,qBAAqB,GAAG,CAAC,CAAC;AAEhC;;;;;;;GAOG;AACH,MAAa,YAAY;IAyCxB,aAAa;IAEb,YACC,4BAAkD,EACjC,MAA4B;QAA5B,WAAM,GAAN,MAAM,CAAsB;QAnC7B,eAAU,GAAG,IAAI,+CAAsB,EAAE,CAAC;QAC3D,mDAAmD;QAC3C,kBAAa,GAAG,CAAC,CAAC;QAE1B,aAAa;QAEb,sBAAsB;QAEtB,mGAAmG;QACnG,gFAAgF;QACxE,0BAAqB,GAAG,CAAC,CAAC;QACjB,aAAQ,GAAG,IAAI,mBAAQ,EAAE,CAAC;QAC1B,eAAU,GAAG,IAAI,uBAAU,EAAE,CAAC;QAE/C,aAAa;QAEb,0BAA0B;QAE1B;;;;;WAKG;QACH,8DAA8D;QACtD,6BAAwB,GAAW,GAAG,CAAC;QAC/C,uEAAuE;QAC/D,0BAAqB,GAAG,CAAC,CAAC;QAClC,6EAA6E;QACrE,+BAA0B,GAAG,CAAC,CAAC;QAQtC,IAAI,OAAO,4BAA4B,KAAK,QAAQ,EAAE;YACrD,IAAI,CAAC,cAAc,GAAG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AAC;;AA3jBF,oCA4jBC;AA3jBA;;GAEG;AACoB,2BAAc,GAAG,CAAC,IAAI,EAAE,AAAV,CAAW;AA0kBjD,SAAgB,kBAAkB,CACjC,iBAAoD,EACpD,iBAAwC;IAExC,OAAO,YAAY,CAAC,MAAM,CAAC,iBAA8B,EAAE,iBAAiB,CAAC,CAAC;AAC/E,CAAC;AALD,gDAKC;AAiBD,SAAgB,uBAAuB,CACtC,UAAwE,EACxE,SAAqB;IAErB,OAAO,SAAS,KAAK,SAAS;QAC7B,CAAC,CAAC,YAAY,CAAC,WAAW,CAAC,UAAsD,CAAC;QAClF,CAAC,CAAC,YAAY,CAAC,WAAW,CAAC,UAAiD,EAAE,SAAS,CAAC,CAAC;AAC3F,CAAC;AAPD,0DAOC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"@fluidframework/core-utils\";\nimport { bufferToString, stringToBuffer } from \"@fluid-internal/client-utils\";\nimport { ITelemetryBaseLogger } from \"@fluidframework/core-interfaces\";\nimport { ITelemetryLoggerExt, createChildLogger } from \"@fluidframework/telemetry-utils\";\nimport {\n\tIdCreationRange,\n\tIIdCompressor,\n\tIIdCompressorCore,\n\tOpSpaceCompressedId,\n\tSerializedIdCompressor,\n\tSerializedIdCompressorWithNoSession,\n\tSerializedIdCompressorWithOngoingSession,\n\tSessionId,\n\tSessionSpaceCompressedId,\n\tStableId,\n} from \"./types\";\nimport { FinalCompressedId, isFinalId, LocalCompressedId, NumericUuid } from \"./identifiers\";\nimport {\n\tcreateSessionId,\n\tlocalIdFromGenCount,\n\tgenCountFromLocalId,\n\tnumericUuidFromStableId,\n\toffsetNumericUuid,\n\tstableIdFromNumericUuid,\n\tsubtractNumericUuids,\n} from \"./utilities\";\nimport {\n\tIndex,\n\treadBoolean,\n\treadNumber,\n\treadNumericUuid,\n\twriteBoolean,\n\twriteNumber,\n\twriteNumericUuid,\n} from \"./persistanceUtilities\";\nimport {\n\tgetAlignedLocal,\n\tgetAlignedFinal,\n\tIdCluster,\n\tlastFinalizedLocal,\n\tSession,\n\tSessions,\n} from \"./sessions\";\nimport { SessionSpaceNormalizer } from \"./sessionSpaceNormalizer\";\nimport { FinalSpace } from \"./finalSpace\";\n\n/**\n * The version of IdCompressor that is currently persisted.\n * This should not be changed without careful consideration to compatibility.\n */\nconst currentWrittenVersion = 1;\n\n/**\n * See {@link IIdCompressor} and {@link IIdCompressorCore}\n *\n * @alpha\n *\n * @deprecated Not intended for public use and will be removed from the public API in the future.\n * Use `createIdCompressor` instead.\n */\nexport class IdCompressor implements IIdCompressor, IIdCompressorCore {\n\t/**\n\t * Max allowed initial cluster size.\n\t */\n\tpublic static readonly maxClusterSize = 2 ** 20;\n\n\t// #region Local state\n\n\tpublic readonly localSessionId: SessionId;\n\tprivate readonly localSession: Session;\n\tprivate readonly normalizer = new SessionSpaceNormalizer();\n\t// The number of IDs generated by the local session\n\tprivate localGenCount = 0;\n\n\t// #endregion\n\n\t// #region Final state\n\n\t// The gen count to be annotated on the range returned by the next call to `takeNextCreationRange`.\n\t// This is updated to be equal to `generatedIdCount` + 1 each time it is called.\n\tprivate nextRangeBaseGenCount = 1;\n\tprivate readonly sessions = new Sessions();\n\tprivate readonly finalSpace = new FinalSpace();\n\n\t// #endregion\n\n\t// #region Ephemeral state\n\n\t/**\n\t * Roughly equates to a minimum of 1M sessions before we start allocating 64 bit IDs.\n\t * Eventually, this can be adjusted dynamically to have cluster reservation policies that\n\t * optimize the number of eager finals.\n\t * It is not readonly as it is accessed by tests for clear-box testing.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/prefer-readonly\n\tprivate nextRequestedClusterSize: number = 512;\n\t// The number of local IDs generated since the last telemetry was sent.\n\tprivate telemetryLocalIdCount = 0;\n\t// The number of eager final IDs generated since the last telemetry was sent.\n\tprivate telemetryEagerFinalIdCount = 0;\n\n\t// #endregion\n\n\tprivate constructor(\n\t\tlocalSessionIdOrDeserialized: SessionId | Sessions,\n\t\tprivate readonly logger?: ITelemetryLoggerExt,\n\t) {\n\t\tif (typeof localSessionIdOrDeserialized === \"string\") {\n\t\t\tthis.localSessionId = localSessionIdOrDeserialized;\n\t\t\tthis.localSession = this.sessions.getOrCreate(localSessionIdOrDeserialized);\n\t\t} else {\n\t\t\t// Deserialize case\n\t\t\tthis.sessions = localSessionIdOrDeserialized;\n\t\t\t// As policy, the first session is always the local session. Preserve this invariant\n\t\t\t// during deserialization.\n\t\t\tconst firstSession = localSessionIdOrDeserialized.sessions().next();\n\t\t\tassert(!firstSession.done, 0x754 /* First session must be present. */);\n\t\t\tthis.localSession = firstSession.value;\n\t\t\tthis.localSessionId = stableIdFromNumericUuid(\n\t\t\t\tthis.localSession.sessionUuid,\n\t\t\t) as SessionId;\n\t\t}\n\t}\n\n\t/**\n\t * @deprecated Use `createIdCompressor` instead.\n\t */\n\tpublic static create(logger?: ITelemetryBaseLogger): IIdCompressor & IIdCompressorCore;\n\t/**\n\t * @deprecated Use `createIdCompressor` instead.\n\t */\n\tpublic static create(\n\t\tsessionId: SessionId,\n\t\tlogger?: ITelemetryBaseLogger,\n\t): IIdCompressor & IIdCompressorCore;\n\tpublic static create(\n\t\tsessionIdOrLogger?: SessionId | ITelemetryBaseLogger,\n\t\tloggerOrUndefined?: ITelemetryBaseLogger,\n\t): IIdCompressor & IIdCompressorCore {\n\t\tlet localSessionId: SessionId;\n\t\tlet logger: ITelemetryBaseLogger | undefined;\n\t\tif (sessionIdOrLogger === undefined) {\n\t\t\tlocalSessionId = createSessionId();\n\t\t} else {\n\t\t\tif (typeof sessionIdOrLogger === \"string\") {\n\t\t\t\tlocalSessionId = sessionIdOrLogger;\n\t\t\t\tlogger = loggerOrUndefined;\n\t\t\t} else {\n\t\t\t\tlocalSessionId = createSessionId();\n\t\t\t\tlogger = loggerOrUndefined;\n\t\t\t}\n\t\t}\n\t\tconst compressor = new IdCompressor(\n\t\t\tlocalSessionId,\n\t\t\tlogger === undefined ? undefined : createChildLogger({ logger }),\n\t\t);\n\t\treturn compressor;\n\t}\n\n\tpublic generateCompressedId(): SessionSpaceCompressedId {\n\t\tthis.localGenCount++;\n\t\tconst lastCluster = this.localSession.getLastCluster();\n\t\tif (lastCluster === undefined) {\n\t\t\tthis.telemetryLocalIdCount++;\n\t\t\treturn this.generateNextLocalId();\n\t\t}\n\n\t\t// If there exists a cluster of final IDs already claimed by the local session that still has room in it,\n\t\t// it is known prior to range sequencing what a local ID's corresponding final ID will be.\n\t\t// In this case, it is safe to return the final ID immediately. This is guaranteed to be safe because\n\t\t// any op that the local session sends that contains one of those final IDs are guaranteed to arrive to\n\t\t// collaborators *after* the one containing the creation range.\n\t\tconst clusterOffset = this.localGenCount - genCountFromLocalId(lastCluster.baseLocalId);\n\t\tif (lastCluster.capacity > clusterOffset) {\n\t\t\tthis.telemetryEagerFinalIdCount++;\n\t\t\t// Space in the cluster: eager final\n\t\t\treturn ((lastCluster.baseFinalId as number) +\n\t\t\t\tclusterOffset) as SessionSpaceCompressedId;\n\t\t}\n\t\t// No space in the cluster, return next local\n\t\tthis.telemetryLocalIdCount++;\n\t\treturn this.generateNextLocalId();\n\t}\n\n\tprivate generateNextLocalId(): LocalCompressedId {\n\t\t// Must tell the normalizer that we generated a local ID\n\t\tthis.normalizer.addLocalRange(this.localGenCount, 1);\n\t\treturn localIdFromGenCount(this.localGenCount);\n\t}\n\n\tpublic takeNextCreationRange(): IdCreationRange {\n\t\tconst count = this.localGenCount - (this.nextRangeBaseGenCount - 1);\n\t\tif (count === 0) {\n\t\t\treturn {\n\t\t\t\tsessionId: this.localSessionId,\n\t\t\t};\n\t\t}\n\t\tconst range: IdCreationRange = {\n\t\t\tsessionId: this.localSessionId,\n\t\t\tids: {\n\t\t\t\tfirstGenCount: this.nextRangeBaseGenCount,\n\t\t\t\tcount,\n\t\t\t\trequestedClusterSize: this.nextRequestedClusterSize,\n\t\t\t},\n\t\t};\n\t\tthis.nextRangeBaseGenCount = this.localGenCount + 1;\n\t\tIdCompressor.assertValidRange(range);\n\t\treturn range;\n\t}\n\n\tprivate static assertValidRange(range: IdCreationRange): void {\n\t\tif (range.ids === undefined) {\n\t\t\treturn;\n\t\t}\n\t\tconst { count, requestedClusterSize } = range.ids;\n\t\tassert(count > 0, 0x755 /* Malformed ID Range. */);\n\t\tassert(requestedClusterSize > 0, 0x876 /* Clusters must have a positive capacity. */);\n\t\tassert(\n\t\t\trequestedClusterSize <= IdCompressor.maxClusterSize,\n\t\t\t0x877 /* Clusters must not exceed max cluster size. */,\n\t\t);\n\t}\n\n\tpublic finalizeCreationRange(range: IdCreationRange): void {\n\t\t// Check if the range has IDs\n\t\tif (range.ids === undefined) {\n\t\t\treturn;\n\t\t}\n\n\t\tIdCompressor.assertValidRange(range);\n\t\tconst { sessionId, ids } = range;\n\t\tconst { count, firstGenCount, requestedClusterSize } = ids;\n\t\tconst session = this.sessions.getOrCreate(sessionId);\n\t\tconst isLocal = session === this.localSession;\n\t\tconst rangeBaseLocal = localIdFromGenCount(firstGenCount);\n\t\tlet lastCluster = session.getLastCluster();\n\t\tif (lastCluster === undefined) {\n\t\t\t// This is the first cluster in the session space\n\t\t\tif (rangeBaseLocal !== -1) {\n\t\t\t\tthrow new Error(\"Ranges finalized out of order.\");\n\t\t\t}\n\t\t\tlastCluster = this.addEmptyCluster(session, requestedClusterSize + count);\n\t\t\tif (isLocal) {\n\t\t\t\tthis.logger?.sendTelemetryEvent({\n\t\t\t\t\teventName: \"RuntimeIdCompressor:FirstCluster\",\n\t\t\t\t\tsessionId: this.localSessionId,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tconst remainingCapacity = lastCluster.capacity - lastCluster.count;\n\t\tif (lastCluster.baseLocalId - lastCluster.count !== rangeBaseLocal) {\n\t\t\tthrow new Error(\"Ranges finalized out of order.\");\n\t\t}\n\n\t\tif (remainingCapacity >= count) {\n\t\t\t// The current range fits in the existing cluster\n\t\t\tlastCluster.count += count;\n\t\t} else {\n\t\t\tconst overflow = count - remainingCapacity;\n\t\t\tconst newClaimedFinalCount = overflow + requestedClusterSize;\n\t\t\tif (lastCluster === this.finalSpace.getLastCluster()) {\n\t\t\t\t// The last cluster in the sessions chain is the last cluster globally, so it can be expanded.\n\t\t\t\tlastCluster.capacity += newClaimedFinalCount;\n\t\t\t\tlastCluster.count += count;\n\t\t\t\tassert(\n\t\t\t\t\t!this.sessions.clusterCollides(lastCluster),\n\t\t\t\t\t0x756 /* Cluster collision detected. */,\n\t\t\t\t);\n\t\t\t\tif (isLocal) {\n\t\t\t\t\tthis.logger?.sendTelemetryEvent({\n\t\t\t\t\t\teventName: \"RuntimeIdCompressor:ClusterExpansion\",\n\t\t\t\t\t\tsessionId: this.localSessionId,\n\t\t\t\t\t\tpreviousCapacity: lastCluster.capacity - newClaimedFinalCount,\n\t\t\t\t\t\tnewCapacity: lastCluster.capacity,\n\t\t\t\t\t\toverflow,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// The last cluster in the sessions chain is *not* the last cluster globally. Fill and overflow to new.\n\t\t\t\tlastCluster.count = lastCluster.capacity;\n\t\t\t\tconst newCluster = this.addEmptyCluster(session, newClaimedFinalCount);\n\t\t\t\tnewCluster.count += overflow;\n\t\t\t\tif (isLocal) {\n\t\t\t\t\tthis.logger?.sendTelemetryEvent({\n\t\t\t\t\t\teventName: \"RuntimeIdCompressor:NewCluster\",\n\t\t\t\t\t\tsessionId: this.localSessionId,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (isLocal) {\n\t\t\tthis.logger?.sendTelemetryEvent({\n\t\t\t\teventName: \"RuntimeIdCompressor:IdCompressorStatus\",\n\t\t\t\teagerFinalIdCount: this.telemetryEagerFinalIdCount,\n\t\t\t\tlocalIdCount: this.telemetryLocalIdCount,\n\t\t\t\tsessionId: this.localSessionId,\n\t\t\t});\n\t\t\tthis.telemetryEagerFinalIdCount = 0;\n\t\t\tthis.telemetryLocalIdCount = 0;\n\t\t}\n\n\t\tassert(!session.isEmpty(), 0x757 /* Empty sessions should not be created. */);\n\t}\n\n\tprivate addEmptyCluster(session: Session, capacity: number): IdCluster {\n\t\tconst newCluster = session.addNewCluster(\n\t\t\tthis.finalSpace.getAllocatedIdLimit(),\n\t\t\tcapacity,\n\t\t\t0,\n\t\t);\n\t\tassert(!this.sessions.clusterCollides(newCluster), 0x758 /* Cluster collision detected. */);\n\t\tthis.finalSpace.addCluster(newCluster);\n\t\treturn newCluster;\n\t}\n\n\tpublic normalizeToOpSpace(id: SessionSpaceCompressedId): OpSpaceCompressedId {\n\t\tif (isFinalId(id)) {\n\t\t\treturn id;\n\t\t} else {\n\t\t\tconst local = id as unknown as LocalCompressedId;\n\t\t\tif (!this.normalizer.contains(local)) {\n\t\t\t\tthrow new Error(\"Invalid ID to normalize.\");\n\t\t\t}\n\t\t\tconst finalForm = this.localSession.tryConvertToFinal(local, true);\n\t\t\treturn finalForm === undefined\n\t\t\t\t? (local as unknown as OpSpaceCompressedId)\n\t\t\t\t: (finalForm as OpSpaceCompressedId);\n\t\t}\n\t}\n\n\tpublic normalizeToSessionSpace(\n\t\tid: OpSpaceCompressedId,\n\t\toriginSessionId: SessionId,\n\t): SessionSpaceCompressedId {\n\t\tif (isFinalId(id)) {\n\t\t\tconst containingCluster = this.localSession.getClusterByAllocatedFinal(id);\n\t\t\tif (containingCluster === undefined) {\n\t\t\t\t// Does not exist in local cluster chain\n\t\t\t\tif (id >= this.finalSpace.getFinalizedIdLimit()) {\n\t\t\t\t\tthrow new Error(\"Unknown op space ID.\");\n\t\t\t\t}\n\t\t\t\treturn id as unknown as SessionSpaceCompressedId;\n\t\t\t} else {\n\t\t\t\tconst alignedLocal = getAlignedLocal(containingCluster, id);\n\t\t\t\tif (this.normalizer.contains(alignedLocal)) {\n\t\t\t\t\treturn alignedLocal;\n\t\t\t\t} else {\n\t\t\t\t\tif (genCountFromLocalId(alignedLocal) > this.localGenCount) {\n\t\t\t\t\t\tthrow new Error(\"Unknown op space ID.\");\n\t\t\t\t\t}\n\t\t\t\t\treturn id as unknown as SessionSpaceCompressedId;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tconst localToNormalize = id as unknown as LocalCompressedId;\n\t\t\tif (originSessionId === this.localSessionId) {\n\t\t\t\tif (this.normalizer.contains(localToNormalize)) {\n\t\t\t\t\treturn localToNormalize;\n\t\t\t\t} else {\n\t\t\t\t\t// We never generated this local ID, so fail\n\t\t\t\t\tthrow new Error(\"Unknown op space ID.\");\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// LocalId from a remote session\n\t\t\t\tconst remoteSession = this.sessions.get(originSessionId);\n\t\t\t\tif (remoteSession === undefined) {\n\t\t\t\t\tthrow new Error(\"No IDs have ever been finalized by the supplied session.\");\n\t\t\t\t}\n\t\t\t\tconst correspondingFinal = remoteSession.tryConvertToFinal(localToNormalize, false);\n\t\t\t\tif (correspondingFinal === undefined) {\n\t\t\t\t\tthrow new Error(\"Unknown op space ID.\");\n\t\t\t\t}\n\t\t\t\treturn correspondingFinal as unknown as SessionSpaceCompressedId;\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic decompress(id: SessionSpaceCompressedId): StableId {\n\t\tif (isFinalId(id)) {\n\t\t\tconst containingCluster = Session.getContainingCluster(id, this.finalSpace.clusters);\n\t\t\tif (containingCluster === undefined) {\n\t\t\t\tthrow new Error(\"Unknown ID\");\n\t\t\t}\n\t\t\tconst alignedLocal = getAlignedLocal(containingCluster, id);\n\t\t\tconst alignedGenCount = genCountFromLocalId(alignedLocal);\n\t\t\tconst lastFinalizedGenCount = genCountFromLocalId(\n\t\t\t\tlastFinalizedLocal(containingCluster),\n\t\t\t);\n\t\t\tif (alignedGenCount > lastFinalizedGenCount) {\n\t\t\t\t// should be an eager final id generated by the local session\n\t\t\t\tif (containingCluster.session === this.localSession) {\n\t\t\t\t\tassert(\n\t\t\t\t\t\t!this.normalizer.contains(alignedLocal),\n\t\t\t\t\t\t0x759 /* Normalizer out of sync. */,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tthrow new Error(\"Unknown ID\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn stableIdFromNumericUuid(\n\t\t\t\toffsetNumericUuid(containingCluster.session.sessionUuid, alignedGenCount - 1),\n\t\t\t);\n\t\t} else {\n\t\t\tconst localToDecompress = id as unknown as LocalCompressedId;\n\t\t\tif (!this.normalizer.contains(localToDecompress)) {\n\t\t\t\tthrow new Error(\"Unknown ID\");\n\t\t\t}\n\t\t\treturn stableIdFromNumericUuid(\n\t\t\t\toffsetNumericUuid(\n\t\t\t\t\tthis.localSession.sessionUuid,\n\t\t\t\t\tgenCountFromLocalId(localToDecompress) - 1,\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\t}\n\n\tpublic recompress(uncompressed: StableId): SessionSpaceCompressedId {\n\t\tconst recompressed = this.tryRecompress(uncompressed);\n\t\tif (recompressed === undefined) {\n\t\t\tthrow new Error(\"Could not recompress.\");\n\t\t}\n\t\treturn recompressed;\n\t}\n\n\tpublic tryRecompress(uncompressed: StableId): SessionSpaceCompressedId | undefined {\n\t\tconst match = this.sessions.getContainingCluster(uncompressed);\n\t\tif (match === undefined) {\n\t\t\tconst numericUncompressed = numericUuidFromStableId(uncompressed);\n\t\t\tconst offset = subtractNumericUuids(numericUncompressed, this.localSession.sessionUuid);\n\t\t\tif (offset < Number.MAX_SAFE_INTEGER) {\n\t\t\t\tconst genCountEquivalent = Number(offset) + 1;\n\t\t\t\tconst localEquivalent = localIdFromGenCount(genCountEquivalent);\n\t\t\t\tif (this.normalizer.contains(localEquivalent)) {\n\t\t\t\t\treturn localEquivalent;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn undefined;\n\t\t} else {\n\t\t\tconst [containingCluster, alignedLocal] = match;\n\t\t\tif (containingCluster.session === this.localSession) {\n\t\t\t\t// Local session\n\t\t\t\tif (this.normalizer.contains(alignedLocal)) {\n\t\t\t\t\treturn alignedLocal;\n\t\t\t\t} else {\n\t\t\t\t\tassert(\n\t\t\t\t\t\tgenCountFromLocalId(alignedLocal) <= this.localGenCount,\n\t\t\t\t\t\t0x75a /* Clusters out of sync. */,\n\t\t\t\t\t);\n\t\t\t\t\t// Id is an eager final\n\t\t\t\t\treturn getAlignedFinal(containingCluster, alignedLocal) as\n\t\t\t\t\t\t| SessionSpaceCompressedId\n\t\t\t\t\t\t| undefined;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Not the local session\n\t\t\t\treturn genCountFromLocalId(alignedLocal) >= lastFinalizedLocal(containingCluster)\n\t\t\t\t\t? (getAlignedFinal(containingCluster, alignedLocal) as\n\t\t\t\t\t\t\t| SessionSpaceCompressedId\n\t\t\t\t\t\t\t| undefined)\n\t\t\t\t\t: undefined;\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic serialize(withSession: true): SerializedIdCompressorWithOngoingSession;\n\tpublic serialize(withSession: false): SerializedIdCompressorWithNoSession;\n\tpublic serialize(hasLocalState: boolean): SerializedIdCompressor {\n\t\tconst { normalizer, finalSpace, sessions } = this;\n\t\tconst sessionIndexMap = new Map<Session, number>();\n\t\tlet sessionIndex = 0;\n\t\tfor (const session of sessions.sessions()) {\n\t\t\t// Filter empty sessions to prevent them accumulating in the serialized state\n\t\t\tif (!session.isEmpty() || hasLocalState) {\n\t\t\t\tsessionIndexMap.set(session, sessionIndex);\n\t\t\t\tsessionIndex++;\n\t\t\t}\n\t\t}\n\t\tconst localStateSize = hasLocalState\n\t\t\t? 1 + // generated ID count\n\t\t\t 1 + // next range base genCount\n\t\t\t 1 + // count of normalizer pairs\n\t\t\t this.normalizer.idRanges.size * 2 // pairs\n\t\t\t: 0;\n\t\t// Layout size, in 8 byte increments\n\t\tconst totalSize =\n\t\t\t1 + // version\n\t\t\t1 + // hasLocalState\n\t\t\t1 + // session count\n\t\t\t1 + // cluster count\n\t\t\tsessionIndexMap.size * 2 + // session IDs\n\t\t\tfinalSpace.clusters.length * 3 + // clusters: (sessionIndex, capacity, count)[]\n\t\t\tlocalStateSize; // local state, if present\n\n\t\tconst serializedFloat = new Float64Array(totalSize);\n\t\tconst serializedUint = new BigUint64Array(serializedFloat.buffer);\n\t\tlet index = 0;\n\t\tindex = writeNumber(serializedFloat, index, currentWrittenVersion);\n\t\tindex = writeBoolean(serializedFloat, index, hasLocalState);\n\t\tindex = writeNumber(serializedFloat, index, sessionIndexMap.size);\n\t\tindex = writeNumber(serializedFloat, index, finalSpace.clusters.length);\n\n\t\tfor (const [session] of sessionIndexMap.entries()) {\n\t\t\tindex = writeNumericUuid(serializedUint, index, session.sessionUuid);\n\t\t}\n\n\t\tfinalSpace.clusters.forEach((cluster) => {\n\t\t\tindex = writeNumber(\n\t\t\t\tserializedFloat,\n\t\t\t\tindex,\n\t\t\t\tsessionIndexMap.get(cluster.session) as number,\n\t\t\t);\n\t\t\tindex = writeNumber(serializedFloat, index, cluster.capacity);\n\t\t\tindex = writeNumber(serializedFloat, index, cluster.count);\n\t\t});\n\n\t\tif (hasLocalState) {\n\t\t\tindex = writeNumber(serializedFloat, index, this.localGenCount);\n\t\t\tindex = writeNumber(serializedFloat, index, this.nextRangeBaseGenCount);\n\t\t\tindex = writeNumber(serializedFloat, index, normalizer.idRanges.size);\n\t\t\tfor (const [leadingGenCount, count] of normalizer.idRanges.entries()) {\n\t\t\t\tindex = writeNumber(serializedFloat, index, leadingGenCount);\n\t\t\t\tindex = writeNumber(serializedFloat, index, count);\n\t\t\t}\n\t\t}\n\n\t\tassert(index === totalSize, 0x75b /* Serialized size was incorrectly calculated. */);\n\t\tthis.logger?.sendTelemetryEvent({\n\t\t\teventName: \"RuntimeIdCompressor:SerializedIdCompressorSize\",\n\t\t\tsize: serializedFloat.byteLength,\n\t\t\tclusterCount: finalSpace.clusters.length,\n\t\t\tsessionCount: sessionIndexMap.size,\n\t\t});\n\n\t\treturn bufferToString(serializedFloat.buffer, \"base64\") as SerializedIdCompressor;\n\t}\n\n\tpublic static deserialize(serialized: SerializedIdCompressorWithOngoingSession): IdCompressor;\n\tpublic static deserialize(\n\t\tserialized: SerializedIdCompressorWithNoSession,\n\t\tnewSessionId: SessionId,\n\t): IdCompressor;\n\tpublic static deserialize(\n\t\tserialized: SerializedIdCompressor,\n\t\tsessionId?: SessionId,\n\t): IdCompressor {\n\t\tconst buffer = stringToBuffer(serialized, \"base64\");\n\t\tconst index: Index = {\n\t\t\tindex: 0,\n\t\t\tbufferFloat: new Float64Array(buffer),\n\t\t\tbufferUint: new BigUint64Array(buffer),\n\t\t};\n\t\tconst version = readNumber(index);\n\t\tassert(version === currentWrittenVersion, 0x75c /* Unknown serialized version. */);\n\t\tconst hasLocalState = readBoolean(index);\n\t\tconst sessionCount = readNumber(index);\n\t\tconst clusterCount = readNumber(index);\n\n\t\t// Sessions\n\t\tlet sessionOffset = 0;\n\t\tconst sessions: [NumericUuid, Session][] = [];\n\t\tif (!hasLocalState) {\n\t\t\t// If !hasLocalState, there won't be a serialized local session ID so insert one at the beginning\n\t\t\tassert(sessionId !== undefined, 0x75d /* Local session ID is undefined. */);\n\t\t\tconst localSessionNumeric = numericUuidFromStableId(sessionId);\n\t\t\tsessions.push([localSessionNumeric, new Session(localSessionNumeric)]);\n\t\t\tsessionOffset = 1;\n\t\t} else {\n\t\t\tassert(\n\t\t\t\tsessionId === undefined,\n\t\t\t\t0x75e /* Local state should not exist in serialized form. */,\n\t\t\t);\n\t\t}\n\n\t\tfor (let i = 0; i < sessionCount; i++) {\n\t\t\tconst numeric = readNumericUuid(index);\n\t\t\tsessions.push([numeric, new Session(numeric)]);\n\t\t}\n\n\t\tconst compressor = new IdCompressor(new Sessions(sessions));\n\n\t\t// Clusters\n\t\tlet baseFinalId = 0;\n\t\tfor (let i = 0; i < clusterCount; i++) {\n\t\t\tconst sessionIndex = readNumber(index);\n\t\t\tconst session = sessions[sessionIndex + sessionOffset][1];\n\t\t\tconst capacity = readNumber(index);\n\t\t\tconst count = readNumber(index);\n\t\t\tconst cluster = session.addNewCluster(\n\t\t\t\tbaseFinalId as FinalCompressedId,\n\t\t\t\tcapacity,\n\t\t\t\tcount,\n\t\t\t);\n\t\t\tcompressor.finalSpace.addCluster(cluster);\n\t\t\tbaseFinalId += capacity;\n\t\t}\n\n\t\t// Local state\n\t\tif (hasLocalState) {\n\t\t\tcompressor.localGenCount = readNumber(index);\n\t\t\tcompressor.nextRangeBaseGenCount = readNumber(index);\n\t\t\tconst normalizerCount = readNumber(index);\n\t\t\tfor (let i = 0; i < normalizerCount; i++) {\n\t\t\t\tcompressor.normalizer.addLocalRange(readNumber(index), readNumber(index));\n\t\t\t}\n\t\t}\n\n\t\tassert(\n\t\t\tindex.index === index.bufferFloat.length,\n\t\t\t0x75f /* Failed to read entire serialized compressor. */,\n\t\t);\n\t\treturn compressor;\n\t}\n\n\tpublic equals(other: IdCompressor, includeLocalState: boolean): boolean {\n\t\tif (\n\t\t\tincludeLocalState &&\n\t\t\t(this.localSessionId !== other.localSessionId ||\n\t\t\t\t!this.localSession.equals(other.localSession) ||\n\t\t\t\t!this.normalizer.equals(other.normalizer) ||\n\t\t\t\tthis.nextRangeBaseGenCount !== other.nextRangeBaseGenCount ||\n\t\t\t\tthis.localGenCount !== other.localGenCount)\n\t\t) {\n\t\t\treturn false;\n\t\t}\n\t\treturn (\n\t\t\tthis.sessions.equals(other.sessions, includeLocalState) &&\n\t\t\tthis.finalSpace.equals(other.finalSpace)\n\t\t);\n\t}\n}\n\n/**\n * Create a new {@link IIdCompressor}.\n * @alpha\n */\nexport function createIdCompressor(\n\tlogger?: ITelemetryBaseLogger,\n): IIdCompressor & IIdCompressorCore;\n/**\n * Create a new {@link IIdCompressor}.\n * @param sessionId - The seed ID for the compressor.\n * @alpha\n */\nexport function createIdCompressor(\n\tsessionId: SessionId,\n\tlogger?: ITelemetryBaseLogger,\n): IIdCompressor & IIdCompressorCore;\nexport function createIdCompressor(\n\tsessionIdOrLogger?: SessionId | ITelemetryBaseLogger,\n\tloggerOrUndefined?: ITelemetryBaseLogger,\n): IIdCompressor & IIdCompressorCore {\n\treturn IdCompressor.create(sessionIdOrLogger as SessionId, loggerOrUndefined);\n}\n\n/**\n * Deserializes the supplied state into an ID compressor.\n * @alpha\n */\nexport function deserializeIdCompressor(\n\tserialized: SerializedIdCompressorWithOngoingSession,\n): IIdCompressor & IIdCompressorCore;\n/**\n * Deserializes the supplied state into an ID compressor.\n * @alpha\n */\nexport function deserializeIdCompressor(\n\tserialized: SerializedIdCompressorWithNoSession,\n\tnewSessionId: SessionId,\n): IIdCompressor & IIdCompressorCore;\nexport function deserializeIdCompressor(\n\tserialized: SerializedIdCompressor | SerializedIdCompressorWithNoSession,\n\tsessionId?: SessionId,\n): IIdCompressor & IIdCompressorCore {\n\treturn sessionId === undefined\n\t\t? IdCompressor.deserialize(serialized as SerializedIdCompressorWithOngoingSession)\n\t\t: IdCompressor.deserialize(serialized as SerializedIdCompressorWithNoSession, sessionId);\n}\n"]}