@fluidframework/tree 2.61.0 → 2.62.0-356644

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 (237) hide show
  1. package/api-report/tree.alpha.api.md +3 -0
  2. package/dist/core/index.d.ts +1 -1
  3. package/dist/core/index.d.ts.map +1 -1
  4. package/dist/core/index.js +2 -1
  5. package/dist/core/index.js.map +1 -1
  6. package/dist/core/rebase/index.d.ts +1 -1
  7. package/dist/core/rebase/index.d.ts.map +1 -1
  8. package/dist/core/rebase/index.js +2 -1
  9. package/dist/core/rebase/index.js.map +1 -1
  10. package/dist/core/rebase/utils.d.ts +10 -0
  11. package/dist/core/rebase/utils.d.ts.map +1 -1
  12. package/dist/core/rebase/utils.js +20 -1
  13. package/dist/core/rebase/utils.js.map +1 -1
  14. package/dist/packageVersion.d.ts +1 -1
  15. package/dist/packageVersion.d.ts.map +1 -1
  16. package/dist/packageVersion.js +1 -1
  17. package/dist/packageVersion.js.map +1 -1
  18. package/dist/shared-tree/schematizingTreeView.js +2 -2
  19. package/dist/shared-tree/schematizingTreeView.js.map +1 -1
  20. package/dist/shared-tree/sharedTree.d.ts +12 -2
  21. package/dist/shared-tree/sharedTree.d.ts.map +1 -1
  22. package/dist/shared-tree/sharedTree.js +71 -33
  23. package/dist/shared-tree/sharedTree.js.map +1 -1
  24. package/dist/shared-tree/treeCheckout.d.ts +13 -7
  25. package/dist/shared-tree/treeCheckout.d.ts.map +1 -1
  26. package/dist/shared-tree/treeCheckout.js +114 -84
  27. package/dist/shared-tree/treeCheckout.js.map +1 -1
  28. package/dist/shared-tree-core/branch.d.ts +3 -0
  29. package/dist/shared-tree-core/branch.d.ts.map +1 -1
  30. package/dist/shared-tree-core/branch.js.map +1 -1
  31. package/dist/shared-tree-core/branchIdCodec.d.ts +11 -0
  32. package/dist/shared-tree-core/branchIdCodec.d.ts.map +1 -0
  33. package/dist/shared-tree-core/branchIdCodec.js +18 -0
  34. package/dist/shared-tree-core/branchIdCodec.js.map +1 -0
  35. package/dist/shared-tree-core/editManager.d.ts +33 -63
  36. package/dist/shared-tree-core/editManager.d.ts.map +1 -1
  37. package/dist/shared-tree-core/editManager.js +437 -290
  38. package/dist/shared-tree-core/editManager.js.map +1 -1
  39. package/dist/shared-tree-core/editManagerCodecs.d.ts +1 -1
  40. package/dist/shared-tree-core/editManagerCodecs.d.ts.map +1 -1
  41. package/dist/shared-tree-core/editManagerCodecs.js +7 -96
  42. package/dist/shared-tree-core/editManagerCodecs.js.map +1 -1
  43. package/dist/shared-tree-core/editManagerCodecsCommons.d.ts +17 -0
  44. package/dist/shared-tree-core/editManagerCodecsCommons.d.ts.map +1 -0
  45. package/dist/shared-tree-core/editManagerCodecsCommons.js +139 -0
  46. package/dist/shared-tree-core/editManagerCodecsCommons.js.map +1 -0
  47. package/dist/shared-tree-core/editManagerCodecsV1toV4.d.ts +16 -0
  48. package/dist/shared-tree-core/editManagerCodecsV1toV4.d.ts.map +1 -0
  49. package/dist/shared-tree-core/editManagerCodecsV1toV4.js +39 -0
  50. package/dist/shared-tree-core/editManagerCodecsV1toV4.js.map +1 -0
  51. package/dist/shared-tree-core/editManagerCodecsV5.d.ts +16 -0
  52. package/dist/shared-tree-core/editManagerCodecsV5.d.ts.map +1 -0
  53. package/dist/shared-tree-core/editManagerCodecsV5.js +58 -0
  54. package/dist/shared-tree-core/editManagerCodecsV5.js.map +1 -0
  55. package/dist/shared-tree-core/{editManagerFormat.d.ts → editManagerFormatCommons.d.ts} +31 -7
  56. package/dist/shared-tree-core/editManagerFormatCommons.d.ts.map +1 -0
  57. package/dist/shared-tree-core/{editManagerFormat.js → editManagerFormatCommons.js} +13 -12
  58. package/dist/shared-tree-core/editManagerFormatCommons.js.map +1 -0
  59. package/dist/shared-tree-core/editManagerFormatV1toV4.d.ts +31 -0
  60. package/dist/shared-tree-core/editManagerFormatV1toV4.d.ts.map +1 -0
  61. package/dist/shared-tree-core/editManagerFormatV1toV4.js +24 -0
  62. package/dist/shared-tree-core/editManagerFormatV1toV4.js.map +1 -0
  63. package/dist/shared-tree-core/editManagerFormatV5.d.ts +62 -0
  64. package/dist/shared-tree-core/editManagerFormatV5.d.ts.map +1 -0
  65. package/dist/shared-tree-core/editManagerFormatV5.js +20 -0
  66. package/dist/shared-tree-core/editManagerFormatV5.js.map +1 -0
  67. package/dist/shared-tree-core/index.d.ts +3 -3
  68. package/dist/shared-tree-core/index.d.ts.map +1 -1
  69. package/dist/shared-tree-core/index.js.map +1 -1
  70. package/dist/shared-tree-core/messageCodecV1ToV4.d.ts +11 -0
  71. package/dist/shared-tree-core/messageCodecV1ToV4.d.ts.map +1 -0
  72. package/dist/shared-tree-core/messageCodecV1ToV4.js +59 -0
  73. package/dist/shared-tree-core/messageCodecV1ToV4.js.map +1 -0
  74. package/dist/shared-tree-core/messageCodecV5.d.ts +11 -0
  75. package/dist/shared-tree-core/messageCodecV5.d.ts.map +1 -0
  76. package/dist/shared-tree-core/messageCodecV5.js +78 -0
  77. package/dist/shared-tree-core/messageCodecV5.js.map +1 -0
  78. package/dist/shared-tree-core/messageCodecs.d.ts.map +1 -1
  79. package/dist/shared-tree-core/messageCodecs.js +16 -47
  80. package/dist/shared-tree-core/messageCodecs.js.map +1 -1
  81. package/dist/shared-tree-core/{messageFormat.d.ts → messageFormatV1ToV4.d.ts} +1 -1
  82. package/dist/shared-tree-core/messageFormatV1ToV4.d.ts.map +1 -0
  83. package/dist/shared-tree-core/{messageFormat.js → messageFormatV1ToV4.js} +1 -1
  84. package/dist/shared-tree-core/messageFormatV1ToV4.js.map +1 -0
  85. package/dist/shared-tree-core/messageFormatV5.d.ts +42 -0
  86. package/dist/shared-tree-core/messageFormatV5.d.ts.map +1 -0
  87. package/dist/shared-tree-core/messageFormatV5.js +20 -0
  88. package/dist/shared-tree-core/messageFormatV5.js.map +1 -0
  89. package/dist/shared-tree-core/messageTypes.d.ts +12 -2
  90. package/dist/shared-tree-core/messageTypes.d.ts.map +1 -1
  91. package/dist/shared-tree-core/messageTypes.js.map +1 -1
  92. package/dist/shared-tree-core/sequenceIdUtils.d.ts +1 -1
  93. package/dist/shared-tree-core/sequenceIdUtils.d.ts.map +1 -1
  94. package/dist/shared-tree-core/sequenceIdUtils.js.map +1 -1
  95. package/dist/shared-tree-core/sharedTreeCore.d.ts +18 -5
  96. package/dist/shared-tree-core/sharedTreeCore.d.ts.map +1 -1
  97. package/dist/shared-tree-core/sharedTreeCore.js +175 -56
  98. package/dist/shared-tree-core/sharedTreeCore.js.map +1 -1
  99. package/dist/simple-tree/api/tree.d.ts +10 -0
  100. package/dist/simple-tree/api/tree.d.ts.map +1 -1
  101. package/dist/simple-tree/api/tree.js.map +1 -1
  102. package/lib/core/index.d.ts +1 -1
  103. package/lib/core/index.d.ts.map +1 -1
  104. package/lib/core/index.js +1 -1
  105. package/lib/core/index.js.map +1 -1
  106. package/lib/core/rebase/index.d.ts +1 -1
  107. package/lib/core/rebase/index.d.ts.map +1 -1
  108. package/lib/core/rebase/index.js +1 -1
  109. package/lib/core/rebase/index.js.map +1 -1
  110. package/lib/core/rebase/utils.d.ts +10 -0
  111. package/lib/core/rebase/utils.d.ts.map +1 -1
  112. package/lib/core/rebase/utils.js +18 -0
  113. package/lib/core/rebase/utils.js.map +1 -1
  114. package/lib/packageVersion.d.ts +1 -1
  115. package/lib/packageVersion.d.ts.map +1 -1
  116. package/lib/packageVersion.js +1 -1
  117. package/lib/packageVersion.js.map +1 -1
  118. package/lib/shared-tree/schematizingTreeView.js +2 -2
  119. package/lib/shared-tree/schematizingTreeView.js.map +1 -1
  120. package/lib/shared-tree/sharedTree.d.ts +12 -2
  121. package/lib/shared-tree/sharedTree.d.ts.map +1 -1
  122. package/lib/shared-tree/sharedTree.js +72 -34
  123. package/lib/shared-tree/sharedTree.js.map +1 -1
  124. package/lib/shared-tree/treeCheckout.d.ts +13 -7
  125. package/lib/shared-tree/treeCheckout.d.ts.map +1 -1
  126. package/lib/shared-tree/treeCheckout.js +115 -85
  127. package/lib/shared-tree/treeCheckout.js.map +1 -1
  128. package/lib/shared-tree-core/branch.d.ts +3 -0
  129. package/lib/shared-tree-core/branch.d.ts.map +1 -1
  130. package/lib/shared-tree-core/branch.js.map +1 -1
  131. package/lib/shared-tree-core/branchIdCodec.d.ts +11 -0
  132. package/lib/shared-tree-core/branchIdCodec.d.ts.map +1 -0
  133. package/lib/shared-tree-core/branchIdCodec.js +13 -0
  134. package/lib/shared-tree-core/branchIdCodec.js.map +1 -0
  135. package/lib/shared-tree-core/editManager.d.ts +33 -63
  136. package/lib/shared-tree-core/editManager.d.ts.map +1 -1
  137. package/lib/shared-tree-core/editManager.js +437 -290
  138. package/lib/shared-tree-core/editManager.js.map +1 -1
  139. package/lib/shared-tree-core/editManagerCodecs.d.ts +1 -1
  140. package/lib/shared-tree-core/editManagerCodecs.d.ts.map +1 -1
  141. package/lib/shared-tree-core/editManagerCodecs.js +4 -93
  142. package/lib/shared-tree-core/editManagerCodecs.js.map +1 -1
  143. package/lib/shared-tree-core/editManagerCodecsCommons.d.ts +17 -0
  144. package/lib/shared-tree-core/editManagerCodecsCommons.d.ts.map +1 -0
  145. package/lib/shared-tree-core/editManagerCodecsCommons.js +134 -0
  146. package/lib/shared-tree-core/editManagerCodecsCommons.js.map +1 -0
  147. package/lib/shared-tree-core/editManagerCodecsV1toV4.d.ts +16 -0
  148. package/lib/shared-tree-core/editManagerCodecsV1toV4.d.ts.map +1 -0
  149. package/lib/shared-tree-core/editManagerCodecsV1toV4.js +35 -0
  150. package/lib/shared-tree-core/editManagerCodecsV1toV4.js.map +1 -0
  151. package/lib/shared-tree-core/editManagerCodecsV5.d.ts +16 -0
  152. package/lib/shared-tree-core/editManagerCodecsV5.d.ts.map +1 -0
  153. package/lib/shared-tree-core/editManagerCodecsV5.js +54 -0
  154. package/lib/shared-tree-core/editManagerCodecsV5.js.map +1 -0
  155. package/lib/shared-tree-core/{editManagerFormat.d.ts → editManagerFormatCommons.d.ts} +31 -7
  156. package/lib/shared-tree-core/editManagerFormatCommons.d.ts.map +1 -0
  157. package/lib/shared-tree-core/{editManagerFormat.js → editManagerFormatCommons.js} +10 -11
  158. package/lib/shared-tree-core/editManagerFormatCommons.js.map +1 -0
  159. package/lib/shared-tree-core/editManagerFormatV1toV4.d.ts +31 -0
  160. package/lib/shared-tree-core/editManagerFormatV1toV4.d.ts.map +1 -0
  161. package/lib/shared-tree-core/editManagerFormatV1toV4.js +20 -0
  162. package/lib/shared-tree-core/editManagerFormatV1toV4.js.map +1 -0
  163. package/lib/shared-tree-core/editManagerFormatV5.d.ts +62 -0
  164. package/lib/shared-tree-core/editManagerFormatV5.d.ts.map +1 -0
  165. package/lib/shared-tree-core/editManagerFormatV5.js +16 -0
  166. package/lib/shared-tree-core/editManagerFormatV5.js.map +1 -0
  167. package/lib/shared-tree-core/index.d.ts +3 -3
  168. package/lib/shared-tree-core/index.d.ts.map +1 -1
  169. package/lib/shared-tree-core/index.js.map +1 -1
  170. package/lib/shared-tree-core/messageCodecV1ToV4.d.ts +11 -0
  171. package/lib/shared-tree-core/messageCodecV1ToV4.d.ts.map +1 -0
  172. package/lib/shared-tree-core/messageCodecV1ToV4.js +55 -0
  173. package/lib/shared-tree-core/messageCodecV1ToV4.js.map +1 -0
  174. package/lib/shared-tree-core/messageCodecV5.d.ts +11 -0
  175. package/lib/shared-tree-core/messageCodecV5.d.ts.map +1 -0
  176. package/lib/shared-tree-core/messageCodecV5.js +74 -0
  177. package/lib/shared-tree-core/messageCodecV5.js.map +1 -0
  178. package/lib/shared-tree-core/messageCodecs.d.ts.map +1 -1
  179. package/lib/shared-tree-core/messageCodecs.js +17 -48
  180. package/lib/shared-tree-core/messageCodecs.js.map +1 -1
  181. package/lib/shared-tree-core/{messageFormat.d.ts → messageFormatV1ToV4.d.ts} +1 -1
  182. package/lib/shared-tree-core/messageFormatV1ToV4.d.ts.map +1 -0
  183. package/lib/shared-tree-core/{messageFormat.js → messageFormatV1ToV4.js} +1 -1
  184. package/lib/shared-tree-core/messageFormatV1ToV4.js.map +1 -0
  185. package/lib/shared-tree-core/messageFormatV5.d.ts +42 -0
  186. package/lib/shared-tree-core/messageFormatV5.d.ts.map +1 -0
  187. package/lib/shared-tree-core/messageFormatV5.js +16 -0
  188. package/lib/shared-tree-core/messageFormatV5.js.map +1 -0
  189. package/lib/shared-tree-core/messageTypes.d.ts +12 -2
  190. package/lib/shared-tree-core/messageTypes.d.ts.map +1 -1
  191. package/lib/shared-tree-core/messageTypes.js.map +1 -1
  192. package/lib/shared-tree-core/sequenceIdUtils.d.ts +1 -1
  193. package/lib/shared-tree-core/sequenceIdUtils.d.ts.map +1 -1
  194. package/lib/shared-tree-core/sequenceIdUtils.js.map +1 -1
  195. package/lib/shared-tree-core/sharedTreeCore.d.ts +18 -5
  196. package/lib/shared-tree-core/sharedTreeCore.d.ts.map +1 -1
  197. package/lib/shared-tree-core/sharedTreeCore.js +176 -57
  198. package/lib/shared-tree-core/sharedTreeCore.js.map +1 -1
  199. package/lib/simple-tree/api/tree.d.ts +10 -0
  200. package/lib/simple-tree/api/tree.d.ts.map +1 -1
  201. package/lib/simple-tree/api/tree.js.map +1 -1
  202. package/package.json +20 -20
  203. package/src/core/index.ts +1 -0
  204. package/src/core/rebase/index.ts +1 -0
  205. package/src/core/rebase/utils.ts +27 -0
  206. package/src/packageVersion.ts +1 -1
  207. package/src/shared-tree/schematizingTreeView.ts +2 -2
  208. package/src/shared-tree/sharedTree.ts +103 -46
  209. package/src/shared-tree/treeCheckout.ts +147 -98
  210. package/src/shared-tree-core/branch.ts +7 -0
  211. package/src/shared-tree-core/branchIdCodec.ts +28 -0
  212. package/src/shared-tree-core/editManager.ts +709 -437
  213. package/src/shared-tree-core/editManagerCodecs.ts +4 -164
  214. package/src/shared-tree-core/editManagerCodecsCommons.ts +236 -0
  215. package/src/shared-tree-core/editManagerCodecsV1toV4.ts +108 -0
  216. package/src/shared-tree-core/editManagerCodecsV5.ts +150 -0
  217. package/src/shared-tree-core/{editManagerFormat.ts → editManagerFormatCommons.ts} +17 -13
  218. package/src/shared-tree-core/editManagerFormatV1toV4.ts +42 -0
  219. package/src/shared-tree-core/editManagerFormatV5.ts +35 -0
  220. package/src/shared-tree-core/index.ts +3 -1
  221. package/src/shared-tree-core/messageCodecV1ToV4.ts +101 -0
  222. package/src/shared-tree-core/messageCodecV5.ts +128 -0
  223. package/src/shared-tree-core/messageCodecs.ts +16 -85
  224. package/src/shared-tree-core/messageFormatV5.ts +50 -0
  225. package/src/shared-tree-core/messageTypes.ts +15 -2
  226. package/src/shared-tree-core/sequenceIdUtils.ts +1 -1
  227. package/src/shared-tree-core/sharedTreeCore.ts +270 -84
  228. package/src/simple-tree/api/tree.ts +15 -0
  229. package/dist/shared-tree-core/editManagerFormat.d.ts.map +0 -1
  230. package/dist/shared-tree-core/editManagerFormat.js.map +0 -1
  231. package/dist/shared-tree-core/messageFormat.d.ts.map +0 -1
  232. package/dist/shared-tree-core/messageFormat.js.map +0 -1
  233. package/lib/shared-tree-core/editManagerFormat.d.ts.map +0 -1
  234. package/lib/shared-tree-core/editManagerFormat.js.map +0 -1
  235. package/lib/shared-tree-core/messageFormat.d.ts.map +0 -1
  236. package/lib/shared-tree-core/messageFormat.js.map +0 -1
  237. /package/src/shared-tree-core/{messageFormat.ts → messageFormatV1ToV4.ts} +0 -0
@@ -7,7 +7,7 @@ import { createEmitter } from "@fluid-internal/client-utils";
7
7
  import { BTree } from "@tylerbu/sorted-btree-es6";
8
8
  import { findAncestor, findCommonAncestor, mintCommit, tagChange, } from "../core/index.js";
9
9
  import { brand, getOrCreate, mapIterable } from "../util/index.js";
10
- import { SharedTreeBranch, onForkTransitive } from "./branch.js";
10
+ import { SharedTreeBranch, onForkTransitive, } from "./branch.js";
11
11
  import { getUpperBoundOfPreviousSequenceId, equalSequenceIds, maxSequenceId, minSequenceId, sequenceIdComparator, } from "./sequenceIdUtils.js";
12
12
  import { TelemetryEventBatcher, } from "@fluidframework/telemetry-utils/internal";
13
13
  export const minimumPossibleSequenceNumber = brand(Number.MIN_SAFE_INTEGER);
@@ -40,25 +40,11 @@ export class EditManager {
40
40
  this.localSessionId = localSessionId;
41
41
  this.mintRevisionTag = mintRevisionTag;
42
42
  this._events = createEmitter();
43
+ this.sharedBranches = new Map();
43
44
  /**
44
- * Records extra data associated with commits in the {@link trunk}.
45
- * This does not include an entry for the {@link trunkBase}.
46
- */
47
- this.trunkMetadata = new Map();
48
- /**
49
- * A map from a sequence id to the commit in the {@link trunk} which has that sequence id.
50
- * This also includes an entry for the {@link trunkBase} which always has the lowest key in the map.
51
- */
52
- this.sequenceMap = new BTree(undefined, sequenceIdComparator);
53
- /**
54
- * Branches are maintained to represent the local change list that the issuing client had
55
- * at the time of submitting the latest known edit on the branch.
56
- * This means the head commit of each branch is always in its original (non-rebased) form.
57
- */
58
- this.peerLocalBranches = new Map();
59
- /**
60
- * Tracks where on the trunk all registered branches are based. Each key is the sequence id of a commit on
61
- * the trunk, and the value is the set of all branches who have that commit as their common ancestor with the trunk.
45
+ * Tracks where on the trunk of the main branch all registered branches are based.
46
+ * Each key is the sequence id of a commit on the trunk,
47
+ * and the value is the set of all branches who have that commit as their common ancestor with the trunk.
62
48
  *
63
49
  * @remarks
64
50
  * This does not include the local branch.
@@ -71,39 +57,28 @@ export class EditManager {
71
57
  * @remarks If there are more than one commit with the same sequence number we assume this refers to the last commit in the batch.
72
58
  */
73
59
  this.minimumSequenceNumber = minimumPossibleSequenceNumber;
74
- /**
75
- * The list of commits (from oldest to most recent) that are on the local branch but not on the trunk.
76
- * When a local commit is sequenced, the first commit in this list shifted onto the tip of the trunk.
77
- */
78
- this.localCommits = [];
79
60
  this.trunkBase = {
80
61
  revision: rootRevision,
81
62
  change: changeFamily.rebaser.compose([]),
82
63
  };
83
- this.sequenceMap.set(minimumPossibleSequenceId, this.trunkBase);
84
64
  if (logger !== undefined) {
85
65
  this.telemetryEventBatcher = new TelemetryEventBatcher({
86
66
  eventName: "rebaseProcessing",
87
67
  category: "performance",
88
68
  }, logger, maxRebaseStatsAggregationCount);
89
69
  }
90
- this.trunk = new SharedTreeBranch(this.trunkBase, changeFamily, mintRevisionTag, this._events, this.telemetryEventBatcher);
91
- this.localBranch = new SharedTreeBranch(this.trunk.getHead(), changeFamily, mintRevisionTag, this._events, this.telemetryEventBatcher);
92
- this.localBranch.events.on("afterChange", (event) => {
93
- if (event.type === "append") {
94
- for (const commit of event.newCommits) {
95
- this.localCommits.push(commit);
96
- }
97
- }
98
- else {
99
- this.localCommits.length = 0;
100
- findCommonAncestor([this.localBranch.getHead(), this.localCommits], this.trunk.getHead());
101
- }
102
- });
70
+ const mainTrunk = new SharedTreeBranch(this.trunkBase, changeFamily, mintRevisionTag, this._events, this.telemetryEventBatcher);
71
+ const mainBranch = this.createSharedBranch("main", undefined, undefined, mainTrunk);
103
72
  // Track all forks of the local branch for purposes of trunk eviction. Unlike the local branch, they have
104
73
  // an unknown lifetime and rebase frequency, so we can not make any assumptions about which trunk commits
105
74
  // they require and therefore we monitor them explicitly.
106
- onForkTransitive(this.localBranch, (fork) => this.registerBranch(fork));
75
+ onForkTransitive(mainBranch.localBranch, (fork) => this.registerBranch(fork));
76
+ }
77
+ getLocalBranch(branchId) {
78
+ return this.getSharedBranch(branchId).localBranch;
79
+ }
80
+ getSharedBranch(branchId) {
81
+ return this.sharedBranches.get(branchId) ?? fail("Branch does not exist");
107
82
  }
108
83
  /**
109
84
  * Make the given branch known to the `EditManager`. The `EditManager` will ensure that all registered
@@ -125,30 +100,32 @@ export class EditManager {
125
100
  const offAfterRebase = branch.events.on("afterChange", (args) => {
126
101
  if (args.type === "rebase") {
127
102
  this.trackBranch(branch);
128
- this.trimTrunk();
103
+ this.trimHistory();
129
104
  }
130
105
  });
131
106
  // When the branch is disposed, update our branch set and trim the trunk
132
107
  const offDispose = branch.events.on("dispose", () => {
133
108
  this.untrackBranch(branch);
134
- this.trimTrunk();
109
+ this.trimHistory();
135
110
  offBeforeRebase();
136
111
  offAfterRebase();
137
112
  offDispose();
138
113
  });
139
114
  }
140
115
  trackBranch(b) {
141
- const trunkCommit = findCommonAncestor(this.trunk.getHead(), b.getHead()) ??
142
- fail(0xad2 /* Expected branch to be related to trunk */);
143
- const sequenceId = this.getCommitSequenceId(trunkCommit);
116
+ const main = this.getSharedBranch("main");
117
+ const trunkCommit = findCommonAncestor(main.trunk.getHead(), b.getHead()) ??
118
+ fail(0xad2 /* Expected branch to be related to main */);
119
+ const sequenceId = main.getCommitSequenceId(trunkCommit.revision);
144
120
  const branches = getOrCreate(this.trunkBranches, sequenceId, () => new Set());
145
121
  assert(!branches.has(b), 0x670 /* Branch was registered more than once */);
146
122
  branches.add(b);
147
123
  }
148
124
  untrackBranch(b) {
149
- const trunkCommit = findCommonAncestor(this.trunk.getHead(), b.getHead()) ??
150
- fail(0xad3 /* Expected branch to be related to trunk */);
151
- const sequenceId = this.getCommitSequenceId(trunkCommit);
125
+ const main = this.getSharedBranch("main");
126
+ const trunkCommit = findCommonAncestor(main.trunk.getHead(), b.getHead()) ??
127
+ fail(0xad3 /* Expected branch to be related to main */);
128
+ const sequenceId = main.getCommitSequenceId(trunkCommit.revision);
152
129
  const branches = this.trunkBranches.get(sequenceId) ?? fail(0xad4 /* Expected branch to be tracked */);
153
130
  assert(branches.delete(b), 0x671 /* Expected branch to be tracked */);
154
131
  if (branches.size === 0) {
@@ -156,54 +133,17 @@ export class EditManager {
156
133
  }
157
134
  }
158
135
  /**
159
- * Promote the oldest un-sequenced commit on the local branch to the head of the trunk.
160
- * @param sequenceId - The sequence id of the new trunk commit
161
- * @remarks This method is a performance optimization for the scenario where this client receives its own change back after sequencing.
162
- * The normal (not optimized) process in this case would be to apply the new sequenced commit to the trunk and then rebase the local branch over the trunk.
163
- * The first commit will be "the same" (as in, it will have the same revision) as the commit that was just sequenced, so the rebase will be a no-op.
164
- * Because the rebase is a no-op, we can skip it entirely and simply remove the commit from the local branch and append it to the trunk.
165
- * Avoiding the overhead of the rebase process, even when it's a no-op, has real measured performance benefits and is worth the added complexity here.
136
+ * Return the sequenced number of the latest sequenced change.
166
137
  */
167
- fastForwardNextLocalCommit(sequenceId) {
168
- // First, push the local commit to the trunk.
169
- // We are mutating our `localCommits` cache here, but there is no need to actually change the `localBranch` itself because it will simply catch up later if/when it next rebases.
170
- const firstLocalCommit = this.localCommits.shift();
171
- assert(firstLocalCommit !== undefined, 0x6b5 /* Received a sequenced change from the local session despite having no local changes */);
172
- const previousSequenceId = this.getCommitSequenceId(this.trunk.getHead());
173
- this.pushGraphCommitToTrunk(sequenceId, firstLocalCommit, this.localSessionId);
174
- // Next, we need to update the sequence IDs that our local branches (user's branches, not peer branches) are associated with.
175
- // In particular, if a local branch is based on the previous trunk head (the branch's first ancestor in the trunk is the commit that was the head before we pushed the new commit)
176
- // and also branches off of the local branch (it has an ancestor that is part of the local branch), it needs to have its sequence number advanced to be that of the new trunk head.
177
- // Intuitively, this makes sense because:
178
- // 1. The trunk's head just advanced forward by some (sequence) amount.
179
- // 2. The local branch is always rebased to be branching off of the head of the trunk (not literally in this case, because of the optimization, but in effect).
180
- // 3. Therefore, the entire local branch just advanced forward by some (sequence) amount, and any commits downstream of it which track the sequence numbers of their base commits on the trunk should also advance.
181
- // This update is not necessarily required for all local branches, since some may have fallen behind the local branch and are based on older trunk commits (such branches do not need updating).
182
- const currentBranches = this.trunkBranches.get(previousSequenceId);
183
- if (currentBranches !== undefined) {
184
- const newBranches = getOrCreate(this.trunkBranches, sequenceId, () => new Set());
185
- for (const branch of currentBranches) {
186
- // Check every branch associated with the old sequence ID and advance it if it is based on the local branch (specifically, on the local branch as it was before we pushed its first commit to the trunk).
187
- // We validate this by checking if the branch's head is a descendant of the local commit that we just pushed.
188
- if (findAncestor(branch.getHead(), (c) => c === firstLocalCommit) !== undefined) {
189
- newBranches.add(branch);
190
- currentBranches.delete(branch);
191
- }
192
- }
193
- // Clean up our trunk branches map by removing any empty sets.
194
- if (currentBranches.size === 0) {
195
- this.trunkBranches.delete(previousSequenceId);
196
- }
197
- if (newBranches.size === 0) {
198
- this.trunkBranches.delete(sequenceId);
138
+ getLatestSequenceNumber() {
139
+ let maxSequenceNumber;
140
+ for (const branch of this.sharedBranches.values()) {
141
+ const branchMax = branch.getCommitSequenceId(branch.trunk.getHead().revision).sequenceNumber;
142
+ if (maxSequenceNumber === undefined || maxSequenceNumber < branchMax) {
143
+ maxSequenceNumber = branchMax;
199
144
  }
200
145
  }
201
- }
202
- /**
203
- * Return the sequence number at which the given commit was sequenced on the trunk, or undefined if the commit is not part of the trunk.
204
- */
205
- getSequenceNumber(trunkCommit) {
206
- return this.trunkMetadata.get(trunkCommit.revision)?.sequenceId.sequenceNumber;
146
+ return maxSequenceNumber;
207
147
  }
208
148
  /**
209
149
  * Advances the minimum sequence number, and removes all commits from the trunk which lie outside the collaboration window,
@@ -212,21 +152,21 @@ export class EditManager {
212
152
  *
213
153
  * @remarks If there are more than one commit with the same sequence number we assume this refers to the last commit in the batch.
214
154
  */
215
- advanceMinimumSequenceNumber(minimumSequenceNumber, trimTrunk = true) {
155
+ advanceMinimumSequenceNumber(minimumSequenceNumber, trimHistory = true) {
216
156
  if (minimumSequenceNumber === this.minimumSequenceNumber) {
217
157
  return;
218
158
  }
219
159
  assert(minimumSequenceNumber > this.minimumSequenceNumber, 0x476 /* number must be larger or equal to current minimumSequenceNumber. */);
220
160
  this.minimumSequenceNumber = minimumSequenceNumber;
221
- if (trimTrunk) {
222
- this.trimTrunk();
161
+ if (trimHistory) {
162
+ this.trimHistory();
223
163
  }
224
164
  }
225
165
  /**
226
166
  * Examines the latest known minimum sequence number and the trunk bases of any registered branches to determine
227
167
  * if any commits on the trunk are unreferenced and unneeded for future computation; those found are evicted from the trunk.
228
168
  */
229
- trimTrunk() {
169
+ trimHistory() {
230
170
  /** The sequence id of the most recent commit on the trunk that will be trimmed */
231
171
  let trunkTailSequenceId = {
232
172
  sequenceNumber: this.minimumSequenceNumber,
@@ -240,118 +180,72 @@ export class EditManager {
240
180
  const sequenceIdBeforeMinimumBranchBase = getUpperBoundOfPreviousSequenceId(minimumBranchBaseSequenceId);
241
181
  trunkTailSequenceId = minSequenceId(trunkTailSequenceId, sequenceIdBeforeMinimumBranchBase);
242
182
  }
243
- const [sequenceId, latestEvicted] = this.getClosestTrunkCommit(maxSequenceId(trunkTailSequenceId, this.sequenceMap.minKey() ?? minimumPossibleSequenceId));
183
+ const mainBranch = this.getSharedBranch("main");
184
+ const [sequenceId, latestEvicted] = mainBranch.getClosestTrunkCommit(maxSequenceId(trunkTailSequenceId, mainBranch.sequenceIdToCommit.minKey() ?? minimumPossibleSequenceId));
244
185
  // Don't do any work if the commit found by the search is already the tail of the trunk
245
- if (latestEvicted !== this.trunkBase) {
246
- // The minimum sequence number informs us that all peer branches are at least caught up to the tail commit,
247
- // so rebase them accordingly. This is necessary to prevent peer branches from referencing any evicted commits.
248
- for (const [, branch] of this.peerLocalBranches) {
249
- branch.rebaseOnto(this.trunk, latestEvicted);
250
- }
251
- // This mutation is a performance hack. If commits are truly immutable, then changing the trunk's tail requires
252
- // regenerating the entire commit graph. Instead, we can simply chop off the tail like this if we're certain
253
- // that there are no outstanding references to any of the commits being removed (other than the references via
254
- // the trunk). The peer branches have been rebased to the head of the trunk, the local branch is already rebased
255
- // to the head of the trunk, and all other branches are tracked by `trunkBranches` and known to be ahead of or at
256
- // `newTrunkBase`. Therefore, no branches should have unique references to any of the commits being evicted here.
257
- // We mutate the most recent of the evicted commits to become the new trunk base. That way, any other commits that
258
- // have parent pointers to the latest evicted commit will stay linked, even though that it is no longer part of the trunk.
259
- const newTrunkBase = latestEvicted;
260
- // The metadata for new trunk base revision needs to be deleted before modifying it.
261
- this.trunkMetadata.delete(newTrunkBase.revision);
262
- // collect the revisions that will be trimmed to send as part of the branch trimmed event
263
- const trimmedRevisions = getPathFromBase(newTrunkBase, this.trunkBase).map((c) => c.revision);
264
- // Dropping the parent field removes (transitively) all references to the evicted commits so they can be garbage collected.
265
- delete newTrunkBase.parent;
266
- this.trunkBase = newTrunkBase;
267
- // Update any state that is derived from trunk commits
268
- this.sequenceMap.editRange(minimumPossibleSequenceId, sequenceId, true, (s, commit) => {
269
- // Cleanup look-aside data for each evicted commit
270
- this.trunkMetadata.delete(commit.revision);
271
- // Delete all evicted commits from `sequenceMap` except for the latest one, which is the new `trunkBase`
272
- if (equalSequenceIds(s, sequenceId)) {
273
- assert(commit === newTrunkBase, 0x729 /* Expected last evicted commit to be new trunk base */);
274
- }
275
- else {
276
- Reflect.defineProperty(commit, "change", {
277
- get: () => assert(false, 0xa5e /* Should not access 'change' property of an evicted commit */),
278
- });
279
- Reflect.defineProperty(commit, "revision", {
280
- get: () => assert(false, 0xa5f /* Should not access 'revision' property of an evicted commit */),
281
- });
282
- Reflect.defineProperty(commit, "parent", {
283
- get: () => assert(false, 0xa60 /* Should not access 'parent' property of an evicted commit */),
284
- });
285
- return { delete: true };
286
- }
186
+ if (latestEvicted === this.trunkBase) {
187
+ return;
188
+ }
189
+ // This mutation is a performance hack. If commits are truly immutable, then changing the trunk's tail requires
190
+ // regenerating the entire commit graph. Instead, we can simply chop off the tail like this if we're certain
191
+ // that there are no outstanding references to any of the commits being removed (other than the references via
192
+ // the trunk). The peer branches have been rebased to the head of the trunk, the local branch is already rebased
193
+ // to the head of the trunk, and all other branches are tracked by `trunkBranches` and known to be ahead of or at
194
+ // `newTrunkBase`. Therefore, no branches should have unique references to any of the commits being evicted here.
195
+ // We mutate the most recent of the evicted commits to become the new trunk base. That way, any other commits that
196
+ // have parent pointers to the latest evicted commit will stay linked, even though that it is no longer part of the trunk.
197
+ const newTrunkBase = latestEvicted;
198
+ // collect the revisions that will be trimmed to send as part of the branch trimmed event
199
+ const trimmedCommits = getPathFromBase(newTrunkBase, this.trunkBase);
200
+ const trimmedRevisions = trimmedCommits.map((c) => c.revision);
201
+ // The minimum sequence number informs us that all peer branches are at least caught up to the tail commit,
202
+ // so rebase them accordingly. This is necessary to prevent peer branches from referencing any evicted commits.
203
+ mainBranch.trimHistory(latestEvicted, sequenceId);
204
+ // Only the last trimmed commit, which is the new trunk base, should remain accessible.
205
+ for (const commit of trimmedCommits.slice(0, -1)) {
206
+ Reflect.defineProperty(commit, "change", {
207
+ get: () => assert(false, 0xa5e /* Should not access 'change' property of an evicted commit */),
208
+ });
209
+ Reflect.defineProperty(commit, "revision", {
210
+ get: () => assert(false, 0xa5f /* Should not access 'revision' property of an evicted commit */),
211
+ });
212
+ Reflect.defineProperty(commit, "parent", {
213
+ get: () => assert(false, 0xa60 /* Should not access 'parent' property of an evicted commit */),
287
214
  });
288
- const trunkSize = getPathFromBase(this.trunk.getHead(), this.trunkBase).length;
289
- assert(this.sequenceMap.size === trunkSize + 1, 0x744 /* The size of the sequenceMap must have one element more than the trunk */);
290
- assert(this.trunkMetadata.size === trunkSize, 0x745 /* The size of the trunkMetadata must be the same as the trunk */);
291
- this._events.emit("ancestryTrimmed", trimmedRevisions);
292
215
  }
216
+ // Dropping the parent field removes (transitively) all references to the evicted commits so they can be garbage collected.
217
+ delete newTrunkBase.parent;
218
+ this.trunkBase = newTrunkBase;
219
+ this._events.emit("ancestryTrimmed", trimmedRevisions);
293
220
  }
294
221
  isEmpty() {
295
- return (this.trunk.getHead() === this.trunkBase &&
296
- this.peerLocalBranches.size === 0 &&
297
- this.localBranch.getHead() === this.trunk.getHead() &&
298
- this.minimumSequenceNumber === minimumPossibleSequenceNumber);
222
+ for (const branch of this.sharedBranches.values()) {
223
+ if (!branch.isEmpty(this.trunkBase)) {
224
+ return false;
225
+ }
226
+ }
227
+ return this.minimumSequenceNumber === minimumPossibleSequenceNumber;
299
228
  }
300
229
  getSummaryData() {
301
- // The assert below is acceptable at present because summarization only ever occurs on a client with no
302
- // local/in-flight changes.
303
- // In the future we may wish to relax this constraint. For that to work, the current implementation of
304
- // `EditManager` would have to be amended in one of two ways:
305
- // A) Changes made by the local session should be represented by a branch in `EditManager.branches`.
306
- // B) The contents of such a branch should be computed on demand based on the trunk.
307
- // Note that option (A) would be a simple change to `addSequencedChanges` whereas (B) would likely require
308
- // rebasing trunk changes over the inverse of trunk changes.
309
- assert(this.localBranch.getHead() === this.trunk.getHead(), 0x428 /* Clients with local changes cannot be used to generate summaries */);
310
230
  // Trimming the trunk before serializing ensures that the trunk data in the summary is as minimal as possible.
311
- this.trimTrunk();
312
- let oldestCommitInCollabWindow = this.getClosestTrunkCommit(this.minimumSequenceNumber)[1];
313
- assert(oldestCommitInCollabWindow.parent !== undefined ||
314
- oldestCommitInCollabWindow === this.trunkBase, 0x8c7 /* Expected oldest commit in collab window to have a parent or be the trunk base */);
315
- // Path construction is exclusive, so we need to use the parent of the oldest commit in the window if it exists
316
- oldestCommitInCollabWindow =
317
- oldestCommitInCollabWindow.parent ?? oldestCommitInCollabWindow;
318
- const trunk = getPathFromBase(this.trunk.getHead(), oldestCommitInCollabWindow).map((c) => {
319
- assert(c !== this.trunkBase, 0xa61 /* Serialized trunk should not include the trunk base */);
320
- const metadata = this.trunkMetadata.get(c.revision) ??
321
- fail(0xad5 /* Expected metadata for trunk commit */);
322
- const commit = {
323
- change: c.change,
324
- revision: c.revision,
325
- sequenceNumber: metadata.sequenceId.sequenceNumber,
326
- sessionId: metadata.sessionId,
327
- };
328
- if (metadata.sequenceId.indexInBatch !== undefined) {
329
- commit.indexInBatch = metadata.sequenceId.indexInBatch;
231
+ this.trimHistory();
232
+ const minSeqNumberToSummarize = {
233
+ sequenceNumber: brand(this.minimumSequenceNumber + 1),
234
+ };
235
+ let minBaseSeqId = minSeqNumberToSummarize;
236
+ const mainBranch = this.getSharedBranch("main");
237
+ const branches = new Map();
238
+ for (const [branchId, branch] of this.sharedBranches) {
239
+ if (branchId !== "main") {
240
+ const branchSummary = branch.getSummaryData(minSeqNumberToSummarize, this.trunkBase.revision);
241
+ branches.set(branchId, branchSummary);
242
+ assert(branchSummary.base !== undefined, "Branch summary must have a base");
243
+ const baseSequenceId = mainBranch.getCommitSequenceId(branchSummary.base);
244
+ minBaseSeqId = minSequenceId(minBaseSeqId, baseSequenceId);
330
245
  }
331
- return commit;
332
- });
333
- const peerLocalBranches = new Map(mapIterable(this.peerLocalBranches.entries(), ([sessionId, branch]) => {
334
- const branchPath = [];
335
- const ancestor = findCommonAncestor([branch.getHead(), branchPath], this.trunk.getHead()) ??
336
- fail(0xad6 /* Expected branch to be based on trunk */);
337
- const base = ancestor === this.trunkBase ? rootRevision : ancestor.revision;
338
- return [
339
- sessionId,
340
- {
341
- base,
342
- commits: branchPath.map((c) => {
343
- assert(c !== this.trunkBase, 0xa62 /* Serialized branch should not include the trunk base */);
344
- const commit = {
345
- change: c.change,
346
- revision: c.revision,
347
- sessionId,
348
- };
349
- return commit;
350
- }),
351
- },
352
- ];
353
- }));
354
- return { trunk, peerLocalBranches };
246
+ }
247
+ const mainSummary = mainBranch.getSummaryData(minBaseSeqId, this.trunkBase.revision);
248
+ return { main: mainSummary, branches, originator: this.localSessionId };
355
249
  }
356
250
  loadSummaryData(data) {
357
251
  assert(this.isEmpty(), 0x68a /* Attempted to load from summary after edit manager was already mutated */);
@@ -359,53 +253,30 @@ export class EditManager {
359
253
  // when hydrating the peer branches below
360
254
  const trunkRevisionCache = new Map();
361
255
  trunkRevisionCache.set(this.trunkBase.revision, this.trunkBase);
362
- this.trunk.setHead(data.trunk.reduce((base, c) => {
363
- const sequenceId = c.indexInBatch === undefined
364
- ? {
365
- sequenceNumber: c.sequenceNumber,
366
- }
367
- : {
368
- sequenceNumber: c.sequenceNumber,
369
- indexInBatch: c.indexInBatch,
370
- };
371
- const commit = mintCommit(base, c);
372
- this.sequenceMap.set(sequenceId, commit);
373
- this.trunkMetadata.set(c.revision, {
374
- sequenceId,
375
- sessionId: c.sessionId,
376
- });
377
- trunkRevisionCache.set(c.revision, commit);
378
- return commit;
379
- }, this.trunkBase));
380
- this.localBranch.setHead(this.trunk.getHead());
381
- for (const [sessionId, branch] of data.peerLocalBranches) {
382
- const commit = trunkRevisionCache.get(branch.base) ??
383
- fail(0xad7 /* Expected summary branch to be based off of a revision in the trunk */);
384
- this.peerLocalBranches.set(sessionId, new SharedTreeBranch(branch.commits.reduce(mintCommit, commit), this.changeFamily, this.mintRevisionTag));
385
- }
386
- }
387
- getCommitSequenceId(trunkCommitOrTrunkBase) {
388
- const id = this.trunkMetadata.get(trunkCommitOrTrunkBase.revision)?.sequenceId;
389
- if (id === undefined) {
390
- assert(trunkCommitOrTrunkBase === this.trunkBase, 0xa63 /* Commit must be either be on the trunk or be the trunk base */);
391
- return minimumPossibleSequenceId;
256
+ const mainBranch = this.sharedBranches.get("main") ?? fail("Main branch must exist");
257
+ mainBranch.loadSummaryData(data.main, trunkRevisionCache);
258
+ if (data.branches !== undefined) {
259
+ for (const [branchId, branchData] of data.branches) {
260
+ const branch = this.createSharedBranch(branchId, branchData.session, mainBranch, mainBranch.trunk.fork());
261
+ branch.loadSummaryData(branchData, trunkRevisionCache);
262
+ }
392
263
  }
393
- return id;
394
264
  }
395
- getTrunkChanges() {
396
- return this.getTrunkCommits().map((c) => c.change);
265
+ getTrunkHead(branchId) {
266
+ return this.getSharedBranch(branchId).trunk.getHead();
397
267
  }
398
- getTrunkCommits() {
399
- return getPathFromBase(this.trunk.getHead(), this.trunkBase);
268
+ getTrunkChanges(branchId) {
269
+ return this.getTrunkCommits(branchId).map((c) => c.change);
400
270
  }
401
- getTrunkHead() {
402
- return this.trunk.getHead();
271
+ getTrunkCommits(branchId) {
272
+ return getPathFromBase(this.getTrunkHead(branchId), this.trunkBase);
403
273
  }
404
- getLocalChanges() {
405
- return this.getLocalCommits().map((c) => c.change);
274
+ getLocalChanges(branchId) {
275
+ return this.getLocalCommits(branchId).map((c) => c.change);
406
276
  }
407
- getLocalCommits() {
408
- return this.localCommits;
277
+ getLocalCommits(branchId) {
278
+ const branch = this.getSharedBranch(branchId);
279
+ return branch.getLocalCommits();
409
280
  }
410
281
  /**
411
282
  * Gets the length of the longest branch maintained by this `EditManager`.
@@ -417,15 +288,36 @@ export class EditManager {
417
288
  */
418
289
  getLongestBranchLength() {
419
290
  let max = 0;
420
- const trunkHead = this.trunk.getHead();
421
- for (const branch of this.peerLocalBranches.values()) {
422
- const branchPath = getPathFromBase(branch.getHead(), trunkHead);
423
- if (branchPath.length > max) {
424
- max = branchPath.length;
425
- }
291
+ for (const branch of this.sharedBranches.values()) {
292
+ max = Math.max(max, branch.getLongestBranchLength());
426
293
  }
427
- const localPath = getPathFromBase(this.localBranch.getHead(), trunkHead);
428
- return Math.max(max, localPath.length);
294
+ return max;
295
+ }
296
+ sequenceBranchCreation(sessionId, referenceSequenceNumber, branchId) {
297
+ if (sessionId === this.localSessionId) {
298
+ assert(this.sharedBranches.has(branchId), "Expected branch to already exist");
299
+ return;
300
+ }
301
+ const mainBranch = this.getSharedBranch("main");
302
+ const branchTrunk = mainBranch.rebasePeer(sessionId, referenceSequenceNumber).fork();
303
+ const sharedBranch = this.createSharedBranch(branchId, sessionId, mainBranch, branchTrunk);
304
+ this.registerBranch(sharedBranch.localBranch);
305
+ onForkTransitive(sharedBranch.localBranch, (fork) => this.registerBranch(fork));
306
+ }
307
+ addBranch(branchId) {
308
+ const main = this.getSharedBranch("main") ?? fail("Main branch must exist");
309
+ this.createSharedBranch(branchId, this.localSessionId, main, this.getLocalBranch("main").fork());
310
+ }
311
+ removeBranch(branchId) {
312
+ assert(branchId !== "main", "Cannot remove main branch");
313
+ const hadBranch = this.sharedBranches.delete(branchId);
314
+ assert(hadBranch, "Expected branch to exist");
315
+ }
316
+ createSharedBranch(branchId, sessionId, parent, branch) {
317
+ const sharedBranch = new SharedBranch(parent, branch, branchId, sessionId, minimumPossibleSequenceId, this.changeFamily, this.mintRevisionTag, this._events, this.telemetryEventBatcher);
318
+ assert(!this.sharedBranches.has(branchId), "A branch with this ID already exists");
319
+ this.sharedBranches.set(branchId, sharedBranch);
320
+ return sharedBranch;
429
321
  }
430
322
  /* eslint-disable jsdoc/check-indentation */
431
323
  /**
@@ -438,34 +330,118 @@ export class EditManager {
438
330
  * - They are not interleaved with messages from other clients.
439
331
  */
440
332
  /* eslint-enable jsdoc/check-indentation */
441
- addSequencedChanges(newCommits, sessionId, sequenceNumber, referenceSequenceNumber) {
333
+ addSequencedChanges(newCommits, sessionId, sequenceNumber, referenceSequenceNumber, branchId = "main") {
442
334
  assert(newCommits.length > 0, 0xad8 /* Expected at least one sequenced change */);
443
335
  assert(sequenceNumber > this.minimumSequenceNumber, 0x713 /* Expected change sequence number to exceed the last known minimum sequence number */);
444
- assert(sequenceNumber >= // This is ">=", not ">" because changes in the same batch will have the same sequence number
445
- (this.sequenceMap.maxKey()?.sequenceNumber ?? minimumPossibleSequenceNumber), 0xa64 /* Attempted to sequence change with an outdated sequence number */);
446
- // Returns the sequence id for the next commit to be processed in the bunch. Since all the commits have the
447
- // same sequence number, only the index in the batch needs to be incremented.
448
- const getNextSequenceId = (sequenceId) => {
449
- return {
450
- sequenceNumber: sequenceId.sequenceNumber,
451
- indexInBatch: (sequenceId.indexInBatch ?? 0) + 1,
452
- };
336
+ const branch = this.getSharedBranch(branchId);
337
+ const onSequenceLocalCommit = (commit, sequenceId, previousSequenceId) => {
338
+ // Next, we need to update the sequence IDs that our local branches (user's branches, not peer branches) are associated with.
339
+ // In particular, if a local branch is based on the previous trunk head (the branch's first ancestor in the trunk is the commit that was the head before we pushed the new commit)
340
+ // and also branches off of the local branch (it has an ancestor that is part of the local branch), it needs to have its sequence number advanced to be that of the new trunk head.
341
+ // Intuitively, this makes sense because:
342
+ // 1. The trunk's head just advanced forward by some (sequence) amount.
343
+ // 2. The local branch is always rebased to be branching off of the head of the trunk (not literally in this case, because of the optimization, but in effect).
344
+ // 3. Therefore, the entire local branch just advanced forward by some (sequence) amount, and any commits downstream of it which track the sequence numbers of their base commits on the trunk should also advance.
345
+ // This update is not necessarily required for all local branches, since some may have fallen behind the local branch and are based on older trunk commits (such branches do not need updating).
346
+ const currentBranches = this.trunkBranches.get(previousSequenceId);
347
+ if (currentBranches !== undefined) {
348
+ const newBranches = getOrCreate(this.trunkBranches, sequenceId, () => new Set());
349
+ for (const forkedBranch of currentBranches) {
350
+ // Check every branch associated with the old sequence ID and advance it if it is based on the local branch (specifically, on the local branch as it was before we pushed its first commit to the trunk).
351
+ // We validate this by checking if the branch's head is a descendant of the local commit that we just pushed.
352
+ if (findAncestor(forkedBranch.getHead(), (c) => c === commit) !== undefined) {
353
+ newBranches.add(forkedBranch);
354
+ currentBranches.delete(forkedBranch);
355
+ }
356
+ }
357
+ // Clean up our trunk branches map by removing any empty sets.
358
+ if (currentBranches.size === 0) {
359
+ this.trunkBranches.delete(previousSequenceId);
360
+ }
361
+ if (newBranches.size === 0) {
362
+ this.trunkBranches.delete(sequenceId);
363
+ }
364
+ }
453
365
  };
454
- const commitsSequenceNumber = this.getBatch(sequenceNumber);
366
+ const areLocalCommits = sessionId === this.localSessionId;
367
+ branch.addSequencedChanges(newCommits, sessionId, sequenceNumber, areLocalCommits, referenceSequenceNumber, onSequenceLocalCommit);
368
+ }
369
+ findLocalCommit(branchId, revision) {
370
+ const commits = [];
371
+ const commit = findAncestor([this.getSharedBranch(branchId).localBranch.getHead(), commits], (c) => c.revision === revision);
372
+ assert(commit !== undefined, 0x599 /* Expected local branch to contain revision */);
373
+ return [commit, commits];
374
+ }
375
+ }
376
+ /**
377
+ * Gets the path from the base of a branch to its head.
378
+ */
379
+ function getPathFromBase(branchHead, baseBranchHead) {
380
+ const path = [];
381
+ assert(findCommonAncestor([branchHead, path], baseBranchHead) !== undefined, 0x573 /* Expected branches to be related */);
382
+ return path;
383
+ }
384
+ class SharedBranch {
385
+ constructor(parentBranch, trunk, id, sessionId, baseCommitSequenceId, changeFamily, mintRevisionTag, branchTrimmer, telemetryEventBatcher) {
386
+ this.parentBranch = parentBranch;
387
+ this.trunk = trunk;
388
+ this.id = id;
389
+ this.sessionId = sessionId;
390
+ this.changeFamily = changeFamily;
391
+ this.mintRevisionTag = mintRevisionTag;
392
+ /**
393
+ * Branches are maintained to represent the local change list that the issuing client had
394
+ * at the time of submitting the latest known edit on the branch.
395
+ * This means the head commit of each branch is always in its original (non-rebased) form.
396
+ */
397
+ this.peerLocalBranches = new Map();
398
+ /**
399
+ * A map from a sequence id to the commit which has that sequence id.
400
+ * This also includes an entry for the {@link trunkBase} which always has the lowest key in the map.
401
+ */
402
+ this.sequenceIdToCommit = new BTree(undefined, sequenceIdComparator);
403
+ /**
404
+ * The list of commits (from oldest to most recent) that are on the local branch but not on the trunk.
405
+ * When a local commit is sequenced, the first commit in this list shifted onto the tip of the trunk.
406
+ */
407
+ this.localCommits = [];
408
+ /**
409
+ * Records extra data associated with sequenced commits.
410
+ * This does not include an entry for the {@link trunkBase}.
411
+ */
412
+ this.commitMetadata = new Map();
413
+ this.localBranch = new SharedTreeBranch(this.trunk.getHead(), changeFamily, mintRevisionTag, branchTrimmer, telemetryEventBatcher);
414
+ this.sequenceIdToCommit.set(baseCommitSequenceId, this.trunk.getHead());
415
+ this.localBranch.events.on("afterChange", (event) => {
416
+ if (event.type === "append") {
417
+ for (const commit of event.newCommits) {
418
+ this.localCommits.push(commit);
419
+ }
420
+ }
421
+ else {
422
+ this.localCommits.length = 0;
423
+ findCommonAncestor([this.localBranch.getHead(), this.localCommits], this.trunk.getHead());
424
+ }
425
+ });
426
+ }
427
+ addSequencedChanges(newCommits, sessionId, sequenceNumber, areLocalCommits, referenceSequenceNumber, onSequenceLocalCommit) {
428
+ assert(sequenceNumber >= // This is ">=", not ">" because changes in the same batch will have the same sequence number
429
+ (this.sequenceIdToCommit.maxKey()?.sequenceNumber ?? minimumPossibleSequenceNumber), 0xa64 /* Attempted to sequence change with an outdated sequence number */);
430
+ const batchSize = this.getBatchSize(sequenceNumber);
455
431
  // The sequence id for the next commit to be processed in the bunch.
456
- let nextSequenceId = commitsSequenceNumber.length === 0
432
+ let nextSequenceId = batchSize === 0
457
433
  ? {
458
434
  sequenceNumber,
459
435
  }
460
436
  : {
461
437
  sequenceNumber,
462
- indexInBatch: commitsSequenceNumber.length,
438
+ indexInBatch: batchSize,
463
439
  };
464
440
  // Local changes, i.e., changes from this client are applied by fast forwarding the local branch commit onto
465
441
  // the trunk.
466
- if (sessionId === this.localSessionId) {
442
+ if (areLocalCommits) {
467
443
  for (const _ of newCommits) {
468
- this.fastForwardNextLocalCommit(nextSequenceId);
444
+ this.sequenceLocalCommit(nextSequenceId, sessionId, onSequenceLocalCommit);
469
445
  nextSequenceId = getNextSequenceId(nextSequenceId);
470
446
  }
471
447
  return;
@@ -475,9 +451,7 @@ export class EditManager {
475
451
  // Get the revision that the remote change is based on and rebase that peer local branch over the part of the
476
452
  // trunk up to the base revision. This will be a no-op if the sending client has not advanced since the last
477
453
  // time we received an edit from it
478
- const [, baseRevisionInTrunk] = this.getClosestTrunkCommit(referenceSequenceNumber);
479
- const peerLocalBranch = getOrCreate(this.peerLocalBranches, sessionId, () => new SharedTreeBranch(baseRevisionInTrunk, this.changeFamily, this.mintRevisionTag));
480
- peerLocalBranch.rebaseOnto(this.trunk, baseRevisionInTrunk);
454
+ const peerLocalBranch = this.rebasePeer(sessionId, referenceSequenceNumber);
481
455
  // Step 2 - Append the changes to the peer branch and rebase the changes to the tip of the trunk.
482
456
  if (peerLocalBranch.getHead() === this.trunk.getHead()) {
483
457
  // If the peer local branch is fully caught up and empty (no changes relative to the trunk) after being
@@ -498,8 +472,8 @@ export class EditManager {
498
472
  // If the merge resulted in any changes to the trunk, update the sequence map and trunk metadata
499
473
  // with the rebased commits.
500
474
  for (const sourceCommit of result.sourceCommits) {
501
- this.sequenceMap.set(nextSequenceId, sourceCommit);
502
- this.trunkMetadata.set(sourceCommit.revision, {
475
+ this.sequenceIdToCommit.set(nextSequenceId, sourceCommit);
476
+ this.commitMetadata.set(sourceCommit.revision, {
503
477
  sequenceId: nextSequenceId,
504
478
  sessionId,
505
479
  });
@@ -510,21 +484,85 @@ export class EditManager {
510
484
  // Step 3 - Rebase the local branch over the updated trunk.
511
485
  this.localBranch.rebaseOnto(this.trunk);
512
486
  }
513
- findLocalCommit(revision) {
514
- const commits = [];
515
- const commit = findAncestor([this.localBranch.getHead(), commits], (c) => c.revision === revision);
516
- assert(commit !== undefined, 0x599 /* Expected local branch to contain revision */);
517
- return [commit, commits];
487
+ isEmpty(baseCommit) {
488
+ return (this.trunk.getHead() === baseCommit &&
489
+ this.peerLocalBranches.size === 0 &&
490
+ this.localBranch.getHead() === this.trunk.getHead());
518
491
  }
519
- pushCommitToTrunk(sequenceId, commit) {
520
- const mintedCommit = mintCommit(this.trunk.getHead(), commit);
521
- this.pushGraphCommitToTrunk(sequenceId, mintedCommit, commit.sessionId);
492
+ rebasePeer(sessionId, referenceSequenceNumber) {
493
+ const [, baseRevisionInTrunk] = this.getClosestTrunkCommit(referenceSequenceNumber);
494
+ const peerLocalBranch = getOrCreate(this.peerLocalBranches, sessionId, () => new SharedTreeBranch(baseRevisionInTrunk, this.changeFamily, this.mintRevisionTag));
495
+ peerLocalBranch.rebaseOnto(this.trunk, baseRevisionInTrunk);
496
+ return peerLocalBranch;
522
497
  }
523
- pushGraphCommitToTrunk(sequenceId, graphCommit, sessionId) {
524
- this.trunk.setHead(graphCommit);
498
+ getPeerBranchOrTrunk(sessionId) {
499
+ return this.peerLocalBranches.get(sessionId) ?? this.trunk;
500
+ }
501
+ /**
502
+ * Gets the length of the longest branch maintained by this `SharedBranch`.
503
+ * This may be the length of a peer branch or the local branch.
504
+ *
505
+ * @remarks
506
+ * The length is counted from the lowest common ancestor with the trunk such that a fully sequenced branch would
507
+ * have length zero.
508
+ */
509
+ getLongestBranchLength() {
510
+ let max = 0;
525
511
  const trunkHead = this.trunk.getHead();
526
- this.sequenceMap.set(sequenceId, trunkHead);
527
- this.trunkMetadata.set(trunkHead.revision, { sequenceId, sessionId });
512
+ for (const branch of this.peerLocalBranches.values()) {
513
+ const branchPath = getPathFromBase(branch.getHead(), trunkHead);
514
+ if (branchPath.length > max) {
515
+ max = branchPath.length;
516
+ }
517
+ }
518
+ const localPath = getPathFromBase(this.localBranch.getHead(), trunkHead);
519
+ return Math.max(max, localPath.length);
520
+ }
521
+ trimHistory(newBase, sequenceId) {
522
+ this.rebasePeers(newBase);
523
+ this.sequenceIdToCommit.editRange(minimumPossibleSequenceId, sequenceId, true, (s, commit) => {
524
+ // Cleanup look-aside data for each evicted commit
525
+ this.commitMetadata.delete(commit.revision);
526
+ // Delete all evicted commits from `sequenceMap` except for the latest one, which is the new `trunkBase`
527
+ if (equalSequenceIds(s, sequenceId)) {
528
+ assert(commit === newBase, 0x729 /* Expected last evicted commit to be new trunk base */);
529
+ }
530
+ else {
531
+ return { delete: true };
532
+ }
533
+ });
534
+ const trunkSize = getPathFromBase(this.trunk.getHead(), newBase).length;
535
+ assert(this.sequenceIdToCommit.size === trunkSize + 1, 0x744 /* The size of the sequenceMap must have one element more than the trunk */);
536
+ assert(this.commitMetadata.size === trunkSize, 0x745 /* The size of the trunkMetadata must be the same as the trunk */);
537
+ }
538
+ rebasePeers(commit) {
539
+ for (const [, branch] of this.peerLocalBranches) {
540
+ branch.rebaseOnto(this.trunk, commit);
541
+ }
542
+ // The metadata for new trunk base revision needs to be deleted before modifying it.
543
+ this.commitMetadata.delete(commit.revision);
544
+ }
545
+ getLocalCommits() {
546
+ return this.localCommits;
547
+ }
548
+ /**
549
+ * Promote the oldest un-sequenced commit on the local branch to the head of the trunk.
550
+ * @param sequenceId - The sequence id of the new trunk commit
551
+ * @remarks This method is a performance optimization for the scenario where this client receives its own change back after sequencing.
552
+ * The normal (not optimized) process in this case would be to apply the new sequenced commit to the trunk and then rebase the local branch over the trunk.
553
+ * The first commit will be "the same" (as in, it will have the same revision) as the commit that was just sequenced, so the rebase will be a no-op.
554
+ * Because the rebase is a no-op, we can skip it entirely and simply remove the commit from the local branch and append it to the trunk.
555
+ * Avoiding the overhead of the rebase process, even when it's a no-op, has real measured performance benefits and is worth the added complexity here.
556
+ */
557
+ sequenceLocalCommit(sequenceId, sessionId, onSequenceLocalCommit) {
558
+ // First, push the local commit to the trunk.
559
+ // We are mutating our `localCommits` cache here,but there is no need to actually change the `localBranch` itself because it will simply catch up later if/when it next rebases.
560
+ const firstLocalCommit = this.localCommits.shift();
561
+ assert(firstLocalCommit !== undefined, 0x6b5 /* Received a sequenced change from the local session despite having no local changes */);
562
+ const prevSequenceId = this.getCommitSequenceId(this.trunk.getHead().revision);
563
+ this.pushGraphCommitToTrunk(sequenceId, firstLocalCommit, sessionId);
564
+ onSequenceLocalCommit(firstLocalCommit, sequenceId, prevSequenceId);
565
+ return firstLocalCommit;
528
566
  }
529
567
  getClosestTrunkCommit(searchBy) {
530
568
  const sequenceId = typeof searchBy === "number"
@@ -536,26 +574,135 @@ export class EditManager {
536
574
  indexInBatch: Number.POSITIVE_INFINITY,
537
575
  }
538
576
  : searchBy;
539
- const commit = this.sequenceMap.getPairOrNextLower(sequenceId);
577
+ const commit = this.sequenceIdToCommit.getPairOrNextLower(sequenceId);
540
578
  assert(commit !== undefined, 0x746 /* sequence id has been evicted */);
541
579
  return commit;
542
580
  }
543
- getBatch(sequenceNumber) {
581
+ pushGraphCommitToTrunk(sequenceId, graphCommit, sessionId) {
582
+ this.trunk.setHead(graphCommit);
583
+ this.registerSequencedCommit(sequenceId, sessionId, graphCommit);
584
+ }
585
+ pushCommitToTrunk(sequenceId, commit) {
586
+ const mintedCommit = mintCommit(this.trunk.getHead(), commit);
587
+ this.pushGraphCommitToTrunk(sequenceId, mintedCommit, commit.sessionId);
588
+ }
589
+ registerSequencedCommit(sequenceId, sessionId, commit) {
590
+ this.sequenceIdToCommit.set(sequenceId, commit);
591
+ this.commitMetadata.set(commit.revision, { sequenceId, sessionId });
592
+ }
593
+ getCommitSequenceId(commitRevision) {
594
+ const id = this.commitMetadata.get(commitRevision)?.sequenceId;
595
+ if (id === undefined) {
596
+ return minimumPossibleSequenceId;
597
+ }
598
+ return id;
599
+ }
600
+ // TODO: Document that this is to handle receiving separate commits with the same sequence ID,
601
+ // as a batch of changes are not guaranteed to be processed as one bunch.
602
+ getBatchSize(sequenceNumber) {
544
603
  const startSequenceId = {
545
604
  sequenceNumber,
546
605
  };
547
606
  const endSequenceId = {
548
607
  sequenceNumber: brand(sequenceNumber + 1),
549
608
  };
550
- return this.sequenceMap.getRange(startSequenceId, endSequenceId, false);
609
+ return this.sequenceIdToCommit.getRange(startSequenceId, endSequenceId, false).length;
610
+ }
611
+ getSummaryData(minSeqNumberToSummarize, trunkBaseRevision) {
612
+ // The assert below is acceptable at present because summarization only ever occurs on a client with no
613
+ // local/in-flight changes.
614
+ // In the future we may wish to relax this constraint. For that to work, the current implementation of
615
+ // `EditManager` would have to be amended in one of two ways:
616
+ // A) Changes made by the local session should be represented by a branch in `EditManager.branches`.
617
+ // B) The contents of such a branch should be computed on demand based on the trunk.
618
+ // Note that option (A) would be a simple change to `addSequencedChanges` whereas (B) would likely require
619
+ // rebasing trunk changes over the inverse of trunk changes.
620
+ assert(this.localBranch.getHead() === this.trunk.getHead(), "Clients with local changes cannot be used to generate summaries");
621
+ let parentHead;
622
+ if (this.parentBranch === undefined) {
623
+ const oldestCommitInCollabWindow = this.getClosestTrunkCommit(minSeqNumberToSummarize)[1];
624
+ // Path construction is exclusive, so we need to use the parent of the oldest commit in the window if it exists
625
+ parentHead = oldestCommitInCollabWindow.parent ?? oldestCommitInCollabWindow;
626
+ }
627
+ else {
628
+ parentHead = this.parentBranch.trunk.getHead();
629
+ }
630
+ const childBranchTrunkCommits = [];
631
+ const forkPointFromMainTrunk = findCommonAncestor([this.trunk.getHead(), childBranchTrunkCommits], parentHead);
632
+ assert(forkPointFromMainTrunk !== undefined, "Expected child branch to be based on main branch");
633
+ const trunk = childBranchTrunkCommits.map((c) => {
634
+ const metadata = this.commitMetadata.get(c.revision) ??
635
+ fail(0xad5 /* Expected metadata for trunk commit */);
636
+ const commit = {
637
+ change: c.change,
638
+ revision: c.revision,
639
+ sequenceNumber: metadata.sequenceId.sequenceNumber,
640
+ sessionId: metadata.sessionId,
641
+ };
642
+ if (metadata.sequenceId.indexInBatch !== undefined) {
643
+ commit.indexInBatch = metadata.sequenceId.indexInBatch;
644
+ }
645
+ return commit;
646
+ });
647
+ const peerLocalBranches = new Map(mapIterable(this.peerLocalBranches.entries(), ([sessionId, branch]) => {
648
+ const branchPath = [];
649
+ const ancestor = findCommonAncestor([branch.getHead(), branchPath], this.trunk.getHead()) ??
650
+ fail(0xad6 /* Expected branch to be based on trunk */);
651
+ const base = ancestor.revision === trunkBaseRevision ? rootRevision : ancestor.revision;
652
+ return [
653
+ sessionId,
654
+ {
655
+ base,
656
+ commits: branchPath.map((c) => {
657
+ const commit = {
658
+ change: c.change,
659
+ revision: c.revision,
660
+ sessionId,
661
+ };
662
+ return commit;
663
+ }),
664
+ },
665
+ ];
666
+ }));
667
+ const trunkBase = this.parentBranch === undefined ? undefined : forkPointFromMainTrunk.revision;
668
+ return { trunk, peerLocalBranches, base: trunkBase, id: this.id, session: this.sessionId };
669
+ }
670
+ loadSummaryData(data, trunkRevisionCache) {
671
+ assert((this.parentBranch === undefined) === (data.base === undefined), "Expected branch base to match presence of parent branch");
672
+ const parentTrunkBase = trunkRevisionCache.get(data.base ?? rootRevision) ??
673
+ fail("Expected base revision to be in trunk cache");
674
+ this.trunk.setHead(data.trunk.reduce((base, c) => {
675
+ const sequenceId = c.indexInBatch === undefined
676
+ ? {
677
+ sequenceNumber: c.sequenceNumber,
678
+ }
679
+ : {
680
+ sequenceNumber: c.sequenceNumber,
681
+ indexInBatch: c.indexInBatch,
682
+ };
683
+ const commit = mintCommit(base, c);
684
+ this.sequenceIdToCommit.set(sequenceId, commit);
685
+ this.commitMetadata.set(c.revision, {
686
+ sequenceId,
687
+ sessionId: c.sessionId,
688
+ });
689
+ trunkRevisionCache.set(c.revision, commit);
690
+ return commit;
691
+ }, parentTrunkBase));
692
+ this.localBranch.setHead(this.trunk.getHead());
693
+ for (const [sessionId, branch] of data.peerLocalBranches) {
694
+ const commit = trunkRevisionCache.get(branch.base) ??
695
+ fail(0xad7 /* Expected summary branch to be based off of a revision in the trunk */);
696
+ this.peerLocalBranches.set(sessionId, new SharedTreeBranch(branch.commits.reduce(mintCommit, commit), this.changeFamily, this.mintRevisionTag));
697
+ }
551
698
  }
552
699
  }
553
- /**
554
- * Gets the path from the base of a branch to its head.
555
- */
556
- function getPathFromBase(branchHead, baseBranchHead) {
557
- const path = [];
558
- assert(findCommonAncestor([branchHead, path], baseBranchHead) !== undefined, 0x573 /* Expected branches to be related */);
559
- return path;
700
+ // Returns the sequence id for the next commit to be processed in the bunch. Since all the commits have the
701
+ // same sequence number, only the index in the batch needs to be incremented.
702
+ function getNextSequenceId(sequenceId) {
703
+ return {
704
+ sequenceNumber: sequenceId.sequenceNumber,
705
+ indexInBatch: (sequenceId.indexInBatch ?? 0) + 1,
706
+ };
560
707
  }
561
708
  //# sourceMappingURL=editManager.js.map