@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.
- package/api-report/tree.alpha.api.md +3 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +2 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/rebase/index.d.ts +1 -1
- package/dist/core/rebase/index.d.ts.map +1 -1
- package/dist/core/rebase/index.js +2 -1
- package/dist/core/rebase/index.js.map +1 -1
- package/dist/core/rebase/utils.d.ts +10 -0
- package/dist/core/rebase/utils.d.ts.map +1 -1
- package/dist/core/rebase/utils.js +20 -1
- package/dist/core/rebase/utils.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.d.ts.map +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/dist/shared-tree/schematizingTreeView.js +2 -2
- package/dist/shared-tree/schematizingTreeView.js.map +1 -1
- package/dist/shared-tree/sharedTree.d.ts +12 -2
- package/dist/shared-tree/sharedTree.d.ts.map +1 -1
- package/dist/shared-tree/sharedTree.js +71 -33
- package/dist/shared-tree/sharedTree.js.map +1 -1
- package/dist/shared-tree/treeCheckout.d.ts +13 -7
- package/dist/shared-tree/treeCheckout.d.ts.map +1 -1
- package/dist/shared-tree/treeCheckout.js +114 -84
- package/dist/shared-tree/treeCheckout.js.map +1 -1
- package/dist/shared-tree-core/branch.d.ts +3 -0
- package/dist/shared-tree-core/branch.d.ts.map +1 -1
- package/dist/shared-tree-core/branch.js.map +1 -1
- package/dist/shared-tree-core/branchIdCodec.d.ts +11 -0
- package/dist/shared-tree-core/branchIdCodec.d.ts.map +1 -0
- package/dist/shared-tree-core/branchIdCodec.js +18 -0
- package/dist/shared-tree-core/branchIdCodec.js.map +1 -0
- package/dist/shared-tree-core/editManager.d.ts +33 -63
- package/dist/shared-tree-core/editManager.d.ts.map +1 -1
- package/dist/shared-tree-core/editManager.js +437 -290
- package/dist/shared-tree-core/editManager.js.map +1 -1
- package/dist/shared-tree-core/editManagerCodecs.d.ts +1 -1
- package/dist/shared-tree-core/editManagerCodecs.d.ts.map +1 -1
- package/dist/shared-tree-core/editManagerCodecs.js +7 -96
- package/dist/shared-tree-core/editManagerCodecs.js.map +1 -1
- package/dist/shared-tree-core/editManagerCodecsCommons.d.ts +17 -0
- package/dist/shared-tree-core/editManagerCodecsCommons.d.ts.map +1 -0
- package/dist/shared-tree-core/editManagerCodecsCommons.js +139 -0
- package/dist/shared-tree-core/editManagerCodecsCommons.js.map +1 -0
- package/dist/shared-tree-core/editManagerCodecsV1toV4.d.ts +16 -0
- package/dist/shared-tree-core/editManagerCodecsV1toV4.d.ts.map +1 -0
- package/dist/shared-tree-core/editManagerCodecsV1toV4.js +39 -0
- package/dist/shared-tree-core/editManagerCodecsV1toV4.js.map +1 -0
- package/dist/shared-tree-core/editManagerCodecsV5.d.ts +16 -0
- package/dist/shared-tree-core/editManagerCodecsV5.d.ts.map +1 -0
- package/dist/shared-tree-core/editManagerCodecsV5.js +58 -0
- package/dist/shared-tree-core/editManagerCodecsV5.js.map +1 -0
- package/dist/shared-tree-core/{editManagerFormat.d.ts → editManagerFormatCommons.d.ts} +31 -7
- package/dist/shared-tree-core/editManagerFormatCommons.d.ts.map +1 -0
- package/dist/shared-tree-core/{editManagerFormat.js → editManagerFormatCommons.js} +13 -12
- package/dist/shared-tree-core/editManagerFormatCommons.js.map +1 -0
- package/dist/shared-tree-core/editManagerFormatV1toV4.d.ts +31 -0
- package/dist/shared-tree-core/editManagerFormatV1toV4.d.ts.map +1 -0
- package/dist/shared-tree-core/editManagerFormatV1toV4.js +24 -0
- package/dist/shared-tree-core/editManagerFormatV1toV4.js.map +1 -0
- package/dist/shared-tree-core/editManagerFormatV5.d.ts +62 -0
- package/dist/shared-tree-core/editManagerFormatV5.d.ts.map +1 -0
- package/dist/shared-tree-core/editManagerFormatV5.js +20 -0
- package/dist/shared-tree-core/editManagerFormatV5.js.map +1 -0
- package/dist/shared-tree-core/index.d.ts +3 -3
- package/dist/shared-tree-core/index.d.ts.map +1 -1
- package/dist/shared-tree-core/index.js.map +1 -1
- package/dist/shared-tree-core/messageCodecV1ToV4.d.ts +11 -0
- package/dist/shared-tree-core/messageCodecV1ToV4.d.ts.map +1 -0
- package/dist/shared-tree-core/messageCodecV1ToV4.js +59 -0
- package/dist/shared-tree-core/messageCodecV1ToV4.js.map +1 -0
- package/dist/shared-tree-core/messageCodecV5.d.ts +11 -0
- package/dist/shared-tree-core/messageCodecV5.d.ts.map +1 -0
- package/dist/shared-tree-core/messageCodecV5.js +78 -0
- package/dist/shared-tree-core/messageCodecV5.js.map +1 -0
- package/dist/shared-tree-core/messageCodecs.d.ts.map +1 -1
- package/dist/shared-tree-core/messageCodecs.js +16 -47
- package/dist/shared-tree-core/messageCodecs.js.map +1 -1
- package/dist/shared-tree-core/{messageFormat.d.ts → messageFormatV1ToV4.d.ts} +1 -1
- package/dist/shared-tree-core/messageFormatV1ToV4.d.ts.map +1 -0
- package/dist/shared-tree-core/{messageFormat.js → messageFormatV1ToV4.js} +1 -1
- package/dist/shared-tree-core/messageFormatV1ToV4.js.map +1 -0
- package/dist/shared-tree-core/messageFormatV5.d.ts +42 -0
- package/dist/shared-tree-core/messageFormatV5.d.ts.map +1 -0
- package/dist/shared-tree-core/messageFormatV5.js +20 -0
- package/dist/shared-tree-core/messageFormatV5.js.map +1 -0
- package/dist/shared-tree-core/messageTypes.d.ts +12 -2
- package/dist/shared-tree-core/messageTypes.d.ts.map +1 -1
- package/dist/shared-tree-core/messageTypes.js.map +1 -1
- package/dist/shared-tree-core/sequenceIdUtils.d.ts +1 -1
- package/dist/shared-tree-core/sequenceIdUtils.d.ts.map +1 -1
- package/dist/shared-tree-core/sequenceIdUtils.js.map +1 -1
- package/dist/shared-tree-core/sharedTreeCore.d.ts +18 -5
- package/dist/shared-tree-core/sharedTreeCore.d.ts.map +1 -1
- package/dist/shared-tree-core/sharedTreeCore.js +175 -56
- package/dist/shared-tree-core/sharedTreeCore.js.map +1 -1
- package/dist/simple-tree/api/tree.d.ts +10 -0
- package/dist/simple-tree/api/tree.d.ts.map +1 -1
- package/dist/simple-tree/api/tree.js.map +1 -1
- package/lib/core/index.d.ts +1 -1
- package/lib/core/index.d.ts.map +1 -1
- package/lib/core/index.js +1 -1
- package/lib/core/index.js.map +1 -1
- package/lib/core/rebase/index.d.ts +1 -1
- package/lib/core/rebase/index.d.ts.map +1 -1
- package/lib/core/rebase/index.js +1 -1
- package/lib/core/rebase/index.js.map +1 -1
- package/lib/core/rebase/utils.d.ts +10 -0
- package/lib/core/rebase/utils.d.ts.map +1 -1
- package/lib/core/rebase/utils.js +18 -0
- package/lib/core/rebase/utils.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.d.ts.map +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/lib/shared-tree/schematizingTreeView.js +2 -2
- package/lib/shared-tree/schematizingTreeView.js.map +1 -1
- package/lib/shared-tree/sharedTree.d.ts +12 -2
- package/lib/shared-tree/sharedTree.d.ts.map +1 -1
- package/lib/shared-tree/sharedTree.js +72 -34
- package/lib/shared-tree/sharedTree.js.map +1 -1
- package/lib/shared-tree/treeCheckout.d.ts +13 -7
- package/lib/shared-tree/treeCheckout.d.ts.map +1 -1
- package/lib/shared-tree/treeCheckout.js +115 -85
- package/lib/shared-tree/treeCheckout.js.map +1 -1
- package/lib/shared-tree-core/branch.d.ts +3 -0
- package/lib/shared-tree-core/branch.d.ts.map +1 -1
- package/lib/shared-tree-core/branch.js.map +1 -1
- package/lib/shared-tree-core/branchIdCodec.d.ts +11 -0
- package/lib/shared-tree-core/branchIdCodec.d.ts.map +1 -0
- package/lib/shared-tree-core/branchIdCodec.js +13 -0
- package/lib/shared-tree-core/branchIdCodec.js.map +1 -0
- package/lib/shared-tree-core/editManager.d.ts +33 -63
- package/lib/shared-tree-core/editManager.d.ts.map +1 -1
- package/lib/shared-tree-core/editManager.js +437 -290
- package/lib/shared-tree-core/editManager.js.map +1 -1
- package/lib/shared-tree-core/editManagerCodecs.d.ts +1 -1
- package/lib/shared-tree-core/editManagerCodecs.d.ts.map +1 -1
- package/lib/shared-tree-core/editManagerCodecs.js +4 -93
- package/lib/shared-tree-core/editManagerCodecs.js.map +1 -1
- package/lib/shared-tree-core/editManagerCodecsCommons.d.ts +17 -0
- package/lib/shared-tree-core/editManagerCodecsCommons.d.ts.map +1 -0
- package/lib/shared-tree-core/editManagerCodecsCommons.js +134 -0
- package/lib/shared-tree-core/editManagerCodecsCommons.js.map +1 -0
- package/lib/shared-tree-core/editManagerCodecsV1toV4.d.ts +16 -0
- package/lib/shared-tree-core/editManagerCodecsV1toV4.d.ts.map +1 -0
- package/lib/shared-tree-core/editManagerCodecsV1toV4.js +35 -0
- package/lib/shared-tree-core/editManagerCodecsV1toV4.js.map +1 -0
- package/lib/shared-tree-core/editManagerCodecsV5.d.ts +16 -0
- package/lib/shared-tree-core/editManagerCodecsV5.d.ts.map +1 -0
- package/lib/shared-tree-core/editManagerCodecsV5.js +54 -0
- package/lib/shared-tree-core/editManagerCodecsV5.js.map +1 -0
- package/lib/shared-tree-core/{editManagerFormat.d.ts → editManagerFormatCommons.d.ts} +31 -7
- package/lib/shared-tree-core/editManagerFormatCommons.d.ts.map +1 -0
- package/lib/shared-tree-core/{editManagerFormat.js → editManagerFormatCommons.js} +10 -11
- package/lib/shared-tree-core/editManagerFormatCommons.js.map +1 -0
- package/lib/shared-tree-core/editManagerFormatV1toV4.d.ts +31 -0
- package/lib/shared-tree-core/editManagerFormatV1toV4.d.ts.map +1 -0
- package/lib/shared-tree-core/editManagerFormatV1toV4.js +20 -0
- package/lib/shared-tree-core/editManagerFormatV1toV4.js.map +1 -0
- package/lib/shared-tree-core/editManagerFormatV5.d.ts +62 -0
- package/lib/shared-tree-core/editManagerFormatV5.d.ts.map +1 -0
- package/lib/shared-tree-core/editManagerFormatV5.js +16 -0
- package/lib/shared-tree-core/editManagerFormatV5.js.map +1 -0
- package/lib/shared-tree-core/index.d.ts +3 -3
- package/lib/shared-tree-core/index.d.ts.map +1 -1
- package/lib/shared-tree-core/index.js.map +1 -1
- package/lib/shared-tree-core/messageCodecV1ToV4.d.ts +11 -0
- package/lib/shared-tree-core/messageCodecV1ToV4.d.ts.map +1 -0
- package/lib/shared-tree-core/messageCodecV1ToV4.js +55 -0
- package/lib/shared-tree-core/messageCodecV1ToV4.js.map +1 -0
- package/lib/shared-tree-core/messageCodecV5.d.ts +11 -0
- package/lib/shared-tree-core/messageCodecV5.d.ts.map +1 -0
- package/lib/shared-tree-core/messageCodecV5.js +74 -0
- package/lib/shared-tree-core/messageCodecV5.js.map +1 -0
- package/lib/shared-tree-core/messageCodecs.d.ts.map +1 -1
- package/lib/shared-tree-core/messageCodecs.js +17 -48
- package/lib/shared-tree-core/messageCodecs.js.map +1 -1
- package/lib/shared-tree-core/{messageFormat.d.ts → messageFormatV1ToV4.d.ts} +1 -1
- package/lib/shared-tree-core/messageFormatV1ToV4.d.ts.map +1 -0
- package/lib/shared-tree-core/{messageFormat.js → messageFormatV1ToV4.js} +1 -1
- package/lib/shared-tree-core/messageFormatV1ToV4.js.map +1 -0
- package/lib/shared-tree-core/messageFormatV5.d.ts +42 -0
- package/lib/shared-tree-core/messageFormatV5.d.ts.map +1 -0
- package/lib/shared-tree-core/messageFormatV5.js +16 -0
- package/lib/shared-tree-core/messageFormatV5.js.map +1 -0
- package/lib/shared-tree-core/messageTypes.d.ts +12 -2
- package/lib/shared-tree-core/messageTypes.d.ts.map +1 -1
- package/lib/shared-tree-core/messageTypes.js.map +1 -1
- package/lib/shared-tree-core/sequenceIdUtils.d.ts +1 -1
- package/lib/shared-tree-core/sequenceIdUtils.d.ts.map +1 -1
- package/lib/shared-tree-core/sequenceIdUtils.js.map +1 -1
- package/lib/shared-tree-core/sharedTreeCore.d.ts +18 -5
- package/lib/shared-tree-core/sharedTreeCore.d.ts.map +1 -1
- package/lib/shared-tree-core/sharedTreeCore.js +176 -57
- package/lib/shared-tree-core/sharedTreeCore.js.map +1 -1
- package/lib/simple-tree/api/tree.d.ts +10 -0
- package/lib/simple-tree/api/tree.d.ts.map +1 -1
- package/lib/simple-tree/api/tree.js.map +1 -1
- package/package.json +20 -20
- package/src/core/index.ts +1 -0
- package/src/core/rebase/index.ts +1 -0
- package/src/core/rebase/utils.ts +27 -0
- package/src/packageVersion.ts +1 -1
- package/src/shared-tree/schematizingTreeView.ts +2 -2
- package/src/shared-tree/sharedTree.ts +103 -46
- package/src/shared-tree/treeCheckout.ts +147 -98
- package/src/shared-tree-core/branch.ts +7 -0
- package/src/shared-tree-core/branchIdCodec.ts +28 -0
- package/src/shared-tree-core/editManager.ts +709 -437
- package/src/shared-tree-core/editManagerCodecs.ts +4 -164
- package/src/shared-tree-core/editManagerCodecsCommons.ts +236 -0
- package/src/shared-tree-core/editManagerCodecsV1toV4.ts +108 -0
- package/src/shared-tree-core/editManagerCodecsV5.ts +150 -0
- package/src/shared-tree-core/{editManagerFormat.ts → editManagerFormatCommons.ts} +17 -13
- package/src/shared-tree-core/editManagerFormatV1toV4.ts +42 -0
- package/src/shared-tree-core/editManagerFormatV5.ts +35 -0
- package/src/shared-tree-core/index.ts +3 -1
- package/src/shared-tree-core/messageCodecV1ToV4.ts +101 -0
- package/src/shared-tree-core/messageCodecV5.ts +128 -0
- package/src/shared-tree-core/messageCodecs.ts +16 -85
- package/src/shared-tree-core/messageFormatV5.ts +50 -0
- package/src/shared-tree-core/messageTypes.ts +15 -2
- package/src/shared-tree-core/sequenceIdUtils.ts +1 -1
- package/src/shared-tree-core/sharedTreeCore.ts +270 -84
- package/src/simple-tree/api/tree.ts +15 -0
- package/dist/shared-tree-core/editManagerFormat.d.ts.map +0 -1
- package/dist/shared-tree-core/editManagerFormat.js.map +0 -1
- package/dist/shared-tree-core/messageFormat.d.ts.map +0 -1
- package/dist/shared-tree-core/messageFormat.js.map +0 -1
- package/lib/shared-tree-core/editManagerFormat.d.ts.map +0 -1
- package/lib/shared-tree-core/editManagerFormat.js.map +0 -1
- package/lib/shared-tree-core/messageFormat.d.ts.map +0 -1
- package/lib/shared-tree-core/messageFormat.js.map +0 -1
- /package/src/shared-tree-core/{messageFormat.ts → messageFormatV1ToV4.ts} +0 -0
|
@@ -21,14 +21,19 @@ import {
|
|
|
21
21
|
} from "../core/index.js";
|
|
22
22
|
import { type Mutable, brand, getOrCreate, mapIterable } from "../util/index.js";
|
|
23
23
|
|
|
24
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
SharedTreeBranch,
|
|
26
|
+
type BranchTrimmingEvents,
|
|
27
|
+
onForkTransitive,
|
|
28
|
+
type BranchId,
|
|
29
|
+
} from "./branch.js";
|
|
25
30
|
import type {
|
|
26
31
|
Commit,
|
|
27
32
|
SeqNumber,
|
|
28
33
|
SequenceId,
|
|
29
34
|
SequencedCommit,
|
|
30
35
|
SummarySessionBranch,
|
|
31
|
-
} from "./
|
|
36
|
+
} from "./editManagerFormatCommons.js";
|
|
32
37
|
import {
|
|
33
38
|
getUpperBoundOfPreviousSequenceId,
|
|
34
39
|
equalSequenceIds,
|
|
@@ -40,6 +45,7 @@ import {
|
|
|
40
45
|
TelemetryEventBatcher,
|
|
41
46
|
type ITelemetryLoggerExt,
|
|
42
47
|
} from "@fluidframework/telemetry-utils/internal";
|
|
48
|
+
import type { Listenable } from "@fluidframework/core-interfaces";
|
|
43
49
|
|
|
44
50
|
export const minimumPossibleSequenceNumber: SeqNumber = brand(Number.MIN_SAFE_INTEGER);
|
|
45
51
|
const minimumPossibleSequenceId: SequenceId = {
|
|
@@ -71,42 +77,12 @@ export class EditManager<
|
|
|
71
77
|
> {
|
|
72
78
|
private readonly _events = createEmitter<BranchTrimmingEvents>();
|
|
73
79
|
|
|
74
|
-
|
|
75
|
-
private readonly trunk: SharedTreeBranch<TEditor, TChangeset>;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Records extra data associated with commits in the {@link trunk}.
|
|
79
|
-
* This does not include an entry for the {@link trunkBase}.
|
|
80
|
-
*/
|
|
81
|
-
private readonly trunkMetadata = new Map<
|
|
82
|
-
RevisionTag,
|
|
83
|
-
{ sequenceId: SequenceId; sessionId: SessionId }
|
|
84
|
-
>();
|
|
85
|
-
/**
|
|
86
|
-
* A map from a sequence id to the commit in the {@link trunk} which has that sequence id.
|
|
87
|
-
* This also includes an entry for the {@link trunkBase} which always has the lowest key in the map.
|
|
88
|
-
*/
|
|
89
|
-
private readonly sequenceMap = new BTree<SequenceId, GraphCommit<TChangeset>>(
|
|
90
|
-
undefined,
|
|
91
|
-
sequenceIdComparator,
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Branches are maintained to represent the local change list that the issuing client had
|
|
96
|
-
* at the time of submitting the latest known edit on the branch.
|
|
97
|
-
* This means the head commit of each branch is always in its original (non-rebased) form.
|
|
98
|
-
*/
|
|
99
|
-
private readonly peerLocalBranches: Map<SessionId, SharedTreeBranch<TEditor, TChangeset>> =
|
|
100
|
-
new Map();
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* This branch holds the changes made by this client which have not yet been confirmed as sequenced changes.
|
|
104
|
-
*/
|
|
105
|
-
public readonly localBranch: SharedTreeBranch<TEditor, TChangeset>;
|
|
80
|
+
private readonly sharedBranches = new Map<BranchId, SharedBranch<TEditor, TChangeset>>();
|
|
106
81
|
|
|
107
82
|
/**
|
|
108
|
-
* Tracks where on the trunk all registered branches are based.
|
|
109
|
-
*
|
|
83
|
+
* Tracks where on the trunk of the main branch all registered branches are based.
|
|
84
|
+
* Each key is the sequence id of a commit on the trunk,
|
|
85
|
+
* and the value is the set of all branches who have that commit as their common ancestor with the trunk.
|
|
110
86
|
*
|
|
111
87
|
* @remarks
|
|
112
88
|
* This does not include the local branch.
|
|
@@ -132,12 +108,6 @@ export class EditManager<
|
|
|
132
108
|
*/
|
|
133
109
|
private trunkBase: GraphCommit<TChangeset>;
|
|
134
110
|
|
|
135
|
-
/**
|
|
136
|
-
* The list of commits (from oldest to most recent) that are on the local branch but not on the trunk.
|
|
137
|
-
* When a local commit is sequenced, the first commit in this list shifted onto the tip of the trunk.
|
|
138
|
-
*/
|
|
139
|
-
private readonly localCommits: GraphCommit<TChangeset>[] = [];
|
|
140
|
-
|
|
141
111
|
private readonly telemetryEventBatcher:
|
|
142
112
|
| TelemetryEventBatcher<keyof RebaseStatsWithDuration>
|
|
143
113
|
| undefined;
|
|
@@ -156,7 +126,6 @@ export class EditManager<
|
|
|
156
126
|
revision: rootRevision,
|
|
157
127
|
change: changeFamily.rebaser.compose([]),
|
|
158
128
|
};
|
|
159
|
-
this.sequenceMap.set(minimumPossibleSequenceId, this.trunkBase);
|
|
160
129
|
|
|
161
130
|
if (logger !== undefined) {
|
|
162
131
|
this.telemetryEventBatcher = new TelemetryEventBatcher(
|
|
@@ -169,39 +138,28 @@ export class EditManager<
|
|
|
169
138
|
);
|
|
170
139
|
}
|
|
171
140
|
|
|
172
|
-
|
|
141
|
+
const mainTrunk = new SharedTreeBranch(
|
|
173
142
|
this.trunkBase,
|
|
174
143
|
changeFamily,
|
|
175
144
|
mintRevisionTag,
|
|
176
145
|
this._events,
|
|
177
146
|
this.telemetryEventBatcher,
|
|
178
147
|
);
|
|
179
|
-
this.localBranch = new SharedTreeBranch(
|
|
180
|
-
this.trunk.getHead(),
|
|
181
|
-
changeFamily,
|
|
182
|
-
mintRevisionTag,
|
|
183
|
-
this._events,
|
|
184
|
-
this.telemetryEventBatcher,
|
|
185
|
-
);
|
|
186
148
|
|
|
187
|
-
this.
|
|
188
|
-
if (event.type === "append") {
|
|
189
|
-
for (const commit of event.newCommits) {
|
|
190
|
-
this.localCommits.push(commit);
|
|
191
|
-
}
|
|
192
|
-
} else {
|
|
193
|
-
this.localCommits.length = 0;
|
|
194
|
-
findCommonAncestor(
|
|
195
|
-
[this.localBranch.getHead(), this.localCommits],
|
|
196
|
-
this.trunk.getHead(),
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
});
|
|
149
|
+
const mainBranch = this.createSharedBranch("main", undefined, undefined, mainTrunk);
|
|
200
150
|
|
|
201
151
|
// Track all forks of the local branch for purposes of trunk eviction. Unlike the local branch, they have
|
|
202
152
|
// an unknown lifetime and rebase frequency, so we can not make any assumptions about which trunk commits
|
|
203
153
|
// they require and therefore we monitor them explicitly.
|
|
204
|
-
onForkTransitive(
|
|
154
|
+
onForkTransitive(mainBranch.localBranch, (fork) => this.registerBranch(fork));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
public getLocalBranch(branchId: BranchId): SharedTreeBranch<TEditor, TChangeset> {
|
|
158
|
+
return this.getSharedBranch(branchId).localBranch;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private getSharedBranch(branchId: BranchId): SharedBranch<TEditor, TChangeset> {
|
|
162
|
+
return this.sharedBranches.get(branchId) ?? fail("Branch does not exist");
|
|
205
163
|
}
|
|
206
164
|
|
|
207
165
|
/**
|
|
@@ -224,13 +182,13 @@ export class EditManager<
|
|
|
224
182
|
const offAfterRebase = branch.events.on("afterChange", (args) => {
|
|
225
183
|
if (args.type === "rebase") {
|
|
226
184
|
this.trackBranch(branch);
|
|
227
|
-
this.
|
|
185
|
+
this.trimHistory();
|
|
228
186
|
}
|
|
229
187
|
});
|
|
230
188
|
// When the branch is disposed, update our branch set and trim the trunk
|
|
231
189
|
const offDispose = branch.events.on("dispose", () => {
|
|
232
190
|
this.untrackBranch(branch);
|
|
233
|
-
this.
|
|
191
|
+
this.trimHistory();
|
|
234
192
|
offBeforeRebase();
|
|
235
193
|
offAfterRebase();
|
|
236
194
|
offDispose();
|
|
@@ -238,10 +196,11 @@ export class EditManager<
|
|
|
238
196
|
}
|
|
239
197
|
|
|
240
198
|
private trackBranch(b: SharedTreeBranch<TEditor, TChangeset>): void {
|
|
199
|
+
const main = this.getSharedBranch("main");
|
|
241
200
|
const trunkCommit =
|
|
242
|
-
findCommonAncestor(
|
|
243
|
-
fail(0xad2 /* Expected branch to be related to
|
|
244
|
-
const sequenceId =
|
|
201
|
+
findCommonAncestor(main.trunk.getHead(), b.getHead()) ??
|
|
202
|
+
fail(0xad2 /* Expected branch to be related to main */);
|
|
203
|
+
const sequenceId = main.getCommitSequenceId(trunkCommit.revision);
|
|
245
204
|
const branches = getOrCreate(this.trunkBranches, sequenceId, () => new Set());
|
|
246
205
|
|
|
247
206
|
assert(!branches.has(b), 0x670 /* Branch was registered more than once */);
|
|
@@ -249,10 +208,11 @@ export class EditManager<
|
|
|
249
208
|
}
|
|
250
209
|
|
|
251
210
|
private untrackBranch(b: SharedTreeBranch<TEditor, TChangeset>): void {
|
|
211
|
+
const main = this.getSharedBranch("main");
|
|
252
212
|
const trunkCommit =
|
|
253
|
-
findCommonAncestor(
|
|
254
|
-
fail(0xad3 /* Expected branch to be related to
|
|
255
|
-
const sequenceId =
|
|
213
|
+
findCommonAncestor(main.trunk.getHead(), b.getHead()) ??
|
|
214
|
+
fail(0xad3 /* Expected branch to be related to main */);
|
|
215
|
+
const sequenceId = main.getCommitSequenceId(trunkCommit.revision);
|
|
256
216
|
const branches =
|
|
257
217
|
this.trunkBranches.get(sequenceId) ?? fail(0xad4 /* Expected branch to be tracked */);
|
|
258
218
|
|
|
@@ -263,60 +223,20 @@ export class EditManager<
|
|
|
263
223
|
}
|
|
264
224
|
|
|
265
225
|
/**
|
|
266
|
-
*
|
|
267
|
-
* @param sequenceId - The sequence id of the new trunk commit
|
|
268
|
-
* @remarks This method is a performance optimization for the scenario where this client receives its own change back after sequencing.
|
|
269
|
-
* 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.
|
|
270
|
-
* 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.
|
|
271
|
-
* 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.
|
|
272
|
-
* 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.
|
|
226
|
+
* Return the sequenced number of the latest sequenced change.
|
|
273
227
|
*/
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
const previousSequenceId = this.getCommitSequenceId(this.trunk.getHead());
|
|
284
|
-
this.pushGraphCommitToTrunk(sequenceId, firstLocalCommit, this.localSessionId);
|
|
285
|
-
|
|
286
|
-
// Next, we need to update the sequence IDs that our local branches (user's branches, not peer branches) are associated with.
|
|
287
|
-
// 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)
|
|
288
|
-
// 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.
|
|
289
|
-
// Intuitively, this makes sense because:
|
|
290
|
-
// 1. The trunk's head just advanced forward by some (sequence) amount.
|
|
291
|
-
// 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).
|
|
292
|
-
// 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.
|
|
293
|
-
// 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).
|
|
294
|
-
const currentBranches = this.trunkBranches.get(previousSequenceId);
|
|
295
|
-
if (currentBranches !== undefined) {
|
|
296
|
-
const newBranches = getOrCreate(this.trunkBranches, sequenceId, () => new Set());
|
|
297
|
-
for (const branch of currentBranches) {
|
|
298
|
-
// 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).
|
|
299
|
-
// We validate this by checking if the branch's head is a descendant of the local commit that we just pushed.
|
|
300
|
-
if (findAncestor(branch.getHead(), (c) => c === firstLocalCommit) !== undefined) {
|
|
301
|
-
newBranches.add(branch);
|
|
302
|
-
currentBranches.delete(branch);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
// Clean up our trunk branches map by removing any empty sets.
|
|
306
|
-
if (currentBranches.size === 0) {
|
|
307
|
-
this.trunkBranches.delete(previousSequenceId);
|
|
308
|
-
}
|
|
309
|
-
if (newBranches.size === 0) {
|
|
310
|
-
this.trunkBranches.delete(sequenceId);
|
|
228
|
+
public getLatestSequenceNumber(): SeqNumber | undefined {
|
|
229
|
+
let maxSequenceNumber: SeqNumber | undefined;
|
|
230
|
+
for (const branch of this.sharedBranches.values()) {
|
|
231
|
+
const branchMax = branch.getCommitSequenceId(
|
|
232
|
+
branch.trunk.getHead().revision,
|
|
233
|
+
).sequenceNumber;
|
|
234
|
+
if (maxSequenceNumber === undefined || maxSequenceNumber < branchMax) {
|
|
235
|
+
maxSequenceNumber = branchMax;
|
|
311
236
|
}
|
|
312
237
|
}
|
|
313
|
-
}
|
|
314
238
|
|
|
315
|
-
|
|
316
|
-
* 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.
|
|
317
|
-
*/
|
|
318
|
-
public getSequenceNumber(trunkCommit: GraphCommit<TChangeset>): SeqNumber | undefined {
|
|
319
|
-
return this.trunkMetadata.get(trunkCommit.revision)?.sequenceId.sequenceNumber;
|
|
239
|
+
return maxSequenceNumber;
|
|
320
240
|
}
|
|
321
241
|
|
|
322
242
|
/**
|
|
@@ -328,7 +248,7 @@ export class EditManager<
|
|
|
328
248
|
*/
|
|
329
249
|
public advanceMinimumSequenceNumber(
|
|
330
250
|
minimumSequenceNumber: SeqNumber,
|
|
331
|
-
|
|
251
|
+
trimHistory = true,
|
|
332
252
|
): void {
|
|
333
253
|
if (minimumSequenceNumber === this.minimumSequenceNumber) {
|
|
334
254
|
return;
|
|
@@ -340,8 +260,8 @@ export class EditManager<
|
|
|
340
260
|
);
|
|
341
261
|
|
|
342
262
|
this.minimumSequenceNumber = minimumSequenceNumber;
|
|
343
|
-
if (
|
|
344
|
-
this.
|
|
263
|
+
if (trimHistory) {
|
|
264
|
+
this.trimHistory();
|
|
345
265
|
}
|
|
346
266
|
}
|
|
347
267
|
|
|
@@ -349,7 +269,7 @@ export class EditManager<
|
|
|
349
269
|
* Examines the latest known minimum sequence number and the trunk bases of any registered branches to determine
|
|
350
270
|
* if any commits on the trunk are unreferenced and unneeded for future computation; those found are evicted from the trunk.
|
|
351
271
|
*/
|
|
352
|
-
private
|
|
272
|
+
private trimHistory(): void {
|
|
353
273
|
/** The sequence id of the most recent commit on the trunk that will be trimmed */
|
|
354
274
|
let trunkTailSequenceId: SequenceId = {
|
|
355
275
|
sequenceNumber: this.minimumSequenceNumber,
|
|
@@ -369,180 +289,98 @@ export class EditManager<
|
|
|
369
289
|
);
|
|
370
290
|
}
|
|
371
291
|
|
|
372
|
-
const
|
|
292
|
+
const mainBranch = this.getSharedBranch("main");
|
|
293
|
+
|
|
294
|
+
const [sequenceId, latestEvicted] = mainBranch.getClosestTrunkCommit(
|
|
373
295
|
maxSequenceId(
|
|
374
296
|
trunkTailSequenceId,
|
|
375
|
-
|
|
297
|
+
mainBranch.sequenceIdToCommit.minKey() ?? minimumPossibleSequenceId,
|
|
376
298
|
),
|
|
377
299
|
);
|
|
378
300
|
|
|
379
301
|
// Don't do any work if the commit found by the search is already the tail of the trunk
|
|
380
|
-
if (latestEvicted
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
for (const [, branch] of this.peerLocalBranches) {
|
|
384
|
-
branch.rebaseOnto(this.trunk, latestEvicted);
|
|
385
|
-
}
|
|
302
|
+
if (latestEvicted === this.trunkBase) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
386
305
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
306
|
+
// This mutation is a performance hack. If commits are truly immutable, then changing the trunk's tail requires
|
|
307
|
+
// regenerating the entire commit graph. Instead, we can simply chop off the tail like this if we're certain
|
|
308
|
+
// that there are no outstanding references to any of the commits being removed (other than the references via
|
|
309
|
+
// the trunk). The peer branches have been rebased to the head of the trunk, the local branch is already rebased
|
|
310
|
+
// to the head of the trunk, and all other branches are tracked by `trunkBranches` and known to be ahead of or at
|
|
311
|
+
// `newTrunkBase`. Therefore, no branches should have unique references to any of the commits being evicted here.
|
|
312
|
+
// We mutate the most recent of the evicted commits to become the new trunk base. That way, any other commits that
|
|
313
|
+
// have parent pointers to the latest evicted commit will stay linked, even though that it is no longer part of the trunk.
|
|
314
|
+
const newTrunkBase = latestEvicted as Mutable<typeof latestEvicted>;
|
|
315
|
+
|
|
316
|
+
// collect the revisions that will be trimmed to send as part of the branch trimmed event
|
|
317
|
+
const trimmedCommits = getPathFromBase(newTrunkBase, this.trunkBase);
|
|
318
|
+
const trimmedRevisions = trimmedCommits.map((c) => c.revision);
|
|
319
|
+
|
|
320
|
+
// The minimum sequence number informs us that all peer branches are at least caught up to the tail commit,
|
|
321
|
+
// so rebase them accordingly. This is necessary to prevent peer branches from referencing any evicted commits.
|
|
322
|
+
mainBranch.trimHistory(latestEvicted, sequenceId);
|
|
323
|
+
|
|
324
|
+
// Only the last trimmed commit, which is the new trunk base, should remain accessible.
|
|
325
|
+
for (const commit of trimmedCommits.slice(0, -1)) {
|
|
326
|
+
Reflect.defineProperty(commit, "change", {
|
|
327
|
+
get: () =>
|
|
328
|
+
assert(false, 0xa5e /* Should not access 'change' property of an evicted commit */),
|
|
329
|
+
});
|
|
330
|
+
Reflect.defineProperty(commit, "revision", {
|
|
331
|
+
get: () =>
|
|
413
332
|
assert(
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
)
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
false,
|
|
422
|
-
0xa5e /* Should not access 'change' property of an evicted commit */,
|
|
423
|
-
),
|
|
424
|
-
});
|
|
425
|
-
Reflect.defineProperty(commit, "revision", {
|
|
426
|
-
get: () =>
|
|
427
|
-
assert(
|
|
428
|
-
false,
|
|
429
|
-
0xa5f /* Should not access 'revision' property of an evicted commit */,
|
|
430
|
-
),
|
|
431
|
-
});
|
|
432
|
-
Reflect.defineProperty(commit, "parent", {
|
|
433
|
-
get: () =>
|
|
434
|
-
assert(
|
|
435
|
-
false,
|
|
436
|
-
0xa60 /* Should not access 'parent' property of an evicted commit */,
|
|
437
|
-
),
|
|
438
|
-
});
|
|
439
|
-
return { delete: true };
|
|
440
|
-
}
|
|
333
|
+
false,
|
|
334
|
+
0xa5f /* Should not access 'revision' property of an evicted commit */,
|
|
335
|
+
),
|
|
336
|
+
});
|
|
337
|
+
Reflect.defineProperty(commit, "parent", {
|
|
338
|
+
get: () =>
|
|
339
|
+
assert(false, 0xa60 /* Should not access 'parent' property of an evicted commit */),
|
|
441
340
|
});
|
|
341
|
+
}
|
|
442
342
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
0x744 /* The size of the sequenceMap must have one element more than the trunk */,
|
|
447
|
-
);
|
|
448
|
-
assert(
|
|
449
|
-
this.trunkMetadata.size === trunkSize,
|
|
450
|
-
0x745 /* The size of the trunkMetadata must be the same as the trunk */,
|
|
451
|
-
);
|
|
343
|
+
// Dropping the parent field removes (transitively) all references to the evicted commits so they can be garbage collected.
|
|
344
|
+
delete newTrunkBase.parent;
|
|
345
|
+
this.trunkBase = newTrunkBase;
|
|
452
346
|
|
|
453
|
-
|
|
454
|
-
}
|
|
347
|
+
this._events.emit("ancestryTrimmed", trimmedRevisions);
|
|
455
348
|
}
|
|
456
349
|
|
|
457
350
|
public isEmpty(): boolean {
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
351
|
+
for (const branch of this.sharedBranches.values()) {
|
|
352
|
+
if (!branch.isEmpty(this.trunkBase)) {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return this.minimumSequenceNumber === minimumPossibleSequenceNumber;
|
|
464
358
|
}
|
|
465
359
|
|
|
466
360
|
public getSummaryData(): SummaryData<TChangeset> {
|
|
467
|
-
// The assert below is acceptable at present because summarization only ever occurs on a client with no
|
|
468
|
-
// local/in-flight changes.
|
|
469
|
-
// In the future we may wish to relax this constraint. For that to work, the current implementation of
|
|
470
|
-
// `EditManager` would have to be amended in one of two ways:
|
|
471
|
-
// A) Changes made by the local session should be represented by a branch in `EditManager.branches`.
|
|
472
|
-
// B) The contents of such a branch should be computed on demand based on the trunk.
|
|
473
|
-
// Note that option (A) would be a simple change to `addSequencedChanges` whereas (B) would likely require
|
|
474
|
-
// rebasing trunk changes over the inverse of trunk changes.
|
|
475
|
-
assert(
|
|
476
|
-
this.localBranch.getHead() === this.trunk.getHead(),
|
|
477
|
-
0x428 /* Clients with local changes cannot be used to generate summaries */,
|
|
478
|
-
);
|
|
479
|
-
|
|
480
361
|
// Trimming the trunk before serializing ensures that the trunk data in the summary is as minimal as possible.
|
|
481
|
-
this.
|
|
482
|
-
|
|
483
|
-
let oldestCommitInCollabWindow = this.getClosestTrunkCommit(this.minimumSequenceNumber)[1];
|
|
484
|
-
assert(
|
|
485
|
-
oldestCommitInCollabWindow.parent !== undefined ||
|
|
486
|
-
oldestCommitInCollabWindow === this.trunkBase,
|
|
487
|
-
0x8c7 /* Expected oldest commit in collab window to have a parent or be the trunk base */,
|
|
488
|
-
);
|
|
489
|
-
|
|
490
|
-
// Path construction is exclusive, so we need to use the parent of the oldest commit in the window if it exists
|
|
491
|
-
oldestCommitInCollabWindow =
|
|
492
|
-
oldestCommitInCollabWindow.parent ?? oldestCommitInCollabWindow;
|
|
362
|
+
this.trimHistory();
|
|
493
363
|
|
|
494
|
-
const
|
|
495
|
-
(
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
364
|
+
const minSeqNumberToSummarize: SequenceId = {
|
|
365
|
+
sequenceNumber: brand(this.minimumSequenceNumber + 1),
|
|
366
|
+
};
|
|
367
|
+
let minBaseSeqId: SequenceId = minSeqNumberToSummarize;
|
|
368
|
+
const mainBranch = this.getSharedBranch("main");
|
|
369
|
+
const branches = new Map<BranchId, SharedBranchSummaryData<TChangeset>>();
|
|
370
|
+
for (const [branchId, branch] of this.sharedBranches) {
|
|
371
|
+
if (branchId !== "main") {
|
|
372
|
+
const branchSummary = branch.getSummaryData(
|
|
373
|
+
minSeqNumberToSummarize,
|
|
374
|
+
this.trunkBase.revision,
|
|
499
375
|
);
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
};
|
|
509
|
-
if (metadata.sequenceId.indexInBatch !== undefined) {
|
|
510
|
-
commit.indexInBatch = metadata.sequenceId.indexInBatch;
|
|
511
|
-
}
|
|
512
|
-
return commit;
|
|
513
|
-
},
|
|
514
|
-
);
|
|
515
|
-
|
|
516
|
-
const peerLocalBranches = new Map<SessionId, SummarySessionBranch<TChangeset>>(
|
|
517
|
-
mapIterable(this.peerLocalBranches.entries(), ([sessionId, branch]) => {
|
|
518
|
-
const branchPath: GraphCommit<TChangeset>[] = [];
|
|
519
|
-
const ancestor =
|
|
520
|
-
findCommonAncestor([branch.getHead(), branchPath], this.trunk.getHead()) ??
|
|
521
|
-
fail(0xad6 /* Expected branch to be based on trunk */);
|
|
522
|
-
|
|
523
|
-
const base = ancestor === this.trunkBase ? rootRevision : ancestor.revision;
|
|
524
|
-
return [
|
|
525
|
-
sessionId,
|
|
526
|
-
{
|
|
527
|
-
base,
|
|
528
|
-
commits: branchPath.map((c) => {
|
|
529
|
-
assert(
|
|
530
|
-
c !== this.trunkBase,
|
|
531
|
-
0xa62 /* Serialized branch should not include the trunk base */,
|
|
532
|
-
);
|
|
533
|
-
const commit: Commit<TChangeset> = {
|
|
534
|
-
change: c.change,
|
|
535
|
-
revision: c.revision,
|
|
536
|
-
sessionId,
|
|
537
|
-
};
|
|
538
|
-
return commit;
|
|
539
|
-
}),
|
|
540
|
-
},
|
|
541
|
-
];
|
|
542
|
-
}),
|
|
543
|
-
);
|
|
544
|
-
|
|
545
|
-
return { trunk, peerLocalBranches };
|
|
376
|
+
branches.set(branchId, branchSummary);
|
|
377
|
+
assert(branchSummary.base !== undefined, "Branch summary must have a base");
|
|
378
|
+
const baseSequenceId = mainBranch.getCommitSequenceId(branchSummary.base);
|
|
379
|
+
minBaseSeqId = minSequenceId(minBaseSeqId, baseSequenceId);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
const mainSummary = mainBranch.getSummaryData(minBaseSeqId, this.trunkBase.revision);
|
|
383
|
+
return { main: mainSummary, branches, originator: this.localSessionId };
|
|
546
384
|
}
|
|
547
385
|
|
|
548
386
|
public loadSummaryData(data: SummaryData<TChangeset>): void {
|
|
@@ -554,76 +392,40 @@ export class EditManager<
|
|
|
554
392
|
// when hydrating the peer branches below
|
|
555
393
|
const trunkRevisionCache = new Map<RevisionTag, GraphCommit<TChangeset>>();
|
|
556
394
|
trunkRevisionCache.set(this.trunkBase.revision, this.trunkBase);
|
|
557
|
-
this.
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
this.sequenceMap.set(sequenceId, commit);
|
|
570
|
-
this.trunkMetadata.set(c.revision, {
|
|
571
|
-
sequenceId,
|
|
572
|
-
sessionId: c.sessionId,
|
|
573
|
-
});
|
|
574
|
-
trunkRevisionCache.set(c.revision, commit);
|
|
575
|
-
return commit;
|
|
576
|
-
}, this.trunkBase),
|
|
577
|
-
);
|
|
578
|
-
|
|
579
|
-
this.localBranch.setHead(this.trunk.getHead());
|
|
580
|
-
|
|
581
|
-
for (const [sessionId, branch] of data.peerLocalBranches) {
|
|
582
|
-
const commit =
|
|
583
|
-
trunkRevisionCache.get(branch.base) ??
|
|
584
|
-
fail(0xad7 /* Expected summary branch to be based off of a revision in the trunk */);
|
|
585
|
-
|
|
586
|
-
this.peerLocalBranches.set(
|
|
587
|
-
sessionId,
|
|
588
|
-
new SharedTreeBranch(
|
|
589
|
-
branch.commits.reduce(mintCommit, commit),
|
|
590
|
-
this.changeFamily,
|
|
591
|
-
this.mintRevisionTag,
|
|
592
|
-
),
|
|
593
|
-
);
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
private getCommitSequenceId(trunkCommitOrTrunkBase: GraphCommit<TChangeset>): SequenceId {
|
|
598
|
-
const id = this.trunkMetadata.get(trunkCommitOrTrunkBase.revision)?.sequenceId;
|
|
599
|
-
if (id === undefined) {
|
|
600
|
-
assert(
|
|
601
|
-
trunkCommitOrTrunkBase === this.trunkBase,
|
|
602
|
-
0xa63 /* Commit must be either be on the trunk or be the trunk base */,
|
|
603
|
-
);
|
|
604
|
-
return minimumPossibleSequenceId;
|
|
395
|
+
const mainBranch = this.sharedBranches.get("main") ?? fail("Main branch must exist");
|
|
396
|
+
mainBranch.loadSummaryData(data.main, trunkRevisionCache);
|
|
397
|
+
if (data.branches !== undefined) {
|
|
398
|
+
for (const [branchId, branchData] of data.branches) {
|
|
399
|
+
const branch = this.createSharedBranch(
|
|
400
|
+
branchId,
|
|
401
|
+
branchData.session,
|
|
402
|
+
mainBranch,
|
|
403
|
+
mainBranch.trunk.fork(),
|
|
404
|
+
);
|
|
405
|
+
branch.loadSummaryData(branchData, trunkRevisionCache);
|
|
406
|
+
}
|
|
605
407
|
}
|
|
606
|
-
return id;
|
|
607
408
|
}
|
|
608
409
|
|
|
609
|
-
public
|
|
610
|
-
return this.
|
|
410
|
+
public getTrunkHead(branchId: BranchId): GraphCommit<TChangeset> {
|
|
411
|
+
return this.getSharedBranch(branchId).trunk.getHead();
|
|
611
412
|
}
|
|
612
413
|
|
|
613
|
-
public
|
|
614
|
-
return
|
|
414
|
+
public getTrunkChanges(branchId: BranchId): TChangeset[] {
|
|
415
|
+
return this.getTrunkCommits(branchId).map((c) => c.change);
|
|
615
416
|
}
|
|
616
417
|
|
|
617
|
-
public
|
|
618
|
-
return this.
|
|
418
|
+
public getTrunkCommits(branchId: BranchId): GraphCommit<TChangeset>[] {
|
|
419
|
+
return getPathFromBase(this.getTrunkHead(branchId), this.trunkBase);
|
|
619
420
|
}
|
|
620
421
|
|
|
621
|
-
public getLocalChanges(): readonly TChangeset[] {
|
|
622
|
-
return this.getLocalCommits().map((c) => c.change);
|
|
422
|
+
public getLocalChanges(branchId: BranchId): readonly TChangeset[] {
|
|
423
|
+
return this.getLocalCommits(branchId).map((c) => c.change);
|
|
623
424
|
}
|
|
624
425
|
|
|
625
|
-
public getLocalCommits(): readonly GraphCommit<TChangeset>[] {
|
|
626
|
-
|
|
426
|
+
public getLocalCommits(branchId: BranchId): readonly GraphCommit<TChangeset>[] {
|
|
427
|
+
const branch = this.getSharedBranch(branchId);
|
|
428
|
+
return branch.getLocalCommits();
|
|
627
429
|
}
|
|
628
430
|
|
|
629
431
|
/**
|
|
@@ -636,15 +438,67 @@ export class EditManager<
|
|
|
636
438
|
*/
|
|
637
439
|
public getLongestBranchLength(): number {
|
|
638
440
|
let max = 0;
|
|
639
|
-
const
|
|
640
|
-
|
|
641
|
-
const branchPath = getPathFromBase(branch.getHead(), trunkHead);
|
|
642
|
-
if (branchPath.length > max) {
|
|
643
|
-
max = branchPath.length;
|
|
644
|
-
}
|
|
441
|
+
for (const branch of this.sharedBranches.values()) {
|
|
442
|
+
max = Math.max(max, branch.getLongestBranchLength());
|
|
645
443
|
}
|
|
646
|
-
|
|
647
|
-
|
|
444
|
+
return max;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
public sequenceBranchCreation(
|
|
448
|
+
sessionId: SessionId,
|
|
449
|
+
referenceSequenceNumber: SeqNumber,
|
|
450
|
+
branchId: BranchId,
|
|
451
|
+
): void {
|
|
452
|
+
if (sessionId === this.localSessionId) {
|
|
453
|
+
assert(this.sharedBranches.has(branchId), "Expected branch to already exist");
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const mainBranch = this.getSharedBranch("main");
|
|
458
|
+
const branchTrunk = mainBranch.rebasePeer(sessionId, referenceSequenceNumber).fork();
|
|
459
|
+
|
|
460
|
+
const sharedBranch = this.createSharedBranch(branchId, sessionId, mainBranch, branchTrunk);
|
|
461
|
+
this.registerBranch(sharedBranch.localBranch);
|
|
462
|
+
onForkTransitive(sharedBranch.localBranch, (fork) => this.registerBranch(fork));
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
public addBranch(branchId: BranchId): void {
|
|
466
|
+
const main = this.getSharedBranch("main") ?? fail("Main branch must exist");
|
|
467
|
+
this.createSharedBranch(
|
|
468
|
+
branchId,
|
|
469
|
+
this.localSessionId,
|
|
470
|
+
main,
|
|
471
|
+
this.getLocalBranch("main").fork(),
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
public removeBranch(branchId: BranchId): void {
|
|
476
|
+
assert(branchId !== "main", "Cannot remove main branch");
|
|
477
|
+
const hadBranch = this.sharedBranches.delete(branchId);
|
|
478
|
+
assert(hadBranch, "Expected branch to exist");
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
private createSharedBranch(
|
|
482
|
+
branchId: BranchId,
|
|
483
|
+
sessionId: SessionId | undefined,
|
|
484
|
+
parent: SharedBranch<TEditor, TChangeset> | undefined,
|
|
485
|
+
branch: SharedTreeBranch<TEditor, TChangeset>,
|
|
486
|
+
): SharedBranch<TEditor, TChangeset> {
|
|
487
|
+
const sharedBranch = new SharedBranch(
|
|
488
|
+
parent,
|
|
489
|
+
branch,
|
|
490
|
+
branchId,
|
|
491
|
+
sessionId,
|
|
492
|
+
minimumPossibleSequenceId,
|
|
493
|
+
this.changeFamily,
|
|
494
|
+
this.mintRevisionTag,
|
|
495
|
+
this._events,
|
|
496
|
+
this.telemetryEventBatcher,
|
|
497
|
+
);
|
|
498
|
+
|
|
499
|
+
assert(!this.sharedBranches.has(branchId), "A branch with this ID already exists");
|
|
500
|
+
this.sharedBranches.set(branchId, sharedBranch);
|
|
501
|
+
return sharedBranch;
|
|
648
502
|
}
|
|
649
503
|
|
|
650
504
|
/* eslint-disable jsdoc/check-indentation */
|
|
@@ -663,60 +517,225 @@ export class EditManager<
|
|
|
663
517
|
sessionId: SessionId,
|
|
664
518
|
sequenceNumber: SeqNumber,
|
|
665
519
|
referenceSequenceNumber: SeqNumber,
|
|
520
|
+
branchId: BranchId = "main",
|
|
666
521
|
): void {
|
|
667
522
|
assert(newCommits.length > 0, 0xad8 /* Expected at least one sequenced change */);
|
|
668
523
|
assert(
|
|
669
524
|
sequenceNumber > this.minimumSequenceNumber,
|
|
670
525
|
0x713 /* Expected change sequence number to exceed the last known minimum sequence number */,
|
|
671
526
|
);
|
|
672
|
-
assert(
|
|
673
|
-
sequenceNumber >= // This is ">=", not ">" because changes in the same batch will have the same sequence number
|
|
674
|
-
(this.sequenceMap.maxKey()?.sequenceNumber ?? minimumPossibleSequenceNumber),
|
|
675
|
-
0xa64 /* Attempted to sequence change with an outdated sequence number */,
|
|
676
|
-
);
|
|
677
527
|
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
const
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
528
|
+
const branch = this.getSharedBranch(branchId);
|
|
529
|
+
|
|
530
|
+
const onSequenceLocalCommit = (
|
|
531
|
+
commit: GraphCommit<TChangeset>,
|
|
532
|
+
sequenceId: SequenceId,
|
|
533
|
+
previousSequenceId: SequenceId,
|
|
534
|
+
): void => {
|
|
535
|
+
// Next, we need to update the sequence IDs that our local branches (user's branches, not peer branches) are associated with.
|
|
536
|
+
// 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)
|
|
537
|
+
// 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.
|
|
538
|
+
// Intuitively, this makes sense because:
|
|
539
|
+
// 1. The trunk's head just advanced forward by some (sequence) amount.
|
|
540
|
+
// 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).
|
|
541
|
+
// 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.
|
|
542
|
+
// 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).
|
|
543
|
+
const currentBranches = this.trunkBranches.get(previousSequenceId);
|
|
544
|
+
if (currentBranches !== undefined) {
|
|
545
|
+
const newBranches = getOrCreate(this.trunkBranches, sequenceId, () => new Set());
|
|
546
|
+
for (const forkedBranch of currentBranches) {
|
|
547
|
+
// 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).
|
|
548
|
+
// We validate this by checking if the branch's head is a descendant of the local commit that we just pushed.
|
|
549
|
+
if (findAncestor(forkedBranch.getHead(), (c) => c === commit) !== undefined) {
|
|
550
|
+
newBranches.add(forkedBranch);
|
|
551
|
+
currentBranches.delete(forkedBranch);
|
|
692
552
|
}
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
for (const _ of newCommits) {
|
|
702
|
-
this.fastForwardNextLocalCommit(nextSequenceId);
|
|
703
|
-
nextSequenceId = getNextSequenceId(nextSequenceId);
|
|
553
|
+
}
|
|
554
|
+
// Clean up our trunk branches map by removing any empty sets.
|
|
555
|
+
if (currentBranches.size === 0) {
|
|
556
|
+
this.trunkBranches.delete(previousSequenceId);
|
|
557
|
+
}
|
|
558
|
+
if (newBranches.size === 0) {
|
|
559
|
+
this.trunkBranches.delete(sequenceId);
|
|
560
|
+
}
|
|
704
561
|
}
|
|
705
|
-
|
|
706
|
-
}
|
|
562
|
+
};
|
|
707
563
|
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
// trunk up to the base revision. This will be a no-op if the sending client has not advanced since the last
|
|
712
|
-
// time we received an edit from it
|
|
713
|
-
const [, baseRevisionInTrunk] = this.getClosestTrunkCommit(referenceSequenceNumber);
|
|
714
|
-
const peerLocalBranch = getOrCreate(
|
|
715
|
-
this.peerLocalBranches,
|
|
564
|
+
const areLocalCommits = sessionId === this.localSessionId;
|
|
565
|
+
branch.addSequencedChanges(
|
|
566
|
+
newCommits,
|
|
716
567
|
sessionId,
|
|
717
|
-
|
|
568
|
+
sequenceNumber,
|
|
569
|
+
areLocalCommits,
|
|
570
|
+
referenceSequenceNumber,
|
|
571
|
+
onSequenceLocalCommit,
|
|
718
572
|
);
|
|
719
|
-
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
public findLocalCommit(
|
|
576
|
+
branchId: BranchId,
|
|
577
|
+
revision: RevisionTag,
|
|
578
|
+
): [commit: GraphCommit<TChangeset>, commitsAfter: GraphCommit<TChangeset>[]] {
|
|
579
|
+
const commits: GraphCommit<TChangeset>[] = [];
|
|
580
|
+
const commit = findAncestor(
|
|
581
|
+
[this.getSharedBranch(branchId).localBranch.getHead(), commits],
|
|
582
|
+
(c) => c.revision === revision,
|
|
583
|
+
);
|
|
584
|
+
assert(commit !== undefined, 0x599 /* Expected local branch to contain revision */);
|
|
585
|
+
return [commit, commits];
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* The in-memory data that summaries contain
|
|
591
|
+
*/
|
|
592
|
+
export interface SummaryData<TChangeset> {
|
|
593
|
+
readonly originator?: SessionId;
|
|
594
|
+
readonly main: SharedBranchSummaryData<TChangeset>;
|
|
595
|
+
readonly branches?: ReadonlyMap<BranchId, SharedBranchSummaryData<TChangeset>>;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
export interface SharedBranchSummaryData<TChangeset> {
|
|
599
|
+
readonly id?: BranchId;
|
|
600
|
+
readonly name?: string;
|
|
601
|
+
readonly session?: SessionId;
|
|
602
|
+
readonly author?: string;
|
|
603
|
+
readonly base?: RevisionTag;
|
|
604
|
+
readonly trunk: readonly SequencedCommit<TChangeset>[];
|
|
605
|
+
readonly peerLocalBranches: ReadonlyMap<SessionId, SummarySessionBranch<TChangeset>>;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Gets the path from the base of a branch to its head.
|
|
610
|
+
*/
|
|
611
|
+
function getPathFromBase<TCommit extends { parent?: TCommit }>(
|
|
612
|
+
branchHead: TCommit,
|
|
613
|
+
baseBranchHead: TCommit,
|
|
614
|
+
): TCommit[] {
|
|
615
|
+
const path: TCommit[] = [];
|
|
616
|
+
assert(
|
|
617
|
+
findCommonAncestor([branchHead, path], baseBranchHead) !== undefined,
|
|
618
|
+
0x573 /* Expected branches to be related */,
|
|
619
|
+
);
|
|
620
|
+
return path;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
class SharedBranch<TEditor extends ChangeFamilyEditor, TChangeset> {
|
|
624
|
+
/**
|
|
625
|
+
* This branch holds the changes made by this client which have not yet been confirmed as sequenced changes.
|
|
626
|
+
*/
|
|
627
|
+
public readonly localBranch: SharedTreeBranch<TEditor, TChangeset>;
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Branches are maintained to represent the local change list that the issuing client had
|
|
631
|
+
* at the time of submitting the latest known edit on the branch.
|
|
632
|
+
* This means the head commit of each branch is always in its original (non-rebased) form.
|
|
633
|
+
*/
|
|
634
|
+
private readonly peerLocalBranches: Map<SessionId, SharedTreeBranch<TEditor, TChangeset>> =
|
|
635
|
+
new Map();
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* A map from a sequence id to the commit which has that sequence id.
|
|
639
|
+
* This also includes an entry for the {@link trunkBase} which always has the lowest key in the map.
|
|
640
|
+
*/
|
|
641
|
+
public readonly sequenceIdToCommit = new BTree<SequenceId, GraphCommit<TChangeset>>(
|
|
642
|
+
undefined,
|
|
643
|
+
sequenceIdComparator,
|
|
644
|
+
);
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* The list of commits (from oldest to most recent) that are on the local branch but not on the trunk.
|
|
648
|
+
* When a local commit is sequenced, the first commit in this list shifted onto the tip of the trunk.
|
|
649
|
+
*/
|
|
650
|
+
private readonly localCommits: GraphCommit<TChangeset>[] = [];
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* Records extra data associated with sequenced commits.
|
|
654
|
+
* This does not include an entry for the {@link trunkBase}.
|
|
655
|
+
*/
|
|
656
|
+
private readonly commitMetadata = new Map<
|
|
657
|
+
RevisionTag,
|
|
658
|
+
{ sequenceId: SequenceId; sessionId: SessionId }
|
|
659
|
+
>();
|
|
660
|
+
|
|
661
|
+
public constructor(
|
|
662
|
+
public readonly parentBranch: SharedBranch<TEditor, TChangeset> | undefined,
|
|
663
|
+
public readonly trunk: SharedTreeBranch<TEditor, TChangeset>,
|
|
664
|
+
private readonly id: BranchId,
|
|
665
|
+
private readonly sessionId: SessionId | undefined,
|
|
666
|
+
baseCommitSequenceId: SequenceId,
|
|
667
|
+
private readonly changeFamily: ChangeFamily<TEditor, TChangeset>,
|
|
668
|
+
private readonly mintRevisionTag: () => RevisionTag,
|
|
669
|
+
branchTrimmer: Listenable<BranchTrimmingEvents>,
|
|
670
|
+
telemetryEventBatcher: TelemetryEventBatcher<keyof RebaseStatsWithDuration> | undefined,
|
|
671
|
+
) {
|
|
672
|
+
this.localBranch = new SharedTreeBranch(
|
|
673
|
+
this.trunk.getHead(),
|
|
674
|
+
changeFamily,
|
|
675
|
+
mintRevisionTag,
|
|
676
|
+
branchTrimmer,
|
|
677
|
+
telemetryEventBatcher,
|
|
678
|
+
);
|
|
679
|
+
|
|
680
|
+
this.sequenceIdToCommit.set(baseCommitSequenceId, this.trunk.getHead());
|
|
681
|
+
|
|
682
|
+
this.localBranch.events.on("afterChange", (event) => {
|
|
683
|
+
if (event.type === "append") {
|
|
684
|
+
for (const commit of event.newCommits) {
|
|
685
|
+
this.localCommits.push(commit);
|
|
686
|
+
}
|
|
687
|
+
} else {
|
|
688
|
+
this.localCommits.length = 0;
|
|
689
|
+
findCommonAncestor(
|
|
690
|
+
[this.localBranch.getHead(), this.localCommits],
|
|
691
|
+
this.trunk.getHead(),
|
|
692
|
+
);
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
public addSequencedChanges(
|
|
698
|
+
newCommits: readonly GraphCommit<TChangeset>[],
|
|
699
|
+
sessionId: SessionId,
|
|
700
|
+
sequenceNumber: SeqNumber,
|
|
701
|
+
areLocalCommits: boolean,
|
|
702
|
+
referenceSequenceNumber: SeqNumber,
|
|
703
|
+
onSequenceLocalCommit: OnSequenceCommit<TChangeset>,
|
|
704
|
+
): void {
|
|
705
|
+
assert(
|
|
706
|
+
sequenceNumber >= // This is ">=", not ">" because changes in the same batch will have the same sequence number
|
|
707
|
+
(this.sequenceIdToCommit.maxKey()?.sequenceNumber ?? minimumPossibleSequenceNumber),
|
|
708
|
+
0xa64 /* Attempted to sequence change with an outdated sequence number */,
|
|
709
|
+
);
|
|
710
|
+
|
|
711
|
+
const batchSize = this.getBatchSize(sequenceNumber);
|
|
712
|
+
// The sequence id for the next commit to be processed in the bunch.
|
|
713
|
+
let nextSequenceId =
|
|
714
|
+
batchSize === 0
|
|
715
|
+
? {
|
|
716
|
+
sequenceNumber,
|
|
717
|
+
}
|
|
718
|
+
: {
|
|
719
|
+
sequenceNumber,
|
|
720
|
+
indexInBatch: batchSize,
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
// Local changes, i.e., changes from this client are applied by fast forwarding the local branch commit onto
|
|
724
|
+
// the trunk.
|
|
725
|
+
if (areLocalCommits) {
|
|
726
|
+
for (const _ of newCommits) {
|
|
727
|
+
this.sequenceLocalCommit(nextSequenceId, sessionId, onSequenceLocalCommit);
|
|
728
|
+
nextSequenceId = getNextSequenceId(nextSequenceId);
|
|
729
|
+
}
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// Remote changes, i.e., changes from remote clients are applied in three steps.
|
|
734
|
+
// Step 1 - Recreate the peer remote client's local environment.
|
|
735
|
+
// Get the revision that the remote change is based on and rebase that peer local branch over the part of the
|
|
736
|
+
// trunk up to the base revision. This will be a no-op if the sending client has not advanced since the last
|
|
737
|
+
// time we received an edit from it
|
|
738
|
+
const peerLocalBranch = this.rebasePeer(sessionId, referenceSequenceNumber);
|
|
720
739
|
|
|
721
740
|
// Step 2 - Append the changes to the peer branch and rebase the changes to the tip of the trunk.
|
|
722
741
|
if (peerLocalBranch.getHead() === this.trunk.getHead()) {
|
|
@@ -737,8 +756,8 @@ export class EditManager<
|
|
|
737
756
|
// If the merge resulted in any changes to the trunk, update the sequence map and trunk metadata
|
|
738
757
|
// with the rebased commits.
|
|
739
758
|
for (const sourceCommit of result.sourceCommits) {
|
|
740
|
-
this.
|
|
741
|
-
this.
|
|
759
|
+
this.sequenceIdToCommit.set(nextSequenceId, sourceCommit);
|
|
760
|
+
this.commitMetadata.set(sourceCommit.revision, {
|
|
742
761
|
sequenceId: nextSequenceId,
|
|
743
762
|
sessionId,
|
|
744
763
|
});
|
|
@@ -751,32 +770,125 @@ export class EditManager<
|
|
|
751
770
|
this.localBranch.rebaseOnto(this.trunk);
|
|
752
771
|
}
|
|
753
772
|
|
|
754
|
-
public
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
[this.localBranch.getHead(), commits],
|
|
760
|
-
(c) => c.revision === revision,
|
|
773
|
+
public isEmpty(baseCommit: GraphCommit<TChangeset>): boolean {
|
|
774
|
+
return (
|
|
775
|
+
this.trunk.getHead() === baseCommit &&
|
|
776
|
+
this.peerLocalBranches.size === 0 &&
|
|
777
|
+
this.localBranch.getHead() === this.trunk.getHead()
|
|
761
778
|
);
|
|
762
|
-
assert(commit !== undefined, 0x599 /* Expected local branch to contain revision */);
|
|
763
|
-
return [commit, commits];
|
|
764
779
|
}
|
|
765
780
|
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
781
|
+
public rebasePeer(
|
|
782
|
+
sessionId: SessionId,
|
|
783
|
+
referenceSequenceNumber: SeqNumber,
|
|
784
|
+
): SharedTreeBranch<TEditor, TChangeset> {
|
|
785
|
+
const [, baseRevisionInTrunk] = this.getClosestTrunkCommit(referenceSequenceNumber);
|
|
786
|
+
const peerLocalBranch = getOrCreate(
|
|
787
|
+
this.peerLocalBranches,
|
|
788
|
+
sessionId,
|
|
789
|
+
() => new SharedTreeBranch(baseRevisionInTrunk, this.changeFamily, this.mintRevisionTag),
|
|
790
|
+
);
|
|
791
|
+
peerLocalBranch.rebaseOnto(this.trunk, baseRevisionInTrunk);
|
|
792
|
+
return peerLocalBranch;
|
|
769
793
|
}
|
|
770
794
|
|
|
771
|
-
|
|
795
|
+
public getPeerBranchOrTrunk(sessionId: SessionId): SharedTreeBranch<TEditor, TChangeset> {
|
|
796
|
+
return this.peerLocalBranches.get(sessionId) ?? this.trunk;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
/**
|
|
800
|
+
* Gets the length of the longest branch maintained by this `SharedBranch`.
|
|
801
|
+
* This may be the length of a peer branch or the local branch.
|
|
802
|
+
*
|
|
803
|
+
* @remarks
|
|
804
|
+
* The length is counted from the lowest common ancestor with the trunk such that a fully sequenced branch would
|
|
805
|
+
* have length zero.
|
|
806
|
+
*/
|
|
807
|
+
public getLongestBranchLength(): number {
|
|
808
|
+
let max = 0;
|
|
809
|
+
const trunkHead = this.trunk.getHead();
|
|
810
|
+
for (const branch of this.peerLocalBranches.values()) {
|
|
811
|
+
const branchPath = getPathFromBase(branch.getHead(), trunkHead);
|
|
812
|
+
if (branchPath.length > max) {
|
|
813
|
+
max = branchPath.length;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
const localPath = getPathFromBase(this.localBranch.getHead(), trunkHead);
|
|
817
|
+
return Math.max(max, localPath.length);
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
public trimHistory(newBase: GraphCommit<TChangeset>, sequenceId: SequenceId): void {
|
|
821
|
+
this.rebasePeers(newBase);
|
|
822
|
+
|
|
823
|
+
this.sequenceIdToCommit.editRange(
|
|
824
|
+
minimumPossibleSequenceId,
|
|
825
|
+
sequenceId,
|
|
826
|
+
true,
|
|
827
|
+
(s, commit) => {
|
|
828
|
+
// Cleanup look-aside data for each evicted commit
|
|
829
|
+
this.commitMetadata.delete(commit.revision);
|
|
830
|
+
// Delete all evicted commits from `sequenceMap` except for the latest one, which is the new `trunkBase`
|
|
831
|
+
if (equalSequenceIds(s, sequenceId)) {
|
|
832
|
+
assert(
|
|
833
|
+
commit === newBase,
|
|
834
|
+
0x729 /* Expected last evicted commit to be new trunk base */,
|
|
835
|
+
);
|
|
836
|
+
} else {
|
|
837
|
+
return { delete: true };
|
|
838
|
+
}
|
|
839
|
+
},
|
|
840
|
+
);
|
|
841
|
+
|
|
842
|
+
const trunkSize = getPathFromBase(this.trunk.getHead(), newBase).length;
|
|
843
|
+
assert(
|
|
844
|
+
this.sequenceIdToCommit.size === trunkSize + 1,
|
|
845
|
+
0x744 /* The size of the sequenceMap must have one element more than the trunk */,
|
|
846
|
+
);
|
|
847
|
+
assert(
|
|
848
|
+
this.commitMetadata.size === trunkSize,
|
|
849
|
+
0x745 /* The size of the trunkMetadata must be the same as the trunk */,
|
|
850
|
+
);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
private rebasePeers(commit: GraphCommit<TChangeset>): void {
|
|
854
|
+
for (const [, branch] of this.peerLocalBranches) {
|
|
855
|
+
branch.rebaseOnto(this.trunk, commit);
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
// The metadata for new trunk base revision needs to be deleted before modifying it.
|
|
859
|
+
this.commitMetadata.delete(commit.revision);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
public getLocalCommits(): readonly GraphCommit<TChangeset>[] {
|
|
863
|
+
return this.localCommits;
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* Promote the oldest un-sequenced commit on the local branch to the head of the trunk.
|
|
868
|
+
* @param sequenceId - The sequence id of the new trunk commit
|
|
869
|
+
* @remarks This method is a performance optimization for the scenario where this client receives its own change back after sequencing.
|
|
870
|
+
* 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.
|
|
871
|
+
* 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.
|
|
872
|
+
* 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.
|
|
873
|
+
* 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.
|
|
874
|
+
*/
|
|
875
|
+
private sequenceLocalCommit(
|
|
772
876
|
sequenceId: SequenceId,
|
|
773
|
-
graphCommit: GraphCommit<TChangeset>,
|
|
774
877
|
sessionId: SessionId,
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
this.
|
|
878
|
+
onSequenceLocalCommit: OnSequenceCommit<TChangeset>,
|
|
879
|
+
): GraphCommit<TChangeset> {
|
|
880
|
+
// First, push the local commit to the trunk.
|
|
881
|
+
// 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.
|
|
882
|
+
const firstLocalCommit = this.localCommits.shift();
|
|
883
|
+
assert(
|
|
884
|
+
firstLocalCommit !== undefined,
|
|
885
|
+
0x6b5 /* Received a sequenced change from the local session despite having no local changes */,
|
|
886
|
+
);
|
|
887
|
+
|
|
888
|
+
const prevSequenceId = this.getCommitSequenceId(this.trunk.getHead().revision);
|
|
889
|
+
this.pushGraphCommitToTrunk(sequenceId, firstLocalCommit, sessionId);
|
|
890
|
+
onSequenceLocalCommit(firstLocalCommit, sequenceId, prevSequenceId);
|
|
891
|
+
return firstLocalCommit;
|
|
780
892
|
}
|
|
781
893
|
|
|
782
894
|
/**
|
|
@@ -785,9 +897,9 @@ export class EditManager<
|
|
|
785
897
|
* @remarks Fails if there is no eligible commit.
|
|
786
898
|
* @returns the closest commit and its sequence id
|
|
787
899
|
*/
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
900
|
+
public getClosestTrunkCommit(searchBy: SeqNumber): [SequenceId, GraphCommit<TChangeset>];
|
|
901
|
+
public getClosestTrunkCommit(searchBy: SequenceId): [SequenceId, GraphCommit<TChangeset>];
|
|
902
|
+
public getClosestTrunkCommit(
|
|
791
903
|
searchBy: SeqNumber | SequenceId,
|
|
792
904
|
): [SequenceId, GraphCommit<TChangeset>] {
|
|
793
905
|
const sequenceId: SequenceId =
|
|
@@ -801,12 +913,45 @@ export class EditManager<
|
|
|
801
913
|
}
|
|
802
914
|
: searchBy;
|
|
803
915
|
|
|
804
|
-
const commit = this.
|
|
916
|
+
const commit = this.sequenceIdToCommit.getPairOrNextLower(sequenceId);
|
|
805
917
|
assert(commit !== undefined, 0x746 /* sequence id has been evicted */);
|
|
806
918
|
return commit;
|
|
807
919
|
}
|
|
808
920
|
|
|
809
|
-
private
|
|
921
|
+
private pushGraphCommitToTrunk(
|
|
922
|
+
sequenceId: SequenceId,
|
|
923
|
+
graphCommit: GraphCommit<TChangeset>,
|
|
924
|
+
sessionId: SessionId,
|
|
925
|
+
): void {
|
|
926
|
+
this.trunk.setHead(graphCommit);
|
|
927
|
+
this.registerSequencedCommit(sequenceId, sessionId, graphCommit);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
private pushCommitToTrunk(sequenceId: SequenceId, commit: Commit<TChangeset>): void {
|
|
931
|
+
const mintedCommit = mintCommit(this.trunk.getHead(), commit);
|
|
932
|
+
this.pushGraphCommitToTrunk(sequenceId, mintedCommit, commit.sessionId);
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
private registerSequencedCommit(
|
|
936
|
+
sequenceId: SequenceId,
|
|
937
|
+
sessionId: SessionId,
|
|
938
|
+
commit: GraphCommit<TChangeset>,
|
|
939
|
+
): void {
|
|
940
|
+
this.sequenceIdToCommit.set(sequenceId, commit);
|
|
941
|
+
this.commitMetadata.set(commit.revision, { sequenceId, sessionId });
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
public getCommitSequenceId(commitRevision: RevisionTag): SequenceId {
|
|
945
|
+
const id = this.commitMetadata.get(commitRevision)?.sequenceId;
|
|
946
|
+
if (id === undefined) {
|
|
947
|
+
return minimumPossibleSequenceId;
|
|
948
|
+
}
|
|
949
|
+
return id;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
// TODO: Document that this is to handle receiving separate commits with the same sequence ID,
|
|
953
|
+
// as a batch of changes are not guaranteed to be processed as one bunch.
|
|
954
|
+
private getBatchSize(sequenceNumber: SeqNumber): number {
|
|
810
955
|
const startSequenceId: SequenceId = {
|
|
811
956
|
sequenceNumber,
|
|
812
957
|
};
|
|
@@ -814,29 +959,156 @@ export class EditManager<
|
|
|
814
959
|
sequenceNumber: brand((sequenceNumber as number) + 1),
|
|
815
960
|
};
|
|
816
961
|
|
|
817
|
-
return this.
|
|
962
|
+
return this.sequenceIdToCommit.getRange(startSequenceId, endSequenceId, false).length;
|
|
818
963
|
}
|
|
819
|
-
}
|
|
820
964
|
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
965
|
+
public getSummaryData(
|
|
966
|
+
minSeqNumberToSummarize: SequenceId,
|
|
967
|
+
trunkBaseRevision: RevisionTag,
|
|
968
|
+
): SharedBranchSummaryData<TChangeset> {
|
|
969
|
+
// The assert below is acceptable at present because summarization only ever occurs on a client with no
|
|
970
|
+
// local/in-flight changes.
|
|
971
|
+
// In the future we may wish to relax this constraint. For that to work, the current implementation of
|
|
972
|
+
// `EditManager` would have to be amended in one of two ways:
|
|
973
|
+
// A) Changes made by the local session should be represented by a branch in `EditManager.branches`.
|
|
974
|
+
// B) The contents of such a branch should be computed on demand based on the trunk.
|
|
975
|
+
// Note that option (A) would be a simple change to `addSequencedChanges` whereas (B) would likely require
|
|
976
|
+
// rebasing trunk changes over the inverse of trunk changes.
|
|
977
|
+
assert(
|
|
978
|
+
this.localBranch.getHead() === this.trunk.getHead(),
|
|
979
|
+
"Clients with local changes cannot be used to generate summaries",
|
|
980
|
+
);
|
|
981
|
+
|
|
982
|
+
let parentHead: GraphCommit<TChangeset>;
|
|
983
|
+
if (this.parentBranch === undefined) {
|
|
984
|
+
const oldestCommitInCollabWindow =
|
|
985
|
+
this.getClosestTrunkCommit(minSeqNumberToSummarize)[1];
|
|
986
|
+
// Path construction is exclusive, so we need to use the parent of the oldest commit in the window if it exists
|
|
987
|
+
parentHead = oldestCommitInCollabWindow.parent ?? oldestCommitInCollabWindow;
|
|
988
|
+
} else {
|
|
989
|
+
parentHead = this.parentBranch.trunk.getHead();
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
const childBranchTrunkCommits: GraphCommit<TChangeset>[] = [];
|
|
993
|
+
const forkPointFromMainTrunk = findCommonAncestor(
|
|
994
|
+
[this.trunk.getHead(), childBranchTrunkCommits],
|
|
995
|
+
parentHead,
|
|
996
|
+
);
|
|
997
|
+
assert(
|
|
998
|
+
forkPointFromMainTrunk !== undefined,
|
|
999
|
+
"Expected child branch to be based on main branch",
|
|
1000
|
+
);
|
|
1001
|
+
|
|
1002
|
+
const trunk = childBranchTrunkCommits.map((c) => {
|
|
1003
|
+
const metadata =
|
|
1004
|
+
this.commitMetadata.get(c.revision) ??
|
|
1005
|
+
fail(0xad5 /* Expected metadata for trunk commit */);
|
|
1006
|
+
const commit: SequencedCommit<TChangeset> = {
|
|
1007
|
+
change: c.change,
|
|
1008
|
+
revision: c.revision,
|
|
1009
|
+
sequenceNumber: metadata.sequenceId.sequenceNumber,
|
|
1010
|
+
sessionId: metadata.sessionId,
|
|
1011
|
+
};
|
|
1012
|
+
if (metadata.sequenceId.indexInBatch !== undefined) {
|
|
1013
|
+
commit.indexInBatch = metadata.sequenceId.indexInBatch;
|
|
1014
|
+
}
|
|
1015
|
+
return commit;
|
|
1016
|
+
});
|
|
1017
|
+
|
|
1018
|
+
const peerLocalBranches = new Map<SessionId, SummarySessionBranch<TChangeset>>(
|
|
1019
|
+
mapIterable(this.peerLocalBranches.entries(), ([sessionId, branch]) => {
|
|
1020
|
+
const branchPath: GraphCommit<TChangeset>[] = [];
|
|
1021
|
+
const ancestor =
|
|
1022
|
+
findCommonAncestor([branch.getHead(), branchPath], this.trunk.getHead()) ??
|
|
1023
|
+
fail(0xad6 /* Expected branch to be based on trunk */);
|
|
1024
|
+
|
|
1025
|
+
const base =
|
|
1026
|
+
ancestor.revision === trunkBaseRevision ? rootRevision : ancestor.revision;
|
|
1027
|
+
return [
|
|
1028
|
+
sessionId,
|
|
1029
|
+
{
|
|
1030
|
+
base,
|
|
1031
|
+
commits: branchPath.map((c) => {
|
|
1032
|
+
const commit: Commit<TChangeset> = {
|
|
1033
|
+
change: c.change,
|
|
1034
|
+
revision: c.revision,
|
|
1035
|
+
sessionId,
|
|
1036
|
+
};
|
|
1037
|
+
return commit;
|
|
1038
|
+
}),
|
|
1039
|
+
},
|
|
1040
|
+
];
|
|
1041
|
+
}),
|
|
1042
|
+
);
|
|
1043
|
+
|
|
1044
|
+
const trunkBase =
|
|
1045
|
+
this.parentBranch === undefined ? undefined : forkPointFromMainTrunk.revision;
|
|
1046
|
+
return { trunk, peerLocalBranches, base: trunkBase, id: this.id, session: this.sessionId };
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
public loadSummaryData(
|
|
1050
|
+
data: SharedBranchSummaryData<TChangeset>,
|
|
1051
|
+
trunkRevisionCache: Map<RevisionTag, GraphCommit<TChangeset>>,
|
|
1052
|
+
): void {
|
|
1053
|
+
assert(
|
|
1054
|
+
(this.parentBranch === undefined) === (data.base === undefined),
|
|
1055
|
+
"Expected branch base to match presence of parent branch",
|
|
1056
|
+
);
|
|
1057
|
+
const parentTrunkBase =
|
|
1058
|
+
trunkRevisionCache.get(data.base ?? rootRevision) ??
|
|
1059
|
+
fail("Expected base revision to be in trunk cache");
|
|
1060
|
+
this.trunk.setHead(
|
|
1061
|
+
data.trunk.reduce((base, c) => {
|
|
1062
|
+
const sequenceId: SequenceId =
|
|
1063
|
+
c.indexInBatch === undefined
|
|
1064
|
+
? {
|
|
1065
|
+
sequenceNumber: c.sequenceNumber,
|
|
1066
|
+
}
|
|
1067
|
+
: {
|
|
1068
|
+
sequenceNumber: c.sequenceNumber,
|
|
1069
|
+
indexInBatch: c.indexInBatch,
|
|
1070
|
+
};
|
|
1071
|
+
const commit = mintCommit(base, c);
|
|
1072
|
+
this.sequenceIdToCommit.set(sequenceId, commit);
|
|
1073
|
+
this.commitMetadata.set(c.revision, {
|
|
1074
|
+
sequenceId,
|
|
1075
|
+
sessionId: c.sessionId,
|
|
1076
|
+
});
|
|
1077
|
+
trunkRevisionCache.set(c.revision, commit);
|
|
1078
|
+
return commit;
|
|
1079
|
+
}, parentTrunkBase),
|
|
1080
|
+
);
|
|
1081
|
+
|
|
1082
|
+
this.localBranch.setHead(this.trunk.getHead());
|
|
1083
|
+
|
|
1084
|
+
for (const [sessionId, branch] of data.peerLocalBranches) {
|
|
1085
|
+
const commit =
|
|
1086
|
+
trunkRevisionCache.get(branch.base) ??
|
|
1087
|
+
fail(0xad7 /* Expected summary branch to be based off of a revision in the trunk */);
|
|
1088
|
+
|
|
1089
|
+
this.peerLocalBranches.set(
|
|
1090
|
+
sessionId,
|
|
1091
|
+
new SharedTreeBranch(
|
|
1092
|
+
branch.commits.reduce(mintCommit, commit),
|
|
1093
|
+
this.changeFamily,
|
|
1094
|
+
this.mintRevisionTag,
|
|
1095
|
+
),
|
|
1096
|
+
);
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
827
1099
|
}
|
|
828
1100
|
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
const path: TCommit[] = [];
|
|
837
|
-
assert(
|
|
838
|
-
findCommonAncestor([branchHead, path], baseBranchHead) !== undefined,
|
|
839
|
-
0x573 /* Expected branches to be related */,
|
|
840
|
-
);
|
|
841
|
-
return path;
|
|
1101
|
+
// Returns the sequence id for the next commit to be processed in the bunch. Since all the commits have the
|
|
1102
|
+
// same sequence number, only the index in the batch needs to be incremented.
|
|
1103
|
+
function getNextSequenceId(sequenceId: SequenceId): SequenceId {
|
|
1104
|
+
return {
|
|
1105
|
+
sequenceNumber: sequenceId.sequenceNumber,
|
|
1106
|
+
indexInBatch: (sequenceId.indexInBatch ?? 0) + 1,
|
|
1107
|
+
};
|
|
842
1108
|
}
|
|
1109
|
+
|
|
1110
|
+
type OnSequenceCommit<TChangeset> = (
|
|
1111
|
+
commit: GraphCommit<TChangeset>,
|
|
1112
|
+
sequenceId: SequenceId,
|
|
1113
|
+
prevSequenceId: SequenceId,
|
|
1114
|
+
) => void;
|