@fluidframework/container-runtime 2.0.0-internal.6.1.0 → 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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persistanceUtilities.js","sourceRoot":"","sources":["../../src/id-compressor/persistanceUtilities.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAKH,MAAM,WAAW,GAAG,MAAM,CAAC,oBAAoB,CAAC,CAAC;AACjD,MAAM,SAAS,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;AAE7B,SAAgB,WAAW,CAAC,MAAoB,EAAE,KAAa,EAAE,KAAa;IAC7E,MAAM,CAAC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ourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable no-bitwise */\nimport { NumericUuid } from \"./identifiers\";\n\nconst halfNumeric = BigInt(\"0xFFFFFFFFFFFFFFFF\");\nconst sixtyFour = BigInt(64);\n\nexport function writeNumber(buffer: Float64Array, index: number, value: number): number {\n\tbuffer[index] = value;\n\treturn index + 1;\n}\n\nexport function writeNumericUuid(\n\tbuffer: BigUint64Array,\n\tindex: number,\n\tvalue: NumericUuid,\n): number {\n\tbuffer[index] = value & halfNumeric;\n\tbuffer[index + 1] = value >> sixtyFour;\n\treturn index + 2; // UUID values are 16 bytes.\n}\n\nexport function writeBoolean(buffer: Float64Array, index: number, value: boolean): number {\n\treturn writeNumber(buffer, index, value ? 1 : 0);\n}\n\n/**\n * Helper type to allow returning a value when reading as well as incrementing an index to make\n * deserialization easier.\n * Should be constructed with two views of the same buffer and index set to 0.\n */\nexport interface Index {\n\tbufferFloat: Float64Array;\n\tbufferUint: BigUint64Array;\n\tindex: number;\n}\n\nexport function readNumber(index: Index): number {\n\tconst value = index.bufferFloat[index.index];\n\tindex.index += 1;\n\treturn value;\n}\n\nexport function readNumericUuid(index: Index): NumericUuid {\n\tconst lowerHalf = index.bufferUint[index.index];\n\tconst upperHalf = index.bufferUint[index.index + 1];\n\tconst value = (upperHalf << sixtyFour) | lowerHalf;\n\tindex.index += 2;\n\treturn value as NumericUuid;\n}\n\nexport function readBoolean(index: Index): boolean {\n\treturn readNumber(index) === 1;\n}\n"]}
@@ -0,0 +1,46 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { AppendOnlySortedMap } from "./appendOnlySortedMap";
6
+ import { LocalCompressedId } from "./identifiers";
7
+ /**
8
+ * The `SessionSpaceNormalizer` tracks the form of the IDs created by the local session.
9
+ * More precisely, it acts as a set of all `LocalCompressedId`s created by the local session and allows querying whether a specific
10
+ * ID was produced by the local session. This information can be used to determine whether the nth ID created by the local session
11
+ * was produced as a local ID (negative) or a final ID (positive).
12
+ *
13
+ * The local and final forms of IDs made by a session can be thought of as two equal-length sparse arrays, aligned such
14
+ * that normalizeLocalToFinal(locals[i]) === finals[i] and vice versa.
15
+ * Below is an example to illustrate how various mappings can arise:
16
+ *
17
+ * ```
18
+ * +- Creation Index
19
+ * / +- Locals
20
+ * / / +- Finals
21
+ * / / /
22
+ * ---+-----+----
23
+ * 0 | -1 | 0 -|___ Two IDs are allocated as locals since no cluster exists. A new cluster is created when acked.
24
+ * 1 | -2 | 1 -|
25
+ * 2 | | 2 -|
26
+ * 3 | | 3 --|-- Three more IDs are allocated as finals eagerly since a cluster exists with available capacity.
27
+ * 4 | | 4 -|
28
+ * 5 | -6 | 10 ----- One ID is allocated as a local (it overflows the existing cluster) and a new cluster is created after ack.
29
+ * 6 | | 11 ----- This ID (and the subsequent few) is allocated eagerly as a final ID into the existing cluster.
30
+ * 7 | | 12
31
+ * 8 | | 13
32
+ * 9 | | 14
33
+ * 10 | -11 | ----- The cluster is out of room, so a local ID is allocated. It has no corresponding final ID since it has not been acked.
34
+ * ```
35
+ *
36
+ * This class stores the set of local IDs and thus their indices, as local IDs are essentially negative offsets from the first ID.
37
+ * The form (local or final) of a given ID index (for example, the 5th ID made by the local session) can be deduced from this information.
38
+ */
39
+ export declare class SessionSpaceNormalizer {
40
+ private readonly localIdRanges;
41
+ get idRanges(): Pick<AppendOnlySortedMap<number, number>, "size" | "entries">;
42
+ addLocalRange(baseGenCount: number, count: number): void;
43
+ contains(query: LocalCompressedId): boolean;
44
+ equals(other: SessionSpaceNormalizer): boolean;
45
+ }
46
+ //# sourceMappingURL=sessionSpaceNormalizer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessionSpaceNormalizer.d.ts","sourceRoot":"","sources":["../../src/id-compressor/sessionSpaceNormalizer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAGlD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,qBAAa,sBAAsB;IAGlC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAiE;IAE/F,IAAW,QAAQ,IAAI,IAAI,CAAC,mBAAmB,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,CAEnF;IAEM,aAAa,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAcxD,QAAQ,CAAC,KAAK,EAAE,iBAAiB,GAAG,OAAO;IAY3C,MAAM,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO;CAMrD"}
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.SessionSpaceNormalizer = void 0;
8
+ const appendOnlySortedMap_1 = require("./appendOnlySortedMap");
9
+ const utilities_1 = require("./utilities");
10
+ /**
11
+ * The `SessionSpaceNormalizer` tracks the form of the IDs created by the local session.
12
+ * More precisely, it acts as a set of all `LocalCompressedId`s created by the local session and allows querying whether a specific
13
+ * ID was produced by the local session. This information can be used to determine whether the nth ID created by the local session
14
+ * was produced as a local ID (negative) or a final ID (positive).
15
+ *
16
+ * The local and final forms of IDs made by a session can be thought of as two equal-length sparse arrays, aligned such
17
+ * that normalizeLocalToFinal(locals[i]) === finals[i] and vice versa.
18
+ * Below is an example to illustrate how various mappings can arise:
19
+ *
20
+ * ```
21
+ * +- Creation Index
22
+ * / +- Locals
23
+ * / / +- Finals
24
+ * / / /
25
+ * ---+-----+----
26
+ * 0 | -1 | 0 -|___ Two IDs are allocated as locals since no cluster exists. A new cluster is created when acked.
27
+ * 1 | -2 | 1 -|
28
+ * 2 | | 2 -|
29
+ * 3 | | 3 --|-- Three more IDs are allocated as finals eagerly since a cluster exists with available capacity.
30
+ * 4 | | 4 -|
31
+ * 5 | -6 | 10 ----- One ID is allocated as a local (it overflows the existing cluster) and a new cluster is created after ack.
32
+ * 6 | | 11 ----- This ID (and the subsequent few) is allocated eagerly as a final ID into the existing cluster.
33
+ * 7 | | 12
34
+ * 8 | | 13
35
+ * 9 | | 14
36
+ * 10 | -11 | ----- The cluster is out of room, so a local ID is allocated. It has no corresponding final ID since it has not been acked.
37
+ * ```
38
+ *
39
+ * This class stores the set of local IDs and thus their indices, as local IDs are essentially negative offsets from the first ID.
40
+ * The form (local or final) of a given ID index (for example, the 5th ID made by the local session) can be deduced from this information.
41
+ */
42
+ class SessionSpaceNormalizer {
43
+ constructor() {
44
+ // Run-length encoding of IDs that were generated as local IDs. They are stored as a list of tuples (genCount, count)
45
+ // that are sorted on the genCount so that contains checks can use a binary search.
46
+ this.localIdRanges = new appendOnlySortedMap_1.AppendOnlySortedMap(utilities_1.compareFiniteNumbers);
47
+ }
48
+ get idRanges() {
49
+ return this.localIdRanges;
50
+ }
51
+ addLocalRange(baseGenCount, count) {
52
+ const last = this.localIdRanges.last();
53
+ if (last !== undefined) {
54
+ const [lastGenCount, lastCount] = last;
55
+ // Check to see if the added run of local IDs is contiguous with the last range added.
56
+ // If it is, simply merge them (this is the common case).
57
+ if (lastGenCount + lastCount === baseGenCount) {
58
+ this.localIdRanges.replaceLast(lastGenCount, lastCount + count);
59
+ return;
60
+ }
61
+ }
62
+ this.localIdRanges.append(baseGenCount, count);
63
+ }
64
+ contains(query) {
65
+ const genCount = (0, utilities_1.genCountFromLocalId)(query);
66
+ const containingBlock = this.localIdRanges.getPairOrNextLower(genCount);
67
+ if (containingBlock !== undefined) {
68
+ const [baseGenCount, count] = containingBlock;
69
+ if (genCount <= baseGenCount + (count - 1)) {
70
+ return true;
71
+ }
72
+ }
73
+ return false;
74
+ }
75
+ equals(other) {
76
+ return this.localIdRanges.equals(other.localIdRanges, (countA, countB) => countA === countB);
77
+ }
78
+ }
79
+ exports.SessionSpaceNormalizer = SessionSpaceNormalizer;
80
+ //# sourceMappingURL=sessionSpaceNormalizer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessionSpaceNormalizer.js","sourceRoot":"","sources":["../../src/id-compressor/sessionSpaceNormalizer.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,+DAA4D;AAE5D,2CAAwE;AAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAa,sBAAsB;IAAnC;QACC,qHAAqH;QACrH,mFAAmF;QAClE,kBAAa,GAAG,IAAI,yCAAmB,CAAiB,gCAAoB,CAAC,CAAC;IAsChG,CAAC;IApCA,IAAW,QAAQ;QAClB,OAAO,IAAI,CAAC,aAAa,CAAC;IAC3B,CAAC;IAEM,aAAa,CAAC,YAAoB,EAAE,KAAa;QACvD,MAAM,IAAI,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;QACvC,IAAI,IAAI,KAAK,SAAS,EAAE;YACvB,MAAM,CAAC,YAAY,EAAE,SAAS,CAAC,GAAG,IAAI,CAAC;YACvC,sFAAsF;YACtF,yDAAyD;YACzD,IAAI,YAAY,GAAG,SAAS,KAAK,YAAY,EAAE;gBAC9C,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,YAAY,EAAE,SAAS,GAAG,KAAK,CAAC,CAAC;gBAChE,OAAO;aACP;SACD;QACD,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;IAChD,CAAC;IAEM,QAAQ,CAAC,KAAwB;QACvC,MAAM,QAAQ,GAAG,IAAA,+BAAmB,EAAC,KAAK,CAAC,CAAC;QAC5C,MAAM,eAAe,GAAG,IAAI,CAAC,aAAa,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC;QACxE,IAAI,eAAe,KAAK,SAAS,EAAE;YAClC,MAAM,CAAC,YAAY,EAAE,KAAK,CAAC,GAAG,eAAe,CAAC;YAC9C,IAAI,QAAQ,IAAI,YAAY,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE;gBAC3C,OAAO,IAAI,CAAC;aACZ;SACD;QACD,OAAO,KAAK,CAAC;IACd,CAAC;IAEM,MAAM,CAAC,KAA6B;QAC1C,OAAO,IAAI,CAAC,aAAa,CAAC,MAAM,CAC/B,KAAK,CAAC,aAAa,EACnB,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC,MAAM,KAAK,MAAM,CACrC,CAAC;IACH,CAAC;CACD;AAzCD,wDAyCC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { AppendOnlySortedMap } from \"./appendOnlySortedMap\";\nimport { LocalCompressedId } from \"./identifiers\";\nimport { compareFiniteNumbers, genCountFromLocalId } from \"./utilities\";\n\n/**\n * The `SessionSpaceNormalizer` tracks the form of the IDs created by the local session.\n * More precisely, it acts as a set of all `LocalCompressedId`s created by the local session and allows querying whether a specific\n * ID was produced by the local session. This information can be used to determine whether the nth ID created by the local session\n * was produced as a local ID (negative) or a final ID (positive).\n *\n * The local and final forms of IDs made by a session can be thought of as two equal-length sparse arrays, aligned such\n * that normalizeLocalToFinal(locals[i]) === finals[i] and vice versa.\n * Below is an example to illustrate how various mappings can arise:\n *\n * ```\n * +- Creation Index\n * / +- Locals\n * / / +- Finals\n * / / /\n * ---+-----+----\n * 0 | -1 | 0 -|___ Two IDs are allocated as locals since no cluster exists. A new cluster is created when acked.\n * 1 | -2 | 1 -|\n * 2 | | 2 -|\n * 3 | | 3 --|-- Three more IDs are allocated as finals eagerly since a cluster exists with available capacity.\n * 4 | | 4 -|\n * 5 | -6 | 10 ----- One ID is allocated as a local (it overflows the existing cluster) and a new cluster is created after ack.\n * 6 | | 11 ----- This ID (and the subsequent few) is allocated eagerly as a final ID into the existing cluster.\n * 7 | | 12\n * 8 | | 13\n * 9 | | 14\n * 10 | -11 | ----- The cluster is out of room, so a local ID is allocated. It has no corresponding final ID since it has not been acked.\n * ```\n *\n * This class stores the set of local IDs and thus their indices, as local IDs are essentially negative offsets from the first ID.\n * The form (local or final) of a given ID index (for example, the 5th ID made by the local session) can be deduced from this information.\n */\nexport class SessionSpaceNormalizer {\n\t// Run-length encoding of IDs that were generated as local IDs. They are stored as a list of tuples (genCount, count)\n\t// that are sorted on the genCount so that contains checks can use a binary search.\n\tprivate readonly localIdRanges = new AppendOnlySortedMap<number, number>(compareFiniteNumbers);\n\n\tpublic get idRanges(): Pick<AppendOnlySortedMap<number, number>, \"size\" | \"entries\"> {\n\t\treturn this.localIdRanges;\n\t}\n\n\tpublic addLocalRange(baseGenCount: number, count: number): void {\n\t\tconst last = this.localIdRanges.last();\n\t\tif (last !== undefined) {\n\t\t\tconst [lastGenCount, lastCount] = last;\n\t\t\t// Check to see if the added run of local IDs is contiguous with the last range added.\n\t\t\t// If it is, simply merge them (this is the common case).\n\t\t\tif (lastGenCount + lastCount === baseGenCount) {\n\t\t\t\tthis.localIdRanges.replaceLast(lastGenCount, lastCount + count);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\tthis.localIdRanges.append(baseGenCount, count);\n\t}\n\n\tpublic contains(query: LocalCompressedId): boolean {\n\t\tconst genCount = genCountFromLocalId(query);\n\t\tconst containingBlock = this.localIdRanges.getPairOrNextLower(genCount);\n\t\tif (containingBlock !== undefined) {\n\t\t\tconst [baseGenCount, count] = containingBlock;\n\t\t\tif (genCount <= baseGenCount + (count - 1)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\n\tpublic equals(other: SessionSpaceNormalizer): boolean {\n\t\treturn this.localIdRanges.equals(\n\t\t\tother.localIdRanges,\n\t\t\t(countA, countB) => countA === countB,\n\t\t);\n\t}\n}\n"]}
@@ -0,0 +1,115 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { SessionId, StableId } from "@fluidframework/runtime-definitions";
6
+ import { FinalCompressedId, LocalCompressedId, NumericUuid } from "./identifiers";
7
+ /**
8
+ * A collection of all sessions known to the compressor (i.e. all finalized/acked allocated UUIDs and their corresponding local and final forms).
9
+ * This collection of all sessions comprises a distributed document's IDs.
10
+ */
11
+ export declare class Sessions {
12
+ private readonly uuidSpace;
13
+ private readonly sessionCache;
14
+ constructor(sessions?: [sessionBase: NumericUuid, session: Session][]);
15
+ sessions(): IterableIterator<Session>;
16
+ getOrCreate(sessionId: SessionId): Session;
17
+ get(sessionId: SessionId): Session | undefined;
18
+ getContainingCluster(query: StableId): [cluster: IdCluster, alignedLocal: LocalCompressedId] | undefined;
19
+ clusterCollides(cluster: IdCluster): boolean;
20
+ equals(other: Sessions, includeLocalState: boolean): boolean;
21
+ }
22
+ /**
23
+ * The IDs created by a specific session, stored as a cluster chain to allow for fast conversions.
24
+ */
25
+ export declare class Session {
26
+ private readonly clusterChain;
27
+ readonly sessionUuid: NumericUuid;
28
+ constructor(sessionId: SessionId | NumericUuid);
29
+ /**
30
+ * Adds a new empty cluster to the cluster chain of this session.
31
+ */
32
+ addNewCluster(baseFinalId: FinalCompressedId, capacity: number, count: number): IdCluster;
33
+ isEmpty(): boolean;
34
+ /**
35
+ * Returns the last cluster in this session's cluster chain, if any.
36
+ */
37
+ getLastCluster(): IdCluster | undefined;
38
+ /**
39
+ * Converts the local ID from this session to a final ID, if possible.
40
+ * @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.
41
+ */
42
+ tryConvertToFinal(searchLocal: LocalCompressedId, includeAllocated: boolean): FinalCompressedId | undefined;
43
+ /**
44
+ * Returns the cluster containing the supplied local ID, if possible.
45
+ * @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.
46
+ */
47
+ getClusterByLocal(localId: LocalCompressedId, includeAllocated: boolean): IdCluster | undefined;
48
+ /**
49
+ * Returns the cluster containing the supplied final ID, if possible.
50
+ */
51
+ getClusterByAllocatedFinal(final: FinalCompressedId): IdCluster | undefined;
52
+ /**
53
+ * Returns the cluster from the supplied cluster chain containing the supplied final ID, if possible.
54
+ * `clusterChain` must be sorted by final/local base ID.
55
+ */
56
+ static getContainingCluster(finalId: FinalCompressedId, clusterChain: readonly IdCluster[]): IdCluster | undefined;
57
+ static binarySearch<S, T>(search: S, arr: readonly T[], comparator: (a: S, b: T) => number): T | undefined;
58
+ equals(other: Session): boolean;
59
+ }
60
+ /**
61
+ * A cluster of final (sequenced via consensus), sequentially allocated compressed IDs.
62
+ * A final ID in a cluster decompresses to a sequentially allocated UUID that is the result of adding its offset within
63
+ * the cluster to base UUID for the session that created it.
64
+ */
65
+ export interface IdCluster {
66
+ /**
67
+ * The session that created this cluster.
68
+ */
69
+ readonly session: Session;
70
+ /**
71
+ * The first final ID in the cluster.
72
+ */
73
+ readonly baseFinalId: FinalCompressedId;
74
+ /**
75
+ * The local ID aligned with `baseFinalId`.
76
+ */
77
+ readonly baseLocalId: LocalCompressedId;
78
+ /**
79
+ * The total number of final IDs reserved for allocation in the cluster.
80
+ * Clusters are reserved in blocks as a performance optimization.
81
+ */
82
+ capacity: number;
83
+ /**
84
+ * The number of final IDs currently allocated in the cluster.
85
+ */
86
+ count: number;
87
+ }
88
+ export declare function clustersEqual(a: IdCluster, b: IdCluster): boolean;
89
+ /**
90
+ * Returns the final ID that is aligned with the supplied local ID within a cluster.
91
+ * Includes allocated IDs.
92
+ */
93
+ export declare function getAlignedFinal(cluster: IdCluster, localWithin: LocalCompressedId): FinalCompressedId | undefined;
94
+ /**
95
+ * Returns the local ID that is aligned with the supplied final ID within a cluster.
96
+ * Fails if the supplied ID does not fall within the cluster bounds.
97
+ */
98
+ export declare function getAlignedLocal(cluster: IdCluster, finalWithin: FinalCompressedId): LocalCompressedId;
99
+ /**
100
+ * Returns the last allocated final ID (i.e. any ID between base final and base final + capacity) within a cluster
101
+ */
102
+ export declare function lastAllocatedFinal(cluster: IdCluster): FinalCompressedId;
103
+ /**
104
+ * Returns the last allocated final ID (i.e. any ID between base final and base final + count) within a cluster
105
+ */
106
+ export declare function lastFinalizedFinal(cluster: IdCluster): FinalCompressedId;
107
+ /**
108
+ * Returns the last allocated local ID (i.e. any ID between base local and base local + capacity) within a cluster
109
+ */
110
+ export declare function lastAllocatedLocal(cluster: IdCluster): LocalCompressedId;
111
+ /**
112
+ * Returns the last allocated local ID (i.e. any ID between base local and base local + count) within a cluster
113
+ */
114
+ export declare function lastFinalizedLocal(cluster: IdCluster): LocalCompressedId;
115
+ //# sourceMappingURL=sessions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessions.d.ts","sourceRoot":"","sources":["../../src/id-compressor/sessions.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,qCAAqC,CAAC;AAW1E,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAElF;;;GAGG;AACH,qBAAa,QAAQ;IAGpB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA8D;IAExF,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAiC;gBAE3C,QAAQ,CAAC,EAAE,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE;IAgBrE,QAAQ,IAAI,gBAAgB,CAAC,OAAO,CAAC;IAIrC,WAAW,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO;IAc1C,GAAG,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,GAAG,SAAS;IAI9C,oBAAoB,CAC1B,KAAK,EAAE,QAAQ,GACb,CAAC,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,iBAAiB,CAAC,GAAG,SAAS;IAmB7D,eAAe,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO;IA0C5C,MAAM,CAAC,KA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C;IAExC;;OAEG;IACH,QAAQ,CAAC,WAAW,EAAE,iBAAiB,CAAC;IAExC;;;OAGG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;CACd;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,OAAO,CAQjE;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC9B,OAAO,EAAE,SAAS,EAClB,WAAW,EAAE,iBAAiB,GAC5B,iBAAiB,GAAG,SAAS,CAO/B;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC9B,OAAO,EAAE,SAAS,EAClB,WAAW,EAAE,iBAAiB,GAC5B,iBAAiB,CAOnB;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,SAAS,GAAG,iBAAiB,CAExE;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,SAAS,GAAG,iBAAiB,CAExE;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,SAAS,GAAG,iBAAiB,CAExE;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,SAAS,GAAG,iBAAiB,CAExE"}
@@ -0,0 +1,305 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ var __importDefault = (this && this.__importDefault) || function (mod) {
7
+ return (mod && mod.__esModule) ? mod : { "default": mod };
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.lastFinalizedLocal = exports.lastAllocatedLocal = exports.lastFinalizedFinal = exports.lastAllocatedFinal = exports.getAlignedLocal = exports.getAlignedFinal = exports.clustersEqual = exports.Session = exports.Sessions = void 0;
11
+ const sorted_btree_1 = __importDefault(require("sorted-btree"));
12
+ const common_utils_1 = require("@fluidframework/common-utils");
13
+ const utilities_1 = require("./utilities");
14
+ /**
15
+ * A collection of all sessions known to the compressor (i.e. all finalized/acked allocated UUIDs and their corresponding local and final forms).
16
+ * This collection of all sessions comprises a distributed document's IDs.
17
+ */
18
+ class Sessions {
19
+ constructor(sessions) {
20
+ // A range-queryable store of all sessions. A btree is used as it solves the predecessor problem for any given UUID, allowing
21
+ // us to quickly find the session that may have produced it.
22
+ this.uuidSpace = new sorted_btree_1.default(undefined, utilities_1.compareBigints);
23
+ // A fast lookup table from session ID to the session object, used to avoid accessing the slower btree
24
+ this.sessionCache = new Map();
25
+ if (sessions !== undefined) {
26
+ // bulk load path
27
+ for (const [numeric, session] of sessions) {
28
+ this.sessionCache.set((0, utilities_1.stableIdFromNumericUuid)(numeric), session);
29
+ }
30
+ this.uuidSpace = new sorted_btree_1.default(sessions, utilities_1.compareBigints);
31
+ if (this.sessionCache.size !== sessions.length ||
32
+ sessions.length !== this.uuidSpace.size) {
33
+ throw new Error("Cannot resume existing session.");
34
+ }
35
+ }
36
+ }
37
+ sessions() {
38
+ return this.sessionCache.values();
39
+ }
40
+ getOrCreate(sessionId) {
41
+ const existing = this.sessionCache.get(sessionId);
42
+ if (existing !== undefined) {
43
+ return existing;
44
+ }
45
+ const session = new Session(sessionId);
46
+ (0, common_utils_1.assert)(this.uuidSpace.set(session.sessionUuid, session), 0x760 /* Duplicate session in map. */);
47
+ this.sessionCache.set(sessionId, session);
48
+ return session;
49
+ }
50
+ get(sessionId) {
51
+ return this.sessionCache.get(sessionId);
52
+ }
53
+ getContainingCluster(query) {
54
+ const numericStable = (0, utilities_1.numericUuidFromStableId)(query);
55
+ const possibleMatch = this.uuidSpace.getPairOrNextLower(numericStable);
56
+ if (possibleMatch === undefined) {
57
+ return undefined;
58
+ }
59
+ const [_, session] = possibleMatch;
60
+ const numericDelta = (0, utilities_1.subtractNumericUuids)(numericStable, session.sessionUuid);
61
+ if (numericDelta > Number.MAX_SAFE_INTEGER) {
62
+ return undefined;
63
+ }
64
+ const alignedLocal = (0, utilities_1.localIdFromGenCount)(Number(numericDelta) + 1);
65
+ const containingCluster = session.getClusterByLocal(alignedLocal, true);
66
+ if (containingCluster === undefined) {
67
+ return undefined;
68
+ }
69
+ return [containingCluster, alignedLocal];
70
+ }
71
+ clusterCollides(cluster) {
72
+ const { session: owningSession, baseLocalId, capacity } = cluster;
73
+ const clusterBaseNumeric = (0, utilities_1.offsetNumericUuid)(owningSession.sessionUuid, (0, utilities_1.genCountFromLocalId)(baseLocalId) - 1);
74
+ const clusterMaxNumeric = (0, utilities_1.offsetNumericUuid)(clusterBaseNumeric, capacity - 1);
75
+ let closestMatch = this.uuidSpace.getPairOrNextLower(clusterMaxNumeric);
76
+ // Find the first session that is not the owner of this new cluster.
77
+ // Once we have that, check to see if its cluster chain overlaps with the new cluster.
78
+ // Consider the following diagram of UUID space:
79
+ // Cluster chain A: |----------------------|
80
+ // Cluster chain B: |----------|
81
+ // Cluster chain C: |-------|
82
+ // While it is true that when adding a cluster to chain C, we would find
83
+ // the next lower session (which is B) and erroneously determine we do not collide
84
+ // with any other session, but this situation is impossible to get into as B would
85
+ // have detected that it collided with A (or the other way around, depending on ordering).
86
+ while (closestMatch !== undefined && closestMatch[1] === owningSession) {
87
+ closestMatch = this.uuidSpace.nextLowerPair(closestMatch[0]);
88
+ }
89
+ if (closestMatch === undefined) {
90
+ return false;
91
+ }
92
+ const [_, session] = closestMatch;
93
+ (0, common_utils_1.assert)(session !== owningSession, 0x761 /* Failed to attempt to detect collisions. */);
94
+ const lastCluster = session.getLastCluster();
95
+ if (lastCluster === undefined) {
96
+ // If the closest session is empty (the local session), then it is guaranteed (probabilistically) that there are no
97
+ // non-empty sessions that have a cluster chain that starts prior to the empty session and collides with the cluster
98
+ // we are checking, so we can return false.
99
+ return false;
100
+ }
101
+ const lastAllocatedNumeric = (0, utilities_1.offsetNumericUuid)(session.sessionUuid, (0, utilities_1.genCountFromLocalId)(lastAllocatedLocal(lastCluster)) - 1);
102
+ return lastAllocatedNumeric >= clusterBaseNumeric;
103
+ }
104
+ equals(other, includeLocalState) {
105
+ const checkIsSubset = (sessionsA, sessionsB) => {
106
+ const first = sessionsA.sessions().next();
107
+ const firstSessionThis = first.done ? undefined : first.value;
108
+ for (const [stableId, session] of sessionsA.sessionCache.entries()) {
109
+ const otherSession = sessionsB.sessionCache.get(stableId);
110
+ if (otherSession === undefined) {
111
+ if (!session.isEmpty() || includeLocalState) {
112
+ return false;
113
+ }
114
+ (0, common_utils_1.assert)(session === firstSessionThis, 0x762 /* The only non-empty session must be the local session. */);
115
+ }
116
+ else if (!session.equals(otherSession)) {
117
+ return false;
118
+ }
119
+ }
120
+ return true;
121
+ };
122
+ return checkIsSubset(this, other) && checkIsSubset(other, this);
123
+ }
124
+ }
125
+ exports.Sessions = Sessions;
126
+ /**
127
+ * The IDs created by a specific session, stored as a cluster chain to allow for fast conversions.
128
+ */
129
+ class Session {
130
+ constructor(sessionId) {
131
+ // All clusters created by this session, in creation order (thus sorted by base final and local ID).
132
+ this.clusterChain = [];
133
+ this.sessionUuid =
134
+ typeof sessionId === "string" ? (0, utilities_1.numericUuidFromStableId)(sessionId) : sessionId;
135
+ }
136
+ /**
137
+ * Adds a new empty cluster to the cluster chain of this session.
138
+ */
139
+ addNewCluster(baseFinalId, capacity, count) {
140
+ const lastCluster = this.getLastCluster();
141
+ const newCluster = {
142
+ session: this,
143
+ baseFinalId,
144
+ baseLocalId: (lastCluster === undefined
145
+ ? -1
146
+ : lastAllocatedLocal(lastCluster) - 1),
147
+ capacity,
148
+ count,
149
+ };
150
+ this.clusterChain.push(newCluster);
151
+ return newCluster;
152
+ }
153
+ isEmpty() {
154
+ return this.clusterChain.length === 0;
155
+ }
156
+ /**
157
+ * Returns the last cluster in this session's cluster chain, if any.
158
+ */
159
+ getLastCluster() {
160
+ return this.clusterChain[this.clusterChain.length - 1];
161
+ }
162
+ /**
163
+ * Converts the local ID from this session to a final ID, if possible.
164
+ * @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.
165
+ */
166
+ tryConvertToFinal(searchLocal, includeAllocated) {
167
+ const containingCluster = this.getClusterByLocal(searchLocal, includeAllocated);
168
+ if (containingCluster === undefined) {
169
+ return undefined;
170
+ }
171
+ return getAlignedFinal(containingCluster, searchLocal);
172
+ }
173
+ /**
174
+ * Returns the cluster containing the supplied local ID, if possible.
175
+ * @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.
176
+ */
177
+ getClusterByLocal(localId, includeAllocated) {
178
+ const lastValidLocal = includeAllocated
179
+ ? lastAllocatedLocal
180
+ : lastFinalizedLocal;
181
+ const matchedCluster = Session.binarySearch(localId, this.clusterChain, (local, cluster) => {
182
+ const lastLocal = lastValidLocal(cluster);
183
+ if (local < lastLocal) {
184
+ return 1;
185
+ }
186
+ else if (local > cluster.baseLocalId) {
187
+ return -1;
188
+ }
189
+ else {
190
+ return 0;
191
+ }
192
+ });
193
+ return matchedCluster;
194
+ }
195
+ /**
196
+ * Returns the cluster containing the supplied final ID, if possible.
197
+ */
198
+ getClusterByAllocatedFinal(final) {
199
+ return Session.getContainingCluster(final, this.clusterChain);
200
+ }
201
+ /**
202
+ * Returns the cluster from the supplied cluster chain containing the supplied final ID, if possible.
203
+ * `clusterChain` must be sorted by final/local base ID.
204
+ */
205
+ static getContainingCluster(finalId, clusterChain) {
206
+ return Session.binarySearch(finalId, clusterChain, (final, cluster) => {
207
+ const lastFinal = lastAllocatedFinal(cluster);
208
+ if (final < cluster.baseFinalId) {
209
+ return -1;
210
+ }
211
+ else if (final > lastFinal) {
212
+ return 1;
213
+ }
214
+ else {
215
+ return 0;
216
+ }
217
+ });
218
+ }
219
+ static binarySearch(search, arr, comparator) {
220
+ let left = 0;
221
+ let right = arr.length - 1;
222
+ while (left <= right) {
223
+ const mid = Math.floor((left + right) / 2);
224
+ const c = comparator(search, arr[mid]);
225
+ if (c === 0) {
226
+ return arr[mid]; // Found the target, return its index.
227
+ }
228
+ else if (c > 0) {
229
+ left = mid + 1; // Continue search on right half.
230
+ }
231
+ else {
232
+ right = mid - 1; // Continue search on left half.
233
+ }
234
+ }
235
+ return undefined; // If we reach here, target is not in array.
236
+ }
237
+ equals(other) {
238
+ for (let i = 0; i < this.clusterChain.length; i++) {
239
+ if (!clustersEqual(this.clusterChain[i], other.clusterChain[i])) {
240
+ return false;
241
+ }
242
+ }
243
+ return this.sessionUuid === other.sessionUuid;
244
+ }
245
+ }
246
+ exports.Session = Session;
247
+ function clustersEqual(a, b) {
248
+ return (a.session.sessionUuid === b.session.sessionUuid &&
249
+ a.baseFinalId === b.baseFinalId &&
250
+ a.baseLocalId === b.baseLocalId &&
251
+ a.capacity === b.capacity &&
252
+ a.count === b.count);
253
+ }
254
+ exports.clustersEqual = clustersEqual;
255
+ /**
256
+ * Returns the final ID that is aligned with the supplied local ID within a cluster.
257
+ * Includes allocated IDs.
258
+ */
259
+ function getAlignedFinal(cluster, localWithin) {
260
+ const clusterOffset = (0, utilities_1.genCountFromLocalId)(localWithin) - (0, utilities_1.genCountFromLocalId)(cluster.baseLocalId);
261
+ if (clusterOffset < cluster.capacity) {
262
+ return (cluster.baseFinalId + clusterOffset);
263
+ }
264
+ return undefined;
265
+ }
266
+ exports.getAlignedFinal = getAlignedFinal;
267
+ /**
268
+ * Returns the local ID that is aligned with the supplied final ID within a cluster.
269
+ * Fails if the supplied ID does not fall within the cluster bounds.
270
+ */
271
+ function getAlignedLocal(cluster, finalWithin) {
272
+ (0, common_utils_1.assert)(finalWithin >= cluster.baseFinalId && finalWithin <= lastAllocatedFinal(cluster), 0x763 /* Supplied ID is not within the cluster. */);
273
+ const finalDelta = finalWithin - cluster.baseFinalId;
274
+ return (cluster.baseLocalId - finalDelta);
275
+ }
276
+ exports.getAlignedLocal = getAlignedLocal;
277
+ /**
278
+ * Returns the last allocated final ID (i.e. any ID between base final and base final + capacity) within a cluster
279
+ */
280
+ function lastAllocatedFinal(cluster) {
281
+ return (cluster.baseFinalId + (cluster.capacity - 1));
282
+ }
283
+ exports.lastAllocatedFinal = lastAllocatedFinal;
284
+ /**
285
+ * Returns the last allocated final ID (i.e. any ID between base final and base final + count) within a cluster
286
+ */
287
+ function lastFinalizedFinal(cluster) {
288
+ return (cluster.baseFinalId + (cluster.count - 1));
289
+ }
290
+ exports.lastFinalizedFinal = lastFinalizedFinal;
291
+ /**
292
+ * Returns the last allocated local ID (i.e. any ID between base local and base local + capacity) within a cluster
293
+ */
294
+ function lastAllocatedLocal(cluster) {
295
+ return (cluster.baseLocalId - (cluster.capacity - 1));
296
+ }
297
+ exports.lastAllocatedLocal = lastAllocatedLocal;
298
+ /**
299
+ * Returns the last allocated local ID (i.e. any ID between base local and base local + count) within a cluster
300
+ */
301
+ function lastFinalizedLocal(cluster) {
302
+ return (cluster.baseLocalId - (cluster.count - 1));
303
+ }
304
+ exports.lastFinalizedLocal = lastFinalizedLocal;
305
+ //# sourceMappingURL=sessions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessions.js","sourceRoot":"","sources":["../../src/id-compressor/sessions.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAEH,gEAAiC;AAEjC,+DAAsD;AACtD,2CAQqB;AAGrB;;;GAGG;AACH,MAAa,QAAQ;IAOpB,YAAmB,QAAyD;QAN5E,6HAA6H;QAC7H,4DAA4D;QAC3C,cAAS,GAAG,IAAI,sBAAK,CAAuB,SAAS,EAAE,0BAAc,CAAC,CAAC;QACxF,sGAAsG;QACrF,iBAAY,GAAG,IAAI,GAAG,EAAsB,CAAC;QAG7D,IAAI,QAAQ,KAAK,SAAS,EAAE;YAC3B,iBAAiB;YACjB,KAAK,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,QAAQ,EAAE;gBAC1C,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,IAAA,mCAAuB,EAAC,OAAO,CAAc,EAAE,OAAO,CAAC,CAAC;aAC9E;YACD,IAAI,CAAC,SAAS,GAAG,IAAI,sBAAK,CAAC,QAAQ,EAAE,0BAA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sB,GAAG,CAAC,OAAO,CAAC,KAAK,GAAG,CAAC,CAAC,CAAsB,CAAC;AACrF,CAAC;AAFD,gDAEC;AAED;;GAEG;AACH,SAAgB,kBAAkB,CAAC,OAAkB;IACpD,OAAO,CAAE,OAAO,CAAC,WAAsB,GAAG,CAAC,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAsB,CAAC;AACxF,CAAC;AAFD,gDAEC;AAED;;GAEG;AACH,SAAgB,kBAAkB,CAAC,OAAkB;IACpD,OAAO,CAAE,OAAO,CAAC,WAAsB,GAAG,CAAC,OAAO,CAAC,KAAK,GAAG,CAAC,CAAC,CAAsB,CAAC;AACrF,CAAC;AAFD,gDAEC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport BTree from \"sorted-btree\";\nimport { SessionId, StableId } from \"@fluidframework/runtime-definitions\";\nimport { assert } from \"@fluidframework/common-utils\";\nimport {\n\tcompareBigints,\n\tlocalIdFromGenCount,\n\tgenCountFromLocalId,\n\tnumericUuidFromStableId,\n\tstableIdFromNumericUuid,\n\tsubtractNumericUuids,\n\toffsetNumericUuid,\n} from \"./utilities\";\nimport { FinalCompressedId, LocalCompressedId, NumericUuid } from \"./identifiers\";\n\n/**\n * A collection of all sessions known to the compressor (i.e. all finalized/acked allocated UUIDs and their corresponding local and final forms).\n * This collection of all sessions comprises a distributed document's IDs.\n */\nexport class Sessions {\n\t// A range-queryable store of all sessions. A btree is used as it solves the predecessor problem for any given UUID, allowing\n\t// us to quickly find the session that may have produced it.\n\tprivate readonly uuidSpace = new BTree<NumericUuid, Session>(undefined, compareBigints);\n\t// A fast lookup table from session ID to the session object, used to avoid accessing the slower btree\n\tprivate readonly sessionCache = new Map<SessionId, Session>();\n\n\tpublic constructor(sessions?: [sessionBase: NumericUuid, session: Session][]) {\n\t\tif (sessions !== undefined) {\n\t\t\t// bulk load path\n\t\t\tfor (const [numeric, session] of sessions) {\n\t\t\t\tthis.sessionCache.set(stableIdFromNumericUuid(numeric) as SessionId, session);\n\t\t\t}\n\t\t\tthis.uuidSpace = new BTree(sessions, compareBigints);\n\t\t\tif (\n\t\t\t\tthis.sessionCache.size !== sessions.length ||\n\t\t\t\tsessions.length !== this.uuidSpace.size\n\t\t\t) {\n\t\t\t\tthrow new Error(\"Cannot resume existing session.\");\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic sessions(): IterableIterator<Session> {\n\t\treturn this.sessionCache.values();\n\t}\n\n\tpublic getOrCreate(sessionId: SessionId): Session {\n\t\tconst existing = this.sessionCache.get(sessionId);\n\t\tif (existing !== undefined) {\n\t\t\treturn existing;\n\t\t}\n\t\tconst session = new Session(sessionId);\n\t\tassert(\n\t\t\tthis.uuidSpace.set(session.sessionUuid, session),\n\t\t\t0x760 /* Duplicate session in map. */,\n\t\t);\n\t\tthis.sessionCache.set(sessionId, session);\n\t\treturn session;\n\t}\n\n\tpublic get(sessionId: SessionId): Session | undefined {\n\t\treturn this.sessionCache.get(sessionId);\n\t}\n\n\tpublic getContainingCluster(\n\t\tquery: StableId,\n\t): [cluster: IdCluster, alignedLocal: LocalCompressedId] | undefined {\n\t\tconst numericStable = numericUuidFromStableId(query);\n\t\tconst possibleMatch = this.uuidSpace.getPairOrNextLower(numericStable);\n\t\tif (possibleMatch === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst [_, session] = possibleMatch;\n\t\tconst numericDelta = subtractNumericUuids(numericStable, session.sessionUuid);\n\t\tif (numericDelta > Number.MAX_SAFE_INTEGER) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst alignedLocal = localIdFromGenCount(Number(numericDelta) + 1);\n\t\tconst containingCluster = session.getClusterByLocal(alignedLocal, true);\n\t\tif (containingCluster === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn [containingCluster, alignedLocal];\n\t}\n\n\tpublic clusterCollides(cluster: IdCluster): boolean {\n\t\tconst { session: owningSession, baseLocalId, capacity } = cluster;\n\t\tconst clusterBaseNumeric = offsetNumericUuid(\n\t\t\towningSession.sessionUuid,\n\t\t\tgenCountFromLocalId(baseLocalId) - 1,\n\t\t);\n\t\tconst clusterMaxNumeric = offsetNumericUuid(clusterBaseNumeric, capacity - 1);\n\t\tlet closestMatch: [NumericUuid, Session] | undefined =\n\t\t\tthis.uuidSpace.getPairOrNextLower(clusterMaxNumeric);\n\t\t// Find the first session that is not the owner of this new cluster.\n\t\t// Once we have that, check to see if its cluster chain overlaps with the new cluster.\n\t\t// Consider the following diagram of UUID space:\n\t\t// Cluster chain A: |----------------------|\n\t\t// Cluster chain B: |----------|\n\t\t// Cluster chain C: |-------|\n\t\t// While it is true that when adding a cluster to chain C, we would find\n\t\t// the next lower session (which is B) and erroneously determine we do not collide\n\t\t// with any other session, but this situation is impossible to get into as B would\n\t\t// have detected that it collided with A (or the other way around, depending on ordering).\n\t\twhile (closestMatch !== undefined && closestMatch[1] === owningSession) {\n\t\t\tclosestMatch = this.uuidSpace.nextLowerPair(closestMatch[0]);\n\t\t}\n\t\tif (closestMatch === undefined) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst [_, session] = closestMatch;\n\t\tassert(session !== owningSession, 0x761 /* Failed to attempt to detect collisions. */);\n\t\tconst lastCluster = session.getLastCluster();\n\t\tif (lastCluster === undefined) {\n\t\t\t// If the closest session is empty (the local session), then it is guaranteed (probabilistically) that there are no\n\t\t\t// non-empty sessions that have a cluster chain that starts prior to the empty session and collides with the cluster\n\t\t\t// we are checking, so we can return false.\n\t\t\treturn false;\n\t\t}\n\t\tconst lastAllocatedNumeric = offsetNumericUuid(\n\t\t\tsession.sessionUuid,\n\t\t\tgenCountFromLocalId(lastAllocatedLocal(lastCluster)) - 1,\n\t\t);\n\t\treturn lastAllocatedNumeric >= clusterBaseNumeric;\n\t}\n\n\tpublic equals(other: Sessions, includeLocalState: boolean): boolean {\n\t\tconst checkIsSubset = (sessionsA: Sessions, sessionsB: Sessions) => {\n\t\t\tconst first = sessionsA.sessions().next();\n\t\t\tconst firstSessionThis = first.done ? undefined : first.value;\n\t\t\tfor (const [stableId, session] of sessionsA.sessionCache.entries()) {\n\t\t\t\tconst otherSession = sessionsB.sessionCache.get(stableId);\n\t\t\t\tif (otherSession === undefined) {\n\t\t\t\t\tif (!session.isEmpty() || includeLocalState) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tassert(\n\t\t\t\t\t\tsession === firstSessionThis,\n\t\t\t\t\t\t0x762 /* The only non-empty session must be the local session. */,\n\t\t\t\t\t);\n\t\t\t\t} else if (!session.equals(otherSession)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn true;\n\t\t};\n\t\treturn checkIsSubset(this, other) && checkIsSubset(other, this);\n\t}\n}\n\n/**\n * The IDs created by a specific session, stored as a cluster chain to allow for fast conversions.\n */\nexport class Session {\n\t// All clusters created by this session, in creation order (thus sorted by base final and local ID).\n\tprivate readonly clusterChain: IdCluster[] = [];\n\t// The numeric form of the SessionId\n\tpublic readonly sessionUuid: NumericUuid;\n\n\tpublic constructor(sessionId: SessionId | NumericUuid) {\n\t\tthis.sessionUuid =\n\t\t\ttypeof sessionId === \"string\" ? numericUuidFromStableId(sessionId) : sessionId;\n\t}\n\n\t/**\n\t * Adds a new empty cluster to the cluster chain of this session.\n\t */\n\tpublic addNewCluster(\n\t\tbaseFinalId: FinalCompressedId,\n\t\tcapacity: number,\n\t\tcount: number,\n\t): IdCluster {\n\t\tconst lastCluster = this.getLastCluster();\n\t\tconst newCluster: IdCluster = {\n\t\t\tsession: this,\n\t\t\tbaseFinalId,\n\t\t\tbaseLocalId: (lastCluster === undefined\n\t\t\t\t? -1\n\t\t\t\t: lastAllocatedLocal(lastCluster) - 1) as LocalCompressedId,\n\t\t\tcapacity,\n\t\t\tcount,\n\t\t};\n\t\tthis.clusterChain.push(newCluster);\n\t\treturn newCluster;\n\t}\n\n\tpublic isEmpty(): boolean {\n\t\treturn this.clusterChain.length === 0;\n\t}\n\n\t/**\n\t * Returns the last cluster in this session's cluster chain, if any.\n\t */\n\tpublic getLastCluster(): IdCluster | undefined {\n\t\treturn this.clusterChain[this.clusterChain.length - 1];\n\t}\n\n\t/**\n\t * Converts the local ID from this session to a final ID, if possible.\n\t * @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.\n\t */\n\tpublic tryConvertToFinal(\n\t\tsearchLocal: LocalCompressedId,\n\t\tincludeAllocated: boolean,\n\t): FinalCompressedId | undefined {\n\t\tconst containingCluster = this.getClusterByLocal(searchLocal, includeAllocated);\n\t\tif (containingCluster === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn getAlignedFinal(containingCluster, searchLocal);\n\t}\n\n\t/**\n\t * Returns the cluster containing the supplied local ID, if possible.\n\t * @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.\n\t */\n\tpublic getClusterByLocal(\n\t\tlocalId: LocalCompressedId,\n\t\tincludeAllocated: boolean,\n\t): IdCluster | undefined {\n\t\tconst lastValidLocal: (cluster: IdCluster) => LocalCompressedId = includeAllocated\n\t\t\t? lastAllocatedLocal\n\t\t\t: lastFinalizedLocal;\n\t\tconst matchedCluster = Session.binarySearch(\n\t\t\tlocalId,\n\t\t\tthis.clusterChain,\n\t\t\t(local, cluster): number => {\n\t\t\t\tconst lastLocal = lastValidLocal(cluster);\n\t\t\t\tif (local < lastLocal) {\n\t\t\t\t\treturn 1;\n\t\t\t\t} else if (local > cluster.baseLocalId) {\n\t\t\t\t\treturn -1;\n\t\t\t\t} else {\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t},\n\t\t);\n\t\treturn matchedCluster;\n\t}\n\n\t/**\n\t * Returns the cluster containing the supplied final ID, if possible.\n\t */\n\tpublic getClusterByAllocatedFinal(final: FinalCompressedId): IdCluster | undefined {\n\t\treturn Session.getContainingCluster(final, this.clusterChain);\n\t}\n\n\t/**\n\t * Returns the cluster from the supplied cluster chain containing the supplied final ID, if possible.\n\t * `clusterChain` must be sorted by final/local base ID.\n\t */\n\tpublic static getContainingCluster(\n\t\tfinalId: FinalCompressedId,\n\t\tclusterChain: readonly IdCluster[],\n\t): IdCluster | undefined {\n\t\treturn Session.binarySearch(finalId, clusterChain, (final, cluster) => {\n\t\t\tconst lastFinal = lastAllocatedFinal(cluster);\n\t\t\tif (final < cluster.baseFinalId) {\n\t\t\t\treturn -1;\n\t\t\t} else if (final > lastFinal) {\n\t\t\t\treturn 1;\n\t\t\t} else {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t});\n\t}\n\n\tstatic binarySearch<S, T>(\n\t\tsearch: S,\n\t\tarr: readonly T[],\n\t\tcomparator: (a: S, b: T) => number,\n\t): T | undefined {\n\t\tlet left = 0;\n\t\tlet right = arr.length - 1;\n\t\twhile (left <= right) {\n\t\t\tconst mid = Math.floor((left + right) / 2);\n\t\t\tconst c = comparator(search, arr[mid]);\n\t\t\tif (c === 0) {\n\t\t\t\treturn arr[mid]; // Found the target, return its index.\n\t\t\t} else if (c > 0) {\n\t\t\t\tleft = mid + 1; // Continue search on right half.\n\t\t\t} else {\n\t\t\t\tright = mid - 1; // Continue search on left half.\n\t\t\t}\n\t\t}\n\t\treturn undefined; // If we reach here, target is not in array.\n\t}\n\n\tpublic equals(other: Session): boolean {\n\t\tfor (let i = 0; i < this.clusterChain.length; i++) {\n\t\t\tif (!clustersEqual(this.clusterChain[i], other.clusterChain[i])) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn this.sessionUuid === other.sessionUuid;\n\t}\n}\n\n/**\n * A cluster of final (sequenced via consensus), sequentially allocated compressed IDs.\n * A final ID in a cluster decompresses to a sequentially allocated UUID that is the result of adding its offset within\n * the cluster to base UUID for the session that created it.\n */\nexport interface IdCluster {\n\t/**\n\t * The session that created this cluster.\n\t */\n\treadonly session: Session;\n\n\t/**\n\t * The first final ID in the cluster.\n\t */\n\treadonly baseFinalId: FinalCompressedId;\n\n\t/**\n\t * The local ID aligned with `baseFinalId`.\n\t */\n\treadonly baseLocalId: LocalCompressedId;\n\n\t/**\n\t * The total number of final IDs reserved for allocation in the cluster.\n\t * Clusters are reserved in blocks as a performance optimization.\n\t */\n\tcapacity: number;\n\n\t/**\n\t * The number of final IDs currently allocated in the cluster.\n\t */\n\tcount: number;\n}\n\nexport function clustersEqual(a: IdCluster, b: IdCluster): boolean {\n\treturn (\n\t\ta.session.sessionUuid === b.session.sessionUuid &&\n\t\ta.baseFinalId === b.baseFinalId &&\n\t\ta.baseLocalId === b.baseLocalId &&\n\t\ta.capacity === b.capacity &&\n\t\ta.count === b.count\n\t);\n}\n\n/**\n * Returns the final ID that is aligned with the supplied local ID within a cluster.\n * Includes allocated IDs.\n */\nexport function getAlignedFinal(\n\tcluster: IdCluster,\n\tlocalWithin: LocalCompressedId,\n): FinalCompressedId | undefined {\n\tconst clusterOffset =\n\t\tgenCountFromLocalId(localWithin) - genCountFromLocalId(cluster.baseLocalId);\n\tif (clusterOffset < cluster.capacity) {\n\t\treturn ((cluster.baseFinalId as number) + clusterOffset) as FinalCompressedId;\n\t}\n\treturn undefined;\n}\n\n/**\n * Returns the local ID that is aligned with the supplied final ID within a cluster.\n * Fails if the supplied ID does not fall within the cluster bounds.\n */\nexport function getAlignedLocal(\n\tcluster: IdCluster,\n\tfinalWithin: FinalCompressedId,\n): LocalCompressedId {\n\tassert(\n\t\tfinalWithin >= cluster.baseFinalId && finalWithin <= lastAllocatedFinal(cluster),\n\t\t0x763 /* Supplied ID is not within the cluster. */,\n\t);\n\tconst finalDelta = finalWithin - cluster.baseFinalId;\n\treturn (cluster.baseLocalId - finalDelta) as LocalCompressedId;\n}\n\n/**\n * Returns the last allocated final ID (i.e. any ID between base final and base final + capacity) within a cluster\n */\nexport function lastAllocatedFinal(cluster: IdCluster): FinalCompressedId {\n\treturn ((cluster.baseFinalId as number) + (cluster.capacity - 1)) as FinalCompressedId;\n}\n\n/**\n * Returns the last allocated final ID (i.e. any ID between base final and base final + count) within a cluster\n */\nexport function lastFinalizedFinal(cluster: IdCluster): FinalCompressedId {\n\treturn ((cluster.baseFinalId as number) + (cluster.count - 1)) as FinalCompressedId;\n}\n\n/**\n * Returns the last allocated local ID (i.e. any ID between base local and base local + capacity) within a cluster\n */\nexport function lastAllocatedLocal(cluster: IdCluster): LocalCompressedId {\n\treturn ((cluster.baseLocalId as number) - (cluster.capacity - 1)) as LocalCompressedId;\n}\n\n/**\n * Returns the last allocated local ID (i.e. any ID between base local and base local + count) within a cluster\n */\nexport function lastFinalizedLocal(cluster: IdCluster): LocalCompressedId {\n\treturn ((cluster.baseLocalId as number) - (cluster.count - 1)) as LocalCompressedId;\n}\n"]}