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

Add DisableGC option to document #117

Merged
merged 1 commit into from
Sep 25, 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
22 changes: 22 additions & 0 deletions Sources/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
import Combine
import Foundation

/**
* `DocumentOptions` are the options to create a new document.
*
* @public
*/
public struct DocumentOptions {
/**
* `disableGC` disables garbage collection if true.
*/
var disableGC: Bool
}

/**
* `DocumentStatus` represents the status of the document.
*/
Expand Down Expand Up @@ -59,6 +71,7 @@ public actor Document {

private let key: DocumentKey
private(set) var status: DocumentStatus
private let opts: DocumentOptions
private var changeID: ChangeID
var checkpoint: Checkpoint
private var localChanges: [Change]
Expand All @@ -81,8 +94,13 @@ public actor Document {
private var presences: [ActorID: StringValueTypeDictionary]

public init(key: String) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to declare as a convenience initializer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The document is an actor. So I can't declare a convenience initializer.

스크린샷 2023-09-25 오후 7 33 48

self.init(key: key, opts: DocumentOptions(disableGC: false))
}

public init(key: String, opts: DocumentOptions) {
self.key = key
self.status = .detached
self.opts = opts
self.root = CRDTRoot()
self.changeID = ChangeID.initial
self.checkpoint = Checkpoint.initial
Expand Down Expand Up @@ -300,6 +318,10 @@ public actor Document {
*/
@discardableResult
func garbageCollect(lessThanOrEqualTo ticket: TimeTicket) -> Int {
if self.opts.disableGC {
return 0
}

if let clone = self.clone {
clone.root.garbageCollect(lessThanOrEqualTo: ticket)
}
Expand Down
27 changes: 27 additions & 0 deletions Tests/Integration/GCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,33 @@ class GCTests: XCTestCase {
XCTAssertEqual(0, len)
}

func test_disable_GC_test() async throws {
let doc = Document(key: "test-doc", opts: DocumentOptions(disableGC: true))

try await doc.update({ root, _ in
root["1"] = Int64(1)
root["2"] = [Int64(1), Int64(2), Int64(3)]
root["3"] = Int64(3)
}, "set 1, 2, 3")

var result = await doc.toSortedJSON()
XCTAssertEqual("{\"1\":1,\"2\":[1,2,3],\"3\":3}", result)

try await doc.update({ root, _ in
root.remove(key: "2")
}, "deletes 2")

result = await doc.toSortedJSON()
XCTAssertEqual("{\"1\":1,\"3\":3}", result)

var len = await doc.getGarbageLength()
XCTAssertEqual(4, len)
len = await doc.garbageCollect(lessThanOrEqualTo: TimeTicket.max)
XCTAssertEqual(0, len)
len = await doc.getGarbageLength()
XCTAssertEqual(4, len)
}

func test_garbage_collection_test2() async throws {
let size = 10000
let doc = Document(key: "test-doc")
Expand Down