@fluid-experimental/tree 0.58.2001 → 0.59.2000-61729

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 (817) hide show
  1. package/README.md +159 -46
  2. package/dist/ChangeCompression.d.ts +39 -0
  3. package/dist/ChangeCompression.d.ts.map +1 -0
  4. package/dist/ChangeCompression.js +117 -0
  5. package/dist/ChangeCompression.js.map +1 -0
  6. package/{lib/default-edits/PersistedTypes.d.ts → dist/ChangeTypes.d.ts} +58 -105
  7. package/dist/ChangeTypes.d.ts.map +1 -0
  8. package/dist/{default-edits/PersistedTypes.js → ChangeTypes.js} +21 -76
  9. package/dist/ChangeTypes.js.map +1 -0
  10. package/dist/Checkout.d.ts +39 -27
  11. package/dist/Checkout.d.ts.map +1 -1
  12. package/dist/Checkout.js +61 -32
  13. package/dist/Checkout.js.map +1 -1
  14. package/dist/Common.d.ts +175 -38
  15. package/dist/Common.d.ts.map +1 -1
  16. package/dist/Common.js +240 -103
  17. package/dist/Common.js.map +1 -1
  18. package/dist/EagerCheckout.d.ts +24 -0
  19. package/dist/EagerCheckout.d.ts.map +1 -0
  20. package/dist/{BasicCheckout.js → EagerCheckout.js} +9 -6
  21. package/dist/EagerCheckout.js.map +1 -0
  22. package/dist/EditLog.d.ts +77 -63
  23. package/dist/EditLog.d.ts.map +1 -1
  24. package/dist/EditLog.js +85 -48
  25. package/dist/EditLog.js.map +1 -1
  26. package/dist/EditUtilities.d.ts +168 -0
  27. package/dist/EditUtilities.d.ts.map +1 -0
  28. package/dist/EditUtilities.js +373 -0
  29. package/dist/EditUtilities.js.map +1 -0
  30. package/dist/EventTypes.d.ts +73 -0
  31. package/dist/EventTypes.d.ts.map +1 -0
  32. package/dist/EventTypes.js +78 -0
  33. package/dist/EventTypes.js.map +1 -0
  34. package/dist/Forest.d.ts +29 -7
  35. package/dist/Forest.d.ts.map +1 -1
  36. package/dist/Forest.js +60 -36
  37. package/dist/Forest.js.map +1 -1
  38. package/dist/HistoryEditFactory.d.ts +20 -0
  39. package/dist/HistoryEditFactory.d.ts.map +1 -0
  40. package/dist/HistoryEditFactory.js +226 -0
  41. package/dist/HistoryEditFactory.js.map +1 -0
  42. package/dist/IdConversion.d.ts +12 -0
  43. package/dist/IdConversion.d.ts.map +1 -0
  44. package/dist/IdConversion.js +98 -0
  45. package/dist/IdConversion.js.map +1 -0
  46. package/dist/Identifiers.d.ts +89 -2
  47. package/dist/Identifiers.d.ts.map +1 -1
  48. package/dist/Identifiers.js +10 -0
  49. package/dist/Identifiers.js.map +1 -1
  50. package/dist/InitialTree.d.ts +2 -2
  51. package/dist/InitialTree.d.ts.map +1 -1
  52. package/dist/InitialTree.js +2 -1
  53. package/dist/InitialTree.js.map +1 -1
  54. package/dist/LazyCheckout.d.ts +28 -0
  55. package/dist/LazyCheckout.d.ts.map +1 -0
  56. package/dist/LazyCheckout.js +44 -0
  57. package/dist/LazyCheckout.js.map +1 -0
  58. package/dist/LogViewer.d.ts +129 -85
  59. package/dist/LogViewer.d.ts.map +1 -1
  60. package/dist/LogViewer.js +111 -85
  61. package/dist/LogViewer.js.map +1 -1
  62. package/dist/MergeHealth.d.ts +221 -0
  63. package/dist/MergeHealth.d.ts.map +1 -0
  64. package/dist/MergeHealth.js +263 -0
  65. package/dist/MergeHealth.js.map +1 -0
  66. package/dist/NodeIdUtilities.d.ts +105 -0
  67. package/dist/NodeIdUtilities.d.ts.map +1 -0
  68. package/dist/NodeIdUtilities.js +60 -0
  69. package/dist/NodeIdUtilities.js.map +1 -0
  70. package/dist/PayloadUtilities.d.ts +42 -0
  71. package/dist/PayloadUtilities.d.ts.map +1 -0
  72. package/dist/PayloadUtilities.js +114 -0
  73. package/dist/PayloadUtilities.js.map +1 -0
  74. package/dist/ReconciliationPath.d.ts +18 -13
  75. package/dist/ReconciliationPath.d.ts.map +1 -1
  76. package/dist/ReconciliationPath.js.map +1 -1
  77. package/dist/RevisionValueCache.d.ts +11 -2
  78. package/dist/RevisionValueCache.d.ts.map +1 -1
  79. package/dist/RevisionValueCache.js +2 -3
  80. package/dist/RevisionValueCache.js.map +1 -1
  81. package/dist/RevisionView.d.ts +83 -0
  82. package/dist/RevisionView.d.ts.map +1 -0
  83. package/dist/RevisionView.js +182 -0
  84. package/dist/RevisionView.js.map +1 -0
  85. package/dist/SerializationUtilities.d.ts +36 -0
  86. package/dist/SerializationUtilities.d.ts.map +1 -0
  87. package/dist/SerializationUtilities.js +102 -0
  88. package/dist/SerializationUtilities.js.map +1 -0
  89. package/dist/SharedTree.d.ts +439 -0
  90. package/dist/SharedTree.d.ts.map +1 -0
  91. package/dist/SharedTree.js +1109 -0
  92. package/dist/SharedTree.js.map +1 -0
  93. package/dist/SharedTreeEncoder.d.ts +102 -0
  94. package/dist/SharedTreeEncoder.d.ts.map +1 -0
  95. package/dist/SharedTreeEncoder.js +313 -0
  96. package/dist/SharedTreeEncoder.js.map +1 -0
  97. package/dist/StringInterner.d.ts +46 -0
  98. package/dist/StringInterner.d.ts.map +1 -0
  99. package/dist/StringInterner.js +61 -0
  100. package/dist/StringInterner.js.map +1 -0
  101. package/dist/Summary.d.ts +40 -0
  102. package/dist/Summary.d.ts.map +1 -0
  103. package/dist/Summary.js +23 -0
  104. package/dist/Summary.js.map +1 -0
  105. package/dist/SummaryBackCompatibility.d.ts +22 -22
  106. package/dist/SummaryBackCompatibility.d.ts.map +1 -1
  107. package/dist/SummaryBackCompatibility.js +30 -33
  108. package/dist/SummaryBackCompatibility.js.map +1 -1
  109. package/dist/SummaryTestUtilities.d.ts +31 -0
  110. package/dist/SummaryTestUtilities.d.ts.map +1 -0
  111. package/dist/SummaryTestUtilities.js +37 -0
  112. package/dist/SummaryTestUtilities.js.map +1 -0
  113. package/dist/Transaction.d.ts +71 -0
  114. package/dist/Transaction.d.ts.map +1 -0
  115. package/dist/Transaction.js +92 -0
  116. package/dist/Transaction.js.map +1 -0
  117. package/dist/TransactionInternal.d.ts +540 -0
  118. package/dist/TransactionInternal.d.ts.map +1 -0
  119. package/dist/TransactionInternal.js +626 -0
  120. package/dist/TransactionInternal.js.map +1 -0
  121. package/dist/TreeCompressor.d.ts +36 -0
  122. package/dist/TreeCompressor.d.ts.map +1 -0
  123. package/dist/TreeCompressor.js +137 -0
  124. package/dist/TreeCompressor.js.map +1 -0
  125. package/dist/TreeNodeHandle.d.ts +12 -18
  126. package/dist/TreeNodeHandle.d.ts.map +1 -1
  127. package/dist/TreeNodeHandle.js +13 -23
  128. package/dist/TreeNodeHandle.js.map +1 -1
  129. package/dist/TreeView.d.ts +166 -0
  130. package/dist/TreeView.d.ts.map +1 -0
  131. package/dist/TreeView.js +218 -0
  132. package/dist/TreeView.js.map +1 -0
  133. package/dist/TreeViewUtilities.d.ts +21 -0
  134. package/dist/TreeViewUtilities.d.ts.map +1 -0
  135. package/dist/TreeViewUtilities.js +77 -0
  136. package/dist/TreeViewUtilities.js.map +1 -0
  137. package/dist/{default-edits/UndoRedoHandler.d.ts → UndoRedoHandler.d.ts} +2 -2
  138. package/dist/UndoRedoHandler.d.ts.map +1 -0
  139. package/dist/{default-edits/UndoRedoHandler.js → UndoRedoHandler.js} +5 -9
  140. package/dist/UndoRedoHandler.js.map +1 -0
  141. package/dist/id-compressor/AppendOnlySortedMap.d.ts +127 -0
  142. package/dist/id-compressor/AppendOnlySortedMap.d.ts.map +1 -0
  143. package/dist/id-compressor/AppendOnlySortedMap.js +283 -0
  144. package/dist/id-compressor/AppendOnlySortedMap.js.map +1 -0
  145. package/dist/id-compressor/IdCompressor.d.ts +389 -0
  146. package/dist/id-compressor/IdCompressor.d.ts.map +1 -0
  147. package/dist/id-compressor/IdCompressor.js +1353 -0
  148. package/dist/id-compressor/IdCompressor.js.map +1 -0
  149. package/dist/id-compressor/IdRange.d.ts +11 -0
  150. package/dist/id-compressor/IdRange.d.ts.map +1 -0
  151. package/dist/id-compressor/IdRange.js +29 -0
  152. package/dist/id-compressor/IdRange.js.map +1 -0
  153. package/dist/id-compressor/NumericUuid.d.ts +63 -0
  154. package/dist/id-compressor/NumericUuid.d.ts.map +1 -0
  155. package/dist/id-compressor/NumericUuid.js +377 -0
  156. package/dist/id-compressor/NumericUuid.js.map +1 -0
  157. package/dist/id-compressor/index.d.ts +12 -0
  158. package/dist/id-compressor/index.d.ts.map +1 -0
  159. package/dist/id-compressor/index.js +26 -0
  160. package/dist/id-compressor/index.js.map +1 -0
  161. package/dist/id-compressor/persisted-types/0.0.1.d.ts +156 -0
  162. package/dist/id-compressor/persisted-types/0.0.1.d.ts.map +1 -0
  163. package/dist/id-compressor/persisted-types/0.0.1.js +7 -0
  164. package/dist/id-compressor/persisted-types/0.0.1.js.map +1 -0
  165. package/dist/id-compressor/persisted-types/index.d.ts +6 -0
  166. package/dist/id-compressor/persisted-types/index.d.ts.map +1 -0
  167. package/dist/id-compressor/persisted-types/index.js +18 -0
  168. package/dist/id-compressor/persisted-types/index.js.map +1 -0
  169. package/dist/index.d.ts +29 -9
  170. package/dist/index.d.ts.map +1 -1
  171. package/dist/index.js +50 -35
  172. package/dist/index.js.map +1 -1
  173. package/dist/persisted-types/0.0.2.d.ts +385 -0
  174. package/dist/persisted-types/0.0.2.d.ts.map +1 -0
  175. package/dist/persisted-types/0.0.2.js +113 -0
  176. package/dist/persisted-types/0.0.2.js.map +1 -0
  177. package/dist/persisted-types/0.1.1.d.ts +314 -0
  178. package/dist/persisted-types/0.1.1.d.ts.map +1 -0
  179. package/dist/persisted-types/0.1.1.js +153 -0
  180. package/dist/persisted-types/0.1.1.js.map +1 -0
  181. package/dist/persisted-types/index.d.ts +7 -0
  182. package/dist/persisted-types/index.d.ts.map +1 -0
  183. package/dist/persisted-types/index.js +20 -0
  184. package/dist/persisted-types/index.js.map +1 -0
  185. package/docs/0-1-1-Compression.md +228 -0
  186. package/docs/Breaking-Change-Migration.md +52 -0
  187. package/docs/Compression.md +2 -2
  188. package/docs/Telemetry.md +43 -0
  189. package/docs/Write-Format.md +19 -0
  190. package/lib/ChangeCompression.d.ts +39 -0
  191. package/lib/ChangeCompression.d.ts.map +1 -0
  192. package/lib/ChangeCompression.js +111 -0
  193. package/lib/ChangeCompression.js.map +1 -0
  194. package/{dist/default-edits/PersistedTypes.d.ts → lib/ChangeTypes.d.ts} +58 -105
  195. package/lib/ChangeTypes.d.ts.map +1 -0
  196. package/lib/{default-edits/PersistedTypes.js → ChangeTypes.js} +15 -68
  197. package/lib/ChangeTypes.js.map +1 -0
  198. package/lib/Checkout.d.ts +39 -27
  199. package/lib/Checkout.d.ts.map +1 -1
  200. package/lib/Checkout.js +53 -24
  201. package/lib/Checkout.js.map +1 -1
  202. package/lib/Common.d.ts +175 -38
  203. package/lib/Common.d.ts.map +1 -1
  204. package/lib/Common.js +226 -101
  205. package/lib/Common.js.map +1 -1
  206. package/lib/EagerCheckout.d.ts +24 -0
  207. package/lib/EagerCheckout.d.ts.map +1 -0
  208. package/lib/{BasicCheckout.js → EagerCheckout.js} +7 -4
  209. package/lib/EagerCheckout.js.map +1 -0
  210. package/lib/EditLog.d.ts +77 -63
  211. package/lib/EditLog.d.ts.map +1 -1
  212. package/lib/EditLog.js +83 -47
  213. package/lib/EditLog.js.map +1 -1
  214. package/lib/EditUtilities.d.ts +168 -0
  215. package/lib/EditUtilities.d.ts.map +1 -0
  216. package/lib/EditUtilities.js +353 -0
  217. package/lib/EditUtilities.js.map +1 -0
  218. package/lib/EventTypes.d.ts +73 -0
  219. package/lib/EventTypes.d.ts.map +1 -0
  220. package/lib/EventTypes.js +75 -0
  221. package/lib/EventTypes.js.map +1 -0
  222. package/lib/Forest.d.ts +29 -7
  223. package/lib/Forest.d.ts.map +1 -1
  224. package/lib/Forest.js +58 -35
  225. package/lib/Forest.js.map +1 -1
  226. package/lib/HistoryEditFactory.d.ts +20 -0
  227. package/lib/HistoryEditFactory.d.ts.map +1 -0
  228. package/lib/{default-edits/HistoryEditFactory.js → HistoryEditFactory.js} +78 -39
  229. package/lib/HistoryEditFactory.js.map +1 -0
  230. package/lib/IdConversion.d.ts +12 -0
  231. package/lib/IdConversion.d.ts.map +1 -0
  232. package/lib/IdConversion.js +91 -0
  233. package/lib/IdConversion.js.map +1 -0
  234. package/lib/Identifiers.d.ts +89 -2
  235. package/lib/Identifiers.d.ts.map +1 -1
  236. package/lib/Identifiers.js +8 -1
  237. package/lib/Identifiers.js.map +1 -1
  238. package/lib/InitialTree.d.ts +2 -2
  239. package/lib/InitialTree.d.ts.map +1 -1
  240. package/lib/InitialTree.js +2 -1
  241. package/lib/InitialTree.js.map +1 -1
  242. package/lib/LazyCheckout.d.ts +28 -0
  243. package/lib/LazyCheckout.d.ts.map +1 -0
  244. package/lib/LazyCheckout.js +40 -0
  245. package/lib/LazyCheckout.js.map +1 -0
  246. package/lib/LogViewer.d.ts +129 -85
  247. package/lib/LogViewer.d.ts.map +1 -1
  248. package/lib/LogViewer.js +103 -77
  249. package/lib/LogViewer.js.map +1 -1
  250. package/lib/MergeHealth.d.ts +221 -0
  251. package/lib/MergeHealth.d.ts.map +1 -0
  252. package/lib/MergeHealth.js +258 -0
  253. package/lib/MergeHealth.js.map +1 -0
  254. package/lib/NodeIdUtilities.d.ts +105 -0
  255. package/lib/NodeIdUtilities.d.ts.map +1 -0
  256. package/lib/NodeIdUtilities.js +53 -0
  257. package/lib/NodeIdUtilities.js.map +1 -0
  258. package/lib/PayloadUtilities.d.ts +42 -0
  259. package/lib/PayloadUtilities.d.ts.map +1 -0
  260. package/lib/PayloadUtilities.js +110 -0
  261. package/lib/PayloadUtilities.js.map +1 -0
  262. package/lib/ReconciliationPath.d.ts +18 -13
  263. package/lib/ReconciliationPath.d.ts.map +1 -1
  264. package/lib/ReconciliationPath.js.map +1 -1
  265. package/lib/RevisionValueCache.d.ts +11 -2
  266. package/lib/RevisionValueCache.d.ts.map +1 -1
  267. package/lib/RevisionValueCache.js +2 -3
  268. package/lib/RevisionValueCache.js.map +1 -1
  269. package/lib/RevisionView.d.ts +83 -0
  270. package/lib/RevisionView.d.ts.map +1 -0
  271. package/lib/RevisionView.js +175 -0
  272. package/lib/RevisionView.js.map +1 -0
  273. package/lib/SerializationUtilities.d.ts +36 -0
  274. package/lib/SerializationUtilities.d.ts.map +1 -0
  275. package/lib/SerializationUtilities.js +95 -0
  276. package/lib/SerializationUtilities.js.map +1 -0
  277. package/lib/SharedTree.d.ts +439 -0
  278. package/lib/SharedTree.d.ts.map +1 -0
  279. package/lib/SharedTree.js +1104 -0
  280. package/lib/SharedTree.js.map +1 -0
  281. package/lib/SharedTreeEncoder.d.ts +102 -0
  282. package/lib/SharedTreeEncoder.d.ts.map +1 -0
  283. package/lib/SharedTreeEncoder.js +308 -0
  284. package/lib/SharedTreeEncoder.js.map +1 -0
  285. package/lib/StringInterner.d.ts +46 -0
  286. package/lib/StringInterner.d.ts.map +1 -0
  287. package/lib/StringInterner.js +57 -0
  288. package/lib/StringInterner.js.map +1 -0
  289. package/lib/Summary.d.ts +40 -0
  290. package/lib/Summary.d.ts.map +1 -0
  291. package/lib/Summary.js +19 -0
  292. package/lib/Summary.js.map +1 -0
  293. package/lib/SummaryBackCompatibility.d.ts +22 -22
  294. package/lib/SummaryBackCompatibility.d.ts.map +1 -1
  295. package/lib/SummaryBackCompatibility.js +29 -32
  296. package/lib/SummaryBackCompatibility.js.map +1 -1
  297. package/lib/SummaryTestUtilities.d.ts +31 -0
  298. package/lib/SummaryTestUtilities.d.ts.map +1 -0
  299. package/lib/SummaryTestUtilities.js +32 -0
  300. package/lib/SummaryTestUtilities.js.map +1 -0
  301. package/lib/Transaction.d.ts +71 -0
  302. package/lib/Transaction.d.ts.map +1 -0
  303. package/lib/Transaction.js +88 -0
  304. package/lib/Transaction.js.map +1 -0
  305. package/lib/TransactionInternal.d.ts +540 -0
  306. package/lib/TransactionInternal.d.ts.map +1 -0
  307. package/lib/TransactionInternal.js +622 -0
  308. package/lib/TransactionInternal.js.map +1 -0
  309. package/lib/TreeCompressor.d.ts +36 -0
  310. package/lib/TreeCompressor.d.ts.map +1 -0
  311. package/lib/TreeCompressor.js +133 -0
  312. package/lib/TreeCompressor.js.map +1 -0
  313. package/lib/TreeNodeHandle.d.ts +12 -18
  314. package/lib/TreeNodeHandle.d.ts.map +1 -1
  315. package/lib/TreeNodeHandle.js +14 -24
  316. package/lib/TreeNodeHandle.js.map +1 -1
  317. package/lib/TreeView.d.ts +166 -0
  318. package/lib/TreeView.d.ts.map +1 -0
  319. package/lib/TreeView.js +214 -0
  320. package/lib/TreeView.js.map +1 -0
  321. package/lib/TreeViewUtilities.d.ts +21 -0
  322. package/lib/TreeViewUtilities.d.ts.map +1 -0
  323. package/lib/TreeViewUtilities.js +71 -0
  324. package/lib/TreeViewUtilities.js.map +1 -0
  325. package/lib/{default-edits/UndoRedoHandler.d.ts → UndoRedoHandler.d.ts} +2 -2
  326. package/lib/UndoRedoHandler.d.ts.map +1 -0
  327. package/lib/{default-edits/UndoRedoHandler.js → UndoRedoHandler.js} +3 -7
  328. package/lib/UndoRedoHandler.js.map +1 -0
  329. package/lib/id-compressor/AppendOnlySortedMap.d.ts +127 -0
  330. package/lib/id-compressor/AppendOnlySortedMap.d.ts.map +1 -0
  331. package/lib/id-compressor/AppendOnlySortedMap.js +278 -0
  332. package/lib/id-compressor/AppendOnlySortedMap.js.map +1 -0
  333. package/lib/id-compressor/IdCompressor.d.ts +389 -0
  334. package/lib/id-compressor/IdCompressor.d.ts.map +1 -0
  335. package/lib/id-compressor/IdCompressor.js +1343 -0
  336. package/lib/id-compressor/IdCompressor.js.map +1 -0
  337. package/lib/id-compressor/IdRange.d.ts +11 -0
  338. package/lib/id-compressor/IdRange.d.ts.map +1 -0
  339. package/lib/id-compressor/IdRange.js +25 -0
  340. package/lib/id-compressor/IdRange.js.map +1 -0
  341. package/lib/id-compressor/NumericUuid.d.ts +63 -0
  342. package/lib/id-compressor/NumericUuid.d.ts.map +1 -0
  343. package/lib/id-compressor/NumericUuid.js +365 -0
  344. package/lib/id-compressor/NumericUuid.js.map +1 -0
  345. package/lib/id-compressor/index.d.ts +12 -0
  346. package/lib/id-compressor/index.d.ts.map +1 -0
  347. package/lib/id-compressor/index.js +12 -0
  348. package/lib/id-compressor/index.js.map +1 -0
  349. package/lib/id-compressor/persisted-types/0.0.1.d.ts +156 -0
  350. package/lib/id-compressor/persisted-types/0.0.1.d.ts.map +1 -0
  351. package/lib/{test/Snapshot.tests.d.ts → id-compressor/persisted-types/0.0.1.js} +1 -1
  352. package/lib/id-compressor/persisted-types/0.0.1.js.map +1 -0
  353. package/lib/id-compressor/persisted-types/index.d.ts +6 -0
  354. package/lib/id-compressor/persisted-types/index.d.ts.map +1 -0
  355. package/lib/id-compressor/persisted-types/index.js +6 -0
  356. package/lib/id-compressor/persisted-types/index.js.map +1 -0
  357. package/lib/index.d.ts +29 -9
  358. package/lib/index.d.ts.map +1 -1
  359. package/lib/index.js +23 -6
  360. package/lib/index.js.map +1 -1
  361. package/lib/persisted-types/0.0.2.d.ts +385 -0
  362. package/lib/persisted-types/0.0.2.d.ts.map +1 -0
  363. package/lib/persisted-types/0.0.2.js +110 -0
  364. package/lib/persisted-types/0.0.2.js.map +1 -0
  365. package/lib/persisted-types/0.1.1.d.ts +314 -0
  366. package/lib/persisted-types/0.1.1.d.ts.map +1 -0
  367. package/lib/persisted-types/0.1.1.js +150 -0
  368. package/lib/persisted-types/0.1.1.js.map +1 -0
  369. package/lib/persisted-types/index.d.ts +7 -0
  370. package/lib/persisted-types/index.d.ts.map +1 -0
  371. package/lib/persisted-types/index.js +8 -0
  372. package/lib/persisted-types/index.js.map +1 -0
  373. package/lib/test/AppendOnlySortedMap.tests.d.ts +6 -0
  374. package/lib/test/AppendOnlySortedMap.tests.d.ts.map +1 -0
  375. package/lib/test/AppendOnlySortedMap.tests.js +169 -0
  376. package/lib/test/AppendOnlySortedMap.tests.js.map +1 -0
  377. package/lib/test/{SnapshotUtilities.tests.d.ts → ChangeCompression.tests.d.ts} +1 -1
  378. package/lib/test/ChangeCompression.tests.d.ts.map +1 -0
  379. package/lib/test/ChangeCompression.tests.js +145 -0
  380. package/lib/test/ChangeCompression.tests.js.map +1 -0
  381. package/lib/test/Checkout.tests.d.ts +2 -3
  382. package/lib/test/Checkout.tests.d.ts.map +1 -1
  383. package/lib/test/Checkout.tests.js +126 -69
  384. package/lib/test/Checkout.tests.js.map +1 -1
  385. package/lib/test/Common.tests.js +60 -2
  386. package/lib/test/Common.tests.js.map +1 -1
  387. package/lib/test/{BasicCheckout.tests.d.ts → EagerCheckout.tests.d.ts} +1 -1
  388. package/lib/test/EagerCheckout.tests.d.ts.map +1 -0
  389. package/lib/test/EagerCheckout.tests.js +20 -0
  390. package/lib/test/EagerCheckout.tests.js.map +1 -0
  391. package/lib/test/Edit.tests.js +22 -14
  392. package/lib/test/Edit.tests.js.map +1 -1
  393. package/lib/test/{Anchors.glassBox.tests.d.ts → EditLog.perf.tests.d.ts} +1 -1
  394. package/lib/test/EditLog.perf.tests.d.ts.map +1 -0
  395. package/lib/test/EditLog.perf.tests.js +30 -0
  396. package/lib/test/EditLog.perf.tests.js.map +1 -0
  397. package/lib/test/EditLog.tests.js +10 -6
  398. package/lib/test/EditLog.tests.js.map +1 -1
  399. package/lib/test/EditUtilities.tests.d.ts +6 -0
  400. package/lib/test/EditUtilities.tests.d.ts.map +1 -0
  401. package/lib/test/EditUtilities.tests.js +503 -0
  402. package/lib/test/EditUtilities.tests.js.map +1 -0
  403. package/lib/test/Forest.perf.tests.d.ts +6 -0
  404. package/lib/test/Forest.perf.tests.d.ts.map +1 -0
  405. package/lib/test/Forest.perf.tests.js +133 -0
  406. package/lib/test/Forest.perf.tests.js.map +1 -0
  407. package/lib/test/Forest.tests.js +54 -27
  408. package/lib/test/Forest.tests.js.map +1 -1
  409. package/lib/test/GenericTransaction.tests.js +12 -3
  410. package/lib/test/GenericTransaction.tests.js.map +1 -1
  411. package/lib/test/HistoryEditFactory.tests.d.ts +6 -0
  412. package/lib/test/HistoryEditFactory.tests.d.ts.map +1 -0
  413. package/lib/test/HistoryEditFactory.tests.js +90 -0
  414. package/lib/test/HistoryEditFactory.tests.js.map +1 -0
  415. package/lib/test/IdCompressor.perf.tests.d.ts +6 -0
  416. package/lib/test/IdCompressor.perf.tests.d.ts.map +1 -0
  417. package/lib/test/IdCompressor.perf.tests.js +304 -0
  418. package/lib/test/IdCompressor.perf.tests.js.map +1 -0
  419. package/lib/test/IdCompressor.tests.d.ts +6 -0
  420. package/lib/test/IdCompressor.tests.d.ts.map +1 -0
  421. package/lib/test/IdCompressor.tests.js +1075 -0
  422. package/lib/test/IdCompressor.tests.js.map +1 -0
  423. package/lib/test/IdConversion.tests.d.ts +6 -0
  424. package/lib/test/IdConversion.tests.d.ts.map +1 -0
  425. package/lib/test/IdConversion.tests.js +36 -0
  426. package/lib/test/IdConversion.tests.js.map +1 -0
  427. package/lib/test/LazyCheckout.tests.d.ts +6 -0
  428. package/lib/test/LazyCheckout.tests.d.ts.map +1 -0
  429. package/lib/test/LazyCheckout.tests.js +22 -0
  430. package/lib/test/LazyCheckout.tests.js.map +1 -0
  431. package/lib/test/LogViewer.tests.js +276 -191
  432. package/lib/test/LogViewer.tests.js.map +1 -1
  433. package/lib/test/{SharedTreeWithAnchors.tests.d.ts → MergeHealthTelemetryHeartbeat.tests.d.ts} +1 -1
  434. package/lib/test/MergeHealthTelemetryHeartbeat.tests.d.ts.map +1 -0
  435. package/lib/test/MergeHealthTelemetryHeartbeat.tests.js +342 -0
  436. package/lib/test/MergeHealthTelemetryHeartbeat.tests.js.map +1 -0
  437. package/lib/test/NumericUuid.perf.tests.d.ts +6 -0
  438. package/lib/test/NumericUuid.perf.tests.d.ts.map +1 -0
  439. package/lib/test/NumericUuid.perf.tests.js +68 -0
  440. package/lib/test/NumericUuid.perf.tests.js.map +1 -0
  441. package/lib/test/NumericUuid.tests.d.ts +6 -0
  442. package/lib/test/NumericUuid.tests.d.ts.map +1 -0
  443. package/lib/test/NumericUuid.tests.js +191 -0
  444. package/lib/test/NumericUuid.tests.js.map +1 -0
  445. package/lib/test/RevisionView.tests.d.ts +6 -0
  446. package/lib/test/RevisionView.tests.d.ts.map +1 -0
  447. package/lib/test/RevisionView.tests.js +133 -0
  448. package/lib/test/RevisionView.tests.js.map +1 -0
  449. package/lib/test/SharedTree.perf.tests.d.ts +6 -0
  450. package/lib/test/SharedTree.perf.tests.d.ts.map +1 -0
  451. package/lib/test/SharedTree.perf.tests.js +39 -0
  452. package/lib/test/SharedTree.perf.tests.js.map +1 -0
  453. package/lib/test/SharedTree.tests.js +15 -3
  454. package/lib/test/SharedTree.tests.js.map +1 -1
  455. package/lib/test/StringInterner.tests.d.ts +6 -0
  456. package/lib/test/StringInterner.tests.d.ts.map +1 -0
  457. package/lib/test/StringInterner.tests.js +71 -0
  458. package/lib/test/StringInterner.tests.js.map +1 -0
  459. package/lib/test/Summary.tests.d.ts +8 -0
  460. package/lib/test/Summary.tests.d.ts.map +1 -0
  461. package/lib/test/Summary.tests.js +407 -0
  462. package/lib/test/Summary.tests.js.map +1 -0
  463. package/lib/test/Transaction.tests.js +109 -329
  464. package/lib/test/Transaction.tests.js.map +1 -1
  465. package/lib/test/TransactionInternal.tests.d.ts +6 -0
  466. package/lib/test/TransactionInternal.tests.d.ts.map +1 -0
  467. package/lib/test/TransactionInternal.tests.js +568 -0
  468. package/lib/test/TransactionInternal.tests.js.map +1 -0
  469. package/lib/test/TreeCompression.tests.d.ts +6 -0
  470. package/lib/test/TreeCompression.tests.d.ts.map +1 -0
  471. package/lib/test/TreeCompression.tests.js +292 -0
  472. package/lib/test/TreeCompression.tests.js.map +1 -0
  473. package/lib/test/TreeView.tests.d.ts +6 -0
  474. package/lib/test/TreeView.tests.d.ts.map +1 -0
  475. package/lib/test/TreeView.tests.js +176 -0
  476. package/lib/test/TreeView.tests.js.map +1 -0
  477. package/lib/test/UndoRedoHandler.tests.js +2 -2
  478. package/lib/test/UndoRedoHandler.tests.js.map +1 -1
  479. package/lib/test/Virtualization.tests.js +146 -62
  480. package/lib/test/Virtualization.tests.js.map +1 -1
  481. package/lib/test/fuzz/Generators.d.ts +19 -0
  482. package/lib/test/fuzz/Generators.d.ts.map +1 -0
  483. package/lib/test/fuzz/Generators.js +420 -0
  484. package/lib/test/fuzz/Generators.js.map +1 -0
  485. package/lib/test/fuzz/SharedTreeFuzzTests.d.ts +20 -0
  486. package/lib/test/fuzz/SharedTreeFuzzTests.d.ts.map +1 -0
  487. package/lib/test/fuzz/SharedTreeFuzzTests.js +217 -0
  488. package/lib/test/fuzz/SharedTreeFuzzTests.js.map +1 -0
  489. package/lib/test/fuzz/Types.d.ts +133 -0
  490. package/lib/test/fuzz/Types.d.ts.map +1 -0
  491. package/lib/test/{GenericTransactionWithAnchors.tests.d.ts → fuzz/Types.js} +2 -2
  492. package/lib/test/fuzz/Types.js.map +1 -0
  493. package/lib/test/utilities/IdCompressorTestUtilities.d.ts +180 -0
  494. package/lib/test/utilities/IdCompressorTestUtilities.d.ts.map +1 -0
  495. package/lib/test/utilities/IdCompressorTestUtilities.js +528 -0
  496. package/lib/test/utilities/IdCompressorTestUtilities.js.map +1 -0
  497. package/lib/test/utilities/MockTransaction.d.ts +26 -7
  498. package/lib/test/utilities/MockTransaction.d.ts.map +1 -1
  499. package/lib/test/utilities/MockTransaction.js +40 -11
  500. package/lib/test/utilities/MockTransaction.js.map +1 -1
  501. package/lib/test/utilities/PendingLocalStateTests.d.ts +12 -0
  502. package/lib/test/utilities/PendingLocalStateTests.d.ts.map +1 -0
  503. package/lib/test/utilities/PendingLocalStateTests.js +105 -0
  504. package/lib/test/utilities/PendingLocalStateTests.js.map +1 -0
  505. package/lib/test/utilities/SharedTreeTests.d.ts +3 -4
  506. package/lib/test/utilities/SharedTreeTests.d.ts.map +1 -1
  507. package/lib/test/utilities/SharedTreeTests.js +696 -439
  508. package/lib/test/utilities/SharedTreeTests.js.map +1 -1
  509. package/lib/test/utilities/SharedTreeVersioningTests.d.ts +11 -0
  510. package/lib/test/utilities/SharedTreeVersioningTests.d.ts.map +1 -0
  511. package/lib/test/utilities/SharedTreeVersioningTests.js +370 -0
  512. package/lib/test/utilities/SharedTreeVersioningTests.js.map +1 -0
  513. package/lib/test/utilities/SummaryLoadPerfTests.d.ts +10 -0
  514. package/lib/test/utilities/SummaryLoadPerfTests.d.ts.map +1 -0
  515. package/lib/test/utilities/SummaryLoadPerfTests.js +102 -0
  516. package/lib/test/utilities/SummaryLoadPerfTests.js.map +1 -0
  517. package/lib/test/utilities/SummarySizeTests.d.ts +11 -0
  518. package/lib/test/utilities/SummarySizeTests.d.ts.map +1 -0
  519. package/lib/test/utilities/SummarySizeTests.js +158 -0
  520. package/lib/test/utilities/SummarySizeTests.js.map +1 -0
  521. package/lib/test/utilities/TestCommon.d.ts +9 -0
  522. package/lib/test/utilities/TestCommon.d.ts.map +1 -0
  523. package/lib/test/utilities/TestCommon.js +13 -0
  524. package/lib/test/utilities/TestCommon.js.map +1 -0
  525. package/lib/test/utilities/TestNode.d.ts +140 -0
  526. package/lib/test/utilities/TestNode.d.ts.map +1 -0
  527. package/lib/test/utilities/TestNode.js +292 -0
  528. package/lib/test/utilities/TestNode.js.map +1 -0
  529. package/lib/test/utilities/TestUtilities.d.ts +84 -70
  530. package/lib/test/utilities/TestUtilities.d.ts.map +1 -1
  531. package/lib/test/utilities/TestUtilities.js +218 -143
  532. package/lib/test/utilities/TestUtilities.js.map +1 -1
  533. package/lib/test/utilities/UndoRedoTests.d.ts +4 -5
  534. package/lib/test/utilities/UndoRedoTests.d.ts.map +1 -1
  535. package/lib/test/utilities/UndoRedoTests.js +138 -149
  536. package/lib/test/utilities/UndoRedoTests.js.map +1 -1
  537. package/package.json +22 -17
  538. package/src/ChangeCompression.ts +159 -0
  539. package/src/{default-edits/PersistedTypes.ts → ChangeTypes.ts} +62 -125
  540. package/src/Checkout.ts +82 -53
  541. package/src/Common.ts +317 -117
  542. package/src/EagerCheckout.ts +38 -0
  543. package/src/EditLog.ts +153 -100
  544. package/src/EditUtilities.ts +559 -0
  545. package/src/EventTypes.ts +74 -0
  546. package/src/Forest.ts +81 -73
  547. package/src/{default-edits/HistoryEditFactory.ts → HistoryEditFactory.ts} +103 -53
  548. package/src/IdConversion.ts +125 -0
  549. package/src/Identifiers.ts +101 -1
  550. package/src/InitialTree.ts +5 -4
  551. package/src/LazyCheckout.ts +51 -0
  552. package/src/LogViewer.ts +242 -166
  553. package/src/MergeHealth.ts +447 -0
  554. package/src/NodeIdUtilities.ts +156 -0
  555. package/src/PayloadUtilities.ts +124 -0
  556. package/src/ReconciliationPath.ts +18 -13
  557. package/src/RevisionValueCache.ts +14 -5
  558. package/src/RevisionView.ts +252 -0
  559. package/src/SerializationUtilities.ts +130 -0
  560. package/src/SharedTree.ts +1501 -0
  561. package/src/SharedTreeEncoder.ts +493 -0
  562. package/src/StringInterner.ts +72 -0
  563. package/src/Summary.ts +48 -0
  564. package/src/SummaryBackCompatibility.ts +47 -57
  565. package/src/SummaryTestUtilities.ts +54 -0
  566. package/src/Transaction.ts +120 -0
  567. package/src/TransactionInternal.ts +1087 -0
  568. package/src/TreeCompressor.ts +213 -0
  569. package/src/TreeNodeHandle.ts +19 -32
  570. package/src/TreeView.ts +322 -0
  571. package/src/TreeViewUtilities.ts +77 -0
  572. package/src/{default-edits/UndoRedoHandler.ts → UndoRedoHandler.ts} +8 -13
  573. package/src/id-compressor/AppendOnlySortedMap.ts +325 -0
  574. package/src/id-compressor/IdCompressor.md +3 -0
  575. package/src/id-compressor/IdCompressor.ts +1848 -0
  576. package/src/id-compressor/IdRange.ts +33 -0
  577. package/src/id-compressor/NumericUuid.ts +414 -0
  578. package/src/id-compressor/index.ts +13 -0
  579. package/src/id-compressor/persisted-types/0.0.1.ts +179 -0
  580. package/src/id-compressor/persisted-types/README.md +3 -0
  581. package/src/id-compressor/persisted-types/index.ts +6 -0
  582. package/src/index.ts +118 -59
  583. package/src/persisted-types/0.0.2.ts +442 -0
  584. package/src/persisted-types/0.1.1.ts +476 -0
  585. package/src/persisted-types/README.md +22 -0
  586. package/src/persisted-types/index.ts +9 -0
  587. package/.mocharc.js +0 -41
  588. package/api/tree.api.md +0 -729
  589. package/dist/BasicCheckout.d.ts +0 -23
  590. package/dist/BasicCheckout.d.ts.map +0 -1
  591. package/dist/BasicCheckout.js.map +0 -1
  592. package/dist/Snapshot.d.ts +0 -198
  593. package/dist/Snapshot.d.ts.map +0 -1
  594. package/dist/Snapshot.js +0 -267
  595. package/dist/Snapshot.js.map +0 -1
  596. package/dist/SnapshotUtilities.d.ts +0 -29
  597. package/dist/SnapshotUtilities.d.ts.map +0 -1
  598. package/dist/SnapshotUtilities.js +0 -73
  599. package/dist/SnapshotUtilities.js.map +0 -1
  600. package/dist/anchored-edits/AnchorResolution.d.ts +0 -144
  601. package/dist/anchored-edits/AnchorResolution.d.ts.map +0 -1
  602. package/dist/anchored-edits/AnchorResolution.js +0 -162
  603. package/dist/anchored-edits/AnchorResolution.js.map +0 -1
  604. package/dist/anchored-edits/Factory.d.ts +0 -56
  605. package/dist/anchored-edits/Factory.d.ts.map +0 -1
  606. package/dist/anchored-edits/Factory.js +0 -79
  607. package/dist/anchored-edits/Factory.js.map +0 -1
  608. package/dist/anchored-edits/PersistedTypes.d.ts +0 -245
  609. package/dist/anchored-edits/PersistedTypes.d.ts.map +0 -1
  610. package/dist/anchored-edits/PersistedTypes.js +0 -131
  611. package/dist/anchored-edits/PersistedTypes.js.map +0 -1
  612. package/dist/anchored-edits/SharedTreeWithAnchors.d.ts +0 -120
  613. package/dist/anchored-edits/SharedTreeWithAnchors.d.ts.map +0 -1
  614. package/dist/anchored-edits/SharedTreeWithAnchors.js +0 -115
  615. package/dist/anchored-edits/SharedTreeWithAnchors.js.map +0 -1
  616. package/dist/anchored-edits/TransactionWithAnchors.d.ts +0 -28
  617. package/dist/anchored-edits/TransactionWithAnchors.d.ts.map +0 -1
  618. package/dist/anchored-edits/TransactionWithAnchors.js +0 -36
  619. package/dist/anchored-edits/TransactionWithAnchors.js.map +0 -1
  620. package/dist/anchored-edits/index.d.ts +0 -10
  621. package/dist/anchored-edits/index.d.ts.map +0 -1
  622. package/dist/anchored-edits/index.js +0 -34
  623. package/dist/anchored-edits/index.js.map +0 -1
  624. package/dist/default-edits/EditUtilities.d.ts +0 -57
  625. package/dist/default-edits/EditUtilities.d.ts.map +0 -1
  626. package/dist/default-edits/EditUtilities.js +0 -192
  627. package/dist/default-edits/EditUtilities.js.map +0 -1
  628. package/dist/default-edits/Factory.d.ts +0 -56
  629. package/dist/default-edits/Factory.d.ts.map +0 -1
  630. package/dist/default-edits/Factory.js +0 -79
  631. package/dist/default-edits/Factory.js.map +0 -1
  632. package/dist/default-edits/HistoryEditFactory.d.ts +0 -19
  633. package/dist/default-edits/HistoryEditFactory.d.ts.map +0 -1
  634. package/dist/default-edits/HistoryEditFactory.js +0 -187
  635. package/dist/default-edits/HistoryEditFactory.js.map +0 -1
  636. package/dist/default-edits/PersistedTypes.d.ts.map +0 -1
  637. package/dist/default-edits/PersistedTypes.js.map +0 -1
  638. package/dist/default-edits/SharedTree.d.ts +0 -111
  639. package/dist/default-edits/SharedTree.d.ts.map +0 -1
  640. package/dist/default-edits/SharedTree.js +0 -124
  641. package/dist/default-edits/SharedTree.js.map +0 -1
  642. package/dist/default-edits/Summary.d.ts +0 -15
  643. package/dist/default-edits/Summary.d.ts.map +0 -1
  644. package/dist/default-edits/Summary.js +0 -35
  645. package/dist/default-edits/Summary.js.map +0 -1
  646. package/dist/default-edits/Transaction.d.ts +0 -41
  647. package/dist/default-edits/Transaction.d.ts.map +0 -1
  648. package/dist/default-edits/Transaction.js +0 -225
  649. package/dist/default-edits/Transaction.js.map +0 -1
  650. package/dist/default-edits/UndoRedoHandler.d.ts.map +0 -1
  651. package/dist/default-edits/UndoRedoHandler.js.map +0 -1
  652. package/dist/default-edits/index.d.ts +0 -13
  653. package/dist/default-edits/index.d.ts.map +0 -1
  654. package/dist/default-edits/index.js +0 -41
  655. package/dist/default-edits/index.js.map +0 -1
  656. package/dist/generic/GenericEditUtilities.d.ts +0 -26
  657. package/dist/generic/GenericEditUtilities.d.ts.map +0 -1
  658. package/dist/generic/GenericEditUtilities.js +0 -45
  659. package/dist/generic/GenericEditUtilities.js.map +0 -1
  660. package/dist/generic/GenericSharedTree.d.ts +0 -221
  661. package/dist/generic/GenericSharedTree.d.ts.map +0 -1
  662. package/dist/generic/GenericSharedTree.js +0 -447
  663. package/dist/generic/GenericSharedTree.js.map +0 -1
  664. package/dist/generic/GenericTransaction.d.ts +0 -87
  665. package/dist/generic/GenericTransaction.d.ts.map +0 -1
  666. package/dist/generic/GenericTransaction.js +0 -144
  667. package/dist/generic/GenericTransaction.js.map +0 -1
  668. package/dist/generic/PersistedTypes.d.ts +0 -194
  669. package/dist/generic/PersistedTypes.d.ts.map +0 -1
  670. package/dist/generic/PersistedTypes.js +0 -42
  671. package/dist/generic/PersistedTypes.js.map +0 -1
  672. package/dist/generic/Summary.d.ts +0 -63
  673. package/dist/generic/Summary.d.ts.map +0 -1
  674. package/dist/generic/Summary.js +0 -64
  675. package/dist/generic/Summary.js.map +0 -1
  676. package/dist/generic/index.d.ts +0 -10
  677. package/dist/generic/index.d.ts.map +0 -1
  678. package/dist/generic/index.js +0 -26
  679. package/dist/generic/index.js.map +0 -1
  680. package/docs/Future.md +0 -155
  681. package/lib/BasicCheckout.d.ts +0 -23
  682. package/lib/BasicCheckout.d.ts.map +0 -1
  683. package/lib/BasicCheckout.js.map +0 -1
  684. package/lib/Snapshot.d.ts +0 -198
  685. package/lib/Snapshot.d.ts.map +0 -1
  686. package/lib/Snapshot.js +0 -263
  687. package/lib/Snapshot.js.map +0 -1
  688. package/lib/SnapshotUtilities.d.ts +0 -29
  689. package/lib/SnapshotUtilities.d.ts.map +0 -1
  690. package/lib/SnapshotUtilities.js +0 -67
  691. package/lib/SnapshotUtilities.js.map +0 -1
  692. package/lib/anchored-edits/AnchorResolution.d.ts +0 -144
  693. package/lib/anchored-edits/AnchorResolution.d.ts.map +0 -1
  694. package/lib/anchored-edits/AnchorResolution.js +0 -152
  695. package/lib/anchored-edits/AnchorResolution.js.map +0 -1
  696. package/lib/anchored-edits/Factory.d.ts +0 -56
  697. package/lib/anchored-edits/Factory.d.ts.map +0 -1
  698. package/lib/anchored-edits/Factory.js +0 -74
  699. package/lib/anchored-edits/Factory.js.map +0 -1
  700. package/lib/anchored-edits/PersistedTypes.d.ts +0 -245
  701. package/lib/anchored-edits/PersistedTypes.d.ts.map +0 -1
  702. package/lib/anchored-edits/PersistedTypes.js +0 -128
  703. package/lib/anchored-edits/PersistedTypes.js.map +0 -1
  704. package/lib/anchored-edits/SharedTreeWithAnchors.d.ts +0 -120
  705. package/lib/anchored-edits/SharedTreeWithAnchors.d.ts.map +0 -1
  706. package/lib/anchored-edits/SharedTreeWithAnchors.js +0 -110
  707. package/lib/anchored-edits/SharedTreeWithAnchors.js.map +0 -1
  708. package/lib/anchored-edits/TransactionWithAnchors.d.ts +0 -28
  709. package/lib/anchored-edits/TransactionWithAnchors.d.ts.map +0 -1
  710. package/lib/anchored-edits/TransactionWithAnchors.js +0 -32
  711. package/lib/anchored-edits/TransactionWithAnchors.js.map +0 -1
  712. package/lib/anchored-edits/index.d.ts +0 -10
  713. package/lib/anchored-edits/index.d.ts.map +0 -1
  714. package/lib/anchored-edits/index.js +0 -11
  715. package/lib/anchored-edits/index.js.map +0 -1
  716. package/lib/default-edits/EditUtilities.d.ts +0 -57
  717. package/lib/default-edits/EditUtilities.d.ts.map +0 -1
  718. package/lib/default-edits/EditUtilities.js +0 -181
  719. package/lib/default-edits/EditUtilities.js.map +0 -1
  720. package/lib/default-edits/Factory.d.ts +0 -56
  721. package/lib/default-edits/Factory.d.ts.map +0 -1
  722. package/lib/default-edits/Factory.js +0 -74
  723. package/lib/default-edits/Factory.js.map +0 -1
  724. package/lib/default-edits/HistoryEditFactory.d.ts +0 -19
  725. package/lib/default-edits/HistoryEditFactory.d.ts.map +0 -1
  726. package/lib/default-edits/HistoryEditFactory.js.map +0 -1
  727. package/lib/default-edits/PersistedTypes.d.ts.map +0 -1
  728. package/lib/default-edits/PersistedTypes.js.map +0 -1
  729. package/lib/default-edits/SharedTree.d.ts +0 -111
  730. package/lib/default-edits/SharedTree.d.ts.map +0 -1
  731. package/lib/default-edits/SharedTree.js +0 -100
  732. package/lib/default-edits/SharedTree.js.map +0 -1
  733. package/lib/default-edits/Summary.d.ts +0 -15
  734. package/lib/default-edits/Summary.d.ts.map +0 -1
  735. package/lib/default-edits/Summary.js +0 -31
  736. package/lib/default-edits/Summary.js.map +0 -1
  737. package/lib/default-edits/Transaction.d.ts +0 -41
  738. package/lib/default-edits/Transaction.d.ts.map +0 -1
  739. package/lib/default-edits/Transaction.js +0 -221
  740. package/lib/default-edits/Transaction.js.map +0 -1
  741. package/lib/default-edits/UndoRedoHandler.d.ts.map +0 -1
  742. package/lib/default-edits/UndoRedoHandler.js.map +0 -1
  743. package/lib/default-edits/index.d.ts +0 -13
  744. package/lib/default-edits/index.d.ts.map +0 -1
  745. package/lib/default-edits/index.js +0 -14
  746. package/lib/default-edits/index.js.map +0 -1
  747. package/lib/generic/GenericEditUtilities.d.ts +0 -26
  748. package/lib/generic/GenericEditUtilities.d.ts.map +0 -1
  749. package/lib/generic/GenericEditUtilities.js +0 -38
  750. package/lib/generic/GenericEditUtilities.js.map +0 -1
  751. package/lib/generic/GenericSharedTree.d.ts +0 -221
  752. package/lib/generic/GenericSharedTree.d.ts.map +0 -1
  753. package/lib/generic/GenericSharedTree.js +0 -443
  754. package/lib/generic/GenericSharedTree.js.map +0 -1
  755. package/lib/generic/GenericTransaction.d.ts +0 -87
  756. package/lib/generic/GenericTransaction.d.ts.map +0 -1
  757. package/lib/generic/GenericTransaction.js +0 -140
  758. package/lib/generic/GenericTransaction.js.map +0 -1
  759. package/lib/generic/PersistedTypes.d.ts +0 -194
  760. package/lib/generic/PersistedTypes.d.ts.map +0 -1
  761. package/lib/generic/PersistedTypes.js +0 -39
  762. package/lib/generic/PersistedTypes.js.map +0 -1
  763. package/lib/generic/Summary.d.ts +0 -63
  764. package/lib/generic/Summary.d.ts.map +0 -1
  765. package/lib/generic/Summary.js +0 -58
  766. package/lib/generic/Summary.js.map +0 -1
  767. package/lib/generic/index.d.ts +0 -10
  768. package/lib/generic/index.d.ts.map +0 -1
  769. package/lib/generic/index.js +0 -11
  770. package/lib/generic/index.js.map +0 -1
  771. package/lib/test/Anchors.glassBox.tests.d.ts.map +0 -1
  772. package/lib/test/Anchors.glassBox.tests.js +0 -410
  773. package/lib/test/Anchors.glassBox.tests.js.map +0 -1
  774. package/lib/test/BasicCheckout.tests.d.ts.map +0 -1
  775. package/lib/test/BasicCheckout.tests.js +0 -8
  776. package/lib/test/BasicCheckout.tests.js.map +0 -1
  777. package/lib/test/GenericTransactionWithAnchors.tests.d.ts.map +0 -1
  778. package/lib/test/GenericTransactionWithAnchors.tests.js +0 -25
  779. package/lib/test/GenericTransactionWithAnchors.tests.js.map +0 -1
  780. package/lib/test/SharedTreeWithAnchors.tests.d.ts.map +0 -1
  781. package/lib/test/SharedTreeWithAnchors.tests.js +0 -420
  782. package/lib/test/SharedTreeWithAnchors.tests.js.map +0 -1
  783. package/lib/test/Snapshot.tests.d.ts.map +0 -1
  784. package/lib/test/Snapshot.tests.js +0 -96
  785. package/lib/test/Snapshot.tests.js.map +0 -1
  786. package/lib/test/SnapshotUtilities.tests.d.ts.map +0 -1
  787. package/lib/test/SnapshotUtilities.tests.js +0 -168
  788. package/lib/test/SnapshotUtilities.tests.js.map +0 -1
  789. package/lib/test/undoRedoStackManager.d.ts +0 -26
  790. package/lib/test/undoRedoStackManager.d.ts.map +0 -1
  791. package/lib/test/undoRedoStackManager.js +0 -176
  792. package/lib/test/undoRedoStackManager.js.map +0 -1
  793. package/lib/test/utilities/SummaryFormatCompatibilityTests.d.ts +0 -13
  794. package/lib/test/utilities/SummaryFormatCompatibilityTests.d.ts.map +0 -1
  795. package/lib/test/utilities/SummaryFormatCompatibilityTests.js +0 -154
  796. package/lib/test/utilities/SummaryFormatCompatibilityTests.js.map +0 -1
  797. package/src/BasicCheckout.ts +0 -34
  798. package/src/Snapshot.ts +0 -363
  799. package/src/SnapshotUtilities.ts +0 -88
  800. package/src/anchored-edits/AnchorResolution.ts +0 -442
  801. package/src/anchored-edits/Factory.ts +0 -94
  802. package/src/anchored-edits/PersistedTypes.ts +0 -310
  803. package/src/anchored-edits/SharedTreeWithAnchors.ts +0 -200
  804. package/src/anchored-edits/TransactionWithAnchors.ts +0 -39
  805. package/src/anchored-edits/index.ts +0 -21
  806. package/src/default-edits/EditUtilities.ts +0 -220
  807. package/src/default-edits/Factory.ts +0 -94
  808. package/src/default-edits/SharedTree.ts +0 -174
  809. package/src/default-edits/Summary.ts +0 -44
  810. package/src/default-edits/Transaction.ts +0 -262
  811. package/src/default-edits/index.ts +0 -29
  812. package/src/generic/GenericEditUtilities.ts +0 -46
  813. package/src/generic/GenericSharedTree.ts +0 -593
  814. package/src/generic/GenericTransaction.ts +0 -194
  815. package/src/generic/PersistedTypes.ts +0 -221
  816. package/src/generic/Summary.ts +0 -113
  817. package/src/generic/index.ts +0 -41
@@ -0,0 +1,1848 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /* eslint-disable @typescript-eslint/restrict-plus-operands */
7
+
8
+ import BTree from 'sorted-btree';
9
+ import {
10
+ assert,
11
+ assertNotUndefined,
12
+ compareFiniteNumbers,
13
+ compareMaps,
14
+ compareStrings,
15
+ fail,
16
+ Mutable,
17
+ setPropertyIfDefined,
18
+ } from '../Common';
19
+ import {
20
+ LocalCompressedId,
21
+ FinalCompressedId,
22
+ SessionSpaceCompressedId,
23
+ StableId,
24
+ OpSpaceCompressedId,
25
+ SessionId,
26
+ CompressedId,
27
+ } from '../Identifiers';
28
+ import { AppendOnlyDoublySortedMap, AppendOnlySortedMap } from './AppendOnlySortedMap';
29
+ import { getIds } from './IdRange';
30
+ import {
31
+ numericUuidEquals,
32
+ getPositiveDelta,
33
+ incrementUuid,
34
+ numericUuidFromStableId,
35
+ NumericUuid,
36
+ stableIdFromNumericUuid,
37
+ isStableId,
38
+ assertIsStableId,
39
+ ensureSessionUuid,
40
+ } from './NumericUuid';
41
+ import type {
42
+ AttributionInfo,
43
+ IdCreationRange,
44
+ SerializedCluster,
45
+ SerializedClusterOverrides,
46
+ SerializedIdCompressor,
47
+ SerializedIdCompressorWithNoSession,
48
+ SerializedIdCompressorWithOngoingSession,
49
+ SerializedLocalState,
50
+ SerializedSessionData,
51
+ UnackedLocalId,
52
+ VersionedSerializedIdCompressor,
53
+ } from './persisted-types';
54
+
55
+ /**
56
+ * A cluster of final (sequenced via consensus), sequentially allocated compressed IDs.
57
+ * A final ID in a cluster decompresses to a UUID that is one of the following:
58
+ * 1. A sequentially allocated UUID that is the result of adding its offset within the cluster to `baseUuid`.
59
+ * 2. An override string (stored in `overrides`) specified at allocation time.
60
+ */
61
+ interface IdCluster {
62
+ /**
63
+ * The UUID corresponding to the first final ID in the cluster.
64
+ */
65
+ readonly baseUuid: NumericUuid;
66
+
67
+ /**
68
+ * The total number of final IDs reserved for allocation in the cluster.
69
+ * Clusters are reserved in blocks as a performance optimization.
70
+ */
71
+ capacity: number;
72
+
73
+ /**
74
+ * The number of final IDs currently allocated in the cluster.
75
+ */
76
+ count: number;
77
+
78
+ /**
79
+ * The session in which this cluster was created
80
+ */
81
+ readonly session: Session;
82
+
83
+ /**
84
+ * Final IDs assigned override strings within this cluster.
85
+ * These are one of the following:
86
+ * 1. The override string
87
+ * 2. The override string and external override details. This occurs when local IDs corresponding to the same override
88
+ * string are created by different sessions before any have been finalized. This can occur due to concurrency or offline.
89
+ * In this case, the string is stored for the final ID that got sequenced first, and that final ID is stored associated with
90
+ * all subsequent final IDs with the same override.
91
+ */
92
+ overrides?: Map<FinalCompressedId, string | UnifiedOverride>;
93
+ }
94
+
95
+ type UnifiedOverride = OverrideCompressionDetails & {
96
+ override: string;
97
+ };
98
+
99
+ /**
100
+ * Data about a SharedTree session.
101
+ * Used to track and allocate identity clusters associated with a particular session ID.
102
+ */
103
+ interface Session {
104
+ readonly sessionUuid: NumericUuid;
105
+
106
+ /**
107
+ * `cluster` is undefined if a new cluster must be allocated when the session requests the next final ID allocation.
108
+ */
109
+ currentClusterDetails: { readonly clusterBase: FinalCompressedId; readonly cluster: IdCluster } | undefined;
110
+
111
+ /**
112
+ * The last local ID known to be finalized for this session.
113
+ */
114
+ lastFinalizedLocalId: LocalCompressedId | undefined;
115
+
116
+ /**
117
+ * The attribution info of the session, if it exists.
118
+ */
119
+ attributionInfo?: AttributionInfo;
120
+ }
121
+
122
+ /**
123
+ * Roughly equates to a minimum of 1M sessions before we start allocating 64 bit IDs.
124
+ * This value must *NOT* change without careful consideration to compatibility.
125
+ */
126
+ export const defaultClusterCapacity = 512;
127
+
128
+ /**
129
+ * The base UUID for the reserved id cluster.
130
+ * This should not be changed without consideration to compatibility.
131
+ */
132
+ const reservedSessionId = ensureSessionUuid(assertIsStableId('decaf40b-3c1a-47f8-a7a1-e8461ddb69ce'));
133
+
134
+ /**
135
+ * The ID override for the initial tree of a SharedTree. An artifact of an unfortunate typo which included an extraneous '6' on the UUID
136
+ * which must be forever preserved for backwards compatibility.
137
+ */
138
+ export const legacySharedTreeInitialTreeId = `24e26f0b-3c1a-47f8-a7a1-e8461ddb69ce6`;
139
+
140
+ /**
141
+ * @returns true if the supplied ID is a final ID.
142
+ */
143
+ export function isFinalId(id: CompressedId): id is FinalCompressedId {
144
+ return id >= 0;
145
+ }
146
+
147
+ /**
148
+ * @returns true if the supplied ID is a local ID.
149
+ */
150
+ export function isLocalId(id: CompressedId): id is LocalCompressedId {
151
+ return id < 0;
152
+ }
153
+
154
+ /**
155
+ * A object for retrieving the session-space IDs for a range of IDs.
156
+ * Optimized to avoid allocating an array of IDs.
157
+ */
158
+ export interface IdRange {
159
+ /**
160
+ * The length of the ID range.
161
+ */
162
+ readonly length: number;
163
+
164
+ /**
165
+ * Returns the ID in range at the provided index.
166
+ */
167
+ get(index: number): SessionSpaceCompressedId;
168
+ }
169
+
170
+ /**
171
+ * A serializable descriptor of a range of session-space IDs.
172
+ * The contained IDs must be retrieved by calling `getIdsFromRange`, which returns an `IdRange`.
173
+ */
174
+ export interface IdRangeDescriptor<TId extends LocalCompressedId | OpSpaceCompressedId> {
175
+ readonly first: TId;
176
+ readonly count: number;
177
+ }
178
+
179
+ /**
180
+ * A cluster in `clustersAndOverridesInversion`, which is mapped from the first stable ID in a cluster.
181
+ */
182
+ interface ClusterInfo {
183
+ readonly clusterBase: FinalCompressedId;
184
+ readonly cluster: IdCluster;
185
+ }
186
+
187
+ interface OverrideCompressionDetails {
188
+ readonly originalOverridingFinal: FinalCompressedId;
189
+ readonly associatedLocalId?: LocalCompressedId;
190
+ }
191
+
192
+ /**
193
+ * An override with a final ID associated with it.
194
+ *
195
+ * `associatedLocalId` is present on this type when a local ID in this session is associated with the override.
196
+ *
197
+ * It may be present even when `overriddenFinalId` was created by another session. This occurs when local IDs corresponding to the
198
+ * same override string are created by different sessions before any have been finalized. `overriddenFinalId` will be set to
199
+ * the *first* finalized ID with that string, but `associatedLocal` will be set to the local session's local ID for that string. This is
200
+ * done to preserve the invariant that an override will always compress into the same session-space ID for the lifetime of the session.
201
+ */
202
+ interface FinalizedOverride extends OverrideCompressionDetails {
203
+ readonly cluster: IdCluster;
204
+ }
205
+
206
+ /**
207
+ * The value of a mapping in `clustersAndOverridesInversion`, which maps an override to the cluster containing it (if finalized) or the
208
+ * local ID corresponding to it (if unfinalized).
209
+ *
210
+ * Override strings associated with local IDs stored in `clustersAndOverridesInversion` are *always* replaced immediately upon finalizing,
211
+ * and thus it is typed as op-space (unacked local).
212
+ */
213
+ type Override = UnackedLocalId | FinalizedOverride;
214
+
215
+ type CompressionMapping = ClusterInfo | Override;
216
+
217
+ /** Prepended to all keys in {@link IdCompressor.clustersAndOverridesInversion} that are override strings and not valid `StableIds` */
218
+ const nonStableOverridePrefix = '\ue15e'; // A character in the Private Use Area of the BMP (https://en.wikipedia.org/wiki/Private_Use_Areas)
219
+
220
+ /** Keys of {@link IdCompressor.clustersAndOverridesInversion} */
221
+ type InversionKey = `${typeof nonStableOverridePrefix}${string}` | StableId;
222
+
223
+ /**
224
+ * A distributed UUID generator and compressor.
225
+ *
226
+ * Generates arbitrary non-colliding v4 UUIDs, called stable IDs, for multiple "sessions" (which can be distributed across the network),
227
+ * providing each session with the ability to map these UUIDs to `numbers`.
228
+ *
229
+ * A session is a unique identifier that denotes a single compressor. New IDs are created through a single compressor API
230
+ * which should then sent in ranges to the server for total ordering (and are subsequently relayed to other clients). When a new ID is
231
+ * created it is said to be created by the compressor's "local" session.
232
+ *
233
+ * For each stable ID created, two numeric IDs are provided by the compressor:
234
+ * 1. A local ID, which is stable for the lifetime of the session (which could be longer than that of the compressor object, as it may
235
+ * be serialized for offline usage). Available as soon as the stable ID is allocated. Local IDs are session-unique and are thus only
236
+ * publicly usable by the compressor that created the stable ID.
237
+ * 2. A final ID, which is stable across serialization and deserialization of an IdCompressor. Available as soon as the range containing
238
+ * the corresponding local ID is totally ordered (via consensus) with respect to other sessions' allocations.
239
+ * Final IDs are known to and publicly usable by any compressor that has received them.
240
+ *
241
+ * Compressors will allocate UUIDs in non-random ways to reduce entropy allowing for optimized storage of the data needed
242
+ * to map the UUIDs to the numbers.
243
+ *
244
+ * A client may optionally supply an "override" for any generated ID, associating an arbitrary string with the local/final ID rather than
245
+ * the UUID that would otherwise be created.
246
+ *
247
+ * The following invariants are upheld by IdCompressor:
248
+ * 1. Local IDs will always decompress to the same UUIDs (or override string) for the lifetime of the session.
249
+ * 2. Final IDs will always decompress to the same UUIDs (or override string).
250
+ * 3. After a server-processed range of local IDs (from any session) is received by a compressor, any of those local IDs may be
251
+ * translated by the compressor into the corresponding final ID. For any given local ID, this translation will always yield the
252
+ * same final ID.
253
+ * 4. A UUID (or override string) will always compress into the same session-space ID for the lifetime of the session.
254
+ *
255
+ * Local IDs are sent across the wire in efficiently-represented ranges. These ranges are created by querying the compressor, and *must*
256
+ * be ordered (i.e. sent to the server) in the order they are created in order to preserve the above invariants.
257
+ *
258
+ * Session-local IDs can be used immediately after creation, but will eventually (after being sequenced) have a corresponding final ID. This
259
+ * could make reasoning about equality of those two forms (the local and final) difficult. For example, if a cache is keyed off of a
260
+ * local ID but is later queried using the final ID (which is semantically equal, as it decompresses to the same UUID/string) it will
261
+ * produce a cache miss. In order to make using collections of both remotely created and locally created IDs easy, regardless of whether the
262
+ * session-local IDs have been finalized, the compressor defines two "spaces" of IDs:
263
+ *
264
+ * 1. Session space: in this space, all IDs are normalized to their "most local form". This means that all IDs created by the local session
265
+ * will be in local form, regardless of if they have been finalized. Remotely created IDs, which could only have been received after
266
+ * finalizing and will never have a local form for the compressor, will of course be final IDs. This space should be used with consumer APIs
267
+ * and data structures, as the lifetime of the IDs is guaranteed to be the same as the compressor object. Care must be taken to not use
268
+ * these IDs across compressor objects, as the local IDs are specific to the compressor that created them.
269
+ *
270
+ * 2. Op space: in this space, all IDs are normalized to their "most final form". This means that all IDs except session-local IDs that
271
+ * have not yet been finalized will be in final ID form. This space is useful for serialization in ops (e.g. references), as other clients
272
+ * that receive them need not do any work to normalize them to *their* session-space in the common case. Note that IDs in op space may move
273
+ * out of Op space over time, namely, when a local ID in this space becomes finalized, and thereafter has a "more final form".
274
+ * Consequentially, it may be useful to restrict parameters of a persisted type to this space (to optimize perf), but it is potentially
275
+ * incorrect to use this type for a runtime variable. This is an asymmetry that does not affect session space, as local IDs are always as
276
+ * "local as possible".
277
+ *
278
+ * These two spaces naturally define a rule: consumers of compressed IDs should use session-space IDs, but serialized forms such as ops
279
+ * should use op-space IDs.
280
+ *
281
+ */
282
+ export class IdCompressor {
283
+ /**
284
+ * Max allowed cluster size
285
+ */
286
+ public static maxClusterSize = 2 ** 20;
287
+
288
+ /**
289
+ * Trivially reach consensus on default cluster size and reserved IDs.
290
+ * These initial values must *NOT* change without careful consideration to compatibility.
291
+ */
292
+ private newClusterCapacity = defaultClusterCapacity;
293
+
294
+ /**
295
+ * The size of each newly created ID cluster.
296
+ */
297
+ public get clusterCapacity(): number {
298
+ return this.newClusterCapacity;
299
+ }
300
+
301
+ /**
302
+ * Must only be set with a value upon which consensus has been reached. Value must be greater than zero and less than
303
+ * `IdCompressor.maxClusterSize`.
304
+ */
305
+ public set clusterCapacity(value: number) {
306
+ assert(value > 0, 'Clusters must have a positive capacity');
307
+ assert(value <= IdCompressor.maxClusterSize, 'Clusters must not exceed max cluster size');
308
+ this.newClusterCapacity = value;
309
+ }
310
+
311
+ /**
312
+ * Session ID -> data about the session's current cluster.
313
+ * Sessions are mutable, and thus should only be created via `createSession`.
314
+ */
315
+ private readonly sessions = new Map<SessionId, Session>();
316
+
317
+ /**
318
+ * The `IdCompressor`'s current local session.
319
+ */
320
+ private readonly localSession: Session;
321
+
322
+ /**
323
+ * Boolean to track whether attribution has been sent with an ID range yet. Prevents unnecessary bloat of ranges.
324
+ */
325
+ private sentAttributionInfo = false;
326
+
327
+ /**
328
+ * The base final ID of the next cluster to be created.
329
+ */
330
+ private nextClusterBaseFinalId: FinalCompressedId = 0 as FinalCompressedId;
331
+
332
+ /**
333
+ * Total number of IDs created locally during the current session.
334
+ */
335
+ private localIdCount = 0;
336
+
337
+ /**
338
+ * The most recent (i.e. smallest, due to being negative) local ID in a range returned by `takeNextCreationRange`.
339
+ * Undefined if no non-empty ranges have ever been returned by this compressor.
340
+ */
341
+ private lastTakenLocalId: LocalCompressedId | undefined;
342
+
343
+ /**
344
+ * Maps local IDs to override strings. This will contain an entry for every override assigned to a local ID generated during
345
+ * the current session, and retains entries for the lifetime of this compressor.
346
+ */
347
+ private readonly localOverrides = new AppendOnlySortedMap<LocalCompressedId, string>(compareFiniteNumbersReversed);
348
+
349
+ /**
350
+ * Maps local IDs to the cluster they belong to (if any). This can be used to efficiently convert a local ID to a
351
+ * final ID by finding an entry <= a given local ID (to find the cluster it is associated with) and checking
352
+ * it against `numFinalizedLocalIds`.
353
+ */
354
+ private readonly localIdToCluster: AppendOnlyDoublySortedMap<
355
+ LocalCompressedId,
356
+ [FinalCompressedId, IdCluster],
357
+ FinalCompressedId
358
+ > = new AppendOnlyDoublySortedMap(
359
+ compareFiniteNumbersReversed,
360
+ (value) => value[0],
361
+ IdCompressor.overrideComparator
362
+ );
363
+
364
+ /**
365
+ * Contains entries for cluster base UUIDs and override strings (both local and final).
366
+ * As a performance optimization, entries for finalized strings also include the containing cluster object.
367
+ * This can be viewed as three separate tables: the inverse table for `localOverrides`, the inverse table for the union of all
368
+ * the overrides of the clusters in `finalIdToCluster`, and the inverse lookup of cluster base UUIDs to their clusters.
369
+ * This is unified as a performance optimization, as the common case does not have overridden IDs. It is a btree due to the need
370
+ * to make range queries.
371
+ */
372
+ private readonly clustersAndOverridesInversion: BTree<InversionKey, CompressionMapping> = new BTree(
373
+ undefined,
374
+ compareStrings
375
+ );
376
+
377
+ /**
378
+ * Maps the first final ID in a cluster to its owning cluster.
379
+ * Can be searched in O(log n) to determine clusters for any final ID.
380
+ */
381
+ private readonly finalIdToCluster: AppendOnlySortedMap<FinalCompressedId, IdCluster> = new AppendOnlySortedMap(
382
+ compareFiniteNumbers
383
+ );
384
+
385
+ /**
386
+ * Helper comparator for searching append-only sorted maps.
387
+ */
388
+ private static overrideComparator<T extends number>(search: T, element: readonly [T, unknown]): number {
389
+ return compareFiniteNumbers(search, element[0]);
390
+ }
391
+
392
+ /**
393
+ * @param localSessionId the `IdCompressor`'s current local session ID.
394
+ * @param reservedIdCount the number of IDs that will be known by this compressor without relying on consensus. The reserved ID count
395
+ * for a given session must be constant for any compressor that contains IDs from that session (i.e. any DDS that uses the ID
396
+ * compressor must have the same reservedIdCount forever). Compressors with different reserved ID counts will fail to synchronize their
397
+ * IDs.
398
+ * @param attributionInfo information used by other clients to attribute IDs made by this client
399
+ */
400
+ public constructor(
401
+ public readonly localSessionId: SessionId,
402
+ public readonly reservedIdCount: number,
403
+ attributionInfo?: AttributionInfo
404
+ ) {
405
+ assert(reservedIdCount >= 0, 'reservedIdCount must be non-negative');
406
+ this.localSession = this.createSession(localSessionId, attributionInfo);
407
+ if (reservedIdCount > 0) {
408
+ const clusterCapacity = this.clusterCapacity;
409
+ this.clusterCapacity = reservedIdCount;
410
+ const reservedIdRange: IdCreationRange = {
411
+ sessionId: reservedSessionId,
412
+ ids: {
413
+ last: -reservedIdCount as UnackedLocalId,
414
+ overrides: [[-1 as UnackedLocalId, legacySharedTreeInitialTreeId]], // Kludge: see `initialTreeId`
415
+ },
416
+ };
417
+ // Reserved final IDs are implicitly finalized and no one locally created them, so finalizing immediately is safe.
418
+ this.finalizeCreationRange(reservedIdRange);
419
+ this.clusterCapacity = clusterCapacity;
420
+ }
421
+ }
422
+
423
+ /**
424
+ * Creates a session object for the supplied ID.
425
+ * Must only be called once per ID.
426
+ * @param sessionId the ID for the session
427
+ * @returns the session object for the supplied ID
428
+ */
429
+ private createSession(sessionId: SessionId, attributionInfo: AttributionInfo | undefined): Session {
430
+ const existingSession = this.sessions.get(sessionId);
431
+ if (existingSession !== undefined) {
432
+ fail('createSession must only be called once for each session ID.');
433
+ }
434
+ const sessionUuid = numericUuidFromStableId(sessionId);
435
+ assert(!this.clustersAndOverridesInversion.has(sessionId));
436
+ const session: Session = {
437
+ sessionUuid,
438
+ currentClusterDetails: undefined,
439
+ lastFinalizedLocalId: undefined,
440
+ };
441
+ setPropertyIfDefined(attributionInfo, session, 'attributionInfo');
442
+ this.sessions.set(sessionId, session);
443
+ return session;
444
+ }
445
+
446
+ private tryGetSession(sessionId: SessionId): Session | undefined {
447
+ return this.sessions.get(sessionId);
448
+ }
449
+
450
+ /**
451
+ * Return the nth reserved ID.
452
+ * @param index the index of the ID to return
453
+ */
454
+ public getReservedId(index: number): SessionSpaceCompressedId & FinalCompressedId {
455
+ if (index < 0 || index >= this.reservedIdCount) {
456
+ fail('Reserved Id index out of bounds');
457
+ }
458
+
459
+ // All reserved IDs are contiguous and finalized during the Compressor's construction, therefore they are always the lowest
460
+ // final IDs, beginning at 0
461
+ return index as SessionSpaceCompressedId & FinalCompressedId;
462
+ }
463
+
464
+ /**
465
+ * Returns an iterable of all IDs created by this compressor.
466
+ */
467
+ public *getAllIdsFromLocalSession(): IterableIterator<SessionSpaceCompressedId> {
468
+ // TODO: this will change when final IDs are returned eagerly
469
+ for (let i = 1; i <= this.localIdCount; i++) {
470
+ yield -i as LocalCompressedId;
471
+ }
472
+ }
473
+
474
+ /**
475
+ * Returns the attribution info associated with the compressor that created the ID, if it exists.
476
+ */
477
+ public attributeId(id: SessionSpaceCompressedId): AttributionInfo | undefined {
478
+ const opSpaceNormalizedId = this.normalizeToOpSpace(id);
479
+ if (isLocalId(opSpaceNormalizedId)) {
480
+ return this.localSession.attributionInfo;
481
+ }
482
+ const [_, cluster] =
483
+ this.getClusterForFinalId(opSpaceNormalizedId) ?? fail('Cluster does not exist for final ID');
484
+
485
+ return cluster.session.attributionInfo;
486
+ }
487
+
488
+ /**
489
+ * Provides the session-space IDs corresponding to a range of IDs.
490
+ * See `IdRange` for more details.
491
+ */
492
+ public getIdsFromRange(
493
+ rangeDescriptor: IdRangeDescriptor<SessionSpaceCompressedId>,
494
+ sessionId: SessionId
495
+ ): IdRange {
496
+ const { first, count } = rangeDescriptor;
497
+ if (sessionId === this.localSessionId) {
498
+ return {
499
+ length: count,
500
+ get: (index: number) => {
501
+ if (index < 0 || index >= count) {
502
+ fail('Index out of bounds of range.');
503
+ }
504
+ return (first - index) as LocalCompressedId;
505
+ },
506
+ };
507
+ } else {
508
+ const session = this.tryGetSession(sessionId) ?? fail('Unknown session, range may not be finalized.');
509
+ const firstNumericUuid = incrementUuid(session.sessionUuid, -first - 1);
510
+ const firstFinal =
511
+ this.compressNumericUuid(firstNumericUuid) ??
512
+ fail('Remote range must be finalized before getting IDs.');
513
+ assert(
514
+ isFinalId(firstFinal),
515
+ 'ID from a remote session ID must have final form, as overrides are impossible by definition.'
516
+ );
517
+ const [baseFinalId, cluster] = this.getClusterForFinalId(firstFinal) ?? fail();
518
+ const numIdsRemainingInFirstCluster = cluster.capacity - (firstFinal - baseFinalId);
519
+ let pivotFinal: FinalCompressedId | undefined;
520
+ if (count > numIdsRemainingInFirstCluster) {
521
+ const compressedPivot = this.compressNumericUuid(
522
+ incrementUuid(firstNumericUuid, numIdsRemainingInFirstCluster)
523
+ );
524
+ // Looking up the actual cluster can be avoided, as it is guaranteed that at most one new cluster will be
525
+ // created when finalizing a range (regardless of size) due to the expansion optimization.
526
+ if (compressedPivot === undefined || isLocalId(compressedPivot)) {
527
+ fail(
528
+ 'ID from a remote session ID must have final form, as overrides are impossible by definition.'
529
+ );
530
+ } else {
531
+ pivotFinal = compressedPivot;
532
+ }
533
+ }
534
+
535
+ return {
536
+ length: count,
537
+ get: (index: number) => {
538
+ if (index < 0 || index >= count) {
539
+ fail('Index out of bounds of range.');
540
+ }
541
+ if (index < numIdsRemainingInFirstCluster) {
542
+ return (firstFinal + index) as FinalCompressedId & SessionSpaceCompressedId;
543
+ } else {
544
+ return ((pivotFinal ?? fail('Pivot must exist if range spans clusters.')) +
545
+ (index - numIdsRemainingInFirstCluster)) as FinalCompressedId & SessionSpaceCompressedId;
546
+ }
547
+ },
548
+ };
549
+ }
550
+ }
551
+
552
+ /**
553
+ * Returns a range of local IDs created by this session in a format for sending to the server for finalizing.
554
+ * The range will include all local IDs generated via calls to `generateCompressedId` since the last time this method was called.
555
+ * @returns the range of session-local IDs, which may be empty. This range must be sent to the server for ordering before
556
+ * it is finalized. Ranges must be sent to the server in the order that they are taken via calls to this method.
557
+ */
558
+ public takeNextCreationRange(): IdCreationRange {
559
+ const lastLocalInRange = -this.localIdCount as UnackedLocalId;
560
+ const lastTakenNormalized = this.lastTakenLocalId ?? 0;
561
+ assert(lastLocalInRange <= lastTakenNormalized);
562
+
563
+ let ids: IdCreationRange.Ids | undefined;
564
+ if (lastLocalInRange !== lastTakenNormalized) {
565
+ const firstLocalInRange = (lastTakenNormalized - 1) as UnackedLocalId;
566
+ const localOverrides = [
567
+ ...this.localOverrides.getRange(
568
+ (lastTakenNormalized - 1) as LocalCompressedId,
569
+ lastLocalInRange as LocalCompressedId
570
+ ),
571
+ ];
572
+ if (localOverrides.length > 0) {
573
+ // Cast: typecript 4.4.4 doesn't infer that `localOverrides` has at least one element and is therefore an `Overrides`
574
+ const overrides = localOverrides as unknown as IdCreationRange.Overrides;
575
+ assert(overrides[0][0] <= firstLocalInRange);
576
+ assert(overrides[overrides.length - 1][0] >= lastLocalInRange);
577
+ ids = {
578
+ overrides,
579
+ };
580
+ const first = firstLocalInRange === overrides[0][0] ? undefined : firstLocalInRange;
581
+ const last = lastLocalInRange === overrides[overrides.length - 1][0] ? undefined : lastLocalInRange;
582
+ setPropertyIfDefined(first, ids, 'first');
583
+ setPropertyIfDefined(last, ids, 'last');
584
+ } else {
585
+ ids = {
586
+ first: firstLocalInRange,
587
+ last: lastLocalInRange,
588
+ };
589
+ }
590
+ this.lastTakenLocalId = lastLocalInRange;
591
+ }
592
+
593
+ const range: Mutable<IdCreationRange> = { sessionId: this.localSessionId };
594
+ if (!this.sentAttributionInfo) {
595
+ setPropertyIfDefined(this.localSession.attributionInfo, range, 'attributionInfo');
596
+ this.sentAttributionInfo = true;
597
+ }
598
+
599
+ if (ids === undefined) {
600
+ return range;
601
+ }
602
+
603
+ assert(
604
+ this.lastTakenLocalId === -this.localIdCount && this.lastTakenLocalId !== lastTakenNormalized,
605
+ 'Non-empty range must properly consume local IDs'
606
+ );
607
+
608
+ range.ids = ids;
609
+ return range;
610
+ }
611
+
612
+ /**
613
+ * Finalizes the supplied range of IDs (which may be from either a remote or local session).
614
+ * @param range the range of session-local IDs to finalize.
615
+ */
616
+ public finalizeCreationRange(range: IdCreationRange): void {
617
+ const { sessionId, attributionInfo } = range;
618
+
619
+ const isLocal = sessionId === this.localSessionId;
620
+ let session = this.tryGetSession(sessionId);
621
+ assert(
622
+ range.attributionInfo === undefined || session === undefined || isLocal,
623
+ 'Attribution info can only be supplied on initial range for a session, and never modified.'
624
+ );
625
+ session ??= this.createSession(sessionId, attributionInfo);
626
+
627
+ const ids = getIds(range);
628
+ if (ids === undefined) {
629
+ return;
630
+ }
631
+
632
+ const { currentClusterDetails } = session;
633
+ const { cluster: currentCluster, clusterBase: currentBaseFinalId } = currentClusterDetails ?? {
634
+ cluster: undefined,
635
+ clusterBase: undefined,
636
+ };
637
+
638
+ const normalizedLastFinalized = session.lastFinalizedLocalId ?? 0;
639
+ const { first: newFirstFinalizedLocalId, last: newLastFinalizedLocalId } = ids;
640
+ assert(newFirstFinalizedLocalId === normalizedLastFinalized - 1, 'Ranges finalized out of order.');
641
+
642
+ // The total number of session-local IDs to finalize
643
+ const finalizeCount = normalizedLastFinalized - newLastFinalizedLocalId;
644
+ assert(finalizeCount >= 1, 'Cannot finalize an empty range.');
645
+
646
+ let initialClusterCount = 0;
647
+ let remainingCount = finalizeCount;
648
+ let newBaseUuid: NumericUuid | undefined;
649
+ if (currentCluster !== undefined && currentBaseFinalId !== undefined) {
650
+ initialClusterCount = currentCluster.count;
651
+ const remainingCapacity = currentCluster.capacity - initialClusterCount;
652
+ const overflow = remainingCount - remainingCapacity;
653
+ const hasRoom = overflow <= 0;
654
+ if (hasRoom || currentBaseFinalId === this.finalIdToCluster.maxKey()) {
655
+ currentCluster.count += remainingCount;
656
+ remainingCount = 0;
657
+ // The common case is that there is room in the cluster, and the new final IDs can simply be added to it
658
+ if (!hasRoom) {
659
+ // The cluster is full but is the last in the list of clusters.
660
+ // This allows it to be expanded instead of allocating a new one.
661
+ const expansionAmount = this.newClusterCapacity + overflow;
662
+ currentCluster.capacity += expansionAmount;
663
+ this.nextClusterBaseFinalId = (this.nextClusterBaseFinalId + expansionAmount) as FinalCompressedId;
664
+ assert(
665
+ this.nextClusterBaseFinalId < Number.MAX_SAFE_INTEGER,
666
+ 'The number of allocated final IDs must not exceed the JS maximum safe integer.'
667
+ );
668
+ this.checkClusterForCollision(currentCluster);
669
+ }
670
+ } else {
671
+ // The range cannot be fully allocated in the existing cluster, so allocate any space left in it and
672
+ // form a new one by incrementing the previous baseUuid
673
+ newBaseUuid = incrementUuid(currentCluster.baseUuid, currentCluster.capacity);
674
+ currentCluster.count += remainingCapacity;
675
+ remainingCount -= remainingCapacity;
676
+ }
677
+ } else {
678
+ // Session has never made a cluster, form a new one with the session UUID as the baseUuid
679
+ newBaseUuid = session.sessionUuid;
680
+ }
681
+
682
+ // Finalizing a range results in one of three cases:
683
+ // 1. All local IDs are finalized into a new cluster (because there was either never a cluster for that session, or the current
684
+ // cluster for the session was full).
685
+ // 2. All local IDs are finalized into the existing (current) cluster for the session.
686
+ // 3. Local IDs are finalized into both the current cluster and a new one, as the current cluster did not have enough room.
687
+ let newCluster: IdCluster | undefined;
688
+ let newBaseFinalId: FinalCompressedId | undefined;
689
+ // The first local ID that will be finalized into a new cluster, if there is one.
690
+ // This lets us quickly compare which cluster an override string will go into.
691
+ let localIdPivot: LocalCompressedId | undefined;
692
+
693
+ // Need to make a new cluster
694
+ if (newBaseUuid !== undefined) {
695
+ if (remainingCount <= 0) {
696
+ fail('Should not create an empty cluster.');
697
+ }
698
+ if (currentCluster !== undefined && currentCluster.capacity !== currentCluster.count) {
699
+ fail('Cluster must be filled before another is allocated.');
700
+ }
701
+
702
+ newBaseFinalId = this.nextClusterBaseFinalId;
703
+ newCluster = {
704
+ baseUuid: newBaseUuid,
705
+ capacity: Math.max(this.newClusterCapacity, remainingCount),
706
+ count: remainingCount,
707
+ session,
708
+ };
709
+
710
+ const usedCapacity = finalizeCount - remainingCount;
711
+ localIdPivot = (newFirstFinalizedLocalId - usedCapacity) as LocalCompressedId;
712
+
713
+ if (isLocal) {
714
+ this.localIdToCluster.append(localIdPivot, [newBaseFinalId, newCluster]);
715
+ }
716
+
717
+ this.checkClusterForCollision(newCluster);
718
+ this.clustersAndOverridesInversion.set(stableIdFromNumericUuid(newCluster.baseUuid), {
719
+ clusterBase: newBaseFinalId,
720
+ cluster: newCluster,
721
+ });
722
+ session.currentClusterDetails = { cluster: newCluster, clusterBase: newBaseFinalId };
723
+ this.nextClusterBaseFinalId = (this.nextClusterBaseFinalId + newCluster.capacity) as FinalCompressedId;
724
+ assert(
725
+ this.nextClusterBaseFinalId < Number.MAX_SAFE_INTEGER,
726
+ 'The number of allocated final IDs must not exceed the JS maximum safe integer.'
727
+ );
728
+ this.finalIdToCluster.append(newBaseFinalId, newCluster);
729
+ }
730
+
731
+ // If there are overrides, we must determine which cluster object (current or overflow) each belongs to and add it.
732
+ const overrides = ids.overrides;
733
+ if (overrides !== undefined) {
734
+ for (let i = 0; i < overrides.length; i++) {
735
+ const [overriddenLocal, override] = overrides[i];
736
+ // Note: recall that local IDs are negative
737
+ assert(i === 0 || overriddenLocal < overrides[i - 1][0], 'Override IDs must be in sorted order.');
738
+ assert(overriddenLocal < normalizedLastFinalized, 'Ranges finalized out of order.');
739
+ assert(
740
+ overriddenLocal >= newLastFinalizedLocalId,
741
+ 'Malformed range: override ID ahead of range start.'
742
+ );
743
+ let cluster: IdCluster;
744
+ let overriddenFinal: FinalCompressedId;
745
+ if (localIdPivot !== undefined && overriddenLocal <= localIdPivot) {
746
+ // Override is at or past the pivot, so it is in a new cluster.
747
+ assert(
748
+ newCluster !== undefined && newBaseFinalId !== undefined,
749
+ 'No cluster was created when overflow occurred.'
750
+ );
751
+ cluster = newCluster;
752
+ overriddenFinal = (newBaseFinalId + (localIdPivot - overriddenLocal)) as FinalCompressedId;
753
+ } else {
754
+ // Override was finalized into an existing cluster
755
+ assert(
756
+ currentCluster !== undefined && currentBaseFinalId !== undefined,
757
+ 'No cluster exists but IDs were finalized.'
758
+ );
759
+ cluster = currentCluster;
760
+ overriddenFinal = (currentBaseFinalId +
761
+ initialClusterCount +
762
+ (normalizedLastFinalized - overriddenLocal) -
763
+ 1) as FinalCompressedId;
764
+ }
765
+ cluster.overrides ??= new Map();
766
+
767
+ const inversionKey = IdCompressor.createInversionKey(override);
768
+ // TODO: This cast can be removed on typescript 4.6
769
+ const existingIds = this.getExistingIdsForNewOverride(inversionKey, true) as [
770
+ LocalCompressedId,
771
+ FinalCompressedId
772
+ ];
773
+ let overrideForCluster: string | FinalCompressedId;
774
+ let associatedLocal: LocalCompressedId | undefined;
775
+ if (existingIds !== undefined) {
776
+ let mostFinalExistingOverride: CompressedId;
777
+ if (typeof existingIds === 'number') {
778
+ mostFinalExistingOverride = existingIds;
779
+ if (isLocalId(mostFinalExistingOverride)) {
780
+ associatedLocal = mostFinalExistingOverride;
781
+ }
782
+ } else {
783
+ [associatedLocal, mostFinalExistingOverride] = existingIds;
784
+ }
785
+ if (isFinalId(mostFinalExistingOverride)) {
786
+ // A previous range already finalized an ID with this override. See `IdCluster` for more.
787
+ overrideForCluster = mostFinalExistingOverride;
788
+ } else {
789
+ assert(
790
+ !isLocal || mostFinalExistingOverride === overriddenLocal,
791
+ 'Cannot have multiple local IDs with identical overrides.'
792
+ );
793
+ // This session has created an ID with this override before, but has not finalized it yet. The incoming
794
+ // range "wins" and will contain the final ID associated with that override, regardless of if that range was
795
+ // made by this session or not.
796
+ overrideForCluster = override;
797
+ }
798
+ } else {
799
+ // This is the first time this override has been associated with any ID
800
+ overrideForCluster = override;
801
+ }
802
+
803
+ assert(!cluster.overrides.has(overriddenFinal), 'Cannot add a second override for final id');
804
+ if (typeof overrideForCluster === 'string') {
805
+ if (isLocal || associatedLocal === undefined) {
806
+ cluster.overrides.set(overriddenFinal, override);
807
+ } else {
808
+ cluster.overrides.set(overriddenFinal, {
809
+ override,
810
+ originalOverridingFinal: overriddenFinal,
811
+ associatedLocalId: associatedLocal,
812
+ });
813
+ }
814
+ } else {
815
+ const unifiedOverride: UnifiedOverride = {
816
+ override,
817
+ originalOverridingFinal: overrideForCluster,
818
+ };
819
+ setPropertyIfDefined(associatedLocal, unifiedOverride, 'associatedLocalId');
820
+ cluster.overrides.set(overriddenFinal, unifiedOverride);
821
+ }
822
+ const finalizedOverride: Mutable<FinalizedOverride> = {
823
+ cluster,
824
+ originalOverridingFinal: overriddenFinal,
825
+ };
826
+ setPropertyIfDefined(associatedLocal, finalizedOverride, 'associatedLocalId');
827
+ const currentOverride = this.clustersAndOverridesInversion.get(inversionKey);
828
+ if (currentOverride === undefined || IdCompressor.isUnfinalizedOverride(currentOverride)) {
829
+ // Update the map to contain a finalized override, but never update it with future finalized overrides with
830
+ // the same string; those should decompress to the first final ID with that override.
831
+ this.clustersAndOverridesInversion.set(inversionKey, finalizedOverride);
832
+ }
833
+ }
834
+ }
835
+
836
+ session.lastFinalizedLocalId = newLastFinalizedLocalId;
837
+ }
838
+
839
+ private checkClusterForCollision(cluster: IdCluster): void {
840
+ const maxClusterUuid = incrementUuid(cluster.baseUuid, cluster.capacity - 1);
841
+ const maxClusterStableId = stableIdFromNumericUuid(maxClusterUuid);
842
+ const closestMatch = this.clustersAndOverridesInversion.getPairOrNextLower(maxClusterStableId);
843
+ if (closestMatch !== undefined) {
844
+ const [inversionKey, compressionMapping] = closestMatch;
845
+ if (!IdCompressor.isClusterInfo(compressionMapping)) {
846
+ if (
847
+ isStableId(inversionKey) &&
848
+ IdCompressor.uuidsMightCollide(inversionKey, maxClusterStableId, cluster.capacity)
849
+ ) {
850
+ const numericOverride = numericUuidFromStableId(inversionKey);
851
+ const delta = getPositiveDelta(maxClusterUuid, numericOverride, cluster.capacity - 1);
852
+ if (delta !== undefined) {
853
+ IdCompressor.failWithCollidingOverride(inversionKey);
854
+ }
855
+ }
856
+ }
857
+ }
858
+ }
859
+
860
+ private static failWithCollidingOverride(override: string): void {
861
+ fail(`Override '${override}' collides with another allocated UUID.`);
862
+ }
863
+
864
+ private static isClusterInfo(compressionMapping: CompressionMapping): compressionMapping is ClusterInfo {
865
+ return (compressionMapping as ClusterInfo).clusterBase !== undefined;
866
+ }
867
+
868
+ private static isUnfinalizedOverride(compressionMapping: CompressionMapping): compressionMapping is UnackedLocalId {
869
+ return typeof compressionMapping === 'number';
870
+ }
871
+
872
+ private static createInversionKey(inversionKey: string): InversionKey {
873
+ // TODO: This cast can be removed on typescript 4.6
874
+ return isStableId(inversionKey) ? inversionKey : (`${nonStableOverridePrefix}${inversionKey}` as InversionKey);
875
+ }
876
+
877
+ private static isStableInversionKey(inversionKey: InversionKey): inversionKey is StableId {
878
+ return inversionKey.charAt(0) !== nonStableOverridePrefix;
879
+ }
880
+
881
+ /**
882
+ * Returns an existing ID associated with an override, or undefined if none exists.
883
+ */
884
+ private getExistingIdsForNewOverride(
885
+ inversionKey: InversionKey,
886
+ isFinalOverride: boolean
887
+ ): SessionSpaceCompressedId | [LocalCompressedId, FinalCompressedId] | undefined {
888
+ const closestMatch = this.clustersAndOverridesInversion.getPairOrNextLower(inversionKey, reusedArray);
889
+ let numericOverride: NumericUuid | undefined;
890
+ let stableOverride: StableId | undefined;
891
+ if (closestMatch !== undefined) {
892
+ const [key, compressionMapping] = closestMatch;
893
+ if (!IdCompressor.isClusterInfo(compressionMapping)) {
894
+ if (key === inversionKey) {
895
+ if (IdCompressor.isUnfinalizedOverride(compressionMapping)) {
896
+ return compressionMapping;
897
+ }
898
+ const finalizedOverride = compressionMapping;
899
+ return finalizedOverride.associatedLocalId !== undefined
900
+ ? [finalizedOverride.associatedLocalId, finalizedOverride.originalOverridingFinal]
901
+ : (finalizedOverride.originalOverridingFinal as SessionSpaceCompressedId);
902
+ }
903
+ } else if (IdCompressor.isStableInversionKey(inversionKey)) {
904
+ stableOverride = inversionKey;
905
+ const cluster = compressionMapping.cluster;
906
+ if (IdCompressor.uuidsMightCollide(inversionKey, key as StableId, cluster.capacity)) {
907
+ numericOverride = numericUuidFromStableId(stableOverride);
908
+ const delta = getPositiveDelta(numericOverride, cluster.baseUuid, cluster.capacity - 1);
909
+ if (delta !== undefined) {
910
+ if (isFinalOverride) {
911
+ IdCompressor.failWithCollidingOverride(inversionKey);
912
+ } else {
913
+ if (delta < cluster.count) {
914
+ return this.normalizeToSessionSpace(
915
+ (compressionMapping.clusterBase + delta) as FinalCompressedId
916
+ );
917
+ } else {
918
+ IdCompressor.failWithCollidingOverride(inversionKey);
919
+ }
920
+ }
921
+ }
922
+ }
923
+ }
924
+ }
925
+
926
+ const override =
927
+ numericOverride ??
928
+ stableOverride ??
929
+ (IdCompressor.isStableInversionKey(inversionKey) ? inversionKey : undefined);
930
+
931
+ if (override !== undefined) {
932
+ const localId = this.getLocalIdForStableId(override);
933
+ if (localId !== undefined) {
934
+ return localId;
935
+ }
936
+ }
937
+
938
+ return undefined;
939
+ }
940
+
941
+ /**
942
+ * Check if `a` might be within `range` of `b`, where both are treated as hex numbers.
943
+ * @param range an integer
944
+ */
945
+ private static uuidsMightCollide(a: StableId, b: StableId, range: number): boolean {
946
+ // Check if any of the UUIDs in the cluster collide (i.e. any in [base, base + capacity)).
947
+ // Optimization: All UUIDs in a cluster are the same string up until the last few characters which encode the offset from
948
+ // the cluster base. So, first compute the length of that shared string, and early out if it is different from the override
949
+ // UUID. This way we usually need not do the more expensive check below.
950
+ const hexDigitsToCheck = 32 - Math.ceil(Math.log2(range) / 2);
951
+ if (a.startsWith(b.slice(0, hexDigitsToCheck))) {
952
+ return true;
953
+ }
954
+
955
+ return false;
956
+ }
957
+
958
+ /**
959
+ * Helper for retrieving an override.
960
+ */
961
+ private static tryGetOverride(cluster: IdCluster, finalId: FinalCompressedId): string | undefined {
962
+ const override = cluster.overrides?.get(finalId);
963
+ if (override === undefined) {
964
+ return undefined;
965
+ }
966
+ if (typeof override === 'string') {
967
+ return override;
968
+ }
969
+ return override.override;
970
+ }
971
+
972
+ /**
973
+ * Generates a new compressed ID or returns an existing one.
974
+ * This should ONLY be called to generate IDs for local operations.
975
+ * @param override Specifies a specific string to be associated with the returned compressed ID.
976
+ * Performance note: assigning override strings incurs a performance overhead.
977
+ * @returns an existing ID if one already exists for `override`, and a new local ID otherwise. The returned ID is in session space.
978
+ */
979
+ public generateCompressedId(override?: string): SessionSpaceCompressedId {
980
+ // If any ID exists for this override (locally or remotely allocated), return it (after ensuring it is in session-space).
981
+ if (override !== undefined) {
982
+ const inversionKey = IdCompressor.createInversionKey(override);
983
+ const existingIds = this.getExistingIdsForNewOverride(inversionKey, false);
984
+ if (existingIds !== undefined) {
985
+ return typeof existingIds === 'number' ? existingIds : existingIds[0];
986
+ } else {
987
+ const newLocalId = this.generateNextLocalId();
988
+ this.localOverrides.append(newLocalId, override);
989
+ // Since the local ID was just created, it is in both session and op space
990
+ const compressionMapping = newLocalId as UnackedLocalId;
991
+ this.clustersAndOverridesInversion.set(inversionKey, compressionMapping);
992
+ return newLocalId;
993
+ }
994
+ } else {
995
+ return this.generateNextLocalId();
996
+ }
997
+ }
998
+
999
+ /**
1000
+ * Generates a range of compressed IDs.
1001
+ * This should ONLY be called to generate IDs for local operations.
1002
+ * @param count the number of IDs to generate, must be > 0.
1003
+ * @returns a persistable descriptor of the ID range.
1004
+ */
1005
+ public generateCompressedIdRange(count: number): IdRangeDescriptor<LocalCompressedId> {
1006
+ assert(count > 0, 'Must generate a nonzero number of IDs.');
1007
+ assert(
1008
+ count <= Number.MAX_SAFE_INTEGER,
1009
+ 'The number of allocated local IDs must not exceed the JS maximum safe integer.'
1010
+ );
1011
+ const first = this.generateNextLocalId();
1012
+ this.localIdCount += count - 1;
1013
+ return { first, count };
1014
+ }
1015
+
1016
+ private generateNextLocalId(): LocalCompressedId {
1017
+ return -++this.localIdCount as LocalCompressedId;
1018
+ }
1019
+
1020
+ /**
1021
+ * Decompresses a previously compressed ID into a UUID or override string.
1022
+ * @param id the compressed ID to be decompressed.
1023
+ * @returns the UUID or override string associated with the compressed ID. Fails if the ID was not generated by this compressor.
1024
+ */
1025
+ public decompress(id: SessionSpaceCompressedId | FinalCompressedId): StableId | string {
1026
+ return this.tryDecompress(id) ?? fail('Compressed ID was not generated by this compressor');
1027
+ }
1028
+
1029
+ /**
1030
+ * Attempts to decompress a previously compressed ID into a UUID or override string.
1031
+ * @param id the compressed ID to be decompressed.
1032
+ * @returns the UUID or override string associated with the compressed ID, or undefined if the ID was not generated by this compressor.
1033
+ */
1034
+ public tryDecompress(id: SessionSpaceCompressedId | FinalCompressedId): StableId | string | undefined {
1035
+ if (isFinalId(id)) {
1036
+ const possibleCluster = this.getClusterForFinalId(id);
1037
+ if (possibleCluster === undefined) {
1038
+ return undefined;
1039
+ } else {
1040
+ const [baseFinalId, cluster] = possibleCluster;
1041
+ const override = IdCompressor.tryGetOverride(cluster, id);
1042
+ if (override !== undefined) {
1043
+ return override;
1044
+ } else {
1045
+ const offsetInCluster = id - baseFinalId;
1046
+ return stableIdFromNumericUuid(cluster.baseUuid, offsetInCluster);
1047
+ }
1048
+ }
1049
+ } else {
1050
+ const idOffset = -id; // Convert to a positive number
1051
+ if (idOffset > this.localIdCount) {
1052
+ // This local ID was never allocated.
1053
+ return undefined;
1054
+ }
1055
+
1056
+ // If this is a local ID with an override, then it must have been allocated on this machine and will be contained in
1057
+ // `localOverrides`s. Otherwise, it is a sequential allocation from the session UUID and can simply be negated and
1058
+ // added to that UUID to obtain the stable ID associated with it.
1059
+ const localOverride = this.localOverrides?.get(id);
1060
+ if (localOverride !== undefined) {
1061
+ return localOverride;
1062
+ } else {
1063
+ return stableIdFromNumericUuid(this.localSession.sessionUuid, idOffset - 1);
1064
+ }
1065
+ }
1066
+ }
1067
+
1068
+ /**
1069
+ * Recompresses a decompressed ID, which could be a UUID or an override string.
1070
+ * @param uncompressed the UUID or override string to recompress.
1071
+ * @returns the `CompressedId` associated with `uncompressed`. Fails if it has not been previously compressed by this compressor.
1072
+ */
1073
+ public recompress(uncompressed: string): SessionSpaceCompressedId {
1074
+ return this.tryRecompress(uncompressed) ?? fail('No such string has ever been compressed');
1075
+ }
1076
+
1077
+ /**
1078
+ * Attempts to recompresses a decompressed ID, which could be a UUID or an override string.
1079
+ * @param uncompressed the UUID or override string to recompress,
1080
+ * @returns the `CompressedId` associated with `uncompressed` or undefined if it has not been previously compressed by this compressor.
1081
+ */
1082
+ public tryRecompress(uncompressed: string): SessionSpaceCompressedId | undefined {
1083
+ return this.recompressInternal(uncompressed);
1084
+ }
1085
+
1086
+ /**
1087
+ * Helper to compress an uncompressed UUID. It can optionally be supplied with the numeric form of `uncompressedUuid` as a
1088
+ * performance optimization.
1089
+ */
1090
+ private recompressInternal(
1091
+ uncompressed: string,
1092
+ uncompressedUuidNumeric?: NumericUuid
1093
+ ): SessionSpaceCompressedId | undefined {
1094
+ let numericUuid = uncompressedUuidNumeric;
1095
+ // TODO: This cast can be removed on typescript 4.6, and should give improved typesafety.
1096
+ const inversionKey = IdCompressor.createInversionKey(uncompressed) as StableId;
1097
+ const isStable = IdCompressor.isStableInversionKey(inversionKey);
1098
+ const closestMatch = this.clustersAndOverridesInversion.getPairOrNextLower(inversionKey, reusedArray);
1099
+ if (closestMatch !== undefined) {
1100
+ const [key, compressionMapping] = closestMatch;
1101
+ if (!IdCompressor.isClusterInfo(compressionMapping)) {
1102
+ if (key === inversionKey) {
1103
+ if (IdCompressor.isUnfinalizedOverride(compressionMapping)) {
1104
+ return compressionMapping;
1105
+ } else {
1106
+ const cluster = compressionMapping.cluster;
1107
+ assert(
1108
+ IdCompressor.tryGetOverride(cluster, compressionMapping.originalOverridingFinal) !==
1109
+ undefined,
1110
+ 'No override for cluster marked as having one.'
1111
+ );
1112
+ return (
1113
+ compressionMapping.associatedLocalId ??
1114
+ (compressionMapping.originalOverridingFinal as SessionSpaceCompressedId)
1115
+ );
1116
+ }
1117
+ }
1118
+ } else {
1119
+ if (!isStable) {
1120
+ return undefined;
1121
+ }
1122
+ const { clusterBase: closestBaseFinalId, cluster: closestCluster } = compressionMapping;
1123
+ numericUuid ??= numericUuidFromStableId(inversionKey);
1124
+ const uuidOffset = getPositiveDelta(numericUuid, closestCluster.baseUuid, closestCluster.count - 1);
1125
+ if (uuidOffset !== undefined) {
1126
+ let targetFinalId = (closestBaseFinalId + uuidOffset) as FinalCompressedId;
1127
+ const override = closestCluster.overrides?.get(targetFinalId);
1128
+ if (typeof override === 'object') {
1129
+ if (override.associatedLocalId !== undefined) {
1130
+ return override.associatedLocalId;
1131
+ }
1132
+ // This may be a UUID that should actually compress into a different final ID that it aligns with, due to
1133
+ // another session having an identical override (see `IdCluster` for more).
1134
+ targetFinalId = override.originalOverridingFinal;
1135
+ }
1136
+ return this.normalizeToSessionSpace(targetFinalId);
1137
+ }
1138
+ }
1139
+ }
1140
+
1141
+ if (isStable) {
1142
+ // May have already computed the numeric UUID, so avoid recomputing if possible
1143
+ // TODO: This cast can be removed on typescript 4.6
1144
+ const localId = this.getLocalIdForStableId(numericUuid ?? inversionKey);
1145
+ if (localId !== undefined) {
1146
+ return localId;
1147
+ }
1148
+ }
1149
+ return undefined;
1150
+ }
1151
+
1152
+ /**
1153
+ * Normalizes a session space ID into op space.
1154
+ * @param id the local ID to normalize.
1155
+ * @returns the ID in op space.
1156
+ */
1157
+ public normalizeToOpSpace(id: SessionSpaceCompressedId): OpSpaceCompressedId {
1158
+ if (isFinalId(id)) {
1159
+ return id;
1160
+ }
1161
+ // Check if this local ID has not been allocated yet
1162
+ if (-id > this.localIdCount) {
1163
+ fail('Supplied local ID was not created by this compressor.');
1164
+ }
1165
+ // Check if this local ID has not been finalized yet
1166
+ const { lastFinalizedLocalId } = this.localSession;
1167
+ if (lastFinalizedLocalId === undefined || id < lastFinalizedLocalId) {
1168
+ const override = this.localOverrides.get(id);
1169
+ if (override !== undefined) {
1170
+ const inversionKey = IdCompressor.createInversionKey(override);
1171
+ const compressionMapping =
1172
+ this.clustersAndOverridesInversion.get(inversionKey) ?? fail('Bimap is malformed.');
1173
+ if (
1174
+ !IdCompressor.isClusterInfo(compressionMapping) &&
1175
+ !IdCompressor.isUnfinalizedOverride(compressionMapping) &&
1176
+ compressionMapping.associatedLocalId === id
1177
+ ) {
1178
+ return compressionMapping.originalOverridingFinal;
1179
+ }
1180
+ }
1181
+ return id as OpSpaceCompressedId;
1182
+ }
1183
+ const [localBase, [finalBase, cluster]] =
1184
+ this.localIdToCluster.getPairOrNextLower(id) ??
1185
+ fail('Locally created cluster should be added to the map when allocated');
1186
+ const correspondingFinal = (finalBase + (localBase - id)) as FinalCompressedId;
1187
+ if (cluster.overrides) {
1188
+ const override = cluster.overrides.get(correspondingFinal);
1189
+ if (typeof override === 'object' && override.originalOverridingFinal !== undefined) {
1190
+ // Rare case of two local IDs with same overrides are created concurrently. See `IdCluster` for more.
1191
+ return override.originalOverridingFinal;
1192
+ }
1193
+ }
1194
+ return correspondingFinal;
1195
+ }
1196
+
1197
+ /**
1198
+ * Normalizes an ID into session space.
1199
+ * @param id the ID to normalize. If it is a local ID, it is assumed to have been created by the session corresponding
1200
+ * to `sessionId`.
1201
+ * @param originSessionId the session from which `id` originated
1202
+ * @returns the session-space ID corresponding to `id`, which might not have been a final ID if the client that created it had not yet
1203
+ * finalized it. This can occur when a client references an ID during the window of time in which it is waiting to receive the ordered
1204
+ * range that contained it from the server.
1205
+ */
1206
+ public normalizeToSessionSpace(id: OpSpaceCompressedId, originSessionId: SessionId): SessionSpaceCompressedId;
1207
+
1208
+ /**
1209
+ * Normalizes a final ID into session space.
1210
+ * @param id the final ID to normalize.
1211
+ * @returns the session-space ID corresponding to `id`.
1212
+ */
1213
+ public normalizeToSessionSpace(id: FinalCompressedId): SessionSpaceCompressedId;
1214
+
1215
+ public normalizeToSessionSpace(id: OpSpaceCompressedId, originSessionId?: SessionId): SessionSpaceCompressedId {
1216
+ const isLocalSession = originSessionId === this.localSessionId;
1217
+ if (isLocalId(id)) {
1218
+ if (isLocalSession) {
1219
+ const localIndex = -id;
1220
+ if (localIndex > this.localIdCount) {
1221
+ fail('Supplied local ID was not created by this compressor.');
1222
+ }
1223
+ return id;
1224
+ } else {
1225
+ const session = this.tryGetSession(originSessionId ?? fail());
1226
+ if (session === undefined) {
1227
+ fail('No IDs have ever been finalized by the supplied session.');
1228
+ }
1229
+ const localCount = -id;
1230
+ const numericUuid = incrementUuid(session.sessionUuid, localCount - 1);
1231
+ return this.compressNumericUuid(numericUuid) ?? fail('ID is not known to this compressor.');
1232
+ }
1233
+ }
1234
+
1235
+ const closestResult = this.localIdToCluster.getPairOrNextLowerByValue(id);
1236
+ if (closestResult !== undefined) {
1237
+ const [localBase, [finalBase, cluster]] = closestResult;
1238
+ const indexInCluster = id - finalBase;
1239
+ if (indexInCluster < cluster.count) {
1240
+ return (localBase - indexInCluster) as LocalCompressedId;
1241
+ }
1242
+ }
1243
+
1244
+ // Check for a unified override finalized first by another session but to which the local session
1245
+ // still has an associated local ID.
1246
+ const [_, cluster] =
1247
+ this.getClusterForFinalId(id) ?? fail('Supplied final ID was not finalized by this compressor.');
1248
+ const override = cluster.overrides?.get(id);
1249
+ if (typeof override === 'object' && override.associatedLocalId !== undefined) {
1250
+ return override.associatedLocalId;
1251
+ }
1252
+ return id as SessionSpaceCompressedId;
1253
+ }
1254
+
1255
+ /**
1256
+ * Returns the session-space compressed ID corresponding to the numeric UUID, or undefined if it is not known to this compressor.
1257
+ * Typically, it will return the session-space ID sequentially aligned with it (which will be local if `numericUuid` was made by
1258
+ * the local session, or final otherwise). However, in the event that the aligned session-space ID was overridden with a UUID
1259
+ * *and* that override UUID was concurrently used in an older ID (earlier, w.r.t. sequencing), this method can return the first
1260
+ * ID to correspond to that override.
1261
+ *
1262
+ * As an example, consider the following two clients:
1263
+ * ClientA, session UUID: A0000000-0000-0000-0000-000000000000
1264
+ * ClientB, session UUID: B0000000-0000-0000-0000-000000000000
1265
+ *
1266
+ * If concurrently, two clients performed:
1267
+ * ClientA: generateCompressedId(override: 'X0000000-0000-0000-0000-000000000000') // aligned with A0000000-0000-0000-0000-000000000000
1268
+ *
1269
+ * ClientB: generateCompressedId() // aligned with B0000000-0000-0000-0000-000000000000
1270
+ * ClientB: generateCompressedId(override: 'X0000000-0000-0000-0000-000000000000') // aligned with B0000000-0000-0000-0000-000000000001
1271
+ *
1272
+ * After sequencing, calling this method and passing the numeric UUID for B0000000-0000-0000-0000-000000000001 would return the
1273
+ * session-space ID corresponding to A0000000-0000-0000-0000-000000000000 (with override X0000000-0000-0000-0000-000000000000).
1274
+ */
1275
+ private compressNumericUuid(numericUuid: NumericUuid): SessionSpaceCompressedId | undefined {
1276
+ const stableId = stableIdFromNumericUuid(numericUuid);
1277
+ const sessionSpaceId = this.recompressInternal(stableId, numericUuid);
1278
+ if (sessionSpaceId === undefined) {
1279
+ return undefined;
1280
+ }
1281
+ return sessionSpaceId;
1282
+ }
1283
+
1284
+ private getLocalIdForStableId(stableId: StableId | NumericUuid): LocalCompressedId | undefined {
1285
+ // TODO: This cast can be removed on typescript 4.6
1286
+ const numericUuid = (
1287
+ typeof stableId === 'string' ? numericUuidFromStableId(stableId) : stableId
1288
+ ) as NumericUuid;
1289
+ const offset = getPositiveDelta(numericUuid, this.localSession.sessionUuid, this.localIdCount - 1);
1290
+ if (offset === undefined) {
1291
+ return undefined;
1292
+ }
1293
+
1294
+ return (-offset - 1) as LocalCompressedId;
1295
+ }
1296
+
1297
+ private getClusterForFinalId(
1298
+ finalId: FinalCompressedId
1299
+ ): readonly [baseFinalId: FinalCompressedId, cluster: IdCluster] | undefined {
1300
+ const possibleCluster = this.finalIdToCluster.getPairOrNextLower(finalId);
1301
+ if (possibleCluster === undefined) {
1302
+ return undefined;
1303
+ }
1304
+ const [clusterBase, cluster] = possibleCluster;
1305
+ if (finalId - clusterBase >= cluster.count) {
1306
+ return undefined;
1307
+ }
1308
+ return possibleCluster;
1309
+ }
1310
+
1311
+ /**
1312
+ * @returns if `other` is equal to this `IdCompressor`. The equality check includes local session state.
1313
+ * @testOnly
1314
+ */
1315
+ public equals(other: IdCompressor, compareLocalState: boolean): boolean {
1316
+ if (compareLocalState) {
1317
+ if (
1318
+ this.localIdCount !== other.localIdCount ||
1319
+ this.localSessionId !== other.localSessionId ||
1320
+ this.lastTakenLocalId !== other.lastTakenLocalId ||
1321
+ this.sentAttributionInfo !== other.sentAttributionInfo
1322
+ ) {
1323
+ return false;
1324
+ }
1325
+ if (!this.localOverrides.equals(other.localOverrides, (a, b) => a === b)) {
1326
+ return false;
1327
+ }
1328
+ if (
1329
+ !compareMaps(this.sessions, other.sessions, (a, b) =>
1330
+ IdCompressor.sessionDataEqual(a, b, true, compareLocalState)
1331
+ )
1332
+ ) {
1333
+ return false;
1334
+ }
1335
+ } else {
1336
+ for (const [keyA, valueA] of this.sessions) {
1337
+ const valueB = other.sessions.get(keyA);
1338
+ if (valueB === undefined) {
1339
+ if (valueA.lastFinalizedLocalId !== undefined) {
1340
+ return false;
1341
+ }
1342
+ } else if (!IdCompressor.sessionDataEqual(valueA, valueB, true, compareLocalState)) {
1343
+ return false;
1344
+ }
1345
+ }
1346
+
1347
+ for (const [keyB, valueB] of other.sessions) {
1348
+ const valueA = this.sessions.get(keyB);
1349
+ if (valueA === undefined) {
1350
+ if (valueB.lastFinalizedLocalId !== undefined) {
1351
+ return false;
1352
+ }
1353
+ }
1354
+ }
1355
+ }
1356
+ if (
1357
+ this.nextClusterBaseFinalId !== other.nextClusterBaseFinalId ||
1358
+ this.newClusterCapacity !== other.newClusterCapacity
1359
+ ) {
1360
+ return false;
1361
+ }
1362
+ if (
1363
+ !this.finalIdToCluster.equals(other.finalIdToCluster, (a, b) =>
1364
+ IdCompressor.idClustersEqual(a, b, true, compareLocalState)
1365
+ )
1366
+ ) {
1367
+ return false;
1368
+ }
1369
+
1370
+ const missingInOne = (_: string, value: CompressionMapping): { break: boolean } | undefined => {
1371
+ if (!compareLocalState && IdCompressor.isUnfinalizedOverride(value)) {
1372
+ return undefined;
1373
+ }
1374
+ return { break: true };
1375
+ };
1376
+
1377
+ const compareCompressionMappings = (a, b) => {
1378
+ const unfinalizedA = IdCompressor.isUnfinalizedOverride(a);
1379
+ const unfinalizedB = IdCompressor.isUnfinalizedOverride(b);
1380
+ if (unfinalizedA) {
1381
+ if (unfinalizedB) {
1382
+ return a === b;
1383
+ }
1384
+ return false;
1385
+ } else if (unfinalizedB) {
1386
+ return false;
1387
+ }
1388
+
1389
+ if (IdCompressor.isClusterInfo(a)) {
1390
+ if (!IdCompressor.isClusterInfo(b) || a.clusterBase !== b.clusterBase) {
1391
+ return false;
1392
+ }
1393
+ } else {
1394
+ if (
1395
+ IdCompressor.isClusterInfo(b) ||
1396
+ (compareLocalState && a.associatedLocalId !== b.associatedLocalId) ||
1397
+ a.originalOverridingFinal !== b.originalOverridingFinal
1398
+ ) {
1399
+ return false;
1400
+ }
1401
+ }
1402
+ if (!IdCompressor.idClustersEqual(a.cluster, b.cluster, true, compareLocalState)) {
1403
+ return false;
1404
+ }
1405
+ return true;
1406
+ };
1407
+
1408
+ const diff = this.clustersAndOverridesInversion.diffAgainst(
1409
+ other.clustersAndOverridesInversion,
1410
+ missingInOne,
1411
+ missingInOne,
1412
+ (_, valA, valB) => {
1413
+ if (!compareCompressionMappings(valA, valB)) {
1414
+ return { break: true };
1415
+ }
1416
+ return undefined;
1417
+ }
1418
+ );
1419
+
1420
+ return diff === undefined;
1421
+ }
1422
+
1423
+ private static sessionDataEqual(a: Session, b: Session, checkCluster = true, compareLocalState = true): boolean {
1424
+ if (
1425
+ a.attributionInfo !== b.attributionInfo ||
1426
+ !numericUuidEquals(a.sessionUuid, b.sessionUuid) ||
1427
+ a.lastFinalizedLocalId !== b.lastFinalizedLocalId
1428
+ ) {
1429
+ return false;
1430
+ }
1431
+ if (a.currentClusterDetails === undefined || b.currentClusterDetails === undefined) {
1432
+ if (a.currentClusterDetails !== b.currentClusterDetails) {
1433
+ return false;
1434
+ }
1435
+ return true;
1436
+ }
1437
+ if (
1438
+ checkCluster &&
1439
+ !IdCompressor.idClustersEqual(
1440
+ a.currentClusterDetails.cluster,
1441
+ b.currentClusterDetails.cluster,
1442
+ false,
1443
+ compareLocalState
1444
+ )
1445
+ ) {
1446
+ return false;
1447
+ }
1448
+ return true;
1449
+ }
1450
+
1451
+ private static idClustersEqual(
1452
+ a: IdCluster,
1453
+ b: IdCluster,
1454
+ checkSessionData = true,
1455
+ compareLocalState = true
1456
+ ): boolean {
1457
+ const areEqual =
1458
+ numericUuidEquals(a.baseUuid, b.baseUuid) &&
1459
+ a.capacity === b.capacity &&
1460
+ a.count === b.count &&
1461
+ (!checkSessionData || IdCompressor.sessionDataEqual(a.session, b.session, false, compareLocalState)) &&
1462
+ (a.overrides === undefined) === (b.overrides === undefined) &&
1463
+ (a.overrides === undefined ||
1464
+ compareMaps(assertNotUndefined(a.overrides), assertNotUndefined(b.overrides), (a, b) => {
1465
+ if (compareLocalState) {
1466
+ if (typeof a === 'string' || typeof b === 'string') {
1467
+ return a === b;
1468
+ }
1469
+ const overridesEqual =
1470
+ a.override === b.override &&
1471
+ a.originalOverridingFinal === b.originalOverridingFinal &&
1472
+ (!compareLocalState || a.associatedLocalId === b.associatedLocalId);
1473
+ return overridesEqual;
1474
+ }
1475
+
1476
+ const uuidA = typeof a === 'string' ? a : a.override;
1477
+ const uuidB = typeof b === 'string' ? b : b.override;
1478
+ if (
1479
+ typeof a !== 'string' &&
1480
+ typeof b !== 'string' &&
1481
+ a.originalOverridingFinal !== b.originalOverridingFinal
1482
+ ) {
1483
+ return false;
1484
+ }
1485
+ return uuidA === uuidB;
1486
+ }));
1487
+ return areEqual;
1488
+ }
1489
+
1490
+ /**
1491
+ * Returns a persistable form of the current state of this `IdCompressor` which can be rehydrated via `IdCompressor.deserialize()`.
1492
+ * This includes finalized state as well as un-finalized state and is therefore suitable for use in offline scenarios.
1493
+ */
1494
+ public serialize(
1495
+ withSession: boolean
1496
+ ): SerializedIdCompressorWithOngoingSession | SerializedIdCompressorWithNoSession;
1497
+
1498
+ /**
1499
+ * Returns a persistable form of the current state of this `IdCompressor` which can be rehydrated via `IdCompressor.deserialize()`.
1500
+ * This includes finalized state as well as un-finalized state and is therefore suitable for use in offline scenarios.
1501
+ */
1502
+ public serialize(withSession: true): SerializedIdCompressorWithOngoingSession;
1503
+
1504
+ /**
1505
+ * Returns a persistable form of the current state of this `IdCompressor` which can be rehydrated via `IdCompressor.deserialize()`.
1506
+ * This only includes finalized state and is therefore suitable for use in summaries.
1507
+ */
1508
+ public serialize(withSession: false): SerializedIdCompressorWithNoSession;
1509
+
1510
+ public serialize(withSession: boolean): SerializedIdCompressor {
1511
+ const serializedSessions: SerializedSessionData[] = [];
1512
+ const sessionIdToSessionIndex = new Map<SessionId, number>();
1513
+
1514
+ for (const [sessionId, session] of this.sessions) {
1515
+ const isLocalSession = sessionId === this.localSessionId;
1516
+ const includeSession =
1517
+ sessionId !== reservedSessionId && // Ignore reserved clusters, but
1518
+ (session.lastFinalizedLocalId !== undefined || // always serialize sessions that made final IDs,
1519
+ (isLocalSession && withSession)); // include the un-acked local session if requested
1520
+
1521
+ if (includeSession) {
1522
+ const sessionData: Mutable<SerializedSessionData> = [sessionId];
1523
+ if (session.attributionInfo !== undefined) {
1524
+ sessionData.push(session.attributionInfo);
1525
+ }
1526
+ sessionIdToSessionIndex.set(sessionId, serializedSessions.length);
1527
+ serializedSessions.push(sessionData);
1528
+ }
1529
+ }
1530
+
1531
+ const serializedClusters: SerializedCluster[] = [];
1532
+ for (const [baseFinalId, cluster] of this.finalIdToCluster.entries()) {
1533
+ const sessionId = stableIdFromNumericUuid(cluster.session.sessionUuid) as SessionId;
1534
+ if (sessionId !== reservedSessionId) {
1535
+ const sessionIndex =
1536
+ sessionIdToSessionIndex.get(sessionId) ??
1537
+ fail('Session object contains wrong session numeric UUID');
1538
+
1539
+ const serializedCluster: Mutable<SerializedCluster> = [sessionIndex, cluster.capacity];
1540
+ if (cluster.count !== cluster.capacity) {
1541
+ serializedCluster.push(cluster.count);
1542
+ }
1543
+
1544
+ if (cluster.overrides !== undefined) {
1545
+ const serializedOverrides: Mutable<SerializedClusterOverrides> = [];
1546
+ for (const [finalId, override] of cluster.overrides) {
1547
+ const finalIdIndex = finalId - baseFinalId;
1548
+ if (typeof override === 'string') {
1549
+ serializedOverrides.push([finalIdIndex, override]);
1550
+ } else if (override.originalOverridingFinal === finalId) {
1551
+ serializedOverrides.push([finalIdIndex, override.override]);
1552
+ } else {
1553
+ serializedOverrides.push([
1554
+ finalIdIndex,
1555
+ override.override,
1556
+ override.originalOverridingFinal,
1557
+ ]);
1558
+ }
1559
+ }
1560
+ serializedCluster.push(serializedOverrides);
1561
+ }
1562
+
1563
+ serializedClusters.push(serializedCluster);
1564
+ }
1565
+ }
1566
+
1567
+ // Reserved session not serialized, and local session is present but may not make IDs
1568
+ assert(serializedSessions.length - this.sessions.size <= 2, 'session not serialized');
1569
+
1570
+ const serializedIdCompressor = {
1571
+ version: currentWrittenVersion,
1572
+ reservedIdCount: this.reservedIdCount,
1573
+ clusterCapacity: this.clusterCapacity,
1574
+ sessions: serializedSessions,
1575
+ clusters: serializedClusters,
1576
+ } as unknown as SerializedIdCompressor;
1577
+
1578
+ if (withSession) {
1579
+ const serializedWithSession = serializedIdCompressor as Mutable<SerializedIdCompressorWithOngoingSession>;
1580
+ serializedWithSession.localSessionIndex = serializedWithSession.sessions.findIndex(
1581
+ ([sessionId]) => sessionId === this.localSessionId
1582
+ );
1583
+ if (this.localIdCount > 0) {
1584
+ serializedWithSession.localState = {
1585
+ localIdCount: this.localIdCount,
1586
+ overrides: [...this.localOverrides.entries()].map((entry) => [...entry]),
1587
+ lastTakenLocalId: this.lastTakenLocalId,
1588
+ sentAttributionInfo: this.sentAttributionInfo,
1589
+ };
1590
+ }
1591
+
1592
+ return serializedWithSession;
1593
+ }
1594
+
1595
+ return serializedIdCompressor;
1596
+ }
1597
+
1598
+ /**
1599
+ * Deserialize an serialized IdCompressor that is part of an ongoing session, thereby resuming that session.
1600
+ */
1601
+ public static deserialize(serialized: SerializedIdCompressorWithOngoingSession): IdCompressor;
1602
+
1603
+ /**
1604
+ * Deserialize an serialized IdCompressor with a new session. The provided serialized compressor
1605
+ * must have an ongoing session.
1606
+ * @param serialized the serialized compressor state
1607
+ * @param newSessionId the session ID for the new compressor.
1608
+ * @param attributionInfo information used by other clients to attribute IDs made by this client
1609
+ */
1610
+ public static deserialize(
1611
+ serialized: SerializedIdCompressorWithNoSession,
1612
+ newSessionId: SessionId,
1613
+ attributionInfo?: AttributionInfo
1614
+ ): IdCompressor;
1615
+
1616
+ public static deserialize(
1617
+ serialized: SerializedIdCompressorWithNoSession | SerializedIdCompressorWithOngoingSession,
1618
+ newSessionIdMaybe?: SessionId,
1619
+ attributionInfoMaybe?: AttributionInfo
1620
+ ): IdCompressor {
1621
+ const hasSession = hasOngoingSession(serialized);
1622
+ let localSessionId: SessionId;
1623
+ let attributionInfo: AttributionInfo | undefined;
1624
+ let serializedLocalState: SerializedLocalState | undefined;
1625
+ if (hasSession) {
1626
+ assert(newSessionIdMaybe === undefined && attributionInfoMaybe === undefined);
1627
+ // TODO: This cast can be removed on typescript 4.6
1628
+ [localSessionId, attributionInfo] =
1629
+ serialized.sessions[(serialized as SerializedIdCompressorWithOngoingSession).localSessionIndex];
1630
+ // TODO: This cast can be removed on typescript 4.6
1631
+ serializedLocalState = (serialized as SerializedIdCompressorWithOngoingSession).localState;
1632
+ } else {
1633
+ assert(newSessionIdMaybe !== undefined);
1634
+ localSessionId = newSessionIdMaybe;
1635
+ attributionInfo = attributionInfoMaybe;
1636
+ }
1637
+
1638
+ const {
1639
+ clusterCapacity,
1640
+ reservedIdCount,
1641
+ sessions: serializedSessions,
1642
+ clusters: serializedClusters,
1643
+ } = serialized;
1644
+
1645
+ const compressor = new IdCompressor(localSessionId, reservedIdCount, attributionInfo);
1646
+ compressor.clusterCapacity = clusterCapacity;
1647
+
1648
+ const localOverridesInverse = new Map<string, LocalCompressedId>();
1649
+ if (serializedLocalState !== undefined) {
1650
+ // Do this part of local rehydration first since the cluster map population needs to query to local overrides
1651
+ compressor.localIdCount = serializedLocalState.localIdCount;
1652
+ compressor.lastTakenLocalId = serializedLocalState.lastTakenLocalId;
1653
+ compressor.sentAttributionInfo = serializedLocalState.sentAttributionInfo;
1654
+ if (serializedLocalState.overrides !== undefined) {
1655
+ for (const [localId, override] of serializedLocalState.overrides) {
1656
+ compressor.localOverrides.append(localId, override);
1657
+ localOverridesInverse.set(override, localId);
1658
+ compressor.clustersAndOverridesInversion.set(
1659
+ IdCompressor.createInversionKey(override),
1660
+ localId as UnackedLocalId
1661
+ );
1662
+ }
1663
+ }
1664
+ }
1665
+
1666
+ const sessionInfos: {
1667
+ readonly session: Session;
1668
+ readonly sessionId: SessionId;
1669
+ }[] = [];
1670
+ for (const serializedSession of serializedSessions) {
1671
+ const [sessionId, attributionInfo] = serializedSession;
1672
+ if (sessionId === localSessionId) {
1673
+ assert(hasSession, 'Cannot resume existing session.');
1674
+ sessionInfos.push({ session: compressor.localSession, sessionId });
1675
+ } else {
1676
+ const session = compressor.createSession(sessionId, attributionInfo);
1677
+ sessionInfos.push({ session, sessionId });
1678
+ }
1679
+ }
1680
+
1681
+ for (const serializedCluster of serializedClusters) {
1682
+ const { sessionIndex, capacity, count, overrides } = deserializeCluster(serializedCluster);
1683
+ const { session, sessionId } = sessionInfos[sessionIndex];
1684
+ const { lastFinalizedLocalId, sessionUuid } = session;
1685
+ const currentIdCount = lastFinalizedLocalId === undefined ? 0 : -lastFinalizedLocalId;
1686
+
1687
+ const cluster: Mutable<IdCluster> = {
1688
+ capacity,
1689
+ count,
1690
+ baseUuid: incrementUuid(sessionUuid, currentIdCount),
1691
+ session,
1692
+ };
1693
+
1694
+ const lastFinalizedNormalized = lastFinalizedLocalId ?? 0;
1695
+ const clusterBase = compressor.nextClusterBaseFinalId;
1696
+ if (serializedLocalState !== undefined && sessionId === compressor.localSessionId) {
1697
+ compressor.localIdToCluster.append((lastFinalizedNormalized - 1) as LocalCompressedId, [
1698
+ clusterBase,
1699
+ cluster,
1700
+ ]);
1701
+ }
1702
+
1703
+ session.lastFinalizedLocalId = (lastFinalizedNormalized - count) as LocalCompressedId;
1704
+ session.currentClusterDetails = { clusterBase, cluster };
1705
+ compressor.nextClusterBaseFinalId = (compressor.nextClusterBaseFinalId + capacity) as FinalCompressedId;
1706
+ compressor.finalIdToCluster.append(clusterBase, cluster);
1707
+ compressor.clustersAndOverridesInversion.set(stableIdFromNumericUuid(cluster.baseUuid), {
1708
+ clusterBase,
1709
+ cluster,
1710
+ });
1711
+
1712
+ if (overrides !== undefined) {
1713
+ cluster.overrides = new Map();
1714
+ for (const [finalIdIndex, override, originalOverridingFinal] of overrides) {
1715
+ const finalId = (clusterBase + finalIdIndex) as FinalCompressedId;
1716
+ if (originalOverridingFinal !== undefined) {
1717
+ const unifiedOverride: Mutable<UnifiedOverride> = {
1718
+ override,
1719
+ originalOverridingFinal,
1720
+ };
1721
+ if (serializedLocalState !== undefined) {
1722
+ setPropertyIfDefined(
1723
+ localOverridesInverse.get(override),
1724
+ unifiedOverride,
1725
+ 'associatedLocalId'
1726
+ );
1727
+ }
1728
+ cluster.overrides.set(finalId, unifiedOverride);
1729
+ } else {
1730
+ const associatedLocal = localOverridesInverse.get(override);
1731
+ if (associatedLocal !== undefined && sessionId !== localSessionId) {
1732
+ // In this case, there is a local ID associated with this override, but this is the first cluster to contain
1733
+ // that override (because only the first cluster will have the string serialized). In this case, the override
1734
+ // needs to hold that local value.
1735
+ cluster.overrides.set(finalId, {
1736
+ override,
1737
+ originalOverridingFinal: finalId,
1738
+ associatedLocalId: associatedLocal,
1739
+ });
1740
+ } else {
1741
+ cluster.overrides.set(finalId, override);
1742
+ }
1743
+ const finalizedOverride: Mutable<FinalizedOverride> = {
1744
+ cluster,
1745
+ originalOverridingFinal: finalId,
1746
+ };
1747
+ if (serializedLocalState !== undefined) {
1748
+ setPropertyIfDefined(associatedLocal, finalizedOverride, 'associatedLocalId');
1749
+ }
1750
+ compressor.clustersAndOverridesInversion.set(
1751
+ IdCompressor.createInversionKey(override),
1752
+ finalizedOverride
1753
+ );
1754
+ }
1755
+ }
1756
+ }
1757
+ }
1758
+
1759
+ assert(
1760
+ compressor.localSession.lastFinalizedLocalId === undefined ||
1761
+ compressor.localIdCount >= -compressor.localSession.lastFinalizedLocalId
1762
+ );
1763
+
1764
+ return compressor;
1765
+ }
1766
+
1767
+ /**
1768
+ * Converts the given serialized compressor to the current version.
1769
+ * @param serializedCompressor the serialized compressor to convert. Must not have been serialized with an ongoing session.
1770
+ * @returns a serialized compressor with no ongoing session.
1771
+ */
1772
+ public static convertToCurrentVersion(
1773
+ serializedCompressor: VersionedSerializedIdCompressor,
1774
+ hasSession: false
1775
+ ): SerializedIdCompressorWithNoSession;
1776
+
1777
+ /**
1778
+ * Converts the given serialized compressor to the current version.
1779
+ * @param serializedCompressor the serialized compressor to convert. Must have been serialized with an ongoing session.
1780
+ * @returns a serialized compressor with the same ongoing session.
1781
+ */
1782
+ public static convertToCurrentVersion(
1783
+ serializedCompressor: VersionedSerializedIdCompressor,
1784
+ hasSession: true
1785
+ ): SerializedIdCompressorWithOngoingSession;
1786
+
1787
+ public static convertToCurrentVersion(
1788
+ serializedCompressor: VersionedSerializedIdCompressor,
1789
+ hasSession: boolean
1790
+ ): SerializedIdCompressor | undefined {
1791
+ if (serializedCompressor.version !== currentWrittenVersion) {
1792
+ fail('Unknown SerializedIdCompressor version number');
1793
+ }
1794
+ const serialized = serializedCompressor as SerializedIdCompressorWithOngoingSession;
1795
+ if (hasSession !== hasOngoingSession(serialized)) {
1796
+ return undefined;
1797
+ }
1798
+ return serialized;
1799
+ }
1800
+ }
1801
+
1802
+ /**
1803
+ * The version of `IdCompressor` that is currently persisted.
1804
+ */
1805
+ const currentWrittenVersion = '0.0.1';
1806
+
1807
+ /**
1808
+ * @returns whether or not the given serialized ID compressor has an ongoing session.
1809
+ */
1810
+ export function hasOngoingSession(
1811
+ serialized: SerializedIdCompressorWithNoSession | SerializedIdCompressorWithOngoingSession
1812
+ ): serialized is SerializedIdCompressorWithOngoingSession {
1813
+ return (serialized as Partial<SerializedIdCompressorWithOngoingSession>).localSessionIndex !== undefined;
1814
+ }
1815
+
1816
+ function deserializeCluster(serializedCluster: SerializedCluster): {
1817
+ sessionIndex: number;
1818
+ capacity: number;
1819
+ count: number;
1820
+ overrides?: SerializedClusterOverrides;
1821
+ } {
1822
+ const [sessionIndex, capacity, countOrOverrides, overrides] = serializedCluster;
1823
+ const hasCount = typeof countOrOverrides === 'number';
1824
+
1825
+ return {
1826
+ sessionIndex,
1827
+ capacity,
1828
+ // TODO: This cast can be removed on typescript 4.6
1829
+ count: (hasCount ? countOrOverrides : capacity) as number,
1830
+ // TODO: This cast can be removed on typescript 4.6
1831
+ overrides: (hasCount ? overrides : countOrOverrides) as SerializedClusterOverrides,
1832
+ };
1833
+ }
1834
+
1835
+ /**
1836
+ * Optimization used by the sorted-btree library to avoid allocating tuples every time a lookup method is called.
1837
+ * Lookup methods on BTree accept a pre-allocated array that it populates with the result of the lookup and retains no ownership
1838
+ * of after the call, so this array may be supplied to any of them. References to this array should not be retained elsewhere and
1839
+ * lookup results should be extracted from the tuple immediately after invocation.
1840
+ */
1841
+ const reusedArray: [any, any] = [] as unknown as [any, any];
1842
+
1843
+ /**
1844
+ * A numeric comparator used for sorting in descending order.
1845
+ */
1846
+ function compareFiniteNumbersReversed<T extends number>(a: T, b: T): number {
1847
+ return b - a;
1848
+ }