Skip to content

Commit

Permalink
Add DisableGC option to document (#136)
Browse files Browse the repository at this point in the history
Add DisableGC option to document
  • Loading branch information
7hong13 authored Oct 17, 2023
1 parent be1e634 commit d244681
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
24 changes: 24 additions & 0 deletions yorkie/src/androidTest/kotlin/dev/yorkie/core/GCTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,30 @@ class GCTest {
}
}

@Test
fun test_disable_gc() = runBlocking {
val documentKey = UUID.randomUUID().toString().toDocKey()
val document = Document(documentKey, Document.Options(disableGC = true))
document.updateAsync { root, _ ->
root["1"] = 1
root.setNewArray("2").apply {
put(1)
put(2)
put(3)
}
root["3"] = 3
}.await()
assertJsonContentEquals("""{"1":1,"2":[1,2,3],"3":3}""", document.toJson())

document.updateAsync { root, _ ->
root.remove("2")
}.await()
assertJsonContentEquals("""{"1":1, "3":3}""", document.toJson())
assertEquals(4, document.garbageLength)
assertEquals(0, document.garbageCollect(TimeTicket.MaxTimeTicket))
assertEquals(4, document.garbageLength)
}

private fun getNodeLength(root: IndexTreeNode<CrdtTreeNode>): Int {
return root.allChildren.fold(root.allChildren.size) { acc, child ->
acc + getNodeLength(child)
Expand Down
13 changes: 12 additions & 1 deletion yorkie/src/main/kotlin/dev/yorkie/document/Document.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import kotlinx.coroutines.withContext
* A CRDT-based data type.
* We can represent the model of the application and edit it even while offline.
*/
public class Document(public val key: Key) {
public class Document(public val key: Key, private val options: Options = Options()) {
private val dispatcher = createSingleThreadDispatcher("Document($key)")
private val scope = CoroutineScope(SupervisorJob() + dispatcher)
private val localChanges = mutableListOf<Change>()
Expand Down Expand Up @@ -353,6 +353,10 @@ public class Document(public val key: Key) {
* Deletes elements that were removed before the given time.
*/
internal fun garbageCollect(ticket: TimeTicket): Int {
if (options.disableGC) {
return 0
}

clone?.root?.garbageCollect(ticket)
return root.garbageCollect(ticket)
}
Expand Down Expand Up @@ -482,6 +486,13 @@ public class Document(public val key: Key) {
Removed,
}

public data class Options(
/**
* Disables garbage collection if true.
*/
public val disableGC: Boolean = false,
)

internal data class RootClone(
val root: CrdtRoot,
val presences: Presences,
Expand Down

0 comments on commit d244681

Please sign in to comment.