@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,503 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { assert } from "@fluidframework/core-utils";
6
+ import { bufferToString, stringToBuffer } from "@fluid-internal/client-utils";
7
+ import { createChildLogger } from "@fluidframework/telemetry-utils";
8
+ import { initialClusterCapacity, } from "./types";
9
+ import { isFinalId } from "./identifiers";
10
+ import { createSessionId, localIdFromGenCount, genCountFromLocalId, numericUuidFromStableId, offsetNumericUuid, stableIdFromNumericUuid, subtractNumericUuids, } from "./utilities";
11
+ import { readBoolean, readNumber, readNumericUuid, writeBoolean, writeNumber, writeNumericUuid, } from "./persistanceUtilities";
12
+ import { getAlignedLocal, getAlignedFinal, lastFinalizedLocal, Session, Sessions, } from "./sessions";
13
+ import { SessionSpaceNormalizer } from "./sessionSpaceNormalizer";
14
+ import { FinalSpace } from "./finalSpace";
15
+ /**
16
+ * The version of IdCompressor that is currently persisted.
17
+ * This should not be changed without careful consideration to compatibility.
18
+ */
19
+ const currentWrittenVersion = 1;
20
+ /**
21
+ * See {@link IIdCompressor} and {@link IIdCompressorCore}
22
+ *
23
+ * @alpha
24
+ *
25
+ * @deprecated Not intended for public use and will be removed from the public API in the future.
26
+ * Use `createIdCompressor` instead.
27
+ */
28
+ export class IdCompressor {
29
+ // #endregion
30
+ constructor(localSessionIdOrDeserialized, logger) {
31
+ this.logger = logger;
32
+ this.normalizer = new SessionSpaceNormalizer();
33
+ // The number of IDs generated by the local session
34
+ this.localGenCount = 0;
35
+ // #endregion
36
+ // #region Final state
37
+ // The gen count to be annotated on the range returned by the next call to `takeNextCreationRange`.
38
+ // This is updated to be equal to `generatedIdCount` + 1 each time it is called.
39
+ this.nextRangeBaseGenCount = 1;
40
+ // The capacity of the next cluster to be created
41
+ this.newClusterCapacity = initialClusterCapacity;
42
+ this.sessions = new Sessions();
43
+ this.finalSpace = new FinalSpace();
44
+ // #endregion
45
+ // #region Telemetry state
46
+ // The number of local IDs generated since the last telemetry was sent.
47
+ this.telemetryLocalIdCount = 0;
48
+ // The number of eager final IDs generated since the last telemetry was sent.
49
+ this.telemetryEagerFinalIdCount = 0;
50
+ if (typeof localSessionIdOrDeserialized === "string") {
51
+ this.localSessionId = localSessionIdOrDeserialized;
52
+ this.localSession = this.sessions.getOrCreate(localSessionIdOrDeserialized);
53
+ }
54
+ else {
55
+ // Deserialize case
56
+ this.sessions = localSessionIdOrDeserialized;
57
+ // As policy, the first session is always the local session. Preserve this invariant
58
+ // during deserialization.
59
+ const firstSession = localSessionIdOrDeserialized.sessions().next();
60
+ assert(!firstSession.done, 0x754 /* First session must be present. */);
61
+ this.localSession = firstSession.value;
62
+ this.localSessionId = stableIdFromNumericUuid(this.localSession.sessionUuid);
63
+ }
64
+ }
65
+ static create(sessionIdOrLogger, loggerOrUndefined) {
66
+ let localSessionId;
67
+ let logger;
68
+ if (sessionIdOrLogger === undefined) {
69
+ localSessionId = createSessionId();
70
+ }
71
+ else {
72
+ if (typeof sessionIdOrLogger === "string") {
73
+ localSessionId = sessionIdOrLogger;
74
+ logger = loggerOrUndefined;
75
+ }
76
+ else {
77
+ localSessionId = createSessionId();
78
+ logger = loggerOrUndefined;
79
+ }
80
+ }
81
+ const compressor = new IdCompressor(localSessionId, logger === undefined ? undefined : createChildLogger({ logger }));
82
+ return compressor;
83
+ }
84
+ /**
85
+ * The size of each newly created ID cluster.
86
+ *
87
+ * @remarks Must only be set with a value upon which consensus has been reached.
88
+ * Value must be greater than zero and less than {@link IdCompressor.maxClusterSize}.
89
+ */
90
+ get clusterCapacity() {
91
+ return this.newClusterCapacity;
92
+ }
93
+ set clusterCapacity(value) {
94
+ if (value <= 0) {
95
+ throw new Error("Clusters must have a positive capacity.");
96
+ }
97
+ if (value > IdCompressor.maxClusterSize) {
98
+ throw new Error("Clusters must not exceed max cluster size.");
99
+ }
100
+ this.newClusterCapacity = value;
101
+ }
102
+ generateCompressedId() {
103
+ this.localGenCount++;
104
+ const lastCluster = this.localSession.getLastCluster();
105
+ if (lastCluster === undefined) {
106
+ this.telemetryLocalIdCount++;
107
+ return this.generateNextLocalId();
108
+ }
109
+ // If there exists a cluster of final IDs already claimed by the local session that still has room in it,
110
+ // it is known prior to range sequencing what a local ID's corresponding final ID will be.
111
+ // In this case, it is safe to return the final ID immediately. This is guaranteed to be safe because
112
+ // any op that the local session sends that contains one of those final IDs are guaranteed to arrive to
113
+ // collaborators *after* the one containing the creation range.
114
+ const clusterOffset = this.localGenCount - genCountFromLocalId(lastCluster.baseLocalId);
115
+ if (lastCluster.capacity > clusterOffset) {
116
+ this.telemetryEagerFinalIdCount++;
117
+ // Space in the cluster: eager final
118
+ return (lastCluster.baseFinalId +
119
+ clusterOffset);
120
+ }
121
+ // No space in the cluster, return next local
122
+ this.telemetryLocalIdCount++;
123
+ return this.generateNextLocalId();
124
+ }
125
+ generateNextLocalId() {
126
+ // Must tell the normalizer that we generated a local ID
127
+ this.normalizer.addLocalRange(this.localGenCount, 1);
128
+ return localIdFromGenCount(this.localGenCount);
129
+ }
130
+ takeNextCreationRange() {
131
+ const count = this.localGenCount - (this.nextRangeBaseGenCount - 1);
132
+ if (count === 0) {
133
+ return {
134
+ sessionId: this.localSessionId,
135
+ };
136
+ }
137
+ const range = {
138
+ sessionId: this.localSessionId,
139
+ ids: {
140
+ firstGenCount: this.nextRangeBaseGenCount,
141
+ count,
142
+ },
143
+ };
144
+ this.nextRangeBaseGenCount = this.localGenCount + 1;
145
+ return range;
146
+ }
147
+ finalizeCreationRange(range) {
148
+ // Check if the range has IDs
149
+ if (range.ids === undefined) {
150
+ return;
151
+ }
152
+ assert(range.ids.count > 0, 0x755 /* Malformed ID Range. */);
153
+ const { sessionId, ids } = range;
154
+ const { count, firstGenCount } = ids;
155
+ const session = this.sessions.getOrCreate(sessionId);
156
+ const isLocal = session === this.localSession;
157
+ const rangeBaseLocal = localIdFromGenCount(firstGenCount);
158
+ let lastCluster = session.getLastCluster();
159
+ if (lastCluster === undefined) {
160
+ // This is the first cluster in the session space
161
+ if (rangeBaseLocal !== -1) {
162
+ throw new Error("Ranges finalized out of order.");
163
+ }
164
+ lastCluster = this.addEmptyCluster(session, this.clusterCapacity + count);
165
+ if (isLocal) {
166
+ this.logger?.sendTelemetryEvent({
167
+ eventName: "RuntimeIdCompressor:FirstCluster",
168
+ sessionId: this.localSessionId,
169
+ });
170
+ }
171
+ }
172
+ const remainingCapacity = lastCluster.capacity - lastCluster.count;
173
+ if (lastCluster.baseLocalId - lastCluster.count !== rangeBaseLocal) {
174
+ throw new Error("Ranges finalized out of order.");
175
+ }
176
+ if (remainingCapacity >= count) {
177
+ // The current range fits in the existing cluster
178
+ lastCluster.count += count;
179
+ }
180
+ else {
181
+ const overflow = count - remainingCapacity;
182
+ const newClaimedFinalCount = overflow + this.clusterCapacity;
183
+ if (lastCluster === this.finalSpace.getLastCluster()) {
184
+ // The last cluster in the sessions chain is the last cluster globally, so it can be expanded.
185
+ lastCluster.capacity += newClaimedFinalCount;
186
+ lastCluster.count += count;
187
+ assert(!this.sessions.clusterCollides(lastCluster), 0x756 /* Cluster collision detected. */);
188
+ if (isLocal) {
189
+ this.logger?.sendTelemetryEvent({
190
+ eventName: "RuntimeIdCompressor:ClusterExpansion",
191
+ sessionId: this.localSessionId,
192
+ previousCapacity: lastCluster.capacity - newClaimedFinalCount,
193
+ newCapacity: lastCluster.capacity,
194
+ overflow,
195
+ });
196
+ }
197
+ }
198
+ else {
199
+ // The last cluster in the sessions chain is *not* the last cluster globally. Fill and overflow to new.
200
+ lastCluster.count = lastCluster.capacity;
201
+ const newCluster = this.addEmptyCluster(session, newClaimedFinalCount);
202
+ newCluster.count += overflow;
203
+ if (isLocal) {
204
+ this.logger?.sendTelemetryEvent({
205
+ eventName: "RuntimeIdCompressor:NewCluster",
206
+ sessionId: this.localSessionId,
207
+ });
208
+ }
209
+ }
210
+ }
211
+ if (isLocal) {
212
+ this.logger?.sendTelemetryEvent({
213
+ eventName: "RuntimeIdCompressor:IdCompressorStatus",
214
+ eagerFinalIdCount: this.telemetryEagerFinalIdCount,
215
+ localIdCount: this.telemetryLocalIdCount,
216
+ sessionId: this.localSessionId,
217
+ });
218
+ this.telemetryEagerFinalIdCount = 0;
219
+ this.telemetryLocalIdCount = 0;
220
+ }
221
+ assert(!session.isEmpty(), 0x757 /* Empty sessions should not be created. */);
222
+ }
223
+ addEmptyCluster(session, capacity) {
224
+ const newCluster = session.addNewCluster(this.finalSpace.getAllocatedIdLimit(), capacity, 0);
225
+ assert(!this.sessions.clusterCollides(newCluster), 0x758 /* Cluster collision detected. */);
226
+ this.finalSpace.addCluster(newCluster);
227
+ return newCluster;
228
+ }
229
+ normalizeToOpSpace(id) {
230
+ if (isFinalId(id)) {
231
+ return id;
232
+ }
233
+ else {
234
+ const local = id;
235
+ if (!this.normalizer.contains(local)) {
236
+ throw new Error("Invalid ID to normalize.");
237
+ }
238
+ const finalForm = this.localSession.tryConvertToFinal(local, true);
239
+ return finalForm === undefined
240
+ ? local
241
+ : finalForm;
242
+ }
243
+ }
244
+ normalizeToSessionSpace(id, originSessionId) {
245
+ if (isFinalId(id)) {
246
+ const containingCluster = this.localSession.getClusterByAllocatedFinal(id);
247
+ if (containingCluster === undefined) {
248
+ // Does not exist in local cluster chain
249
+ if (id >= this.finalSpace.getFinalizedIdLimit()) {
250
+ throw new Error("Unknown op space ID.");
251
+ }
252
+ return id;
253
+ }
254
+ else {
255
+ const alignedLocal = getAlignedLocal(containingCluster, id);
256
+ if (this.normalizer.contains(alignedLocal)) {
257
+ return alignedLocal;
258
+ }
259
+ else {
260
+ if (genCountFromLocalId(alignedLocal) > this.localGenCount) {
261
+ throw new Error("Unknown op space ID.");
262
+ }
263
+ return id;
264
+ }
265
+ }
266
+ }
267
+ else {
268
+ const localToNormalize = id;
269
+ if (originSessionId === this.localSessionId) {
270
+ if (this.normalizer.contains(localToNormalize)) {
271
+ return localToNormalize;
272
+ }
273
+ else {
274
+ // We never generated this local ID, so fail
275
+ throw new Error("Unknown op space ID.");
276
+ }
277
+ }
278
+ else {
279
+ // LocalId from a remote session
280
+ const remoteSession = this.sessions.get(originSessionId);
281
+ if (remoteSession === undefined) {
282
+ throw new Error("No IDs have ever been finalized by the supplied session.");
283
+ }
284
+ const correspondingFinal = remoteSession.tryConvertToFinal(localToNormalize, false);
285
+ if (correspondingFinal === undefined) {
286
+ throw new Error("Unknown op space ID.");
287
+ }
288
+ return correspondingFinal;
289
+ }
290
+ }
291
+ }
292
+ decompress(id) {
293
+ if (isFinalId(id)) {
294
+ const containingCluster = Session.getContainingCluster(id, this.finalSpace.clusters);
295
+ if (containingCluster === undefined) {
296
+ throw new Error("Unknown ID");
297
+ }
298
+ const alignedLocal = getAlignedLocal(containingCluster, id);
299
+ const alignedGenCount = genCountFromLocalId(alignedLocal);
300
+ const lastFinalizedGenCount = genCountFromLocalId(lastFinalizedLocal(containingCluster));
301
+ if (alignedGenCount > lastFinalizedGenCount) {
302
+ // should be an eager final id generated by the local session
303
+ if (containingCluster.session === this.localSession) {
304
+ assert(!this.normalizer.contains(alignedLocal), 0x759 /* Normalizer out of sync. */);
305
+ }
306
+ else {
307
+ throw new Error("Unknown ID");
308
+ }
309
+ }
310
+ return stableIdFromNumericUuid(offsetNumericUuid(containingCluster.session.sessionUuid, alignedGenCount - 1));
311
+ }
312
+ else {
313
+ const localToDecompress = id;
314
+ if (!this.normalizer.contains(localToDecompress)) {
315
+ throw new Error("Unknown ID");
316
+ }
317
+ return stableIdFromNumericUuid(offsetNumericUuid(this.localSession.sessionUuid, genCountFromLocalId(localToDecompress) - 1));
318
+ }
319
+ }
320
+ recompress(uncompressed) {
321
+ const recompressed = this.tryRecompress(uncompressed);
322
+ if (recompressed === undefined) {
323
+ throw new Error("Could not recompress.");
324
+ }
325
+ return recompressed;
326
+ }
327
+ tryRecompress(uncompressed) {
328
+ const match = this.sessions.getContainingCluster(uncompressed);
329
+ if (match === undefined) {
330
+ const numericUncompressed = numericUuidFromStableId(uncompressed);
331
+ const offset = subtractNumericUuids(numericUncompressed, this.localSession.sessionUuid);
332
+ if (offset < Number.MAX_SAFE_INTEGER) {
333
+ const genCountEquivalent = Number(offset) + 1;
334
+ const localEquivalent = localIdFromGenCount(genCountEquivalent);
335
+ if (this.normalizer.contains(localEquivalent)) {
336
+ return localEquivalent;
337
+ }
338
+ }
339
+ return undefined;
340
+ }
341
+ else {
342
+ const [containingCluster, alignedLocal] = match;
343
+ if (containingCluster.session === this.localSession) {
344
+ // Local session
345
+ if (this.normalizer.contains(alignedLocal)) {
346
+ return alignedLocal;
347
+ }
348
+ else {
349
+ assert(genCountFromLocalId(alignedLocal) <= this.localGenCount, 0x75a /* Clusters out of sync. */);
350
+ // Id is an eager final
351
+ return getAlignedFinal(containingCluster, alignedLocal);
352
+ }
353
+ }
354
+ else {
355
+ // Not the local session
356
+ return genCountFromLocalId(alignedLocal) >= lastFinalizedLocal(containingCluster)
357
+ ? getAlignedFinal(containingCluster, alignedLocal)
358
+ : undefined;
359
+ }
360
+ }
361
+ }
362
+ serialize(hasLocalState) {
363
+ const { normalizer, finalSpace, sessions } = this;
364
+ const sessionIndexMap = new Map();
365
+ let sessionIndex = 0;
366
+ for (const session of sessions.sessions()) {
367
+ // Filter empty sessions to prevent them accumulating in the serialized state
368
+ if (!session.isEmpty() || hasLocalState) {
369
+ sessionIndexMap.set(session, sessionIndex);
370
+ sessionIndex++;
371
+ }
372
+ }
373
+ const localStateSize = hasLocalState
374
+ ? 1 + // generated ID count
375
+ 1 + // next range base genCount
376
+ 1 + // count of normalizer pairs
377
+ this.normalizer.idRanges.size * 2 // pairs
378
+ : 0;
379
+ // Layout size, in 8 byte increments
380
+ const totalSize = 1 + // version
381
+ 1 + // hasLocalState
382
+ 1 + // cluster capacity
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 = writeNumber(serializedFloat, index, currentWrittenVersion);
392
+ index = writeBoolean(serializedFloat, index, hasLocalState);
393
+ index = writeNumber(serializedFloat, index, this.clusterCapacity);
394
+ index = writeNumber(serializedFloat, index, sessionIndexMap.size);
395
+ index = writeNumber(serializedFloat, index, finalSpace.clusters.length);
396
+ for (const [session] of sessionIndexMap.entries()) {
397
+ index = writeNumericUuid(serializedUint, index, session.sessionUuid);
398
+ }
399
+ finalSpace.clusters.forEach((cluster) => {
400
+ index = writeNumber(serializedFloat, index, sessionIndexMap.get(cluster.session));
401
+ index = writeNumber(serializedFloat, index, cluster.capacity);
402
+ index = writeNumber(serializedFloat, index, cluster.count);
403
+ });
404
+ if (hasLocalState) {
405
+ index = writeNumber(serializedFloat, index, this.localGenCount);
406
+ index = writeNumber(serializedFloat, index, this.nextRangeBaseGenCount);
407
+ index = writeNumber(serializedFloat, index, normalizer.idRanges.size);
408
+ for (const [leadingGenCount, count] of normalizer.idRanges.entries()) {
409
+ index = writeNumber(serializedFloat, index, leadingGenCount);
410
+ index = writeNumber(serializedFloat, index, count);
411
+ }
412
+ }
413
+ assert(index === totalSize, 0x75b /* Serialized size was incorrectly calculated. */);
414
+ this.logger?.sendTelemetryEvent({
415
+ eventName: "RuntimeIdCompressor:SerializedIdCompressorSize",
416
+ size: serializedFloat.byteLength,
417
+ clusterCount: finalSpace.clusters.length,
418
+ sessionCount: sessionIndexMap.size,
419
+ });
420
+ return bufferToString(serializedFloat.buffer, "base64");
421
+ }
422
+ static deserialize(serialized, sessionId) {
423
+ const buffer = stringToBuffer(serialized, "base64");
424
+ const index = {
425
+ index: 0,
426
+ bufferFloat: new Float64Array(buffer),
427
+ bufferUint: new BigUint64Array(buffer),
428
+ };
429
+ const version = readNumber(index);
430
+ assert(version === currentWrittenVersion, 0x75c /* Unknown serialized version. */);
431
+ const hasLocalState = readBoolean(index);
432
+ const clusterCapacity = readNumber(index);
433
+ const sessionCount = readNumber(index);
434
+ const clusterCount = readNumber(index);
435
+ // Sessions
436
+ let sessionOffset = 0;
437
+ const sessions = [];
438
+ if (!hasLocalState) {
439
+ // If !hasLocalState, there won't be a serialized local session ID so insert one at the beginning
440
+ assert(sessionId !== undefined, 0x75d /* Local session ID is undefined. */);
441
+ const localSessionNumeric = numericUuidFromStableId(sessionId);
442
+ sessions.push([localSessionNumeric, new Session(localSessionNumeric)]);
443
+ sessionOffset = 1;
444
+ }
445
+ else {
446
+ assert(sessionId === undefined, 0x75e /* Local state should not exist in serialized form. */);
447
+ }
448
+ for (let i = 0; i < sessionCount; i++) {
449
+ const numeric = readNumericUuid(index);
450
+ sessions.push([numeric, new Session(numeric)]);
451
+ }
452
+ const compressor = new IdCompressor(new Sessions(sessions));
453
+ compressor.clusterCapacity = clusterCapacity;
454
+ // Clusters
455
+ let baseFinalId = 0;
456
+ for (let i = 0; i < clusterCount; i++) {
457
+ const sessionIndex = readNumber(index);
458
+ const session = sessions[sessionIndex + sessionOffset][1];
459
+ const capacity = readNumber(index);
460
+ const count = readNumber(index);
461
+ const cluster = session.addNewCluster(baseFinalId, capacity, count);
462
+ compressor.finalSpace.addCluster(cluster);
463
+ baseFinalId += capacity;
464
+ }
465
+ // Local state
466
+ if (hasLocalState) {
467
+ compressor.localGenCount = readNumber(index);
468
+ compressor.nextRangeBaseGenCount = readNumber(index);
469
+ const normalizerCount = readNumber(index);
470
+ for (let i = 0; i < normalizerCount; i++) {
471
+ compressor.normalizer.addLocalRange(readNumber(index), readNumber(index));
472
+ }
473
+ }
474
+ assert(index.index === index.bufferFloat.length, 0x75f /* Failed to read entire serialized compressor. */);
475
+ return compressor;
476
+ }
477
+ equals(other, includeLocalState) {
478
+ if (includeLocalState &&
479
+ (this.localSessionId !== other.localSessionId ||
480
+ !this.localSession.equals(other.localSession) ||
481
+ !this.normalizer.equals(other.normalizer) ||
482
+ this.nextRangeBaseGenCount !== other.nextRangeBaseGenCount ||
483
+ this.localGenCount !== other.localGenCount)) {
484
+ return false;
485
+ }
486
+ return (this.newClusterCapacity === other.newClusterCapacity &&
487
+ this.sessions.equals(other.sessions, includeLocalState) &&
488
+ this.finalSpace.equals(other.finalSpace));
489
+ }
490
+ }
491
+ /**
492
+ * Max allowed initial cluster size.
493
+ */
494
+ IdCompressor.maxClusterSize = 2 ** 20;
495
+ export function createIdCompressor(sessionIdOrLogger, loggerOrUndefined) {
496
+ return IdCompressor.create(sessionIdOrLogger, loggerOrUndefined);
497
+ }
498
+ export function deserializeIdCompressor(serialized, sessionId) {
499
+ return sessionId === undefined
500
+ ? IdCompressor.deserialize(serialized)
501
+ : IdCompressor.deserialize(serialized, sessionId);
502
+ }
503
+ //# sourceMappingURL=idCompressor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idCompressor.js","sourceRoot":"","sources":["../src/idCompressor.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAE9E,OAAO,EAAuB,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AACzF,OAAO,EAWN,sBAAsB,GACtB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAqB,SAAS,EAAkC,MAAM,eAAe,CAAC;AAC7F,OAAO,EACN,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,uBAAuB,EACvB,iBAAiB,EACjB,uBAAuB,EACvB,oBAAoB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EAEN,WAAW,EACX,UAAU,EACV,eAAe,EACf,YAAY,EACZ,WAAW,EACX,gBAAgB,GAChB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACN,eAAe,EACf,eAAe,EAEf,kBAAkB,EAClB,OAAO,EACP,QAAQ,GACR,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C;;;GAGG;AACH,MAAM,qBAAqB,GAAG,CAAC,CAAC;AAEhC;;;;;;;GAOG;AACH,MAAM,OAAO,YAAY;IAmCxB,aAAa;IAEb,YACC,4BAAkD,EACjC,MAA4B;QAA5B,WAAM,GAAN,MAAM,CAAsB;QA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AC,kBAAkB;YACpD,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,iBAAiB,CAAC;YACvD,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CACxC,CAAC;IACH,CAAC;;AA7jBD;;GAEG;AACoB,2BAAc,GAAG,CAAC,IAAI,EAAE,AAAV,CAAW;AA6kBjD,MAAM,UAAU,kBAAkB,CACjC,iBAAoD,EACpD,iBAAwC;IAExC,OAAO,YAAY,CAAC,MAAM,CAAC,iBAA8B,EAAE,iBAAiB,CAAC,CAAC;AAC/E,CAAC;AAiBD,MAAM,UAAU,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","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\tinitialClusterCapacity,\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\t// The capacity of the next cluster to be created\n\tprivate newClusterCapacity = initialClusterCapacity;\n\tprivate readonly sessions = new Sessions();\n\tprivate readonly finalSpace = new FinalSpace();\n\n\t// #endregion\n\n\t// #region Telemetry state\n\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\t/**\n\t * The size of each newly created ID cluster.\n\t *\n\t * @remarks Must only be set with a value upon which consensus has been reached.\n\t * Value must be greater than zero and less than {@link IdCompressor.maxClusterSize}.\n\t */\n\tpublic get clusterCapacity(): number {\n\t\treturn this.newClusterCapacity;\n\t}\n\tpublic set clusterCapacity(value: number) {\n\t\tif (value <= 0) {\n\t\t\tthrow new Error(\"Clusters must have a positive capacity.\");\n\t\t}\n\t\tif (value > IdCompressor.maxClusterSize) {\n\t\t\tthrow new Error(\"Clusters must not exceed max cluster size.\");\n\t\t}\n\t\tthis.newClusterCapacity = value;\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},\n\t\t};\n\t\tthis.nextRangeBaseGenCount = this.localGenCount + 1;\n\t\treturn range;\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\tassert(range.ids.count > 0, 0x755 /* Malformed ID Range. */);\n\t\tconst { sessionId, ids } = range;\n\t\tconst { count, firstGenCount } = 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, this.clusterCapacity + 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 + this.clusterCapacity;\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 + // cluster capacity\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, this.clusterCapacity);\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 clusterCapacity = readNumber(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\t\tcompressor.clusterCapacity = clusterCapacity;\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.newClusterCapacity === other.newClusterCapacity &&\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"]}
@@ -0,0 +1,32 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { OpSpaceCompressedId, SessionSpaceCompressedId } from "./types";
6
+ /**
7
+ * A compressed ID that is stable and unique within the scope of network of compressors (i.e. a document).
8
+ * It can only be used/decompressed in the context of the originating document.
9
+ */
10
+ export type FinalCompressedId = number & {
11
+ readonly FinalCompressedId: "5d83d1e2-98b7-4e4e-a889-54c855cfa73d";
12
+ readonly OpNormalized: "9209432d-a959-4df7-b2ad-767ead4dbcae";
13
+ };
14
+ /**
15
+ * A compressed ID that is local to a session (can only be decompressed when paired with a SessionId).
16
+ * Internally, it should not be persisted outside a scope annotated with the originating SessionId in order to be unambiguous.
17
+ * If external persistence is needed (e.g. by a client), a StableId should be used instead.
18
+ */
19
+ export type LocalCompressedId = number & {
20
+ readonly LocalCompressedId: "6fccb42f-e2a4-4243-bd29-f13d12b9c6d1";
21
+ } & SessionSpaceCompressedId;
22
+ /**
23
+ * @returns true if the supplied ID is a final ID.
24
+ */
25
+ export declare function isFinalId(id: SessionSpaceCompressedId | OpSpaceCompressedId): id is FinalCompressedId;
26
+ /**
27
+ * A StableId which is suitable for use as a session identifier
28
+ */
29
+ export type NumericUuid = bigint & {
30
+ readonly NumericUuid: "be04dd4d-9d7e-4337-a833-eec64c61aa46";
31
+ };
32
+ //# sourceMappingURL=identifiers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identifiers.d.ts","sourceRoot":"","sources":["../src/identifiers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC;AAExE;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG;IACxC,QAAQ,CAAC,iBAAiB,EAAE,sCAAsC,CAAC;IAGnE,QAAQ,CAAC,YAAY,EAAE,sCAAsC,CAAC;CAC9D,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG;IACxC,QAAQ,CAAC,iBAAiB,EAAE,sCAAsC,CAAC;CACnE,GAAG,wBAAwB,CAAC;AAE7B;;GAEG;AACH,wBAAgB,SAAS,CACxB,EAAE,EAAE,wBAAwB,GAAG,mBAAmB,GAChD,EAAE,IAAI,iBAAiB,CAEzB;AAED;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG;IAClC,QAAQ,CAAC,WAAW,EAAE,sCAAsC,CAAC;CAC7D,CAAC"}
@@ -0,0 +1,11 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * @returns true if the supplied ID is a final ID.
7
+ */
8
+ export function isFinalId(id) {
9
+ return id >= 0;
10
+ }
11
+ //# sourceMappingURL=identifiers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identifiers.js","sourceRoot":"","sources":["../src/identifiers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAwBH;;GAEG;AACH,MAAM,UAAU,SAAS,CACxB,EAAkD;IAElD,OAAO,EAAE,IAAI,CAAC,CAAC;AAChB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { OpSpaceCompressedId, SessionSpaceCompressedId } from \"./types\";\n\n/**\n * A compressed ID that is stable and unique within the scope of network of compressors (i.e. a document).\n * It can only be used/decompressed in the context of the originating document.\n */\nexport type FinalCompressedId = number & {\n\treadonly FinalCompressedId: \"5d83d1e2-98b7-4e4e-a889-54c855cfa73d\";\n\n\t// Same brand as OpNormalizedCompressedId, as final IDs are always finally normalized\n\treadonly OpNormalized: \"9209432d-a959-4df7-b2ad-767ead4dbcae\";\n};\n\n/**\n * A compressed ID that is local to a session (can only be decompressed when paired with a SessionId).\n * Internally, it should not be persisted outside a scope annotated with the originating SessionId in order to be unambiguous.\n * If external persistence is needed (e.g. by a client), a StableId should be used instead.\n */\nexport type LocalCompressedId = number & {\n\treadonly LocalCompressedId: \"6fccb42f-e2a4-4243-bd29-f13d12b9c6d1\";\n} & SessionSpaceCompressedId; // Same brand as CompressedId, as local IDs are always locally normalized\n\n/**\n * @returns true if the supplied ID is a final ID.\n */\nexport function isFinalId(\n\tid: SessionSpaceCompressedId | OpSpaceCompressedId,\n): id is FinalCompressedId {\n\treturn id >= 0;\n}\n\n/**\n * A StableId which is suitable for use as a session identifier\n */\nexport type NumericUuid = bigint & {\n\treadonly NumericUuid: \"be04dd4d-9d7e-4337-a833-eec64c61aa46\";\n};\n"]}