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