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

Fix missing collection of removed elements from the root #125

Merged
merged 1 commit into from
Nov 2, 2023
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
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