Skip to content

Commit

Permalink
Fix getGarbageLength to return correct size (yorkie-team#145)
Browse files Browse the repository at this point in the history
* Fix `getGarbageLength` to return correct size
  • Loading branch information
skhugh authored Jan 16, 2024
1 parent c871cb4 commit 99713c1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
strategy:
fail-fast: false
matrix:
api-level: [ 23, 26, 30 ]
api-level: [ 23 ]
target: [ google_apis ]
runs-on: macos-latest
timeout-minutes: 60
Expand Down Expand Up @@ -98,9 +98,9 @@ jobs:
script: |
./gradlew yorkie:connectedCheck -Pandroid.testInstrumentationRunnerArguments.notAnnotation=androidx.test.filters.LargeTest --no-build-cache --no-daemon --stacktrace
./gradlew yorkie:connectedCheck -Pandroid.testInstrumentationRunnerArguments.annotation=androidx.test.filters.LargeTest --no-build-cache --no-daemon --stacktrace
- if: ${{ matrix.api-level == 30 }}
- if: ${{ matrix.api-level == 23 }}
run: ./gradlew yorkie:jacocoDebugTestReport
- if: ${{ matrix.api-level == 30 }}
- if: ${{ matrix.api-level == 23 }}
uses: actions/upload-artifact@v3
with:
name: android-test-artifact
Expand Down
56 changes: 56 additions & 0 deletions yorkie/src/androidTest/kotlin/dev/yorkie/core/GCTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dev.yorkie.document.json.JsonTree.TextNode
import dev.yorkie.document.json.TreeBuilder.element
import dev.yorkie.document.json.TreeBuilder.text
import dev.yorkie.document.time.TimeTicket
import dev.yorkie.document.time.TimeTicket.Companion.MaxTimeTicket
import dev.yorkie.gson
import dev.yorkie.util.IndexTreeNode
import java.util.UUID
Expand Down Expand Up @@ -547,6 +548,61 @@ class GCTest {
}
}

@Test
fun test_getGarbageLength() {
runBlocking {
val c1 = createClient()
val c2 = createClient()
val documentKey = UUID.randomUUID().toString().toDocKey()
val d1 = Document(documentKey)
val d2 = Document(documentKey)

c1.activateAsync().await()
c2.activateAsync().await()

// 1. initial state
c1.attachAsync(d1, isRealTimeSync = false).await()
d1.updateAsync { root, _ ->
val point = root.setNewObject("point")
point["x"] = 0
point["y"] = 0
}.await()
c1.syncAsync().await()
c2.attachAsync(d2, isRealTimeSync = false).await()

// 2. client1 updates doc
d1.updateAsync { root, _ ->
root.remove("point")
}.await()
// point, x, y
assertEquals(d1.garbageLength, 3)

// 3. client2 updates doc
d2.updateAsync { root, _ ->
val point = root.getAs<JsonObject>("point")
point.remove("x")
}.await()
// x
assertEquals(d2.garbageLength, 1)

c1.syncAsync().await()
c2.syncAsync().await()
c1.syncAsync().await()

// point, x, y
val gcNodeLength = 3
assertEquals(d1.garbageLength, gcNodeLength)
assertEquals(d2.garbageLength, gcNodeLength)

// Actual garbage-collected nodes
assertEquals(d1.garbageCollect(MaxTimeTicket), gcNodeLength)
assertEquals(d2.garbageCollect(MaxTimeTicket), gcNodeLength)

c1.deactivateAsync().await()
c2.deactivateAsync().await()
}
}

private fun getNodeLength(root: IndexTreeNode<CrdtTreeNode>): Int {
return root.allChildren.fold(root.allChildren.size) { acc, child ->
acc + getNodeLength(child)
Expand Down
8 changes: 5 additions & 3 deletions yorkie/src/main/kotlin/dev/yorkie/document/crdt/CrdtRoot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,18 @@ internal class CrdtRoot(val rootObject: CrdtObject) {
*/
fun getGarbageLength(): Int {
var count = 0
val seen = mutableSetOf<TimeTicket>()
removedElementSetByCreatedAt.forEach { createdAt ->
count++
seen += createdAt
val pair = elementPairMapByCreatedAt[createdAt] ?: return@forEach
if (pair.element is CrdtContainer) {
pair.element.getDescendants { _, _ ->
count++
pair.element.getDescendants { element, _ ->
seen += element.createdAt
false
}
}
}
count += seen.size

elementHasRemovedNodesSetByCreatedAt.forEach { createdAt ->
val pair = elementPairMapByCreatedAt[createdAt] ?: return@forEach
Expand Down

0 comments on commit 99713c1

Please sign in to comment.