Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements ChangePack, CheckPoint #25

Merged
merged 3 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions yorkie/src/main/kotlin/dev/yorkie/document/change/ChangePack.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.yorkie.document.change

import com.google.protobuf.ByteString
import dev.yorkie.document.time.TimeTicket

/**
* [ChangePack] is a unit for delivering changes in a document to the remote.
*
* @property documentKey the key of the document.
* @property checkPoint It is used to determine the client received changes.
* @property snapshot a byte array that encodes the document.
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
* @property minSyncedTicket the minimum logical time taken by clients who attach to the document.
* It is used to collect garbage on the replica on the client.
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
*/
internal class ChangePack(
val documentKey: String,
val checkPoint: CheckPoint,
val changes: List<Change>,
val snapshot: ByteString?,
val minSyncedTicket: TimeTicket?,
) {

/**
* Returns the whether this [ChangePack] has changes or not.
*/
val hasChanges
get() = changes.isNotEmpty()

/**
* Returns the whether this [ChangePack] has a snapshot or not.
*/
val hasSnapshot
get() = snapshot?.isEmpty == false
}
42 changes: 42 additions & 0 deletions yorkie/src/main/kotlin/dev/yorkie/document/change/CheckPoint.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.yorkie.document.change

import kotlin.math.max

/**
* [CheckPoint] is used to determine the changes sent and received by the client.
* This is immutable.
*/
internal data class CheckPoint(
val serverSeq: Long,
val clientSeq: Int,
) {

/**
* Creates a new instance with increased client sequence.
*/
fun increaseClientSeq(increase: Int): CheckPoint {
return if (increase == 0) {
this
} else {
CheckPoint(serverSeq, clientSeq + increase)
}
}

/**
* Creates a new instance with the given [CheckPoint] if it is greater
* than the values of internal properties.
*/
fun forward(checkPoint: CheckPoint): CheckPoint {
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
return if (this == checkPoint) {
this
} else {
val serverSeq = max(serverSeq, checkPoint.serverSeq)
val clientSeq = max(clientSeq, checkPoint.clientSeq)
CheckPoint(serverSeq, clientSeq)
}
}

companion object {
val InitialCheckPoint = CheckPoint(0, 0)
}
}