0

我正在做一个项目,我必须同步来自多个用户的离线数据,这些用户以安全的方式处理相同的数据,我已经开始研究如何去做。

第一个想法当然是在需要同步的每一行数据上保存时间戳和脏标志,但是当三个以上的用户处理相同的数据时,依靠这个是有风险的。因此,经过一番研究,我遇到了Merkle Tree的概念,它用于各种解决方案以避免数据退化。

但我想知道你会怎么做呢?我了解如何散列字符串的值,但是如何创建对象的散列?您是否会获取每个参数并将其相互连接并从该连接中创建散列?

考虑这个组成的对象。您将如何创建它的 sha1-hash 并将 Merkle Tree 解决方案应用于此?

@Entity(tableName = "tasks")
data class Task(
    @PrimaryKey
    val taskId: String,

    val title: String,

    /** Status of the given task.
     * Enumerated Values: 0 (Active), 1 (Inactive), 2 (Completed)
     */
    @TypeConverters(StatusConverter::class)
    val status: Task.Status,

    @TypeConverters(DateConverter::class)
    var startDate: Date? = null,

    @Embedded
    val syncEntity: SyncEntity
)  : Syncable by syncEntity

这是Syncable界面:

interface Syncable {
    val createdAt: Long
    val updatedAt: Long?
    val deleted: Boolean
}

/**
 * Base class for all Room entities that are synchronized.
 */
data class SyncEntity(
    /** Specifies the point in time when an object was first created */
    @ColumnInfo(name = "created_at")
    override val createdAt: Long = System.currentTimeMillis(),

    /** Specifies the point in time when an object was last updated. If
     * the objects hasn't been updated the value returns null
     * */
    @ColumnInfo(name = "updated_at") override val updatedAt: Long? = null,

    /**
     * Specifies if an object is marked as deleted
     */
    override val deleted: Boolean = false
) : Syncable

提供接口实现的Task委托。SyncEntitySyncable

希望这个问题有意义吗?

4

0 回答 0