-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOrderedKeyedArrayMergeStrategy.ts
More file actions
612 lines (522 loc) · 17.3 KB
/
OrderedKeyedArrayMergeStrategy.ts
File metadata and controls
612 lines (522 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
import { deepEqual } from 'fast-equals'
import { isEmpty } from 'lodash-es'
import type { MergeConfig } from '../../types/conflictTypes.js'
import type { JsonArray, JsonObject } from '../../types/jsonTypes.js'
import type { MergeResult } from '../../types/mergeResult.js'
import {
combineResults,
isNonEmpty,
noConflict,
withConflict,
} from '../../types/mergeResult.js'
import { hasSameOrder, lcs, pushAll } from '../../utils/arrayUtils.js'
import { setsEqual, setsIntersect } from '../../utils/setUtils.js'
import { buildConflictMarkers } from '../ConflictMarkerBuilder.js'
import type { KeyExtractor } from './nodeUtils.js'
import {
buildKeyedMap,
filterEmptyTextNodes,
toJsonArray,
} from './nodeUtils.js'
// ============================================================================
// Strategy Interface
// ============================================================================
export interface KeyedArrayMergeStrategy {
merge(config: MergeConfig): MergeResult
}
// ============================================================================
// Ordered Strategy
// ============================================================================
interface ArrayMergeState {
ancestorKeys: string[]
localKeys: string[]
otherKeys: string[]
ancestorMap: Map<string, JsonObject>
localMap: Map<string, JsonObject>
otherMap: Map<string, JsonObject>
// Position maps for O(1) lookups - computed once, reused everywhere
ancestorPos: Map<string, number>
localPos: Map<string, number>
otherPos: Map<string, number>
ancestorSet: Set<string>
}
interface GapKeys {
readonly ancestor: string[]
readonly local: string[]
readonly other: string[]
}
interface GapSets {
localDeleted: Set<string>
otherDeleted: Set<string>
localAdded: Set<string>
otherAdded: Set<string>
allKeys: Set<string>
}
const computeGapSets = (
gapA: string[],
gapL: string[],
gapO: string[]
): GapSets => {
const ancestorSet = new Set(gapA)
const localSet = new Set(gapL)
const otherSet = new Set(gapO)
return {
localDeleted: new Set(gapA.filter(k => !localSet.has(k))),
otherDeleted: new Set(gapA.filter(k => !otherSet.has(k))),
localAdded: new Set(gapL.filter(k => !ancestorSet.has(k))),
otherAdded: new Set(gapO.filter(k => !ancestorSet.has(k))),
allKeys: new Set([...gapA, ...gapL, ...gapO]),
}
}
// ============================================================================
// Ordering Analysis
// ============================================================================
interface OrderingAnalysis {
canMerge: boolean
localMoved: Set<string>
otherMoved: Set<string>
}
export class OrderedKeyedArrayMergeStrategy implements KeyedArrayMergeStrategy {
constructor(
private readonly ancestor: JsonArray,
private readonly local: JsonArray,
private readonly other: JsonArray,
private readonly attribute: string,
private readonly keyField: KeyExtractor
) {}
merge(config: MergeConfig): MergeResult {
const ctx = this.buildArrayMergeState()
const orderingAnalysis = this.analyzeOrderings(ctx)
if (!orderingAnalysis.canMerge) {
return this.buildFullArrayConflict(config, ctx)
}
const hasDivergedOrderings =
orderingAnalysis.localMoved.size > 0 ||
orderingAnalysis.otherMoved.size > 0
if (hasDivergedOrderings) {
return this.processDivergedOrderings(config, ctx, orderingAnalysis)
}
return this.processWithSpine(config, ctx)
}
// ============================================================================
// Context Building
// ============================================================================
private buildArrayMergeState(): ArrayMergeState {
const ancestorKeys = this.ancestor.map(item =>
this.keyField(item as JsonObject)
)
const localKeys = this.local.map(item => this.keyField(item as JsonObject))
const otherKeys = this.other.map(item => this.keyField(item as JsonObject))
return {
ancestorKeys,
localKeys,
otherKeys,
ancestorMap: buildKeyedMap(this.ancestor, this.keyField),
localMap: buildKeyedMap(this.local, this.keyField),
otherMap: buildKeyedMap(this.other, this.keyField),
// Position maps computed once for O(1) lookups
ancestorPos: new Map(ancestorKeys.map((k, i) => [k, i])),
localPos: new Map(localKeys.map((k, i) => [k, i])),
otherPos: new Map(otherKeys.map((k, i) => [k, i])),
ancestorSet: new Set(ancestorKeys),
}
}
// ============================================================================
// Ordering Analysis
// ============================================================================
private analyzeOrderings(ctx: ArrayMergeState): OrderingAnalysis {
// Fast path: identical orderings
if (hasSameOrder(ctx.localKeys, ctx.otherKeys)) {
return {
canMerge: true,
localMoved: new Set(),
otherMoved: new Set(),
}
}
// Detect which elements moved in each version (relative to ancestor)
const localMoved = this.getMovedElements(ctx, ctx.localPos)
const otherMoved = this.getMovedElements(ctx, ctx.otherPos)
// Overlapping moves = conflict (C4 scenario)
if (setsIntersect(localMoved, otherMoved)) {
return { canMerge: false, localMoved, otherMoved }
}
// If orderings differ but no ancestor elements moved, the difference
// must come from added elements at different positions (C6 scenario)
if (localMoved.size === 0 && otherMoved.size === 0) {
return { canMerge: false, localMoved, otherMoved }
}
return { canMerge: true, localMoved, otherMoved }
}
/**
* Finds elements that changed relative order between ancestor and modified arrays.
* Uses upper triangle optimization to avoid redundant pair comparisons.
* Complexity: O(n²) for n elements - acceptable for typical metadata array sizes.
*/
private getMovedElements(
ctx: ArrayMergeState,
modifiedPos: Map<string, number>
): Set<string> {
const moved = new Set<string>()
// Only check upper triangle (i < j) to avoid duplicate comparisons
for (let i = 0; i < ctx.ancestorKeys.length; i++) {
const a = ctx.ancestorKeys[i]
const aMod = modifiedPos.get(a)
if (aMod === undefined) continue
for (let j = i + 1; j < ctx.ancestorKeys.length; j++) {
const b = ctx.ancestorKeys[j]
const bMod = modifiedPos.get(b)
if (bMod === undefined) continue
// In ancestor: a comes before b (i < j)
// Check if order reversed in modified
if (aMod > bMod) {
moved.add(a)
moved.add(b)
}
}
}
return moved
}
// ============================================================================
// Diverged Orderings Processing
// ============================================================================
private processDivergedOrderings(
config: MergeConfig,
ctx: ArrayMergeState,
analysis: OrderingAnalysis
): MergeResult {
const mergedKeys = this.computeMergedKeyOrder(ctx, analysis)
if (mergedKeys === null) {
return this.buildFullArrayConflict(config, ctx)
}
return this.processKeyOrder(config, ctx, mergedKeys)
}
/**
* Computes merged key order by combining disjoint reorderings.
* Also includes elements added in local or other (not in ancestor).
* Returns null if concurrent disjoint additions are detected (conflict).
*/
private computeMergedKeyOrder(
ctx: ArrayMergeState,
analysis: OrderingAnalysis
): string[] | null {
const { localMoved, otherMoved } = analysis
// Build ordered lists for moved elements in a single pass each
const localMovedOrdered: string[] = []
const otherMovedOrdered: string[] = []
const localAdditions: string[] = []
const otherAdditions: string[] = []
for (const key of ctx.localKeys) {
if (localMoved.has(key)) {
localMovedOrdered.push(key)
} else if (!ctx.ancestorSet.has(key)) {
localAdditions.push(key)
}
}
for (const key of ctx.otherKeys) {
if (otherMoved.has(key)) {
otherMovedOrdered.push(key)
} else if (!ctx.ancestorSet.has(key) && !ctx.localMap.has(key)) {
// Only add if not already added by local
otherAdditions.push(key)
}
}
// Conflict: both sides added different elements - ambiguous ordering
if (localAdditions.length > 0 && otherAdditions.length > 0) {
return null
}
// Build result by traversing ancestor and inserting moved groups
const result: string[] = []
let localMovedInserted = false
let otherMovedInserted = false
for (const key of ctx.ancestorKeys) {
if (localMoved.has(key)) {
if (!localMovedInserted) {
pushAll(result, localMovedOrdered)
localMovedInserted = true
}
} else if (otherMoved.has(key)) {
if (!otherMovedInserted) {
pushAll(result, otherMovedOrdered)
otherMovedInserted = true
}
} else {
result.push(key)
}
}
// Append additions at the end (preserving their relative order)
pushAll(result, localAdditions, otherAdditions)
return result
}
// ============================================================================
// Spine-based Processing
// ============================================================================
private processWithSpine(
config: MergeConfig,
ctx: ArrayMergeState
): MergeResult {
const spine = lcs(
lcs(ctx.ancestorKeys, ctx.localKeys),
lcs(ctx.ancestorKeys, ctx.otherKeys)
)
return this.processSpine(config, spine, ctx)
}
// ============================================================================
// Common Processing
// ============================================================================
/**
* Processes elements in the given key order, merging each element.
* Handles elements present in any of the three versions.
*/
private processKeyOrder(
config: MergeConfig,
ctx: ArrayMergeState,
keys: string[]
): MergeResult {
const results: MergeResult[] = []
for (const key of keys) {
const result = this.mergeElementWithPresenceCheck(config, key, ctx)
if (result && !isEmpty(result.output)) {
results.push(result)
}
}
return combineResults(results)
}
/**
* Merges an element considering it might not exist in all three versions.
* Handles additions and deletions alongside modifications.
*/
private mergeElementWithPresenceCheck(
config: MergeConfig,
key: string,
ctx: ArrayMergeState
): MergeResult | null {
const inA = ctx.ancestorMap.has(key)
const inL = ctx.localMap.has(key)
const inO = ctx.otherMap.has(key)
// Element exists in all three - standard merge
if (inA && inL && inO) {
return this.mergeElement(config, key, ctx)
}
// Handle additions and deletions
return this.mergeGapElement(config, key, ctx)
}
private wrapKeys(keys: string[], map: Map<string, JsonObject>): JsonArray {
return keys.map(k => ({
[this.attribute]: toJsonArray(map.get(k)!),
}))
}
private buildFullArrayConflict(
config: MergeConfig,
ctx: ArrayMergeState
): MergeResult {
return withConflict(
buildConflictMarkers(
config,
this.wrapKeys(ctx.localKeys, ctx.localMap),
this.wrapKeys(ctx.ancestorKeys, ctx.ancestorMap),
this.wrapKeys(ctx.otherKeys, ctx.otherMap)
)
)
}
// Spine processing
private processSpine(
config: MergeConfig,
spine: string[],
ctx: ArrayMergeState
): MergeResult {
const results: MergeResult[] = []
let aIdx = 0
let lIdx = 0
let oIdx = 0
for (const anchor of spine) {
const gapA = this.collectUntil(ctx.ancestorKeys, aIdx, anchor)
const gapL = this.collectUntil(ctx.localKeys, lIdx, anchor)
const gapO = this.collectUntil(ctx.otherKeys, oIdx, anchor)
aIdx += gapA.length
lIdx += gapL.length
oIdx += gapO.length
const gaps: GapKeys = { ancestor: gapA, local: gapL, other: gapO }
const gapResult = this.mergeGap(config, gaps, ctx)
if (isNonEmpty(gapResult)) {
results.push(gapResult)
}
results.push(this.mergeElement(config, anchor, ctx))
aIdx++
lIdx++
oIdx++
}
const finalGaps: GapKeys = {
ancestor: ctx.ancestorKeys.slice(aIdx),
local: ctx.localKeys.slice(lIdx),
other: ctx.otherKeys.slice(oIdx),
}
const finalResult = this.mergeGap(config, finalGaps, ctx)
if (isNonEmpty(finalResult)) {
results.push(finalResult)
}
return combineResults(results)
}
private collectUntil(
keys: string[],
startIdx: number,
anchor: string
): string[] {
const result: string[] = []
for (let i = startIdx; i < keys.length && keys[i] !== anchor; i++) {
result.push(keys[i])
}
return result
}
// Gap merging
private mergeGap(
config: MergeConfig,
gaps: GapKeys,
ctx: ArrayMergeState
): MergeResult {
if (
gaps.ancestor.length === 0 &&
gaps.local.length === 0 &&
gaps.other.length === 0
) {
return noConflict([])
}
const sets = computeGapSets(gaps.ancestor, gaps.local, gaps.other)
const gapConflict = this.detectGapConflict(config, gaps, sets, ctx)
if (gapConflict) {
return gapConflict
}
return this.mergeGapElements(config, sets, ctx)
}
private detectGapConflict(
config: MergeConfig,
gaps: GapKeys,
sets: GapSets,
ctx: ArrayMergeState
): MergeResult | null {
const { localDeleted, otherDeleted, localAdded, otherAdded } = sets
const hasDeletionConflict =
localDeleted.size > 0 &&
otherDeleted.size > 0 &&
!setsEqual(localDeleted, otherDeleted)
const hasAdditionConflict =
localAdded.size > 0 &&
otherAdded.size > 0 &&
!setsIntersect(localAdded, otherAdded) &&
!setsEqual(localAdded, otherAdded)
if (hasDeletionConflict || hasAdditionConflict) {
return this.buildGapConflict(config, gaps, ctx)
}
return null
}
private buildGapConflict(
config: MergeConfig,
gaps: GapKeys,
ctx: ArrayMergeState
): MergeResult {
return withConflict(
filterEmptyTextNodes(
buildConflictMarkers(
config,
this.wrapKeys(gaps.local, ctx.localMap),
this.wrapKeys(gaps.ancestor, ctx.ancestorMap),
this.wrapKeys(gaps.other, ctx.otherMap)
)
)
)
}
private mergeGapElements(
config: MergeConfig,
sets: GapSets,
ctx: ArrayMergeState
): MergeResult {
const results: MergeResult[] = []
for (const key of sets.allKeys) {
const result = this.mergeGapElement(config, key, ctx)
if (result) {
results.push(result)
}
}
return combineResults(results)
}
/**
* Merges a single element based on its presence pattern across versions.
* Uses ElementPresence enum for readable pattern matching.
*/
private mergeGapElement(
config: MergeConfig,
key: string,
ctx: ArrayMergeState
): MergeResult | null {
const aVal = ctx.ancestorMap.get(key)
const lVal = ctx.localMap.get(key)
const oVal = ctx.otherMap.get(key)
if (aVal !== undefined) {
if (lVal === undefined) {
// Both deleted - nothing to output
if (oVal === undefined) {
return null
}
// Local deleted - conflict if other modified
return deepEqual(aVal, oVal)
? null
: this.buildElementConflict(config, null, aVal, oVal)
}
// Other deleted - conflict if local modified
return deepEqual(aVal, lVal)
? null
: this.buildElementConflict(config, lVal, aVal, null)
}
// Both added - accept if identical, conflict otherwise
if (lVal !== undefined && oVal !== undefined) {
return deepEqual(lVal, oVal)
? noConflict([this.wrapElement(lVal)])
: this.buildElementConflict(config, lVal, null, oVal)
}
// Only one side added
return noConflict([this.wrapElement(lVal ?? oVal!)])
}
// Element helpers
private wrapElement(value: JsonObject): JsonObject {
return { [this.attribute]: toJsonArray(value) }
}
private mergeElement(
config: MergeConfig,
key: string,
ctx: ArrayMergeState
): MergeResult {
const aVal = ctx.ancestorMap.get(key)
const lVal = ctx.localMap.get(key)
const oVal = ctx.otherMap.get(key)
// All equal
if (deepEqual(aVal, lVal) && deepEqual(lVal, oVal)) {
return noConflict([this.wrapElement(lVal!)])
}
// Other unchanged → take local
if (deepEqual(aVal, oVal) && lVal) {
return noConflict([this.wrapElement(lVal)])
}
// Local unchanged → take other
if (deepEqual(aVal, lVal) && oVal) {
return noConflict([this.wrapElement(oVal)])
}
// Both changed to same
if (deepEqual(lVal, oVal)) {
return noConflict([this.wrapElement(lVal!)])
}
return this.buildElementConflict(config, lVal, aVal, oVal)
}
private buildElementConflict(
config: MergeConfig,
lVal: JsonObject | null | undefined,
aVal: JsonObject | null | undefined,
oVal: JsonObject | null | undefined
): MergeResult {
const wrap = (val: JsonObject | null | undefined) =>
val ? this.wrapElement(val) : ({} as JsonObject)
return withConflict(
filterEmptyTextNodes(
buildConflictMarkers(config, wrap(lVal), wrap(aVal), wrap(oVal))
)
)
}
}