From 8a926d4c9500274ddfe082df71626911f89f520a Mon Sep 17 00:00:00 2001 From: hiddenviewer Date: Fri, 1 Nov 2024 14:56:50 +0900 Subject: [PATCH] Add test case for removing node from indexes during GC --- Sources/Document/CRDT/CRDTText.swift | 14 ++++++++--- Sources/Document/CRDT/RGATreeSplit.swift | 14 ++++++++--- Sources/Document/Json/JSONText.swift | 14 ++++++++--- Tests/Integration/TextIntegrationTests.swift | 4 +-- Tests/Unit/Document/DocumentTests.swift | 26 ++++++++++++++++++++ 5 files changed, 58 insertions(+), 14 deletions(-) diff --git a/Sources/Document/CRDT/CRDTText.swift b/Sources/Document/CRDT/CRDTText.swift index 735c1888..398c1ce3 100644 --- a/Sources/Document/CRDT/CRDTText.swift +++ b/Sources/Document/CRDT/CRDTText.swift @@ -313,11 +313,17 @@ final class CRDTText: CRDTElement { } /** - * `checkWeight` returns false when there is an incorrect weight node. - * for debugging purpose. + * `getTreeByIndex` returns the tree by index for debugging. + */ + func getTreeByIndex() -> SplayTree { + return self.rgaTreeSplit.getTreeByIndex() + } + + /** + * `getTreeByID` returns the tree by ID for debugging. */ - func checkWeight() -> Bool { - self.rgaTreeSplit.checkWeight() + func getTreeByID() -> LLRBTree> { + return self.rgaTreeSplit.getTreeByID() } /** diff --git a/Sources/Document/CRDT/RGATreeSplit.swift b/Sources/Document/CRDT/RGATreeSplit.swift index cb45b056..d92e9b21 100644 --- a/Sources/Document/CRDT/RGATreeSplit.swift +++ b/Sources/Document/CRDT/RGATreeSplit.swift @@ -535,11 +535,17 @@ class RGATreeSplit { } /** - * `checkWeight` returns false when there is an incorrect weight node. - * for debugging purpose. + * `getTreeByIndex` returns the tree by index for debugging purpose. + */ + public func getTreeByIndex() -> SplayTree { + return self.treeByIndex + } + + /** + * `getTreeByID` returns the tree by ID for debugging purpose. */ - public func checkWeight() -> Bool { - self.treeByIndex.checkWeight() + public func getTreeByID() -> LLRBTree> { + return self.treeByID } /** diff --git a/Sources/Document/Json/JSONText.swift b/Sources/Document/Json/JSONText.swift index ad3bb8fc..11f2801b 100644 --- a/Sources/Document/Json/JSONText.swift +++ b/Sources/Document/Json/JSONText.swift @@ -247,11 +247,17 @@ public class JSONText { } /** - * `checkWeight` returns false when there is an incorrect weight node. - * for debugging purpose. + * `getTreeByIndex` returns the tree by index for debugging. + */ + func getTreeByIndex() -> SplayTree? { + return self.text?.getTreeByIndex() + } + + /** + * `getTreeByID` returns the tree by ID for debugging. */ - public func checkWeight() -> Bool { - self.text?.checkWeight() ?? false + func getTreeByID() -> LLRBTree>? { + return self.text?.getTreeByID() } /** diff --git a/Tests/Integration/TextIntegrationTests.swift b/Tests/Integration/TextIntegrationTests.swift index bc5ad756..399168b7 100644 --- a/Tests/Integration/TextIntegrationTests.swift +++ b/Tests/Integration/TextIntegrationTests.swift @@ -238,8 +238,8 @@ final class TextIntegrationTests: XCTestCase { try await c2.sync() try await c1.sync() - let d1Check = await(d1.getRoot().k1 as? JSONText)?.checkWeight() ?? false - let d2Check = await(d2.getRoot().k1 as? JSONText)?.checkWeight() ?? false + let d1Check = await(d1.getRoot().k1 as? JSONText)?.getTreeByIndex()?.checkWeight() ?? false + let d2Check = await(d2.getRoot().k1 as? JSONText)?.getTreeByIndex()?.checkWeight() ?? false XCTAssertTrue(d1Check) XCTAssertTrue(d2Check) } diff --git a/Tests/Unit/Document/DocumentTests.swift b/Tests/Unit/Document/DocumentTests.swift index 96cc0c8a..d26a639c 100644 --- a/Tests/Unit/Document/DocumentTests.swift +++ b/Tests/Unit/Document/DocumentTests.swift @@ -1056,6 +1056,32 @@ class DocumentTests: XCTestCase { XCTAssertEqual(length, 0) } + func test_should_purge_node_from_indexes_during_GC() async throws { + let doc = Document(key: "test-doc") + + try await doc.update { root, _ in + root.k1 = JSONText() + } + var size = await(doc.getRoot().k1 as? JSONText)?.getTreeByID()?.size + XCTAssertEqual(size, 1) + + try await doc.update { root, _ in + (root.k1 as? JSONText)?.edit(0, 0, "ABC") + } + size = await(doc.getRoot().k1 as? JSONText)?.getTreeByID()?.size + XCTAssertEqual(size, 2) + + try await doc.update { root, _ in + (root.k1 as? JSONText)?.edit(1, 3, "") + } + size = await(doc.getRoot().k1 as? JSONText)?.getTreeByID()?.size + XCTAssertEqual(size, 3) + + await doc.garbageCollect(TimeTicket.max) + size = await(doc.getRoot().k1 as? JSONText)?.getTreeByID()?.size + XCTAssertEqual(size, 2) + } + func test_escapes_string_for_object() async throws { let target = Document(key: "test-doc") try await target.update { root, _ in