Skip to content

Commit

Permalink
Fix missing collection of removed elements from the root (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
humdrum authored Nov 2, 2023
1 parent d80ad48 commit 46c7351
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Sources/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ public actor Document {
return self.root.garbageLength
}

/**
* `getGarbageLengthFromClone` returns the length of elements should be purged from clone.
*/
func getGarbageLengthFromClone() -> Int {
return self.clone?.root.garbageLength ?? 0
}

/**
* `toJSON` returns the JSON encoding of this array.
*/
Expand Down
5 changes: 4 additions & 1 deletion Sources/Document/Operation/SetOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ struct SetOperation: Operation {
}

let value = self.value.deepcopy()
parent.set(key: self.key, value: value)
let removed = parent.set(key: self.key, value: value)
root.registerElement(value, parent: parent)
if let removed {
root.registerRemovedElement(removed)
}

guard let path = try? root.createPath(createdAt: parentCreatedAt) else {
throw YorkieError.unexpected(message: "fail to get path")
Expand Down
30 changes: 30 additions & 0 deletions Tests/Integration/GCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -739,4 +739,34 @@ class GCTests: XCTestCase {
try await client1.deactivate()
try await client2.deactivate()
}

func test_can_collect_removed_elements_from_both_root_and_clone() async throws {
let options = ClientOptions()
let docKey = "\(self.description)-\(Date().description)".toDocKey

let doc = Document(key: docKey)

let client = Client(rpcAddress: rpcAddress, options: options)

try await client.activate()

try await client.attach(doc, [:], false)

try await doc.update { root, _ in
root.point = ["x": Int64(0), "y": Int64(0)]
}

try await doc.update { root, _ in
root.point = ["x": Int64(1), "y": Int64(1)]
}

try await doc.update { root, _ in
root.point = ["x": Int64(2), "y": Int64(2)]
}

var len = await doc.getGarbageLength()
XCTAssertEqual(len, 6)
len = await doc.getGarbageLengthFromClone()
XCTAssertEqual(len, 6)
}
}
2 changes: 1 addition & 1 deletion Tests/Unit/Document/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ class DocumentTests: XCTestCase {
{}
""")
var length = await target.getGarbageLength()
XCTAssertEqual(length, 1)
XCTAssertEqual(length, 2)

await target.garbageCollect(lessThanOrEqualTo: TimeTicket.max)
result = await target.toSortedJSON()
Expand Down

0 comments on commit 46c7351

Please sign in to comment.