genosdb-server
Version:
GenosDB Server: A persistent Node.js peer for the GenosDB distributed graph database. Provides a reliable file-system storage layer, ensuring data durability and network resilience for your real-time, peer-to-peer applications.
29 lines (24 loc) • 1.24 kB
JavaScript
// Maximum allowed future timestamp drift (2 hours)
const MAX_FUTURE_DRIFT_MS = 7200000;
/**
* Resolves a conflict between a local node and an incoming remote change.
* Uses Last-Write-Wins (LWW) strategy at the object level.
*
* @param {{ value: any, timestamp: any } | null} localNode - Local node.
* @param {{ value: any, timestamp: any }} remoteChange - Incoming remote change.
* @param {HybridClock} clock - Hybrid clock to compare timestamps.
* @returns {{ resolved: boolean, value?: any, timestamp?: any }}
*/
export function resolveConflict(localNode, remoteChange, clock) {
let ts = remoteChange.timestamp;
// Cap remote timestamp to prevent excessive future drift
if (ts?.physical > Date.now() + MAX_FUTURE_DRIFT_MS) {
ts = { physical: Date.now() + MAX_FUTURE_DRIFT_MS, logical: ts.logical };
}
// If local node doesn't exist, accept remote change
if (!localNode?.timestamp) return { resolved: true, value: remoteChange.value, timestamp: ts };
// If local timestamp is newer or equal, do not overwrite
if (clock.compare(ts, localNode.timestamp) <= 0) return { resolved: false };
// Remote wins: apply LWW at object level
return { resolved: true, value: remoteChange.value, timestamp: ts };
}