@fluidframework/container-runtime 2.0.0-internal.6.1.1 → 2.0.0-internal.6.2.0

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 (363) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +4 -3
  3. package/dist/batchTracker.d.ts +1 -1
  4. package/dist/batchTracker.js +1 -1
  5. package/dist/batchTracker.js.map +1 -1
  6. package/dist/blobManager.d.ts +4 -20
  7. package/dist/blobManager.d.ts.map +1 -1
  8. package/dist/blobManager.js +47 -125
  9. package/dist/blobManager.js.map +1 -1
  10. package/dist/containerRuntime.d.ts +82 -14
  11. package/dist/containerRuntime.d.ts.map +1 -1
  12. package/dist/containerRuntime.js +236 -138
  13. package/dist/containerRuntime.js.map +1 -1
  14. package/dist/dataStore.d.ts.map +1 -1
  15. package/dist/dataStore.js +1 -2
  16. package/dist/dataStore.js.map +1 -1
  17. package/dist/dataStoreContext.d.ts.map +1 -1
  18. package/dist/dataStoreContext.js +4 -5
  19. package/dist/dataStoreContext.js.map +1 -1
  20. package/dist/dataStoreContexts.d.ts +1 -2
  21. package/dist/dataStoreContexts.d.ts.map +1 -1
  22. package/dist/dataStoreContexts.js.map +1 -1
  23. package/dist/dataStoreRegistry.js +2 -2
  24. package/dist/dataStoreRegistry.js.map +1 -1
  25. package/dist/dataStores.d.ts.map +1 -1
  26. package/dist/dataStores.js +4 -5
  27. package/dist/dataStores.js.map +1 -1
  28. package/dist/error.d.ts +14 -0
  29. package/dist/error.d.ts.map +1 -0
  30. package/dist/error.js +21 -0
  31. package/dist/error.js.map +1 -0
  32. package/dist/gc/garbageCollection.d.ts +1 -1
  33. package/dist/gc/garbageCollection.d.ts.map +1 -1
  34. package/dist/gc/garbageCollection.js +23 -5
  35. package/dist/gc/garbageCollection.js.map +1 -1
  36. package/dist/gc/gcConfigs.d.ts.map +1 -1
  37. package/dist/gc/gcConfigs.js +5 -3
  38. package/dist/gc/gcConfigs.js.map +1 -1
  39. package/dist/gc/gcDefinitions.d.ts +2 -0
  40. package/dist/gc/gcDefinitions.d.ts.map +1 -1
  41. package/dist/gc/gcDefinitions.js.map +1 -1
  42. package/dist/gc/gcTelemetry.d.ts.map +1 -1
  43. package/dist/gc/gcTelemetry.js +2 -0
  44. package/dist/gc/gcTelemetry.js.map +1 -1
  45. package/dist/id-compressor/appendOnlySortedMap.d.ts +8 -30
  46. package/dist/id-compressor/appendOnlySortedMap.d.ts.map +1 -1
  47. package/dist/id-compressor/appendOnlySortedMap.js +25 -67
  48. package/dist/id-compressor/appendOnlySortedMap.js.map +1 -1
  49. package/dist/id-compressor/finalSpace.d.ts +29 -0
  50. package/dist/id-compressor/finalSpace.d.ts.map +1 -0
  51. package/dist/id-compressor/finalSpace.js +62 -0
  52. package/dist/id-compressor/finalSpace.js.map +1 -0
  53. package/dist/id-compressor/idCompressor.d.ts +25 -250
  54. package/dist/id-compressor/idCompressor.d.ts.map +1 -1
  55. package/dist/id-compressor/idCompressor.js +385 -1149
  56. package/dist/id-compressor/idCompressor.js.map +1 -1
  57. package/dist/id-compressor/identifiers.d.ts +32 -0
  58. package/dist/id-compressor/identifiers.d.ts.map +1 -0
  59. package/dist/id-compressor/identifiers.js +15 -0
  60. package/dist/id-compressor/identifiers.js.map +1 -0
  61. package/dist/id-compressor/index.d.ts +5 -6
  62. package/dist/id-compressor/index.d.ts.map +1 -1
  63. package/dist/id-compressor/index.js +20 -26
  64. package/dist/id-compressor/index.js.map +1 -1
  65. package/dist/id-compressor/persistanceUtilities.d.ts +22 -0
  66. package/dist/id-compressor/persistanceUtilities.d.ts.map +1 -0
  67. package/dist/id-compressor/persistanceUtilities.js +43 -0
  68. package/dist/id-compressor/persistanceUtilities.js.map +1 -0
  69. package/dist/id-compressor/sessionSpaceNormalizer.d.ts +46 -0
  70. package/dist/id-compressor/sessionSpaceNormalizer.d.ts.map +1 -0
  71. package/dist/id-compressor/sessionSpaceNormalizer.js +80 -0
  72. package/dist/id-compressor/sessionSpaceNormalizer.js.map +1 -0
  73. package/dist/id-compressor/sessions.d.ts +115 -0
  74. package/dist/id-compressor/sessions.d.ts.map +1 -0
  75. package/dist/id-compressor/sessions.js +305 -0
  76. package/dist/id-compressor/sessions.js.map +1 -0
  77. package/dist/id-compressor/utilities.d.ts +49 -0
  78. package/dist/id-compressor/utilities.d.ts.map +1 -0
  79. package/dist/id-compressor/utilities.js +166 -0
  80. package/dist/id-compressor/utilities.js.map +1 -0
  81. package/dist/index.d.ts +3 -3
  82. package/dist/index.d.ts.map +1 -1
  83. package/dist/index.js +6 -4
  84. package/dist/index.js.map +1 -1
  85. package/dist/opLifecycle/opCompressor.d.ts.map +1 -1
  86. package/dist/opLifecycle/opCompressor.js +1 -2
  87. package/dist/opLifecycle/opCompressor.js.map +1 -1
  88. package/dist/opLifecycle/opSplitter.d.ts.map +1 -1
  89. package/dist/opLifecycle/opSplitter.js +2 -3
  90. package/dist/opLifecycle/opSplitter.js.map +1 -1
  91. package/dist/opLifecycle/outbox.d.ts +1 -0
  92. package/dist/opLifecycle/outbox.d.ts.map +1 -1
  93. package/dist/opLifecycle/outbox.js +10 -11
  94. package/dist/opLifecycle/outbox.js.map +1 -1
  95. package/dist/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
  96. package/dist/opLifecycle/remoteMessageProcessor.js +11 -5
  97. package/dist/opLifecycle/remoteMessageProcessor.js.map +1 -1
  98. package/dist/packageVersion.d.ts +1 -1
  99. package/dist/packageVersion.js +1 -1
  100. package/dist/packageVersion.js.map +1 -1
  101. package/dist/pendingStateManager.d.ts +12 -5
  102. package/dist/pendingStateManager.d.ts.map +1 -1
  103. package/dist/pendingStateManager.js +24 -10
  104. package/dist/pendingStateManager.js.map +1 -1
  105. package/dist/scheduleManager.d.ts.map +1 -1
  106. package/dist/scheduleManager.js +4 -5
  107. package/dist/scheduleManager.js.map +1 -1
  108. package/dist/summary/index.d.ts +2 -2
  109. package/dist/summary/index.d.ts.map +1 -1
  110. package/dist/summary/index.js +2 -1
  111. package/dist/summary/index.js.map +1 -1
  112. package/dist/summary/orderedClientElection.d.ts +1 -2
  113. package/dist/summary/orderedClientElection.d.ts.map +1 -1
  114. package/dist/summary/orderedClientElection.js +2 -3
  115. package/dist/summary/orderedClientElection.js.map +1 -1
  116. package/dist/summary/runningSummarizer.d.ts +27 -4
  117. package/dist/summary/runningSummarizer.d.ts.map +1 -1
  118. package/dist/summary/runningSummarizer.js +237 -66
  119. package/dist/summary/runningSummarizer.js.map +1 -1
  120. package/dist/summary/summarizer.d.ts +6 -5
  121. package/dist/summary/summarizer.d.ts.map +1 -1
  122. package/dist/summary/summarizer.js +70 -67
  123. package/dist/summary/summarizer.js.map +1 -1
  124. package/dist/summary/summarizerClientElection.d.ts +1 -1
  125. package/dist/summary/summarizerClientElection.d.ts.map +1 -1
  126. package/dist/summary/summarizerClientElection.js.map +1 -1
  127. package/dist/summary/summarizerTypes.d.ts +38 -25
  128. package/dist/summary/summarizerTypes.d.ts.map +1 -1
  129. package/dist/summary/summarizerTypes.js.map +1 -1
  130. package/dist/summary/summaryCollection.d.ts +1 -2
  131. package/dist/summary/summaryCollection.d.ts.map +1 -1
  132. package/dist/summary/summaryCollection.js.map +1 -1
  133. package/dist/summary/summaryGenerator.d.ts +9 -3
  134. package/dist/summary/summaryGenerator.d.ts.map +1 -1
  135. package/dist/summary/summaryGenerator.js +42 -38
  136. package/dist/summary/summaryGenerator.js.map +1 -1
  137. package/dist/summary/summaryManager.d.ts +7 -6
  138. package/dist/summary/summaryManager.d.ts.map +1 -1
  139. package/dist/summary/summaryManager.js +22 -15
  140. package/dist/summary/summaryManager.js.map +1 -1
  141. package/lib/batchTracker.d.ts +1 -1
  142. package/lib/batchTracker.js +1 -1
  143. package/lib/batchTracker.js.map +1 -1
  144. package/lib/blobManager.d.ts +4 -20
  145. package/lib/blobManager.d.ts.map +1 -1
  146. package/lib/blobManager.js +46 -124
  147. package/lib/blobManager.js.map +1 -1
  148. package/lib/containerRuntime.d.ts +82 -14
  149. package/lib/containerRuntime.d.ts.map +1 -1
  150. package/lib/containerRuntime.js +223 -123
  151. package/lib/containerRuntime.js.map +1 -1
  152. package/lib/dataStore.d.ts.map +1 -1
  153. package/lib/dataStore.js +1 -2
  154. package/lib/dataStore.js.map +1 -1
  155. package/lib/dataStoreContext.d.ts.map +1 -1
  156. package/lib/dataStoreContext.js +1 -2
  157. package/lib/dataStoreContext.js.map +1 -1
  158. package/lib/dataStoreContexts.d.ts +1 -2
  159. package/lib/dataStoreContexts.d.ts.map +1 -1
  160. package/lib/dataStoreContexts.js.map +1 -1
  161. package/lib/dataStoreRegistry.js +1 -1
  162. package/lib/dataStoreRegistry.js.map +1 -1
  163. package/lib/dataStores.d.ts.map +1 -1
  164. package/lib/dataStores.js +1 -2
  165. package/lib/dataStores.js.map +1 -1
  166. package/lib/error.d.ts +14 -0
  167. package/lib/error.d.ts.map +1 -0
  168. package/lib/error.js +17 -0
  169. package/lib/error.js.map +1 -0
  170. package/lib/gc/garbageCollection.d.ts +1 -1
  171. package/lib/gc/garbageCollection.d.ts.map +1 -1
  172. package/lib/gc/garbageCollection.js +22 -4
  173. package/lib/gc/garbageCollection.js.map +1 -1
  174. package/lib/gc/gcConfigs.d.ts.map +1 -1
  175. package/lib/gc/gcConfigs.js +3 -1
  176. package/lib/gc/gcConfigs.js.map +1 -1
  177. package/lib/gc/gcDefinitions.d.ts +2 -0
  178. package/lib/gc/gcDefinitions.d.ts.map +1 -1
  179. package/lib/gc/gcDefinitions.js.map +1 -1
  180. package/lib/gc/gcTelemetry.d.ts.map +1 -1
  181. package/lib/gc/gcTelemetry.js +2 -0
  182. package/lib/gc/gcTelemetry.js.map +1 -1
  183. package/lib/id-compressor/appendOnlySortedMap.d.ts +8 -30
  184. package/lib/id-compressor/appendOnlySortedMap.d.ts.map +1 -1
  185. package/lib/id-compressor/appendOnlySortedMap.js +24 -65
  186. package/lib/id-compressor/appendOnlySortedMap.js.map +1 -1
  187. package/lib/id-compressor/finalSpace.d.ts +29 -0
  188. package/lib/id-compressor/finalSpace.d.ts.map +1 -0
  189. package/lib/id-compressor/finalSpace.js +58 -0
  190. package/lib/id-compressor/finalSpace.js.map +1 -0
  191. package/lib/id-compressor/idCompressor.d.ts +25 -250
  192. package/lib/id-compressor/idCompressor.d.ts.map +1 -1
  193. package/lib/id-compressor/idCompressor.js +381 -1139
  194. package/lib/id-compressor/idCompressor.js.map +1 -1
  195. package/lib/id-compressor/identifiers.d.ts +32 -0
  196. package/lib/id-compressor/identifiers.d.ts.map +1 -0
  197. package/lib/id-compressor/identifiers.js +11 -0
  198. package/lib/id-compressor/identifiers.js.map +1 -0
  199. package/lib/id-compressor/index.d.ts +5 -6
  200. package/lib/id-compressor/index.d.ts.map +1 -1
  201. package/lib/id-compressor/index.js +5 -6
  202. package/lib/id-compressor/index.js.map +1 -1
  203. package/lib/id-compressor/persistanceUtilities.d.ts +22 -0
  204. package/lib/id-compressor/persistanceUtilities.d.ts.map +1 -0
  205. package/lib/id-compressor/persistanceUtilities.js +34 -0
  206. package/lib/id-compressor/persistanceUtilities.js.map +1 -0
  207. package/lib/id-compressor/sessionSpaceNormalizer.d.ts +46 -0
  208. package/lib/id-compressor/sessionSpaceNormalizer.d.ts.map +1 -0
  209. package/lib/id-compressor/sessionSpaceNormalizer.js +76 -0
  210. package/lib/id-compressor/sessionSpaceNormalizer.js.map +1 -0
  211. package/lib/id-compressor/sessions.d.ts +115 -0
  212. package/lib/id-compressor/sessions.d.ts.map +1 -0
  213. package/lib/id-compressor/sessions.js +290 -0
  214. package/lib/id-compressor/sessions.js.map +1 -0
  215. package/lib/id-compressor/utilities.d.ts +49 -0
  216. package/lib/id-compressor/utilities.d.ts.map +1 -0
  217. package/lib/id-compressor/utilities.js +148 -0
  218. package/lib/id-compressor/utilities.js.map +1 -0
  219. package/lib/index.d.ts +3 -3
  220. package/lib/index.d.ts.map +1 -1
  221. package/lib/index.js +2 -2
  222. package/lib/index.js.map +1 -1
  223. package/lib/opLifecycle/opCompressor.d.ts.map +1 -1
  224. package/lib/opLifecycle/opCompressor.js +1 -2
  225. package/lib/opLifecycle/opCompressor.js.map +1 -1
  226. package/lib/opLifecycle/opSplitter.d.ts.map +1 -1
  227. package/lib/opLifecycle/opSplitter.js +1 -2
  228. package/lib/opLifecycle/opSplitter.js.map +1 -1
  229. package/lib/opLifecycle/outbox.d.ts +1 -0
  230. package/lib/opLifecycle/outbox.d.ts.map +1 -1
  231. package/lib/opLifecycle/outbox.js +6 -7
  232. package/lib/opLifecycle/outbox.js.map +1 -1
  233. package/lib/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
  234. package/lib/opLifecycle/remoteMessageProcessor.js +12 -6
  235. package/lib/opLifecycle/remoteMessageProcessor.js.map +1 -1
  236. package/lib/packageVersion.d.ts +1 -1
  237. package/lib/packageVersion.js +1 -1
  238. package/lib/packageVersion.js.map +1 -1
  239. package/lib/pendingStateManager.d.ts +12 -5
  240. package/lib/pendingStateManager.d.ts.map +1 -1
  241. package/lib/pendingStateManager.js +21 -7
  242. package/lib/pendingStateManager.js.map +1 -1
  243. package/lib/scheduleManager.d.ts.map +1 -1
  244. package/lib/scheduleManager.js +1 -2
  245. package/lib/scheduleManager.js.map +1 -1
  246. package/lib/summary/index.d.ts +2 -2
  247. package/lib/summary/index.d.ts.map +1 -1
  248. package/lib/summary/index.js +1 -1
  249. package/lib/summary/index.js.map +1 -1
  250. package/lib/summary/orderedClientElection.d.ts +1 -2
  251. package/lib/summary/orderedClientElection.d.ts.map +1 -1
  252. package/lib/summary/orderedClientElection.js +1 -2
  253. package/lib/summary/orderedClientElection.js.map +1 -1
  254. package/lib/summary/runningSummarizer.d.ts +27 -4
  255. package/lib/summary/runningSummarizer.d.ts.map +1 -1
  256. package/lib/summary/runningSummarizer.js +237 -66
  257. package/lib/summary/runningSummarizer.js.map +1 -1
  258. package/lib/summary/summarizer.d.ts +6 -5
  259. package/lib/summary/summarizer.d.ts.map +1 -1
  260. package/lib/summary/summarizer.js +68 -65
  261. package/lib/summary/summarizer.js.map +1 -1
  262. package/lib/summary/summarizerClientElection.d.ts +1 -1
  263. package/lib/summary/summarizerClientElection.d.ts.map +1 -1
  264. package/lib/summary/summarizerClientElection.js.map +1 -1
  265. package/lib/summary/summarizerTypes.d.ts +38 -25
  266. package/lib/summary/summarizerTypes.d.ts.map +1 -1
  267. package/lib/summary/summarizerTypes.js.map +1 -1
  268. package/lib/summary/summaryCollection.d.ts +1 -2
  269. package/lib/summary/summaryCollection.d.ts.map +1 -1
  270. package/lib/summary/summaryCollection.js.map +1 -1
  271. package/lib/summary/summaryGenerator.d.ts +9 -3
  272. package/lib/summary/summaryGenerator.d.ts.map +1 -1
  273. package/lib/summary/summaryGenerator.js +43 -39
  274. package/lib/summary/summaryGenerator.js.map +1 -1
  275. package/lib/summary/summaryManager.d.ts +7 -6
  276. package/lib/summary/summaryManager.d.ts.map +1 -1
  277. package/lib/summary/summaryManager.js +23 -16
  278. package/lib/summary/summaryManager.js.map +1 -1
  279. package/package.json +27 -24
  280. package/src/batchTracker.ts +1 -1
  281. package/src/blobManager.ts +57 -146
  282. package/src/containerRuntime.ts +331 -158
  283. package/src/dataStore.ts +1 -2
  284. package/src/dataStoreContext.ts +3 -6
  285. package/src/dataStoreContexts.ts +1 -2
  286. package/src/dataStoreRegistry.ts +1 -1
  287. package/src/dataStores.ts +3 -5
  288. package/src/error.ts +18 -0
  289. package/src/gc/garbageCollection.ts +38 -5
  290. package/src/gc/gcConfigs.ts +4 -2
  291. package/src/gc/gcDefinitions.ts +2 -0
  292. package/src/gc/gcTelemetry.ts +2 -0
  293. package/src/id-compressor/appendOnlySortedMap.ts +25 -86
  294. package/src/id-compressor/finalSpace.ts +67 -0
  295. package/src/id-compressor/idCompressor.ts +455 -1681
  296. package/src/id-compressor/identifiers.ts +42 -0
  297. package/src/id-compressor/index.ts +11 -20
  298. package/src/id-compressor/persistanceUtilities.ts +58 -0
  299. package/src/id-compressor/sessionSpaceNormalizer.ts +83 -0
  300. package/src/id-compressor/sessions.ts +405 -0
  301. package/src/id-compressor/utilities.ts +187 -0
  302. package/src/index.ts +7 -1
  303. package/src/opLifecycle/opCompressor.ts +1 -2
  304. package/src/opLifecycle/opSplitter.ts +4 -4
  305. package/src/opLifecycle/outbox.ts +13 -10
  306. package/src/opLifecycle/remoteMessageProcessor.ts +19 -6
  307. package/src/packageVersion.ts +1 -1
  308. package/src/pendingStateManager.ts +49 -27
  309. package/src/scheduleManager.ts +5 -4
  310. package/src/summary/index.ts +3 -1
  311. package/src/summary/orderedClientElection.ts +6 -4
  312. package/src/summary/runningSummarizer.ts +276 -95
  313. package/src/summary/summarizer.ts +22 -12
  314. package/src/summary/summarizerClientElection.ts +1 -1
  315. package/src/summary/summarizerTypes.ts +40 -25
  316. package/src/summary/summaryCollection.ts +1 -2
  317. package/src/summary/summaryGenerator.ts +49 -52
  318. package/src/summary/summaryManager.ts +33 -11
  319. package/dist/id-compressor/idRange.d.ts +0 -11
  320. package/dist/id-compressor/idRange.d.ts.map +0 -1
  321. package/dist/id-compressor/idRange.js +0 -29
  322. package/dist/id-compressor/idRange.js.map +0 -1
  323. package/dist/id-compressor/numericUuid.d.ts +0 -59
  324. package/dist/id-compressor/numericUuid.d.ts.map +0 -1
  325. package/dist/id-compressor/numericUuid.js +0 -325
  326. package/dist/id-compressor/numericUuid.js.map +0 -1
  327. package/dist/id-compressor/sessionIdNormalizer.d.ts +0 -138
  328. package/dist/id-compressor/sessionIdNormalizer.d.ts.map +0 -1
  329. package/dist/id-compressor/sessionIdNormalizer.js +0 -483
  330. package/dist/id-compressor/sessionIdNormalizer.js.map +0 -1
  331. package/dist/id-compressor/utils.d.ts +0 -57
  332. package/dist/id-compressor/utils.d.ts.map +0 -1
  333. package/dist/id-compressor/utils.js +0 -90
  334. package/dist/id-compressor/utils.js.map +0 -1
  335. package/dist/id-compressor/uuidUtilities.d.ts +0 -28
  336. package/dist/id-compressor/uuidUtilities.d.ts.map +0 -1
  337. package/dist/id-compressor/uuidUtilities.js +0 -104
  338. package/dist/id-compressor/uuidUtilities.js.map +0 -1
  339. package/lib/id-compressor/idRange.d.ts +0 -11
  340. package/lib/id-compressor/idRange.d.ts.map +0 -1
  341. package/lib/id-compressor/idRange.js +0 -25
  342. package/lib/id-compressor/idRange.js.map +0 -1
  343. package/lib/id-compressor/numericUuid.d.ts +0 -59
  344. package/lib/id-compressor/numericUuid.d.ts.map +0 -1
  345. package/lib/id-compressor/numericUuid.js +0 -315
  346. package/lib/id-compressor/numericUuid.js.map +0 -1
  347. package/lib/id-compressor/sessionIdNormalizer.d.ts +0 -138
  348. package/lib/id-compressor/sessionIdNormalizer.d.ts.map +0 -1
  349. package/lib/id-compressor/sessionIdNormalizer.js +0 -479
  350. package/lib/id-compressor/sessionIdNormalizer.js.map +0 -1
  351. package/lib/id-compressor/utils.d.ts +0 -57
  352. package/lib/id-compressor/utils.d.ts.map +0 -1
  353. package/lib/id-compressor/utils.js +0 -79
  354. package/lib/id-compressor/utils.js.map +0 -1
  355. package/lib/id-compressor/uuidUtilities.d.ts +0 -28
  356. package/lib/id-compressor/uuidUtilities.d.ts.map +0 -1
  357. package/lib/id-compressor/uuidUtilities.js +0 -96
  358. package/lib/id-compressor/uuidUtilities.js.map +0 -1
  359. package/src/id-compressor/idRange.ts +0 -35
  360. package/src/id-compressor/numericUuid.ts +0 -383
  361. package/src/id-compressor/sessionIdNormalizer.ts +0 -609
  362. package/src/id-compressor/utils.ts +0 -114
  363. package/src/id-compressor/uuidUtilities.ts +0 -120
@@ -2,105 +2,78 @@
2
2
  * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
3
  * Licensed under the MIT License.
4
4
  */
5
- /* eslint-disable @typescript-eslint/restrict-plus-operands */
6
- import { assert } from "@fluidframework/common-utils";
7
- import BTree from "sorted-btree";
5
+ import { assert, bufferToString, stringToBuffer } from "@fluidframework/common-utils";
6
+ import { initialClusterCapacity, } from "@fluidframework/runtime-definitions";
8
7
  import { createChildLogger } from "@fluidframework/telemetry-utils";
9
- import { hasAtLeastLength, compareFiniteNumbers, compareFiniteNumbersReversed, compareMaps, compareStrings, fail, setPropertyIfDefined, } from "./utils";
10
- import { assertIsStableId, isStableId } from "./uuidUtilities";
11
- import { AppendOnlySortedMap } from "./appendOnlySortedMap";
12
- import { getIds } from "./idRange";
13
- import { numericUuidEquals, getPositiveDelta, incrementUuid, numericUuidFromStableId, stableIdFromNumericUuid, ensureSessionUuid, } from "./numericUuid";
14
- import { SessionIdNormalizer } from "./sessionIdNormalizer";
8
+ import { isFinalId } from "./identifiers";
9
+ import { createSessionId, localIdFromGenCount, genCountFromLocalId, numericUuidFromStableId, offsetNumericUuid, stableIdFromNumericUuid, subtractNumericUuids, } from "./utilities";
10
+ import { readBoolean, readNumber, readNumericUuid, writeBoolean, writeNumber, writeNumericUuid, } from "./persistanceUtilities";
11
+ import { getAlignedLocal, getAlignedFinal, lastFinalizedLocal, Session, Sessions, } from "./sessions";
12
+ import { SessionSpaceNormalizer } from "./sessionSpaceNormalizer";
13
+ import { FinalSpace } from "./finalSpace";
15
14
  /**
16
- * Roughly equates to a minimum of 1M sessions before we start allocating 64 bit IDs.
17
- * This value must *NOT* change without careful consideration to compatibility.
15
+ * The version of IdCompressor that is currently persisted.
16
+ * This should not be changed without careful consideration to compatibility.
18
17
  */
19
- export const defaultClusterCapacity = 512;
18
+ const currentWrittenVersion = 1;
20
19
  /**
21
- * The base UUID for the reserved id cluster.
22
- * This should not be changed without consideration to compatibility.
23
- */
24
- const reservedSessionId = ensureSessionUuid(assertIsStableId("decaf40b-3c1a-47f8-a7a1-e8461ddb69ce"));
25
- /**
26
- * @returns true if the supplied ID is a final ID.
27
- */
28
- export function isFinalId(id) {
29
- return id >= 0;
30
- }
31
- /**
32
- * @returns true if the supplied ID is a local ID.
33
- */
34
- export function isLocalId(id) {
35
- return id < 0;
36
- }
37
- /** Prepended to all keys in {@link IdCompressor.clustersAndOverridesInversion} that are override strings and not valid `StableIds` */
38
- const nonStableOverridePrefix = "\ue15e"; // A character in the Private Use Area of the BMP (https://en.wikipedia.org/wiki/Private_Use_Areas)
39
- /**
40
- * See {@link IIdCompressor}
20
+ * See {@link IIdCompressor} and {@link IIdCompressorCore}
41
21
  */
42
22
  export class IdCompressor {
43
- /**
44
- * @param localSessionId - the `IdCompressor`'s current local session ID.
45
- * {@link generateStableId}.
46
- */
47
- constructor(localSessionId, logger) {
48
- this.localSessionId = localSessionId;
49
- /**
50
- * Trivially reach consensus on default cluster size and reserved IDs.
51
- * These initial values must *NOT* change without careful consideration to compatibility.
52
- */
53
- this.newClusterCapacity = defaultClusterCapacity;
54
- /**
55
- * Session ID -\> data about the session's current cluster.
56
- * Sessions are mutable, and thus should only be created via `createSession`.
57
- */
58
- this.sessions = new Map();
59
- /**
60
- * The base final ID of the next cluster to be created.
61
- */
62
- this.nextClusterBaseFinalId = 0;
63
- /**
64
- * Total number of IDs created locally during the current session.
65
- */
66
- this.localIdCount = 0;
67
- /**
68
- * Maps local IDs to override strings. This will contain an entry for every override assigned to a local ID generated during
69
- * the current session, and retains entries for the lifetime of this compressor.
70
- */
71
- this.localOverrides = new AppendOnlySortedMap(compareFiniteNumbersReversed);
72
- /**
73
- * Maps local IDs to the final ID they are associated with (if any), and maps final IDs to the corresponding local ID (if any).
74
- * This is used to efficiently compute normalization. This map can be thought of as mapping ranges of "optimistic uncertainty"
75
- * (local IDs) to the result of consensus (reserved ranges of final IDs, a.k.a. clusters). Any given range of local IDs
76
- * does not necessarily span an entire cluster, as some session-space IDs may be allocated *after* a cluster has been allocated
77
- * but before it is full. In this case, there is no uncertainty, as the range of final IDs was reserved when the cluster was created.
78
- * However, there is always a range of local IDs with size \>= 1 associated with the beginning of every cluster, as clusters are only
79
- * created *after* they are needed and thus there is some period of uncertainty after local IDs have been handed out but before the
80
- * range containing them has been finalized. There may also be ranges of local IDs that do not start at the beginning of a
81
- * cluster; this happens when a cluster is expanded instead of allocating a new one.
82
- * Additionally, session space IDs associated with an override string will also always be local IDs, because there is uncertainty as
83
- * to whether another client simultaneously allocated the same override and could get sequenced first (a.k.a. unification) and its
84
- * final ID would be associated with that override.
85
- * See `SessionIdNormalizer` for more.
86
- */
87
- this.sessionIdNormalizer = new SessionIdNormalizer();
88
- /**
89
- * Contains entries for cluster base UUIDs and override strings (both local and final).
90
- * As a performance optimization, entries for finalized strings also include the containing cluster object.
91
- * This can be viewed as three separate tables: the inverse table for `localOverrides`, the inverse table for the union of all
92
- * the overrides of the clusters in `finalIdToCluster`, and the inverse lookup of cluster base UUIDs to their clusters.
93
- * This is unified as a performance optimization, as the common case does not have overridden IDs. It is a btree due to the need
94
- * to make range queries.
95
- */
96
- this.clustersAndOverridesInversion = new BTree(undefined, compareStrings);
97
- /**
98
- * Maps the first final ID in a cluster to its owning cluster.
99
- * Can be searched in O(log n) to determine clusters for any final ID.
100
- */
101
- this.finalIdToCluster = new AppendOnlySortedMap(compareFiniteNumbers);
102
- this.localSession = this.createSession(localSessionId);
103
- this.logger = createChildLogger({ logger });
23
+ // -----------------------
24
+ constructor(localSessionIdOrDeserialized, logger) {
25
+ this.logger = logger;
26
+ this.normalizer = new SessionSpaceNormalizer();
27
+ // The number of IDs generated by the local session
28
+ this.localGenCount = 0;
29
+ // -----------------------
30
+ // ----- Final state -----
31
+ // The gen count to be annotated on the range returned by the next call to `takeNextCreationRange`.
32
+ // This is updated to be equal to `generatedIdCount` + 1 each time it is called.
33
+ this.nextRangeBaseGenCount = 1;
34
+ // The capacity of the next cluster to be created
35
+ this.newClusterCapacity = initialClusterCapacity;
36
+ this.sessions = new Sessions();
37
+ this.finalSpace = new FinalSpace();
38
+ // -----------------------
39
+ // ----- Telemetry state -----
40
+ // The number of local IDs generated since the last telemetry was sent.
41
+ this.telemetryLocalIdCount = 0;
42
+ // The number of eager final IDs generated since the last telemetry was sent.
43
+ this.telemetryEagerFinalIdCount = 0;
44
+ if (typeof localSessionIdOrDeserialized === "string") {
45
+ this.localSessionId = localSessionIdOrDeserialized;
46
+ this.localSession = this.sessions.getOrCreate(localSessionIdOrDeserialized);
47
+ }
48
+ else {
49
+ // Deserialize case
50
+ this.sessions = localSessionIdOrDeserialized;
51
+ // As policy, the first session is always the local session. Preserve this invariant
52
+ // during deserialization.
53
+ const firstSession = localSessionIdOrDeserialized.sessions().next();
54
+ assert(!firstSession.done, 0x754 /* First session must be present. */);
55
+ this.localSession = firstSession.value;
56
+ this.localSessionId = stableIdFromNumericUuid(this.localSession.sessionUuid);
57
+ }
58
+ }
59
+ static create(sessionIdOrLogger, loggerOrUndefined) {
60
+ let localSessionId;
61
+ let logger;
62
+ if (sessionIdOrLogger === undefined) {
63
+ localSessionId = createSessionId();
64
+ }
65
+ else {
66
+ if (typeof sessionIdOrLogger === "string") {
67
+ localSessionId = sessionIdOrLogger;
68
+ logger = loggerOrUndefined;
69
+ }
70
+ else {
71
+ localSessionId = createSessionId();
72
+ logger = loggerOrUndefined;
73
+ }
74
+ }
75
+ const compressor = new IdCompressor(localSessionId, logger === undefined ? undefined : createChildLogger({ logger }));
76
+ return compressor;
104
77
  }
105
78
  /**
106
79
  * The size of each newly created ID cluster.
@@ -113,184 +86,77 @@ export class IdCompressor {
113
86
  * `IdCompressor.maxClusterSize`.
114
87
  */
115
88
  set clusterCapacity(value) {
116
- assert(value > 0, 0x481 /* Clusters must have a positive capacity */);
117
- assert(value <= IdCompressor.maxClusterSize, 0x482 /* Clusters must not exceed max cluster size */);
89
+ if (value <= 0) {
90
+ throw new Error("Clusters must have a positive capacity.");
91
+ }
92
+ if (value > IdCompressor.maxClusterSize) {
93
+ throw new Error("Clusters must not exceed max cluster size.");
94
+ }
118
95
  this.newClusterCapacity = value;
119
96
  }
120
- /**
121
- * Creates a session object for the supplied ID.
122
- * Must only be called once per ID.
123
- * @param sessionId - the ID for the session
124
- * @returns the session object for the supplied ID
125
- */
126
- createSession(sessionId) {
127
- assert(!this.clustersAndOverridesInversion.has(sessionId), 0x484 /* Attempted to create duplicate session */);
128
- const existingSession = this.sessions.get(sessionId);
129
- if (existingSession !== undefined) {
130
- fail("createSession must only be called once for each session ID.");
131
- }
132
- const sessionUuid = numericUuidFromStableId(sessionId);
133
- const session = {
134
- sessionUuid,
135
- currentClusterDetails: undefined,
136
- lastFinalizedLocalId: undefined,
137
- };
138
- this.sessions.set(sessionId, session);
139
- return session;
97
+ generateCompressedId() {
98
+ this.localGenCount++;
99
+ const lastCluster = this.localSession.getLastCluster();
100
+ if (lastCluster === undefined) {
101
+ this.telemetryLocalIdCount++;
102
+ return this.generateNextLocalId();
103
+ }
104
+ // If there exists a cluster of final IDs already claimed by the local session that still has room in it,
105
+ // it is known prior to range sequencing what a local ID's corresponding final ID will be.
106
+ // In this case, it is safe to return the final ID immediately. This is guaranteed to be safe because
107
+ // any op that the local session sends that contains one of those final IDs are guaranteed to arrive to
108
+ // collaborators *after* the one containing the creation range.
109
+ const clusterOffset = this.localGenCount - genCountFromLocalId(lastCluster.baseLocalId);
110
+ if (lastCluster.capacity > clusterOffset) {
111
+ this.telemetryEagerFinalIdCount++;
112
+ // Space in the cluster: eager final
113
+ return (lastCluster.baseFinalId +
114
+ clusterOffset);
115
+ }
116
+ // No space in the cluster, return next local
117
+ this.telemetryLocalIdCount++;
118
+ return this.generateNextLocalId();
119
+ }
120
+ generateNextLocalId() {
121
+ // Must tell the normalizer that we generated a local ID
122
+ this.normalizer.addLocalRange(this.localGenCount, 1);
123
+ return localIdFromGenCount(this.localGenCount);
140
124
  }
141
- /**
142
- * Returns a range of local IDs created by this session in a format for sending to the server for finalizing.
143
- * The range will include all local IDs generated via calls to `generateCompressedId` since the last time this method was called.
144
- * @returns the range of session-local IDs, which may be empty. This range must be sent to the server for ordering before
145
- * it is finalized. Ranges must be sent to the server in the order that they are taken via calls to this method.
146
- */
147
125
  takeNextCreationRange() {
148
- const lastLocalInRange = -this.localIdCount;
149
- const lastTakenNormalized = this.lastTakenLocalId ?? 0;
150
- assert(lastLocalInRange <= lastTakenNormalized, 0x485 /* Inconsistent local ID state */);
151
- let ids;
152
- if (lastLocalInRange !== lastTakenNormalized) {
153
- const firstLocalInRange = (lastTakenNormalized - 1);
154
- const overrides = [
155
- ...this.localOverrides.getRange((lastTakenNormalized - 1), lastLocalInRange),
156
- ];
157
- if (hasAtLeastLength(overrides, 1)) {
158
- assert(overrides[0][0] <= firstLocalInRange, 0x486 /* Inconsistent override state */);
159
- assert(overrides[overrides.length - 1][0] >= lastLocalInRange, 0x487 /* Inconsistent override state */);
160
- ids = {
161
- overrides,
162
- };
163
- const first = firstLocalInRange === overrides[0][0] ? undefined : firstLocalInRange;
164
- const last = lastLocalInRange === overrides[overrides.length - 1][0]
165
- ? undefined
166
- : lastLocalInRange;
167
- setPropertyIfDefined(first, ids, "first");
168
- setPropertyIfDefined(last, ids, "last");
169
- }
170
- else {
171
- ids = {
172
- first: firstLocalInRange,
173
- last: lastLocalInRange,
174
- };
175
- }
176
- this.lastTakenLocalId = lastLocalInRange;
177
- }
178
- const range = { sessionId: this.localSessionId };
179
- if (ids === undefined) {
180
- return range;
126
+ const count = this.localGenCount - (this.nextRangeBaseGenCount - 1);
127
+ if (count === 0) {
128
+ return {
129
+ sessionId: this.localSessionId,
130
+ };
181
131
  }
182
- assert(this.lastTakenLocalId === -this.localIdCount &&
183
- this.lastTakenLocalId !== lastTakenNormalized, 0x488 /* Non-empty range must properly consume local IDs */);
184
- range.ids = ids;
132
+ const range = {
133
+ sessionId: this.localSessionId,
134
+ ids: {
135
+ firstGenCount: this.nextRangeBaseGenCount,
136
+ count,
137
+ },
138
+ };
139
+ this.nextRangeBaseGenCount = this.localGenCount + 1;
185
140
  return range;
186
141
  }
187
- /**
188
- * Finalizes the supplied range of IDs (which may be from either a remote or local session).
189
- * @param range - the range of session-local IDs to finalize.
190
- */
191
142
  finalizeCreationRange(range) {
192
- const { sessionId } = range;
193
- const isLocal = sessionId === this.localSessionId;
194
- const session = this.sessions.get(sessionId) ?? this.createSession(sessionId);
195
- const ids = getIds(range);
196
- if (ids === undefined) {
143
+ // Check if the range has IDs
144
+ if (range.ids === undefined) {
197
145
  return;
198
146
  }
199
- const { currentClusterDetails } = session;
200
- const { cluster: currentCluster, clusterBase: currentBaseFinalId } = currentClusterDetails ?? {
201
- cluster: undefined,
202
- clusterBase: undefined,
203
- };
204
- const currentClusterExists = currentCluster !== undefined && currentBaseFinalId !== undefined;
205
- const normalizedLastFinalizedLocal = session.lastFinalizedLocalId ?? 0;
206
- const { first: newFirstFinalizedLocal, last: newLastFinalizedLocal } = ids;
207
- assert(newFirstFinalizedLocal === normalizedLastFinalizedLocal - 1, 0x489 /* Ranges finalized out of order. */);
208
- // The total number of session-local IDs to finalize
209
- const finalizeCount = normalizedLastFinalizedLocal - newLastFinalizedLocal;
210
- assert(finalizeCount >= 1, 0x48a /* Cannot finalize an empty range. */);
211
- let eagerFinalIdCount = 0;
212
- let initialClusterCount = 0;
213
- let remainingCount = finalizeCount;
214
- let newBaseUuid;
215
- if (currentClusterExists) {
216
- if (isLocal) {
217
- const lastKnownFinal = this.sessionIdNormalizer.getLastFinalId() ??
218
- fail("Cluster exists but normalizer does not have an entry for it.");
219
- const lastAlignedFinalInCluster = (currentBaseFinalId +
220
- Math.min(currentCluster.count + finalizeCount, currentCluster.capacity) -
221
- 1);
222
- if (lastAlignedFinalInCluster > lastKnownFinal) {
223
- this.sessionIdNormalizer.addFinalIds((lastKnownFinal + 1), lastAlignedFinalInCluster, currentCluster);
224
- }
225
- }
226
- initialClusterCount = currentCluster.count;
227
- const remainingCapacity = currentCluster.capacity - initialClusterCount;
228
- const overflow = remainingCount - remainingCapacity;
229
- const hasRoom = overflow <= 0;
230
- if (hasRoom || currentBaseFinalId === this.finalIdToCluster.maxKey()) {
231
- currentCluster.count += remainingCount;
232
- eagerFinalIdCount = remainingCount;
233
- remainingCount = 0;
234
- // The common case is that there is room in the cluster, and the new final IDs can simply be added to it
235
- if (!hasRoom) {
236
- // The cluster is full but is the last in the list of clusters.
237
- // This allows it to be expanded instead of allocating a new one.
238
- const expansionAmount = this.newClusterCapacity + overflow;
239
- const previousCapacity = currentCluster.capacity;
240
- currentCluster.capacity += expansionAmount;
241
- this.nextClusterBaseFinalId = (this.nextClusterBaseFinalId +
242
- expansionAmount);
243
- assert(this.nextClusterBaseFinalId < Number.MAX_SAFE_INTEGER, 0x48b /* The number of allocated final IDs must not exceed the JS maximum safe integer. */);
244
- this.checkClusterForCollision(currentCluster);
245
- if (isLocal) {
246
- // Example with cluster size of 3:
247
- // Ids generated so far: -1 1 2 -4 -5 <-- note positive numbers are eager finals
248
- // Cluster: [ 0 1 2 ]
249
- // ~ finalizing happens, causing expansion of 2 (overflow) + 3 (cluster capacity) ~
250
- // Cluster: [ 0 1 2 3 4 _ _ _ ]
251
- // corresponding locals: -1 -4 -5
252
- // lastFinalizedLocalId^ ^newLastFinalizedLocalId = -5
253
- // overflow = 2: ----
254
- // localIdPivot^
255
- // lastFinalizedFinal^
256
- const newLastFinalizedFinal = (currentBaseFinalId +
257
- currentCluster.count -
258
- 1);
259
- assert(session.lastFinalizedLocalId !== undefined, 0x48c /* Cluster already exists for session but there is no finalized local ID */);
260
- const finalPivot = (newLastFinalizedFinal -
261
- overflow +
262
- 1);
263
- // Inform the normalizer of all IDs that we now know will end up being finalized into this cluster, including the ones
264
- // that were given out as locals (non-eager) because they exceeded the bounds of the current cluster before it was expanded.
265
- // It is safe to associate the unfinalized locals with their future final IDs even before the ranges for those locals are
266
- // actually finalized, because total order broadcast guarantees that any usage of those final IDs will be observed after
267
- // the finalization of the ranges.
268
- this.sessionIdNormalizer.registerFinalIdBlock(finalPivot, expansionAmount, currentCluster);
269
- this.logger?.sendTelemetryEvent({
270
- eventName: "RuntimeIdCompressor:ClusterExpansion",
271
- sessionId: this.localSessionId,
272
- previousCapacity,
273
- newCapacity: currentCluster.capacity,
274
- overflow,
275
- });
276
- }
277
- }
278
- }
279
- else {
280
- // The range cannot be fully allocated in the existing cluster, so allocate any space left in it and
281
- // form a new one by incrementing the previous baseUuid
282
- newBaseUuid = incrementUuid(currentCluster.baseUuid, currentCluster.capacity);
283
- currentCluster.count += remainingCapacity;
284
- remainingCount -= remainingCapacity;
285
- this.logger?.sendTelemetryEvent({
286
- eventName: "RuntimeIdCompressor:OverfilledCluster",
287
- sessionId: this.localSessionId,
288
- });
289
- }
290
- }
291
- else {
292
- // Session has never made a cluster, form a new one with the session UUID as the baseUuid
293
- newBaseUuid = session.sessionUuid;
147
+ assert(range.ids.count > 0, 0x755 /* Malformed ID Range. */);
148
+ const { sessionId, ids } = range;
149
+ const { count, firstGenCount } = ids;
150
+ const session = this.sessions.getOrCreate(sessionId);
151
+ const isLocal = session === this.localSession;
152
+ const rangeBaseLocal = localIdFromGenCount(firstGenCount);
153
+ let lastCluster = session.getLastCluster();
154
+ if (lastCluster === undefined) {
155
+ // This is the first cluster in the session space
156
+ if (rangeBaseLocal !== -1) {
157
+ throw new Error("Ranges finalized out of order.");
158
+ }
159
+ lastCluster = this.addEmptyCluster(session, this.clusterCapacity + count);
294
160
  if (isLocal) {
295
161
  this.logger?.sendTelemetryEvent({
296
162
  eventName: "RuntimeIdCompressor:FirstCluster",
@@ -298,951 +164,327 @@ export class IdCompressor {
298
164
  });
299
165
  }
300
166
  }
301
- // Finalizing a range results in one of three cases:
302
- // 1. All local IDs are finalized into a new cluster (because there was either never a cluster for that session, or the current
303
- // cluster for the session was full).
304
- // 2. All local IDs are finalized into the existing (current) cluster for the session.
305
- // 3. Local IDs are finalized into both the current cluster and a new one, as the current cluster did not have enough room.
306
- let newCluster;
307
- let newBaseFinalId;
308
- // The first local ID that will be finalized into a new cluster, if there is one.
309
- // This lets us quickly compare which cluster an override string will go into.
310
- let localIdPivot;
311
- // Need to make a new cluster
312
- if (newBaseUuid !== undefined) {
313
- if (remainingCount <= 0) {
314
- fail("Should not create an empty cluster.");
315
- }
316
- if (currentCluster !== undefined && currentCluster.capacity !== currentCluster.count) {
317
- fail("Cluster must be filled before another is allocated.");
318
- }
319
- newBaseFinalId = this.nextClusterBaseFinalId;
320
- const newCapacity = Math.max(this.newClusterCapacity, remainingCount);
321
- newCluster = {
322
- baseUuid: newBaseUuid,
323
- capacity: newCapacity,
324
- count: remainingCount,
325
- session,
326
- };
327
- const usedCapacity = finalizeCount - remainingCount;
328
- localIdPivot = (newFirstFinalizedLocal - usedCapacity);
329
- if (isLocal) {
330
- this.logger?.sendTelemetryEvent({
331
- eventName: "RuntimeIdCompressor:NewCluster",
332
- sessionId: this.localSessionId,
333
- clusterCapacity: newCapacity,
334
- clusterCount: remainingCount,
335
- });
336
- this.sessionIdNormalizer.registerFinalIdBlock(newBaseFinalId, newCluster.capacity, newCluster);
337
- }
338
- this.checkClusterForCollision(newCluster);
339
- this.clustersAndOverridesInversion.set(stableIdFromNumericUuid(newCluster.baseUuid), {
340
- clusterBase: newBaseFinalId,
341
- cluster: newCluster,
342
- });
343
- session.currentClusterDetails = { cluster: newCluster, clusterBase: newBaseFinalId };
344
- this.nextClusterBaseFinalId = (this.nextClusterBaseFinalId +
345
- newCluster.capacity);
346
- assert(this.nextClusterBaseFinalId < Number.MAX_SAFE_INTEGER, 0x48e /* The number of allocated final IDs must not exceed the JS maximum safe integer. */);
347
- this.finalIdToCluster.append(newBaseFinalId, newCluster);
167
+ const remainingCapacity = lastCluster.capacity - lastCluster.count;
168
+ if (lastCluster.baseLocalId - lastCluster.count !== rangeBaseLocal) {
169
+ throw new Error("Ranges finalized out of order.");
348
170
  }
349
- // If there are overrides, we must determine which cluster object (current or overflow) each belongs to and add it.
350
- const overrides = ids.overrides;
351
- if (overrides !== undefined) {
352
- for (let i = 0; i < overrides.length; i++) {
353
- const [overriddenLocal, override] = overrides[i];
354
- // Note: recall that local IDs are negative
355
- assert(i === 0 || overriddenLocal < overrides[i - 1][0], 0x48f /* Override IDs must be in sorted order. */);
356
- assert(overriddenLocal < normalizedLastFinalizedLocal, 0x490 /* Ranges finalized out of order. */);
357
- assert(overriddenLocal >= newLastFinalizedLocal, 0x491 /* Malformed range: override ID ahead of range start. */);
358
- let cluster;
359
- let overriddenFinal;
360
- if (localIdPivot !== undefined && overriddenLocal <= localIdPivot) {
361
- // Override is at or past the pivot, so it is in a new cluster.
362
- assert(newCluster !== undefined && newBaseFinalId !== undefined, 0x492 /* No cluster was created when overflow occurred. */);
363
- cluster = newCluster;
364
- overriddenFinal = (newBaseFinalId +
365
- (localIdPivot - overriddenLocal));
366
- }
367
- else {
368
- // Override was finalized into an existing cluster
369
- assert(currentCluster !== undefined && currentBaseFinalId !== undefined, 0x493 /* No cluster exists but IDs were finalized. */);
370
- cluster = currentCluster;
371
- overriddenFinal = (currentBaseFinalId +
372
- initialClusterCount +
373
- (normalizedLastFinalizedLocal - overriddenLocal) -
374
- 1);
375
- }
376
- cluster.overrides ?? (cluster.overrides = new Map());
377
- const inversionKey = IdCompressor.createInversionKey(override);
378
- const existingIds = this.getExistingIdsForNewOverride(inversionKey, true);
379
- let overrideForCluster;
380
- let associatedLocal;
381
- if (existingIds !== undefined) {
382
- let mostFinalExistingOverride;
383
- if (typeof existingIds === "number") {
384
- mostFinalExistingOverride = existingIds;
385
- if (isLocalId(mostFinalExistingOverride)) {
386
- associatedLocal = mostFinalExistingOverride;
387
- }
388
- }
389
- else {
390
- [associatedLocal, mostFinalExistingOverride] = existingIds;
391
- }
392
- if (isFinalId(mostFinalExistingOverride)) {
393
- // A previous range already finalized an ID with this override. See `IdCluster` for more.
394
- overrideForCluster = mostFinalExistingOverride;
395
- }
396
- else {
397
- assert(!isLocal || mostFinalExistingOverride === overriddenLocal, 0x494 /* Cannot have multiple local IDs with identical overrides. */);
398
- // This session has created an ID with this override before, but has not finalized it yet. The incoming
399
- // range "wins" and will contain the final ID associated with that override, regardless of if that range was
400
- // made by this session or not.
401
- overrideForCluster = override;
402
- }
403
- }
404
- else {
405
- // This is the first time this override has been associated with any ID
406
- overrideForCluster = override;
407
- }
408
- assert(!cluster.overrides.has(overriddenFinal), 0x495 /* Cannot add a second override for final id */);
409
- if (typeof overrideForCluster === "string") {
410
- if (isLocal || associatedLocal === undefined) {
411
- cluster.overrides.set(overriddenFinal, override);
412
- }
413
- else {
414
- cluster.overrides.set(overriddenFinal, {
415
- override,
416
- originalOverridingFinal: overriddenFinal,
417
- associatedLocalId: associatedLocal,
418
- });
419
- }
420
- }
421
- else {
422
- const unifiedOverride = {
423
- override,
424
- originalOverridingFinal: overrideForCluster,
425
- };
426
- setPropertyIfDefined(associatedLocal, unifiedOverride, "associatedLocalId");
427
- cluster.overrides.set(overriddenFinal, unifiedOverride);
171
+ if (remainingCapacity >= count) {
172
+ // The current range fits in the existing cluster
173
+ lastCluster.count += count;
174
+ }
175
+ else {
176
+ const overflow = count - remainingCapacity;
177
+ const newClaimedFinalCount = overflow + this.clusterCapacity;
178
+ if (lastCluster === this.finalSpace.getLastCluster()) {
179
+ // The last cluster in the sessions chain is the last cluster globally, so it can be expanded.
180
+ lastCluster.capacity += newClaimedFinalCount;
181
+ lastCluster.count += count;
182
+ assert(!this.sessions.clusterCollides(lastCluster), 0x756 /* Cluster collision detected. */);
183
+ if (isLocal) {
184
+ this.logger?.sendTelemetryEvent({
185
+ eventName: "RuntimeIdCompressor:ClusterExpansion",
186
+ sessionId: this.localSessionId,
187
+ previousCapacity: lastCluster.capacity - newClaimedFinalCount,
188
+ newCapacity: lastCluster.capacity,
189
+ overflow,
190
+ });
428
191
  }
429
- const finalizedOverride = {
430
- cluster,
431
- originalOverridingFinal: overriddenFinal,
432
- };
433
- setPropertyIfDefined(associatedLocal, finalizedOverride, "associatedLocalId");
434
- const currentOverride = this.clustersAndOverridesInversion.get(inversionKey);
435
- if (currentOverride === undefined ||
436
- IdCompressor.isUnfinalizedOverride(currentOverride)) {
437
- // Update the map to contain a finalized override, but never update it with future finalized overrides with
438
- // the same string; those should decompress to the first final ID with that override.
439
- this.clustersAndOverridesInversion.set(inversionKey, finalizedOverride);
192
+ }
193
+ else {
194
+ // The last cluster in the sessions chain is *not* the last cluster globally. Fill and overflow to new.
195
+ lastCluster.count = lastCluster.capacity;
196
+ const newCluster = this.addEmptyCluster(session, newClaimedFinalCount);
197
+ newCluster.count += overflow;
198
+ if (isLocal) {
199
+ this.logger?.sendTelemetryEvent({
200
+ eventName: "RuntimeIdCompressor:NewCluster",
201
+ sessionId: this.localSessionId,
202
+ });
440
203
  }
441
204
  }
442
205
  }
443
206
  if (isLocal) {
444
207
  this.logger?.sendTelemetryEvent({
445
208
  eventName: "RuntimeIdCompressor:IdCompressorStatus",
446
- eagerFinalIdCount: eagerFinalIdCount - (overrides?.length ?? 0),
447
- localIdCount: remainingCount + (overrides?.length ?? 0),
448
- overridesCount: overrides?.length ?? 0,
209
+ eagerFinalIdCount: this.telemetryEagerFinalIdCount,
210
+ localIdCount: this.telemetryLocalIdCount,
449
211
  sessionId: this.localSessionId,
450
212
  });
213
+ this.telemetryEagerFinalIdCount = 0;
214
+ this.telemetryLocalIdCount = 0;
451
215
  }
452
- session.lastFinalizedLocalId = newLastFinalizedLocal;
453
- }
454
- checkClusterForCollision(cluster) {
455
- const maxClusterUuid = incrementUuid(cluster.baseUuid, cluster.capacity - 1);
456
- const maxClusterStableId = stableIdFromNumericUuid(maxClusterUuid);
457
- const closestMatch = this.clustersAndOverridesInversion.getPairOrNextLower(maxClusterStableId);
458
- if (closestMatch !== undefined) {
459
- const [inversionKey, compressionMapping] = closestMatch;
460
- if (!IdCompressor.isClusterInfo(compressionMapping)) {
461
- if (isStableId(inversionKey) &&
462
- IdCompressor.uuidsMightCollide(inversionKey, maxClusterStableId, cluster.capacity)) {
463
- const numericOverride = numericUuidFromStableId(inversionKey);
464
- const delta = getPositiveDelta(maxClusterUuid, numericOverride, cluster.capacity - 1);
465
- if (delta !== undefined) {
466
- IdCompressor.failWithCollidingOverride(inversionKey);
467
- }
468
- }
469
- }
470
- }
471
- }
472
- static failWithCollidingOverride(override) {
473
- fail(`Override '${override}' collides with another allocated UUID.`);
474
- }
475
- static isClusterInfo(compressionMapping) {
476
- return compressionMapping.clusterBase !== undefined;
477
- }
478
- static isUnfinalizedOverride(compressionMapping) {
479
- return typeof compressionMapping === "number";
480
- }
481
- static createInversionKey(inversionKey) {
482
- return isStableId(inversionKey)
483
- ? inversionKey
484
- : `${nonStableOverridePrefix}${inversionKey}`;
485
- }
486
- static isStableInversionKey(inversionKey) {
487
- return inversionKey.charAt(0) !== nonStableOverridePrefix;
488
- }
489
- /**
490
- * Returns an existing ID associated with an override, or undefined if none exists.
491
- */
492
- getExistingIdsForNewOverride(inversionKey, isFinalOverride) {
493
- const closestMatch = this.clustersAndOverridesInversion.getPairOrNextLower(inversionKey, reusedArray);
494
- let numericOverride;
495
- let stableOverride;
496
- if (closestMatch !== undefined) {
497
- const [key, compressionMapping] = closestMatch;
498
- if (!IdCompressor.isClusterInfo(compressionMapping)) {
499
- if (key === inversionKey) {
500
- if (IdCompressor.isUnfinalizedOverride(compressionMapping)) {
501
- return compressionMapping;
502
- }
503
- const finalizedOverride = compressionMapping;
504
- return finalizedOverride.associatedLocalId !== undefined
505
- ? [
506
- finalizedOverride.associatedLocalId,
507
- finalizedOverride.originalOverridingFinal,
508
- ]
509
- : finalizedOverride.originalOverridingFinal;
510
- }
511
- }
512
- else if (IdCompressor.isStableInversionKey(inversionKey)) {
513
- stableOverride = inversionKey;
514
- const cluster = compressionMapping.cluster;
515
- if (IdCompressor.uuidsMightCollide(inversionKey, key, cluster.capacity)) {
516
- numericOverride = numericUuidFromStableId(stableOverride);
517
- const delta = getPositiveDelta(numericOverride, cluster.baseUuid, cluster.capacity - 1);
518
- if (delta !== undefined) {
519
- if (!isFinalOverride) {
520
- if (delta >= cluster.count) {
521
- // TODO:#283: Properly implement unification
522
- return undefined;
523
- }
524
- return this.normalizeToSessionSpace((compressionMapping.clusterBase + delta));
525
- }
526
- }
527
- }
528
- }
529
- }
530
- const override = numericOverride ??
531
- stableOverride ??
532
- (IdCompressor.isStableInversionKey(inversionKey) ? inversionKey : undefined);
533
- if (override !== undefined) {
534
- const sessionSpaceId = this.getCompressedIdForStableId(override);
535
- if (sessionSpaceId !== undefined) {
536
- return sessionSpaceId;
537
- }
538
- }
539
- return undefined;
216
+ assert(!session.isEmpty(), 0x757 /* Empty sessions should not be created. */);
540
217
  }
541
- /**
542
- * Check if `a` might be within `range` of `b`, where both are treated as hex numbers.
543
- * @param range - an integer
544
- */
545
- static uuidsMightCollide(a, b, range) {
546
- // Check if any of the UUIDs in the cluster collide (i.e. any in [base, base + capacity)).
547
- // Optimization: All UUIDs in a cluster are the same string up until the last few characters which encode the offset from
548
- // the cluster base. So, first compute the length of that shared string, and early out if it is different from the override
549
- // UUID. This way we usually need not do the more expensive check below.
550
- const hexDigitsToCheck = 32 - Math.ceil(Math.log2(range) / 2);
551
- if (a.startsWith(b.slice(0, hexDigitsToCheck))) {
552
- return true;
553
- }
554
- return false;
218
+ addEmptyCluster(session, capacity) {
219
+ const newCluster = session.addNewCluster(this.finalSpace.getAllocatedIdLimit(), capacity, 0);
220
+ assert(!this.sessions.clusterCollides(newCluster), 0x758 /* Cluster collision detected. */);
221
+ this.finalSpace.addCluster(newCluster);
222
+ return newCluster;
555
223
  }
556
- /**
557
- * Helper for retrieving an override.
558
- */
559
- static tryGetOverride(cluster, finalId) {
560
- const override = cluster.overrides?.get(finalId);
561
- if (override === undefined) {
562
- return undefined;
563
- }
564
- if (typeof override === "string") {
565
- return override;
566
- }
567
- return override.override;
568
- }
569
- /**
570
- * Generates a new compressed ID or returns an existing one.
571
- * This should ONLY be called to generate IDs for local operations.
572
- * @param override - Specifies a specific string to be associated with the returned compressed ID.
573
- * Performance note: assigning override strings incurs a performance overhead.
574
- * @returns an existing ID if one already exists for `override`, and a new local ID otherwise. The returned ID is in session space.
575
- */
576
- generateCompressedId(override) {
577
- let overrideInversionKey;
578
- if (override !== undefined) {
579
- overrideInversionKey = IdCompressor.createInversionKey(override);
580
- const existingIds = this.getExistingIdsForNewOverride(overrideInversionKey, false);
581
- if (existingIds !== undefined) {
582
- return typeof existingIds === "number" ? existingIds : existingIds[0];
583
- }
584
- }
585
- // Bump local counter regardless, then attempt to optimistically return a final ID.
586
- // If the local session has reserved a cluster range via consensus, it is safe to hand out final IDs prior to
587
- // finalizing the range that includes these locals.
588
- const newLocalId = -++this.localIdCount;
589
- const { currentClusterDetails } = this.localSession;
590
- const { sessionIdNormalizer } = this;
591
- let eagerFinalId;
592
- let cluster;
593
- if (currentClusterDetails !== undefined) {
594
- cluster = currentClusterDetails.cluster;
595
- const lastFinalKnown = sessionIdNormalizer.getLastFinalId();
596
- if (lastFinalKnown !== undefined &&
597
- lastFinalKnown - currentClusterDetails.clusterBase + 1 < cluster.capacity) {
598
- eagerFinalId = (lastFinalKnown + 1);
599
- }
600
- }
601
- if (overrideInversionKey !== undefined) {
602
- const registeredLocal = sessionIdNormalizer.addLocalId();
603
- assert(registeredLocal === newLocalId, 0x496 /* Session ID Normalizer produced unexpected local ID */);
604
- if (eagerFinalId !== undefined) {
605
- sessionIdNormalizer.addFinalIds(eagerFinalId, eagerFinalId, cluster ?? fail("No cluster when generating compressed ID"));
606
- }
607
- this.localOverrides.append(newLocalId, override ?? fail("Override must be defined"));
608
- // Since the local ID was just created, it is in both session and op space
609
- const compressionMapping = newLocalId;
610
- this.clustersAndOverridesInversion.set(overrideInversionKey, compressionMapping);
611
- }
612
- else if (eagerFinalId !== undefined) {
613
- sessionIdNormalizer.addFinalIds(eagerFinalId, eagerFinalId, cluster ?? fail("No cluster when generating compressed ID"));
614
- return eagerFinalId;
224
+ normalizeToOpSpace(id) {
225
+ if (isFinalId(id)) {
226
+ return id;
615
227
  }
616
228
  else {
617
- const registeredLocal = sessionIdNormalizer.addLocalId();
618
- assert(registeredLocal === newLocalId, 0x497 /* Session ID Normalizer produced unexpected local ID */);
229
+ const local = id;
230
+ if (!this.normalizer.contains(local)) {
231
+ throw new Error("Invalid ID to normalize.");
232
+ }
233
+ const finalForm = this.localSession.tryConvertToFinal(local, true);
234
+ return finalForm === undefined
235
+ ? local
236
+ : finalForm;
619
237
  }
620
- return newLocalId;
621
238
  }
622
- /**
623
- * Decompresses a previously compressed ID into a UUID or override string.
624
- * @param id - the compressed ID to be decompressed.
625
- * @returns the UUID or override string associated with the compressed ID. Fails if the ID was not generated by this compressor.
626
- */
627
- decompress(id) {
628
- return this.tryDecompress(id) ?? fail("Compressed ID was not generated by this compressor");
629
- }
630
- /**
631
- * Attempts to decompress a previously compressed ID into a UUID or override string.
632
- * @param id - the compressed ID to be decompressed.
633
- * @returns the UUID or override string associated with the compressed ID, or undefined if the ID was not generated by this compressor.
634
- */
635
- tryDecompress(id) {
239
+ normalizeToSessionSpace(id, originSessionId) {
636
240
  if (isFinalId(id)) {
637
- const possibleCluster = this.getClusterForFinalId(id);
638
- if (possibleCluster === undefined) {
639
- // It may be an unfinalized eager final ID, so check with normalizer to get the offset from the session UUID
640
- const creationIndex = this.sessionIdNormalizer.getCreationIndex(id);
641
- if (creationIndex !== undefined) {
642
- return stableIdFromNumericUuid(this.localSession.sessionUuid, creationIndex);
241
+ const containingCluster = this.localSession.getClusterByAllocatedFinal(id);
242
+ if (containingCluster === undefined) {
243
+ // Does not exist in local cluster chain
244
+ if (id >= this.finalSpace.getFinalizedIdLimit()) {
245
+ throw new Error("Unknown op space ID.");
643
246
  }
644
- return undefined;
247
+ return id;
645
248
  }
646
249
  else {
647
- const [baseFinalId, cluster] = possibleCluster;
648
- const override = IdCompressor.tryGetOverride(cluster, id);
649
- if (override !== undefined) {
650
- return override;
250
+ const alignedLocal = getAlignedLocal(containingCluster, id);
251
+ if (this.normalizer.contains(alignedLocal)) {
252
+ return alignedLocal;
651
253
  }
652
254
  else {
653
- const offsetInCluster = id - baseFinalId;
654
- return stableIdFromNumericUuid(cluster.baseUuid, offsetInCluster);
255
+ if (genCountFromLocalId(alignedLocal) > this.localGenCount) {
256
+ throw new Error("Unknown op space ID.");
257
+ }
258
+ return id;
655
259
  }
656
260
  }
657
261
  }
658
262
  else {
659
- const idOffset = -id; // Convert to a positive number
660
- if (idOffset > this.localIdCount) {
661
- // This local ID was never allocated.
662
- return undefined;
663
- }
664
- // If this is a local ID with an override, then it must have been allocated on this machine and will be contained in
665
- // `localOverrides`s. Otherwise, it is a sequential allocation from the session UUID and can simply be negated and
666
- // added to that UUID to obtain the stable ID associated with it.
667
- const localOverride = this.localOverrides?.get(id);
668
- return localOverride !== undefined
669
- ? localOverride
670
- : stableIdFromNumericUuid(this.localSession.sessionUuid, idOffset - 1);
671
- }
672
- }
673
- /**
674
- * Recompresses a decompressed ID, which could be a UUID or an override string.
675
- * @param uncompressed - the UUID or override string to recompress.
676
- * @returns the `CompressedId` associated with `uncompressed`. Fails if it has not been previously compressed by this compressor.
677
- */
678
- recompress(uncompressed) {
679
- return this.tryRecompress(uncompressed) ?? fail("No such string has ever been compressed");
680
- }
681
- /**
682
- * Attempts to recompresses a decompressed ID, which could be a UUID or an override string.
683
- * @param uncompressed - the UUID or override string to recompress,
684
- * @returns the `CompressedId` associated with `uncompressed` or undefined if it has not been previously compressed by this compressor.
685
- */
686
- tryRecompress(uncompressed) {
687
- return this.recompressInternal(uncompressed);
688
- }
689
- /**
690
- * Helper to compress an uncompressed UUID. It can optionally be supplied with the numeric form of `uncompressedUuid` as a
691
- * performance optimization.
692
- */
693
- recompressInternal(uncompressed, uncompressedUuidNumeric) {
694
- let numericUuid = uncompressedUuidNumeric;
695
- const inversionKey = IdCompressor.createInversionKey(uncompressed);
696
- const isStable = IdCompressor.isStableInversionKey(inversionKey);
697
- const closestMatch = this.clustersAndOverridesInversion.getPairOrNextLower(inversionKey, reusedArray);
698
- if (closestMatch !== undefined) {
699
- const [key, compressionMapping] = closestMatch;
700
- if (!IdCompressor.isClusterInfo(compressionMapping)) {
701
- if (key === inversionKey) {
702
- return IdCompressor.isUnfinalizedOverride(compressionMapping)
703
- ? compressionMapping
704
- : compressionMapping.associatedLocalId ??
705
- compressionMapping.originalOverridingFinal;
263
+ const localToNormalize = id;
264
+ if (originSessionId === this.localSessionId) {
265
+ if (this.normalizer.contains(localToNormalize)) {
266
+ return localToNormalize;
267
+ }
268
+ else {
269
+ // We never generated this local ID, so fail
270
+ throw new Error("Unknown op space ID.");
706
271
  }
707
272
  }
708
273
  else {
709
- if (!isStable) {
710
- return undefined;
274
+ // LocalId from a remote session
275
+ const remoteSession = this.sessions.get(originSessionId);
276
+ if (remoteSession === undefined) {
277
+ throw new Error("No IDs have ever been finalized by the supplied session.");
711
278
  }
712
- const { clusterBase: closestBaseFinalId, cluster: closestCluster } = compressionMapping;
713
- numericUuid ?? (numericUuid = numericUuidFromStableId(inversionKey));
714
- const uuidOffset = getPositiveDelta(numericUuid, closestCluster.baseUuid, closestCluster.count - 1);
715
- if (uuidOffset !== undefined) {
716
- let targetFinalId = (closestBaseFinalId + uuidOffset);
717
- const override = closestCluster.overrides?.get(targetFinalId);
718
- if (typeof override === "object") {
719
- if (override.associatedLocalId !== undefined) {
720
- return override.associatedLocalId;
721
- }
722
- // This may be a UUID that should actually compress into a different final ID that it aligns with, due to
723
- // another session having an identical override (see `IdCluster` for more).
724
- targetFinalId = override.originalOverridingFinal;
725
- }
726
- return this.normalizeToSessionSpace(targetFinalId);
279
+ const correspondingFinal = remoteSession.tryConvertToFinal(localToNormalize, false);
280
+ if (correspondingFinal === undefined) {
281
+ throw new Error("Unknown op space ID.");
727
282
  }
283
+ return correspondingFinal;
728
284
  }
729
285
  }
730
- if (isStable) {
731
- // May have already computed the numeric UUID, so avoid recomputing if possible
732
- const sessionSpaceId = this.getCompressedIdForStableId(numericUuid ?? inversionKey);
733
- if (sessionSpaceId !== undefined) {
734
- return sessionSpaceId;
735
- }
736
- }
737
- return undefined;
738
286
  }
739
- /**
740
- * Normalizes a session space ID into op space.
741
- * @param id - the local ID to normalize.
742
- * @returns the ID in op space.
743
- */
744
- normalizeToOpSpace(id) {
287
+ decompress(id) {
745
288
  if (isFinalId(id)) {
746
- return id;
747
- }
748
- // Check if this local ID has not been allocated yet
749
- if (-id > this.localIdCount) {
750
- fail("Supplied local ID was not created by this compressor.");
751
- }
752
- // Check if this local ID has not been finalized yet.
753
- // Comparing lastFinalizedLocalId is a safe check for eager final IDs because the local IDs corresponding to them
754
- // are never handed out to a consumer, and thus could not be passed into this method.
755
- const { lastFinalizedLocalId } = this.localSession;
756
- if (lastFinalizedLocalId === undefined || id < lastFinalizedLocalId) {
757
- // Eager final IDs do not have overrides in the cluster until finalizing
758
- // This means that using the normalizer to get the final/cluster associated would succeed but would not have the override,
759
- // so checking localOverrides first is necessary.
760
- const override = this.localOverrides.get(id);
761
- if (override !== undefined) {
762
- const inversionKey = IdCompressor.createInversionKey(override);
763
- const compressionMapping = this.clustersAndOverridesInversion.get(inversionKey) ??
764
- fail("Bimap is malformed.");
765
- return !IdCompressor.isClusterInfo(compressionMapping) &&
766
- !IdCompressor.isUnfinalizedOverride(compressionMapping) &&
767
- compressionMapping.associatedLocalId === id
768
- ? compressionMapping.originalOverridingFinal
769
- : id;
770
- }
771
- const possibleFinal = this.sessionIdNormalizer.getFinalId(id);
772
- return possibleFinal?.[0] ?? id;
773
- }
774
- const [correspondingFinal, cluster] = this.sessionIdNormalizer.getFinalId(id) ??
775
- fail("Locally created cluster should be added to the map when allocated");
776
- if (cluster.overrides) {
777
- const override = cluster.overrides.get(correspondingFinal);
778
- if (typeof override === "object" && override.originalOverridingFinal !== undefined) {
779
- // Rare case of two local IDs with same overrides are created concurrently. See `IdCluster` for more.
780
- return override.originalOverridingFinal;
781
- }
782
- }
783
- return correspondingFinal;
784
- }
785
- normalizeToSessionSpace(id, sessionIdIfLocal) {
786
- if (isLocalId(id)) {
787
- if (sessionIdIfLocal === undefined || sessionIdIfLocal === this.localSessionId) {
788
- const localIndex = -id;
789
- if (localIndex > this.localIdCount) {
790
- fail("Supplied local ID was not created by this compressor.");
289
+ const containingCluster = Session.getContainingCluster(id, this.finalSpace.clusters);
290
+ if (containingCluster === undefined) {
291
+ throw new Error("Unknown ID");
292
+ }
293
+ const alignedLocal = getAlignedLocal(containingCluster, id);
294
+ const alignedGenCount = genCountFromLocalId(alignedLocal);
295
+ const lastFinalizedGenCount = genCountFromLocalId(lastFinalizedLocal(containingCluster));
296
+ if (alignedGenCount > lastFinalizedGenCount) {
297
+ // should be an eager final id generated by the local session
298
+ if (containingCluster.session === this.localSession) {
299
+ assert(!this.normalizer.contains(alignedLocal), 0x759 /* Normalizer out of sync. */);
300
+ }
301
+ else {
302
+ throw new Error("Unknown ID");
791
303
  }
792
- return id;
793
- }
794
- else {
795
- const session = this.sessions.get(sessionIdIfLocal) ??
796
- fail("No IDs have ever been finalized by the supplied session.");
797
- const localCount = -id;
798
- const numericUuid = incrementUuid(session.sessionUuid, localCount - 1);
799
- return (this.compressNumericUuid(numericUuid) ??
800
- fail("ID is not known to this compressor."));
801
304
  }
305
+ return stableIdFromNumericUuid(offsetNumericUuid(containingCluster.session.sessionUuid, alignedGenCount - 1));
802
306
  }
803
- const normalizedId = this.sessionIdNormalizer.getSessionSpaceId(id);
804
- if (normalizedId !== undefined) {
805
- return normalizedId;
806
- }
807
- // Check for a unified override finalized first by another session but to which the local session
808
- // still has an associated local ID.
809
- const [_, cluster] = this.getClusterForFinalId(id) ??
810
- fail("Supplied final ID was not finalized by this compressor.");
811
- const override = cluster.overrides?.get(id);
812
- if (typeof override === "object" && override.associatedLocalId !== undefined) {
813
- return override.associatedLocalId;
814
- }
815
- return id;
816
- }
817
- /**
818
- * Returns the session-space compressed ID corresponding to the numeric UUID, or undefined if it is not known to this compressor.
819
- * Typically, it will return the session-space ID sequentially aligned with it (which will be local if `numericUuid` was made by
820
- * the local session, or final otherwise). However, in the event that the aligned session-space ID was overridden with a UUID
821
- * *and* that override UUID was concurrently used in an older ID (earlier, w.r.t. sequencing), this method can return the first
822
- * ID to correspond to that override.
823
- *
824
- * As an example, consider the following two clients:
825
- * ClientA, session UUID: A0000000-0000-0000-0000-000000000000
826
- * ClientB, session UUID: B0000000-0000-0000-0000-000000000000
827
- *
828
- * If concurrently, two clients performed:
829
- * ClientA: generateCompressedId(override: 'X0000000-0000-0000-0000-000000000000') // aligned with A0000000-0000-0000-0000-000000000000
830
- *
831
- * ClientB: generateCompressedId() // aligned with B0000000-0000-0000-0000-000000000000
832
- * ClientB: generateCompressedId(override: 'X0000000-0000-0000-0000-000000000000') // aligned with B0000000-0000-0000-0000-000000000001
833
- *
834
- * After sequencing, calling this method and passing the numeric UUID for B0000000-0000-0000-0000-000000000001 would return the
835
- * session-space ID corresponding to A0000000-0000-0000-0000-000000000000 (with override X0000000-0000-0000-0000-000000000000).
836
- */
837
- compressNumericUuid(numericUuid) {
838
- const stableId = stableIdFromNumericUuid(numericUuid);
839
- const sessionSpaceId = this.recompressInternal(stableId, numericUuid);
840
- if (sessionSpaceId === undefined) {
841
- return undefined;
842
- }
843
- return sessionSpaceId;
844
- }
845
- /**
846
- * Returns a compressed ID for the supplied stable ID if it was created by the local session, and undefined otherwise.
847
- */
848
- getCompressedIdForStableId(stableId) {
849
- const numericUuid = typeof stableId === "string" ? numericUuidFromStableId(stableId) : stableId;
850
- const creationIndex = getPositiveDelta(numericUuid, this.localSession.sessionUuid, this.localIdCount - 1);
851
- if (creationIndex !== undefined) {
852
- const sessionSpaceId = this.sessionIdNormalizer.getIdByCreationIndex(creationIndex);
853
- if (sessionSpaceId !== undefined) {
854
- return sessionSpaceId;
307
+ else {
308
+ const localToDecompress = id;
309
+ if (!this.normalizer.contains(localToDecompress)) {
310
+ throw new Error("Unknown ID");
855
311
  }
312
+ return stableIdFromNumericUuid(offsetNumericUuid(this.localSession.sessionUuid, genCountFromLocalId(localToDecompress) - 1));
856
313
  }
857
- return undefined;
858
314
  }
859
- getClusterForFinalId(finalId) {
860
- const possibleCluster = this.finalIdToCluster.getPairOrNextLower(finalId);
861
- if (possibleCluster === undefined) {
862
- return undefined;
863
- }
864
- const [clusterBase, cluster] = possibleCluster;
865
- if (finalId - clusterBase >= cluster.count) {
866
- return undefined;
315
+ recompress(uncompressed) {
316
+ const recompressed = this.tryRecompress(uncompressed);
317
+ if (recompressed === undefined) {
318
+ throw new Error("Could not recompress.");
867
319
  }
868
- return possibleCluster;
320
+ return recompressed;
869
321
  }
870
- /**
871
- * @returns if `other` is equal to this `IdCompressor`. The equality check includes local session state only if specified.
872
- * \@testOnly
873
- */
874
- equals(other, compareLocalState) {
875
- if (compareLocalState) {
876
- if (this.localIdCount !== other.localIdCount ||
877
- this.localSessionId !== other.localSessionId ||
878
- this.lastTakenLocalId !== other.lastTakenLocalId) {
879
- return false;
880
- }
881
- if (!this.localOverrides.equals(other.localOverrides, (a, b) => a === b)) {
882
- return false;
883
- }
884
- if (!compareMaps(this.sessions, other.sessions, (a, b) => IdCompressor.sessionDataEqual(a, b, true, compareLocalState))) {
885
- return false;
886
- }
887
- if (!this.sessionIdNormalizer.equals(other.sessionIdNormalizer, (a, b) => IdCompressor.idClustersEqual(a, b, false, compareLocalState))) {
888
- return false;
889
- }
890
- }
891
- else {
892
- for (const [keyA, valueA] of this.sessions) {
893
- const valueB = other.sessions.get(keyA);
894
- if (valueB === undefined) {
895
- if (valueA.lastFinalizedLocalId !== undefined) {
896
- return false;
897
- }
898
- }
899
- else if (!IdCompressor.sessionDataEqual(valueA, valueB, true, compareLocalState)) {
900
- return false;
901
- }
902
- }
903
- for (const [keyB, valueB] of other.sessions) {
904
- const valueA = this.sessions.get(keyB);
905
- if (valueA === undefined) {
906
- if (valueB.lastFinalizedLocalId !== undefined) {
907
- return false;
908
- }
322
+ tryRecompress(uncompressed) {
323
+ const match = this.sessions.getContainingCluster(uncompressed);
324
+ if (match === undefined) {
325
+ const numericUncompressed = numericUuidFromStableId(uncompressed);
326
+ const offset = subtractNumericUuids(numericUncompressed, this.localSession.sessionUuid);
327
+ if (offset < Number.MAX_SAFE_INTEGER) {
328
+ const genCountEquivalent = Number(offset) + 1;
329
+ const localEquivalent = localIdFromGenCount(genCountEquivalent);
330
+ if (this.normalizer.contains(localEquivalent)) {
331
+ return localEquivalent;
909
332
  }
910
333
  }
334
+ return undefined;
911
335
  }
912
- if (this.nextClusterBaseFinalId !== other.nextClusterBaseFinalId ||
913
- this.newClusterCapacity !== other.newClusterCapacity) {
914
- return false;
915
- }
916
- if (!this.finalIdToCluster.equals(other.finalIdToCluster, (a, b) => IdCompressor.idClustersEqual(a, b, true, compareLocalState))) {
917
- return false;
918
- }
919
- const missingInOne = (_, value) => {
920
- if (!compareLocalState && IdCompressor.isUnfinalizedOverride(value)) {
921
- return undefined;
922
- }
923
- return { break: true };
924
- };
925
- const compareCompressionMappings = (a, b) => {
926
- const unfinalizedA = IdCompressor.isUnfinalizedOverride(a);
927
- const unfinalizedB = IdCompressor.isUnfinalizedOverride(b);
928
- if (unfinalizedA) {
929
- if (unfinalizedB) {
930
- return a === b;
336
+ else {
337
+ const [containingCluster, alignedLocal] = match;
338
+ if (containingCluster.session === this.localSession) {
339
+ // Local session
340
+ if (this.normalizer.contains(alignedLocal)) {
341
+ return alignedLocal;
931
342
  }
932
- return false;
933
- }
934
- else if (unfinalizedB) {
935
- return false;
936
- }
937
- if (IdCompressor.isClusterInfo(a)) {
938
- if (!IdCompressor.isClusterInfo(b) || a.clusterBase !== b.clusterBase) {
939
- return false;
343
+ else {
344
+ assert(genCountFromLocalId(alignedLocal) <= this.localGenCount, 0x75a /* Clusters out of sync. */);
345
+ // Id is an eager final
346
+ return getAlignedFinal(containingCluster, alignedLocal);
940
347
  }
941
348
  }
942
349
  else {
943
- if (IdCompressor.isClusterInfo(b) ||
944
- (compareLocalState && a.associatedLocalId !== b.associatedLocalId) ||
945
- a.originalOverridingFinal !== b.originalOverridingFinal) {
946
- return false;
947
- }
948
- }
949
- if (!IdCompressor.idClustersEqual(a.cluster, b.cluster, true, compareLocalState)) {
950
- return false;
951
- }
952
- return true;
953
- };
954
- const diff = this.clustersAndOverridesInversion.diffAgainst(other.clustersAndOverridesInversion, missingInOne, missingInOne, (_, valA, valB) => {
955
- if (!compareCompressionMappings(valA, valB)) {
956
- return { break: true };
957
- }
958
- return undefined;
350
+ // Not the local session
351
+ return genCountFromLocalId(alignedLocal) >= lastFinalizedLocal(containingCluster)
352
+ ? getAlignedFinal(containingCluster, alignedLocal)
353
+ : undefined;
354
+ }
355
+ }
356
+ }
357
+ serialize(hasLocalState) {
358
+ const { normalizer, finalSpace, sessions } = this;
359
+ const sessionIndexMap = new Map();
360
+ let sessionIndex = 0;
361
+ for (const session of sessions.sessions()) {
362
+ // Filter empty sessions to prevent them accumulating in the serialized state
363
+ if (!session.isEmpty() || hasLocalState) {
364
+ sessionIndexMap.set(session, sessionIndex);
365
+ sessionIndex++;
366
+ }
367
+ }
368
+ const localStateSize = hasLocalState
369
+ ? 1 + // generated ID count
370
+ 1 + // next range base genCount
371
+ 1 + // count of normalizer pairs
372
+ this.normalizer.idRanges.size * 2 // pairs
373
+ : 0;
374
+ // Layout size, in 8 byte increments
375
+ const totalSize = 1 + // version
376
+ 1 + // hasLocalState
377
+ 1 + // cluster capacity
378
+ 1 + // session count
379
+ 1 + // cluster count
380
+ sessionIndexMap.size * 2 + // session IDs
381
+ finalSpace.clusters.length * 3 + // clusters: (sessionIndex, capacity, count)[]
382
+ localStateSize; // local state, if present
383
+ const serializedFloat = new Float64Array(totalSize);
384
+ const serializedUint = new BigUint64Array(serializedFloat.buffer);
385
+ let index = 0;
386
+ index = writeNumber(serializedFloat, index, currentWrittenVersion);
387
+ index = writeBoolean(serializedFloat, index, hasLocalState);
388
+ index = writeNumber(serializedFloat, index, this.clusterCapacity);
389
+ index = writeNumber(serializedFloat, index, sessionIndexMap.size);
390
+ index = writeNumber(serializedFloat, index, finalSpace.clusters.length);
391
+ for (const [session] of sessionIndexMap.entries()) {
392
+ index = writeNumericUuid(serializedUint, index, session.sessionUuid);
393
+ }
394
+ finalSpace.clusters.forEach((cluster) => {
395
+ index = writeNumber(serializedFloat, index, sessionIndexMap.get(cluster.session));
396
+ index = writeNumber(serializedFloat, index, cluster.capacity);
397
+ index = writeNumber(serializedFloat, index, cluster.count);
959
398
  });
960
- return diff === undefined;
961
- }
962
- static sessionDataEqual(a, b, checkCluster = true, compareLocalState = true) {
963
- if (!numericUuidEquals(a.sessionUuid, b.sessionUuid) ||
964
- a.lastFinalizedLocalId !== b.lastFinalizedLocalId) {
965
- return false;
966
- }
967
- if (a.currentClusterDetails === undefined || b.currentClusterDetails === undefined) {
968
- if (a.currentClusterDetails !== b.currentClusterDetails) {
969
- return false;
970
- }
971
- return true;
972
- }
973
- if (checkCluster &&
974
- !IdCompressor.idClustersEqual(a.currentClusterDetails.cluster, b.currentClusterDetails.cluster, false, compareLocalState)) {
975
- return false;
976
- }
977
- return true;
978
- }
979
- static idClustersEqual(a, b, checkSessionData = true, compareLocalState = true) {
980
- const areEqual = numericUuidEquals(a.baseUuid, b.baseUuid) &&
981
- a.capacity === b.capacity &&
982
- a.count === b.count &&
983
- (!checkSessionData ||
984
- IdCompressor.sessionDataEqual(a.session, b.session, false, compareLocalState)) &&
985
- (a.overrides === undefined) === (b.overrides === undefined) &&
986
- (a.overrides === undefined ||
987
- compareMaps(a.overrides ?? fail("Overrides must be defined"), b.overrides ?? fail("Overrides must be defined"), (overrideA, overrideB) => {
988
- if (compareLocalState) {
989
- if (typeof overrideA === "string" || typeof overrideB === "string") {
990
- return overrideA === overrideB;
991
- }
992
- const overridesEqual = overrideA.override === overrideB.override &&
993
- overrideA.originalOverridingFinal ===
994
- overrideB.originalOverridingFinal &&
995
- (!compareLocalState ||
996
- overrideA.associatedLocalId === overrideB.associatedLocalId);
997
- return overridesEqual;
998
- }
999
- const uuidA = typeof overrideA === "string" ? overrideA : overrideA.override;
1000
- const uuidB = typeof overrideB === "string" ? overrideB : overrideB.override;
1001
- if (typeof overrideA !== "string" &&
1002
- typeof overrideB !== "string" &&
1003
- overrideA.originalOverridingFinal !== overrideB.originalOverridingFinal) {
1004
- return false;
1005
- }
1006
- return uuidA === uuidB;
1007
- }));
1008
- return areEqual;
1009
- }
1010
- serialize(withSession) {
1011
- const serializedSessions = [];
1012
- const sessionIdToSessionIndex = new Map();
1013
- for (const [sessionId, session] of this.sessions) {
1014
- const isLocalSession = sessionId === this.localSessionId;
1015
- const includeSession = sessionId !== reservedSessionId && // Ignore reserved clusters, but
1016
- (session.lastFinalizedLocalId !== undefined || // always serialize sessions that made final IDs,
1017
- (isLocalSession && withSession)); // include the un-acked local session if requested
1018
- if (includeSession) {
1019
- const sessionData = [sessionId];
1020
- sessionIdToSessionIndex.set(sessionId, serializedSessions.length);
1021
- serializedSessions.push(sessionData);
1022
- }
1023
- }
1024
- const serializedClusters = [];
1025
- for (const [baseFinalId, cluster] of this.finalIdToCluster.entries()) {
1026
- const sessionId = stableIdFromNumericUuid(cluster.session.sessionUuid);
1027
- if (sessionId !== reservedSessionId) {
1028
- const sessionIndex = sessionIdToSessionIndex.get(sessionId) ??
1029
- fail("Session object contains wrong session numeric UUID");
1030
- const serializedCluster = [
1031
- sessionIndex,
1032
- cluster.capacity,
1033
- ];
1034
- if (cluster.count !== cluster.capacity) {
1035
- serializedCluster.push(cluster.count);
1036
- }
1037
- if (cluster.overrides !== undefined) {
1038
- const serializedOverrides = [];
1039
- for (const [finalId, override] of cluster.overrides) {
1040
- const finalIdIndex = finalId - baseFinalId;
1041
- if (typeof override === "string") {
1042
- serializedOverrides.push([finalIdIndex, override]);
1043
- }
1044
- else if (override.originalOverridingFinal === finalId) {
1045
- serializedOverrides.push([finalIdIndex, override.override]);
1046
- }
1047
- else {
1048
- serializedOverrides.push([
1049
- finalIdIndex,
1050
- override.override,
1051
- override.originalOverridingFinal,
1052
- ]);
1053
- }
1054
- }
1055
- serializedCluster.push(serializedOverrides);
1056
- }
1057
- serializedClusters.push(serializedCluster);
1058
- }
1059
- }
1060
- // Reserved session not serialized, and local session is present but may not make IDs
1061
- assert(serializedSessions.length - this.sessions.size <= 2, 0x498 /* session not serialized */);
1062
- const serializedIdCompressor = {
1063
- version: currentWrittenVersion,
1064
- clusterCapacity: this.clusterCapacity,
1065
- sessions: serializedSessions,
1066
- clusters: serializedClusters,
1067
- };
1068
- if (withSession) {
1069
- const serializedWithSession = serializedIdCompressor;
1070
- serializedWithSession.localSessionIndex = serializedWithSession.sessions.findIndex(([sessionId]) => sessionId === this.localSessionId);
1071
- if (this.localIdCount > 0) {
1072
- serializedWithSession.localState = {
1073
- localIdCount: this.localIdCount,
1074
- overrides: [...this.localOverrides.entries()].map((entry) => [...entry]),
1075
- lastTakenLocalId: this.lastTakenLocalId,
1076
- sessionNormalizer: this.sessionIdNormalizer.serialize(),
1077
- };
399
+ if (hasLocalState) {
400
+ index = writeNumber(serializedFloat, index, this.localGenCount);
401
+ index = writeNumber(serializedFloat, index, this.nextRangeBaseGenCount);
402
+ index = writeNumber(serializedFloat, index, normalizer.idRanges.size);
403
+ for (const [leadingGenCount, count] of normalizer.idRanges.entries()) {
404
+ index = writeNumber(serializedFloat, index, leadingGenCount);
405
+ index = writeNumber(serializedFloat, index, count);
1078
406
  }
1079
- return serializedWithSession;
1080
407
  }
408
+ assert(index === totalSize, 0x75b /* Serialized size was incorrectly calculated. */);
1081
409
  this.logger?.sendTelemetryEvent({
1082
410
  eventName: "RuntimeIdCompressor:SerializedIdCompressorSize",
1083
- size: JSON.stringify(serializedIdCompressor).length,
1084
- clusterCount: serializedIdCompressor.clusters.length,
1085
- sessionCount: serializedIdCompressor.sessions.length,
411
+ size: serializedFloat.byteLength,
412
+ clusterCount: finalSpace.clusters.length,
413
+ sessionCount: sessionIndexMap.size,
1086
414
  });
1087
- return serializedIdCompressor;
1088
- }
1089
- static deserialize(...args) {
1090
- const [serialized, newSessionIdMaybe] = args;
1091
- const { clusterCapacity, sessions: serializedSessions, clusters: serializedClusters, } = serialized;
1092
- let localSessionId;
1093
- let serializedLocalState;
1094
- if (newSessionIdMaybe === undefined) {
1095
- // Alias of serialized, but known to be a SerializedIdCompressorWithOngoingSession
1096
- const [serializedWithSession] = args;
1097
- const serializedSessionData = serializedSessions[serializedWithSession.localSessionIndex];
1098
- localSessionId = serializedSessionData[0];
1099
- serializedLocalState = serializedWithSession.localState;
415
+ return bufferToString(serializedFloat.buffer, "base64");
416
+ }
417
+ static deserialize(serialized, sessionId) {
418
+ const buffer = stringToBuffer(serialized, "base64");
419
+ const index = {
420
+ index: 0,
421
+ bufferFloat: new Float64Array(buffer),
422
+ bufferUint: new BigUint64Array(buffer),
423
+ };
424
+ const version = readNumber(index);
425
+ assert(version === currentWrittenVersion, 0x75c /* Unknown serialized version. */);
426
+ const hasLocalState = readBoolean(index);
427
+ const clusterCapacity = readNumber(index);
428
+ const sessionCount = readNumber(index);
429
+ const clusterCount = readNumber(index);
430
+ // Sessions
431
+ let sessionOffset = 0;
432
+ const sessions = [];
433
+ if (!hasLocalState) {
434
+ // If !hasLocalState, there won't be a serialized local session ID so insert one at the beginning
435
+ assert(sessionId !== undefined, 0x75d /* Local session ID is undefined. */);
436
+ const localSessionNumeric = numericUuidFromStableId(sessionId);
437
+ sessions.push([localSessionNumeric, new Session(localSessionNumeric)]);
438
+ sessionOffset = 1;
1100
439
  }
1101
440
  else {
1102
- localSessionId = newSessionIdMaybe;
441
+ assert(sessionId === undefined, 0x75e /* Local state should not exist in serialized form. */);
1103
442
  }
1104
- const compressor = new IdCompressor(localSessionId);
1105
- compressor.clusterCapacity = clusterCapacity;
1106
- const localOverridesInverse = new Map();
1107
- if (serializedLocalState !== undefined) {
1108
- // Do this part of local rehydration first since the cluster map population needs to query to local overrides
1109
- compressor.localIdCount = serializedLocalState.localIdCount;
1110
- compressor.lastTakenLocalId = serializedLocalState.lastTakenLocalId;
1111
- if (serializedLocalState.overrides !== undefined) {
1112
- for (const [localId, override] of serializedLocalState.overrides) {
1113
- compressor.localOverrides.append(localId, override);
1114
- localOverridesInverse.set(override, localId);
1115
- compressor.clustersAndOverridesInversion.set(IdCompressor.createInversionKey(override), localId);
1116
- }
1117
- }
443
+ for (let i = 0; i < sessionCount; i++) {
444
+ const numeric = readNumericUuid(index);
445
+ sessions.push([numeric, new Session(numeric)]);
1118
446
  }
1119
- const sessionInfos = [];
1120
- for (const serializedSession of serializedSessions) {
1121
- const [sessionId] = serializedSession;
1122
- if (sessionId === localSessionId) {
1123
- assert(hasOngoingSession(serialized), 0x499 /* Cannot resume existing session. */);
1124
- sessionInfos.push({ session: compressor.localSession, sessionId });
1125
- }
1126
- else {
1127
- const session = compressor.createSession(sessionId);
1128
- sessionInfos.push({ session, sessionId });
1129
- }
1130
- }
1131
- for (const serializedCluster of serializedClusters) {
1132
- const { sessionIndex, capacity, count, overrides } = deserializeCluster(serializedCluster);
1133
- const { session, sessionId } = sessionInfos[sessionIndex];
1134
- const { lastFinalizedLocalId, sessionUuid } = session;
1135
- const currentIdCount = lastFinalizedLocalId === undefined ? 0 : -lastFinalizedLocalId;
1136
- const cluster = {
1137
- capacity,
1138
- count,
1139
- baseUuid: incrementUuid(sessionUuid, currentIdCount),
1140
- session,
1141
- };
1142
- const lastFinalizedNormalized = lastFinalizedLocalId ?? 0;
1143
- const clusterBase = compressor.nextClusterBaseFinalId;
1144
- session.lastFinalizedLocalId = (lastFinalizedNormalized - count);
1145
- session.currentClusterDetails = { clusterBase, cluster };
1146
- compressor.nextClusterBaseFinalId = (compressor.nextClusterBaseFinalId +
1147
- capacity);
1148
- compressor.finalIdToCluster.append(clusterBase, cluster);
1149
- compressor.clustersAndOverridesInversion.set(stableIdFromNumericUuid(cluster.baseUuid), {
1150
- clusterBase,
1151
- cluster,
1152
- });
1153
- if (overrides !== undefined) {
1154
- cluster.overrides = new Map();
1155
- for (const [finalIdIndex, override, originalOverridingFinal] of overrides) {
1156
- const finalId = (clusterBase + finalIdIndex);
1157
- if (originalOverridingFinal !== undefined) {
1158
- const unifiedOverride = {
1159
- override,
1160
- originalOverridingFinal,
1161
- };
1162
- if (serializedLocalState !== undefined) {
1163
- setPropertyIfDefined(localOverridesInverse.get(override), unifiedOverride, "associatedLocalId");
1164
- }
1165
- cluster.overrides.set(finalId, unifiedOverride);
1166
- }
1167
- else {
1168
- const associatedLocal = localOverridesInverse.get(override);
1169
- if (associatedLocal !== undefined && sessionId !== localSessionId) {
1170
- // In this case, there is a local ID associated with this override, but this is the first cluster to contain
1171
- // that override (because only the first cluster will have the string serialized). In this case, the override
1172
- // needs to hold that local value.
1173
- cluster.overrides.set(finalId, {
1174
- override,
1175
- originalOverridingFinal: finalId,
1176
- associatedLocalId: associatedLocal,
1177
- });
1178
- }
1179
- else {
1180
- cluster.overrides.set(finalId, override);
1181
- }
1182
- const finalizedOverride = {
1183
- cluster,
1184
- originalOverridingFinal: finalId,
1185
- };
1186
- if (serializedLocalState !== undefined) {
1187
- setPropertyIfDefined(associatedLocal, finalizedOverride, "associatedLocalId");
1188
- }
1189
- compressor.clustersAndOverridesInversion.set(IdCompressor.createInversionKey(override), finalizedOverride);
1190
- }
1191
- }
1192
- }
1193
- }
1194
- if (serializedLocalState !== undefined) {
1195
- compressor.sessionIdNormalizer = SessionIdNormalizer.deserialize(serializedLocalState.sessionNormalizer, (finalId) => {
1196
- const [_, cluster] = compressor.finalIdToCluster.getPairOrNextLower(finalId) ??
1197
- fail("Final in serialized normalizer was never created.");
1198
- return cluster;
1199
- });
1200
- }
1201
- assert(compressor.localSession.lastFinalizedLocalId === undefined ||
1202
- compressor.localIdCount >= -compressor.localSession.lastFinalizedLocalId, 0x49a /* Inconsistent last finalized state when deserializing */);
447
+ const compressor = new IdCompressor(new Sessions(sessions));
448
+ compressor.clusterCapacity = clusterCapacity;
449
+ // Clusters
450
+ let baseFinalId = 0;
451
+ for (let i = 0; i < clusterCount; i++) {
452
+ const sessionIndex = readNumber(index);
453
+ const session = sessions[sessionIndex + sessionOffset][1];
454
+ const capacity = readNumber(index);
455
+ const count = readNumber(index);
456
+ const cluster = session.addNewCluster(baseFinalId, capacity, count);
457
+ compressor.finalSpace.addCluster(cluster);
458
+ baseFinalId += capacity;
459
+ }
460
+ // Local state
461
+ if (hasLocalState) {
462
+ compressor.localGenCount = readNumber(index);
463
+ compressor.nextRangeBaseGenCount = readNumber(index);
464
+ const normalizerCount = readNumber(index);
465
+ for (let i = 0; i < normalizerCount; i++) {
466
+ compressor.normalizer.addLocalRange(readNumber(index), readNumber(index));
467
+ }
468
+ }
469
+ assert(index.index === index.bufferFloat.length, 0x75f /* Failed to read entire serialized compressor. */);
1203
470
  return compressor;
1204
471
  }
1205
- static convertToCurrentVersion(serializedCompressor, hasSession) {
1206
- if (serializedCompressor.version !== currentWrittenVersion) {
1207
- fail("Unknown SerializedIdCompressor version number");
1208
- }
1209
- const serialized = serializedCompressor;
1210
- if (hasSession !== hasOngoingSession(serialized)) {
1211
- return undefined;
472
+ equals(other, includeLocalState) {
473
+ if (includeLocalState &&
474
+ (this.localSessionId !== other.localSessionId ||
475
+ !this.localSession.equals(other.localSession) ||
476
+ !this.normalizer.equals(other.normalizer) ||
477
+ this.nextRangeBaseGenCount !== other.nextRangeBaseGenCount ||
478
+ this.localGenCount !== other.localGenCount)) {
479
+ return false;
1212
480
  }
1213
- return serialized;
481
+ return (this.newClusterCapacity === other.newClusterCapacity &&
482
+ this.sessions.equals(other.sessions, includeLocalState) &&
483
+ this.finalSpace.equals(other.finalSpace));
1214
484
  }
1215
485
  }
1216
486
  /**
1217
- * Max allowed cluster size
487
+ * Max allowed initial cluster size.
1218
488
  */
1219
489
  IdCompressor.maxClusterSize = 2 ** 20;
1220
- /**
1221
- * The version of `IdCompressor` that is currently persisted.
1222
- */
1223
- const currentWrittenVersion = "0.0.1";
1224
- /**
1225
- * @returns whether or not the given serialized ID compressor has an ongoing session.
1226
- */
1227
- export function hasOngoingSession(serialized) {
1228
- return (serialized.localSessionIndex !==
1229
- undefined);
1230
- }
1231
- function deserializeCluster(serializedCluster) {
1232
- const [sessionIndex, capacity, countOrOverrides, overrides] = serializedCluster;
1233
- const hasCount = typeof countOrOverrides === "number";
1234
- return {
1235
- sessionIndex,
1236
- capacity,
1237
- count: hasCount ? countOrOverrides : capacity,
1238
- overrides: hasCount ? overrides : countOrOverrides,
1239
- };
1240
- }
1241
- /**
1242
- * Optimization used by the sorted-btree library to avoid allocating tuples every time a lookup method is called.
1243
- * Lookup methods on BTree accept a pre-allocated array that it populates with the result of the lookup and retains no ownership
1244
- * of after the call, so this array may be supplied to any of them. References to this array should not be retained elsewhere and
1245
- * lookup results should be extracted from the tuple immediately after invocation.
1246
- */
1247
- const reusedArray = [];
1248
490
  //# sourceMappingURL=idCompressor.js.map