|
| 1 | +import { OplogEntry } from '@entgldb/protocol'; |
| 2 | +import { IConflictResolver, ConflictResolutionResult } from './conflict-resolver'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Recursive JSON merge conflict resolution strategy |
| 6 | + * Performs deep merge of JSON objects with intelligent array handling |
| 7 | + */ |
| 8 | +export class RecursiveNodeMergeConflictResolver implements IConflictResolver { |
| 9 | + resolve(local: any | null, remote: OplogEntry): ConflictResolutionResult { |
| 10 | + // If no local document, apply remote |
| 11 | + if (local === null || local === undefined) { |
| 12 | + const content = remote.data ? JSON.parse(Buffer.from(remote.data).toString('utf-8')) : {}; |
| 13 | + const newDoc = { |
| 14 | + collection: remote.collection, |
| 15 | + key: remote.key, |
| 16 | + content, |
| 17 | + updatedAt: remote.timestamp, |
| 18 | + isDeleted: remote.operation === 'delete' |
| 19 | + }; |
| 20 | + return ConflictResolutionResult.apply(newDoc); |
| 21 | + } |
| 22 | + |
| 23 | + // If remote is delete, check timestamp |
| 24 | + if (remote.operation === 'delete') { |
| 25 | + if (this.compareTimestamps(remote.timestamp, local.updatedAt) > 0) { |
| 26 | + const newDoc = { |
| 27 | + collection: remote.collection, |
| 28 | + key: remote.key, |
| 29 | + content: {}, |
| 30 | + updatedAt: remote.timestamp, |
| 31 | + isDeleted: true |
| 32 | + }; |
| 33 | + return ConflictResolutionResult.apply(newDoc); |
| 34 | + } |
| 35 | + return ConflictResolutionResult.ignore(); |
| 36 | + } |
| 37 | + |
| 38 | + const localContent = local.content; |
| 39 | + const remoteContent = remote.data ? JSON.parse(Buffer.from(remote.data).toString('utf-8')) : {}; |
| 40 | + const localTs = local.updatedAt; |
| 41 | + const remoteTs = remote.timestamp; |
| 42 | + |
| 43 | + // If either is undefined/null, use LWW |
| 44 | + if (!localContent || !remoteContent) { |
| 45 | + if (this.compareTimestamps(remoteTs, localTs) > 0) { |
| 46 | + return ConflictResolutionResult.apply({ |
| 47 | + collection: remote.collection, |
| 48 | + key: remote.key, |
| 49 | + content: remoteContent, |
| 50 | + updatedAt: remoteTs, |
| 51 | + isDeleted: false |
| 52 | + }); |
| 53 | + } |
| 54 | + return ConflictResolutionResult.ignore(); |
| 55 | + } |
| 56 | + |
| 57 | + // Perform recursive merge |
| 58 | + const mergedContent = this.mergeJson(localContent, localTs, remoteContent, remoteTs); |
| 59 | + const maxTimestamp = this.compareTimestamps(remoteTs, localTs) > 0 ? remoteTs : localTs; |
| 60 | + |
| 61 | + const mergedDoc = { |
| 62 | + collection: remote.collection, |
| 63 | + key: remote.key, |
| 64 | + content: mergedContent, |
| 65 | + updatedAt: maxTimestamp, |
| 66 | + isDeleted: false |
| 67 | + }; |
| 68 | + |
| 69 | + return ConflictResolutionResult.apply(mergedDoc); |
| 70 | + } |
| 71 | + |
| 72 | + private mergeJson(local: any, localTs: any, remote: any, remoteTs: any): any { |
| 73 | + // If types differ, use LWW |
| 74 | + const localType = this.getType(local); |
| 75 | + const remoteType = this.getType(remote); |
| 76 | + |
| 77 | + if (localType !== remoteType) { |
| 78 | + return this.compareTimestamps(remoteTs, localTs) > 0 ? remote : local; |
| 79 | + } |
| 80 | + |
| 81 | + // Handle objects |
| 82 | + if (localType === 'object') { |
| 83 | + return this.mergeObjects(local, localTs, remote, remoteTs); |
| 84 | + } |
| 85 | + |
| 86 | + // Handle arrays |
| 87 | + if (localType === 'array') { |
| 88 | + return this.mergeArrays(local, localTs, remote, remoteTs); |
| 89 | + } |
| 90 | + |
| 91 | + // Primitives - use LWW |
| 92 | + if (local === remote) { |
| 93 | + return local; |
| 94 | + } |
| 95 | + return this.compareTimestamps(remoteTs, localTs) > 0 ? remote : local; |
| 96 | + } |
| 97 | + |
| 98 | + private mergeObjects(local: any, localTs: any, remote: any, remoteTs: any): any { |
| 99 | + const result: any = {}; |
| 100 | + const processedKeys = new Set<string>(); |
| 101 | + |
| 102 | + // Process local keys |
| 103 | + for (const key of Object.keys(local)) { |
| 104 | + processedKeys.add(key); |
| 105 | + |
| 106 | + if (key in remote) { |
| 107 | + // Collision - merge recursively |
| 108 | + result[key] = this.mergeJson(local[key], localTs, remote[key], remoteTs); |
| 109 | + } else { |
| 110 | + // Only in local |
| 111 | + result[key] = local[key]; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Add remaining remote keys |
| 116 | + for (const key of Object.keys(remote)) { |
| 117 | + if (!processedKeys.has(key)) { |
| 118 | + result[key] = remote[key]; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return result; |
| 123 | + } |
| 124 | + |
| 125 | + private mergeArrays(local: any[], localTs: any, remote: any[], remoteTs: any): any[] { |
| 126 | + // Heuristic: check if arrays contain objects |
| 127 | + const localHasObjects = this.hasObjects(local); |
| 128 | + const remoteHasObjects = this.hasObjects(remote); |
| 129 | + |
| 130 | + // If both don't have objects or mismatch, use LWW |
| 131 | + if (!localHasObjects || !remoteHasObjects || localHasObjects !== remoteHasObjects) { |
| 132 | + return this.compareTimestamps(remoteTs, localTs) > 0 ? remote : local; |
| 133 | + } |
| 134 | + |
| 135 | + // Both have objects - try to merge by ID |
| 136 | + const localMap = this.mapById(local); |
| 137 | + const remoteMap = this.mapById(remote); |
| 138 | + |
| 139 | + // If couldn't create ID maps, fallback to LWW |
| 140 | + if (!localMap || !remoteMap) { |
| 141 | + return this.compareTimestamps(remoteTs, localTs) > 0 ? remote : local; |
| 142 | + } |
| 143 | + |
| 144 | + const result: any[] = []; |
| 145 | + const processedIds = new Set<string>(); |
| 146 | + |
| 147 | + // Process local items |
| 148 | + for (const [id, localItem] of localMap.entries()) { |
| 149 | + processedIds.add(id); |
| 150 | + |
| 151 | + if (remoteMap.has(id)) { |
| 152 | + // Merge recursively |
| 153 | + const remoteItem = remoteMap.get(id)!; |
| 154 | + result.push(this.mergeJson(localItem, localTs, remoteItem, remoteTs)); |
| 155 | + } else { |
| 156 | + // Keep local item |
| 157 | + result.push(localItem); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // Add new remote items |
| 162 | + for (const [id, remoteItem] of remoteMap.entries()) { |
| 163 | + if (!processedIds.has(id)) { |
| 164 | + result.push(remoteItem); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return result; |
| 169 | + } |
| 170 | + |
| 171 | + private hasObjects(arr: any[]): boolean { |
| 172 | + if (arr.length === 0) return false; |
| 173 | + return this.getType(arr[0]) === 'object'; |
| 174 | + } |
| 175 | + |
| 176 | + private mapById(arr: any[]): Map<string, any> | null { |
| 177 | + const map = new Map<string, any>(); |
| 178 | + |
| 179 | + for (const item of arr) { |
| 180 | + if (this.getType(item) !== 'object') return null; |
| 181 | + |
| 182 | + let id: string | null = null; |
| 183 | + if ('id' in item) id = String(item.id); |
| 184 | + else if ('_id' in item) id = String(item._id); |
| 185 | + |
| 186 | + if (!id) return null; // Missing ID |
| 187 | + if (map.has(id)) return null; // Duplicate ID |
| 188 | + |
| 189 | + map.set(id, item); |
| 190 | + } |
| 191 | + |
| 192 | + return map; |
| 193 | + } |
| 194 | + |
| 195 | + private getType(value: any): string { |
| 196 | + if (value === null || value === undefined) return 'null'; |
| 197 | + if (Array.isArray(value)) return 'array'; |
| 198 | + return typeof value; |
| 199 | + } |
| 200 | + |
| 201 | + private compareTimestamps(a: any, b: any): number { |
| 202 | + if (a.wallTime > b.wallTime) return 1; |
| 203 | + if (a.wallTime < b.wallTime) return -1; |
| 204 | + |
| 205 | + if (a.counter > b.counter) return 1; |
| 206 | + if (a.counter < b.counter) return -1; |
| 207 | + |
| 208 | + return a.nodeId.localeCompare(b.nodeId); |
| 209 | + } |
| 210 | +} |
0 commit comments