-
Notifications
You must be signed in to change notification settings - Fork 3
[Feature] 화이트보드 오브젝트의 실시간 데이터 일관성을 보장하기 위한 레지스터 구현 #151
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d43bb1e
feature: 실시간 데이터 일관성 보장을 위한 Register구현(#149)
eemdeeks 04bddb2
feature: 클래스로 LWW레지스터 수정(#149)
eemdeeks 4669258
feature: LWW레지스터 집합 설계(#149)
eemdeeks 88a22ac
feature: LWW레지스터 집합 구현(#149)
eemdeeks adb517a
refactor: LWWRegister 리펙토링(#149)
eemdeeks 938c65f
feature: LWWRegister 테스트(#149)
eemdeeks d2ce52d
refactor: LWWRegister에 deepcopy사용하기 위한 merge
eemdeeks f277227
refactor: whiteboardObject deepCopy로 관리
eemdeeks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Domain/Domain/Sources/Interface/WhiteboardObjectRegistersInterface.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // | ||
| // WhiteboardObjectRegistersInterface.swift | ||
| // Domain | ||
| // | ||
| // Created by 박승찬 on 1/6/25. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| public protocol WhiteboardObjectRegistersInterface { | ||
| /// 집합에 레지스터가 있는지 확인합니다. | ||
| /// - Parameter register: 확인할 레지스터 | ||
| /// - Returns: 오브젝트 존재 여부 | ||
| func contains(register: LWWRegister) async -> Bool | ||
|
|
||
| /// 집합에 레지스터를 추가합니다. | ||
| /// - Parameter object: 추가할 레지스터 | ||
| func insert(register: LWWRegister) async | ||
|
|
||
| /// 집합에서 레지스터를 삭제합니다. | ||
| /// - Parameter register: 삭제할 레지스터 | ||
| func remove(register: LWWRegister) async | ||
|
|
||
| /// 모든 화이트보드 오브젝트 레지스터들을 삭제합니다. | ||
| func removeAll() async | ||
|
|
||
| /// 집합에 있는 레지스터를 업데이트 합니다. | ||
| /// - Parameter register: 업데이트할 레지스터 | ||
| func update(register: LWWRegister) async | ||
|
|
||
| /// ID로 집합에있는 레지스터를 가져옵니다. | ||
| /// - Parameter id: 가져올 레지스터의 오브젝트 ID | ||
| /// - Returns: 레지스터 | ||
| func fetchObjectByID(id: UUID) async -> LWWRegister? | ||
|
|
||
| /// 모든 화이트보드 오브젝트 레지스터들을 가져옵니다. | ||
| /// - Returns: 화이트보드 레지스터 배열 | ||
| func fetchAll() async -> [LWWRegister] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // | ||
| // LWWRegister.swift | ||
| // Domain | ||
| // | ||
| // Created by 박승찬 on 1/6/25. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| public struct LWWRegister { | ||
| public let whiteboardObject: WhiteboardObject | ||
| private let timestamp: Timestamp | ||
|
|
||
| public init(whiteboardObject: WhiteboardObject, timestamp: Timestamp) { | ||
| self.whiteboardObject = whiteboardObject | ||
| self.timestamp = timestamp | ||
| } | ||
|
|
||
| public func merge(register: LWWRegister) -> LWWRegister { | ||
| timestamp < register.timestamp ? register: self | ||
| } | ||
| } | ||
|
|
||
| extension LWWRegister: Hashable { | ||
| public func hash(into hasher: inout Hasher) { | ||
| hasher.combine(whiteboardObject) | ||
| } | ||
| } | ||
|
|
||
| extension LWWRegister: Comparable { | ||
| public static func < (lhs: LWWRegister, rhs: LWWRegister) -> Bool { | ||
| lhs.timestamp < rhs.timestamp | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // | ||
| // Timestamp.swift | ||
| // Domain | ||
| // | ||
| // Created by 박승찬 on 1/6/25. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| public struct Timestamp: Comparable { | ||
| let updatedAt: Date | ||
| let updatedBy: UUID | ||
|
|
||
| public init(updatedAt: Date, updatedBy: UUID) { | ||
| self.updatedAt = updatedAt | ||
| self.updatedBy = updatedBy | ||
| } | ||
|
|
||
| public static func < (lhs: Timestamp, rhs: Timestamp) -> Bool { | ||
| if lhs.updatedAt == rhs.updatedAt { return lhs.updatedBy < rhs.updatedBy } | ||
| return lhs.updatedAt < rhs.updatedAt | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
Domain/Domain/Sources/Model/WhiteboardObjectRegisters.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // | ||
| // WhiteboardObjectRegisters.swift | ||
| // Domain | ||
| // | ||
| // Created by 박승찬 on 1/6/25. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| actor WhiteboardObjectRegisters: WhiteboardObjectRegistersInterface { | ||
| private var registers: Set<LWWRegister> | ||
|
|
||
| init() { | ||
| registers = [] | ||
| } | ||
|
|
||
| func contains(register: LWWRegister) async -> Bool { | ||
| registers.contains(register) | ||
| } | ||
|
|
||
| func insert(register: LWWRegister) async { | ||
| registers.insert(register) | ||
| } | ||
|
|
||
| func remove(register: LWWRegister) async { | ||
| registers.remove(register) | ||
| } | ||
|
|
||
| func removeAll() async { | ||
| registers.removeAll() | ||
| } | ||
|
|
||
| func update(register: LWWRegister) async { | ||
| if registers.contains(register) { | ||
| registers.remove(register) | ||
| await insert(register: register.merge(register: register)) | ||
| } else { | ||
| await insert(register: register) | ||
| } | ||
| } | ||
|
|
||
| func fetchObjectByID(id: UUID) async -> LWWRegister? { | ||
| registers.first(where: { $0.whiteboardObject.id == id }) | ||
| } | ||
|
|
||
| func fetchAll() async -> [LWWRegister] { | ||
| Array(registers.sorted { $0 < $1 }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // | ||
| // LWWRegisterTests.swift | ||
| // DomainTests | ||
| // | ||
| // Created by 박승찬 on 1/8/25. | ||
| // | ||
|
|
||
| import Domain | ||
| import XCTest | ||
|
|
||
| final class LWWRegisterTests: XCTestCase { | ||
| private var register: LWWRegister! | ||
| private var defaultTimestamp: Timestamp! | ||
| private var defaultDate: Date! | ||
| private var defaultObject: WhiteboardObject! | ||
|
|
||
| override func setUp() { | ||
| super.setUp() | ||
| defaultObject = TextObject( | ||
| id: UUID(), | ||
| centerPosition: CGPoint(x: 0, y: 0), | ||
| size: CGSize(width: 100, height: 100), | ||
| text: "default") | ||
| defaultDate = Date() | ||
| defaultTimestamp = Timestamp(updatedAt: defaultDate, updatedBy: UUID()) | ||
| register = LWWRegister(whiteboardObject: defaultObject, timestamp: defaultTimestamp) | ||
| } | ||
|
|
||
| override func tearDown() { | ||
| register = nil | ||
| defaultTimestamp = nil | ||
| defaultDate = nil | ||
| defaultObject = nil | ||
| } | ||
|
|
||
| // Timestamp가 같을 때 | ||
| func testMergeWhenEqualTimestmap() { | ||
| // 준비 | ||
| let textObject = TextObject( | ||
| id: UUID(), | ||
| centerPosition: CGPoint(x: 50, y: 50), | ||
| size: CGSize(width: 200, height: 200), | ||
| text: "equal") | ||
| let mockRegister = LWWRegister(whiteboardObject: textObject, timestamp: defaultTimestamp) | ||
|
|
||
| // 실행 | ||
| let sut = register.merge(register: mockRegister) | ||
|
|
||
| // 검증 | ||
| XCTAssertEqual(sut, register) | ||
| } | ||
|
|
||
| // 새로 들어온 updatedAt이 더 빠를 때 | ||
| func testMergeWhenIncomingTimestampIsEarlier() { | ||
| // 준비 | ||
| let earlierDate = defaultDate.addingTimeInterval(-10) | ||
| let earlierTimestamp = Timestamp(updatedAt: earlierDate, updatedBy: UUID()) | ||
| let textObject = TextObject( | ||
| id: UUID(), | ||
| centerPosition: CGPoint(x: 50, y: 50), | ||
| size: CGSize(width: 200, height: 200), | ||
| text: "incoming") | ||
| let mockRegister = LWWRegister(whiteboardObject: textObject, timestamp: earlierTimestamp) | ||
|
|
||
| // 실행 | ||
| let sut = register.merge(register: mockRegister) | ||
|
|
||
| // 검증 | ||
| XCTAssertEqual(sut, register) | ||
| } | ||
|
|
||
| // updatedAt은 같지만 새로 들어온 UUID가 작을 때 | ||
| func testMergeWhenIncomingTimestampHasSmallerUUID() { | ||
| // 준비 | ||
| guard let smallerUUID = UUID(uuidString: "00000000-0000-0000-0000-000000000000") else { | ||
| XCTFail("Test UUID생성 실패") | ||
| return | ||
| } | ||
| let smallerTimestamp = Timestamp(updatedAt: defaultDate, updatedBy: smallerUUID) | ||
| let textObject = TextObject( | ||
| id: UUID(), | ||
| centerPosition: CGPoint(x: 50, y: 50), | ||
| size: CGSize(width: 200, height: 200), | ||
| text: "incoming") | ||
| let mockRegister = LWWRegister(whiteboardObject: textObject, timestamp: smallerTimestamp) | ||
|
|
||
| // 실행 | ||
| let sut = register.merge(register: mockRegister) | ||
|
|
||
| // 검증 | ||
| XCTAssertEqual(sut, register) | ||
| } | ||
|
|
||
| // 새로 들어온 updatedAt이 더 느릴 때 | ||
| func testMergeWhenIncomingTimestampIsLater() { | ||
| // 준비 | ||
| let laterDate = defaultDate.addingTimeInterval(10) | ||
| let laterTimestamp = Timestamp(updatedAt: laterDate, updatedBy: UUID()) | ||
| let textObject = TextObject( | ||
| id: UUID(), | ||
| centerPosition: CGPoint(x: 50, y: 50), | ||
| size: CGSize(width: 200, height: 200), | ||
| text: "incoming") | ||
| let mockRegister = LWWRegister(whiteboardObject: textObject, timestamp: laterTimestamp) | ||
|
|
||
| // 실행 | ||
| let sut = register.merge(register: mockRegister) | ||
|
|
||
| // 검증 | ||
| XCTAssertEqual(sut, mockRegister) | ||
| } | ||
|
|
||
| // updatedAt은 같지만 새로 들어온 UUID가 클 때 | ||
| func testMergeWhenIncomingTimestampHasLargerUUID() { | ||
| // 준비 | ||
| guard let largerUUID = UUID(uuidString: "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF") else { | ||
| XCTFail("Test UUID생성 실패") | ||
| return | ||
| } | ||
| let largerTimestamp = Timestamp(updatedAt: defaultDate, updatedBy: largerUUID) | ||
| let textObject = TextObject( | ||
| id: UUID(), | ||
| centerPosition: CGPoint(x: 50, y: 50), | ||
| size: CGSize(width: 200, height: 200), | ||
| text: "incoming") | ||
| let mockRegister = LWWRegister(whiteboardObject: textObject, timestamp: largerTimestamp) | ||
|
|
||
| // 실행 | ||
| let sut = register.merge(register: mockRegister) | ||
|
|
||
| // 검증 | ||
| XCTAssertEqual(sut, mockRegister) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 요 부분 덕분에 updatedAt이 같더라도, 우선 순위가 생기겠군요 죠습니다 🦈