LwwMerger.kt 28 lignes · 1192 octets
package fr.ebii.card2vcf.sync

object LwwMerger {
    /** Returns remote if remoteTs >= localTs (remote wins ties), else local. */
    fun <T> pickLww(local: T, localTs: Long, remote: T, remoteTs: Long): T =
        if (remoteTs >= localTs) remote else local

    data class TaskPlacement(
        val colonneId: String,
        val ordre: Int,
        val updatedAt: Long,
    )

    fun pickTaskMove(local: TaskPlacement, remote: TaskPlacement): TaskPlacement =
        pickLww(local, local.updatedAt, remote, remote.updatedAt)

    /** Append-only: insert interaction only if serverId is not already known locally. */
    fun shouldInsertInteraction(existingServerIds: Set<String>, remoteServerId: String): Boolean =
        remoteServerId !in existingServerIds

    /** LWW update : true si le distant est strictement plus récent que le local. */
    fun shouldUpdateInteraction(localTs: Long, remoteTs: Long): Boolean =
        remoteTs > localTs

    /** Caller deletes local rows by serverId; returns remaining ids after tombstone application. */
    fun applyTombstoneIds(localServerIds: Set<String>, tombstoneIds: Set<String>): Set<String> =
        localServerIds - tombstoneIds
}