-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of https://github.com/boostcampwm-2024/web33-Nocta โฆ
โฆinto Feature/#104_ํญ_๋ธ๋ผ์ฐ์ง_๋ฐ์ํ_๊ตฌํ
- Loading branch information
Showing
5 changed files
with
444 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,129 @@ | ||
import { LinkedList } from "./LinkedList"; | ||
import { NodeId, Node } from "./Node"; | ||
import { RemoteInsertOperation, RemoteDeleteOperation, SerializedProps } from "./Interfaces"; | ||
|
||
export class CRDT { | ||
clock: number; | ||
client: number; | ||
textLinkedList: LinkedList; | ||
|
||
constructor(client: number) { | ||
this.clock = 0; // ์ด CRDT์ ๋ ผ๋ฆฌ์ ์๊ฐ ์ค์ | ||
this.client = client; | ||
this.textLinkedList = new LinkedList(); | ||
} | ||
|
||
/** | ||
* ๋ก์ปฌ์์ ์ฝ์ ์ฐ์ฐ์ ์ํํ๊ณ , ์๊ฒฉ์ ์ ํํ ์ฐ์ฐ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. | ||
* @param index ์ฝ์ ํ ์ธ๋ฑ์ค | ||
* @param value ์ฝ์ ํ ๊ฐ | ||
* @returns ์๊ฒฉ์ ์ ํํ ์ฝ์ ์ฐ์ฐ ๊ฐ์ฒด | ||
*/ | ||
localInsert(index: number, value: string): RemoteInsertOperation { | ||
const id = new NodeId((this.clock += 1), this.client); | ||
const remoteInsertion = this.textLinkedList.insertAtIndex(index, value, id); | ||
return { node: remoteInsertion.node }; | ||
} | ||
|
||
/** | ||
* ๋ก์ปฌ์์ ์ญ์ ์ฐ์ฐ์ ์ํํ๊ณ , ์๊ฒฉ์ ์ ํํ ์ฐ์ฐ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. | ||
* @param index ์ญ์ ํ ์ธ๋ฑ์ค | ||
* @returns ์๊ฒฉ์ ์ ํํ ์ญ์ ์ฐ์ฐ ๊ฐ์ฒด | ||
*/ | ||
localDelete(index: number): RemoteDeleteOperation { | ||
// ์ ํจํ ์ธ๋ฑ์ค์ธ์ง ํ์ธ | ||
if (index < 0 || index >= this.textLinkedList.spread().length) { | ||
throw new Error(`์ ํจํ์ง ์์ ์ธ๋ฑ์ค์ ๋๋ค: ${index}`); | ||
} | ||
|
||
// ์ญ์ ํ ๋ ธ๋ ์ฐพ๊ธฐ | ||
const nodeToDelete = this.textLinkedList.findByIndex(index); | ||
if (!nodeToDelete) { | ||
throw new Error(`์ญ์ ํ ๋ ธ๋๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. ์ธ๋ฑ์ค: ${index}`); | ||
} | ||
|
||
// ์ญ์ ์ฐ์ฐ ๊ฐ์ฒด ์์ฑ | ||
const operation: RemoteDeleteOperation = { | ||
targetId: nodeToDelete.id, | ||
clock: this.clock + 1, | ||
}; | ||
|
||
// ๋ก์ปฌ ์ญ์ ์ํ | ||
this.textLinkedList.deleteNode(nodeToDelete.id); | ||
|
||
// ํด๋ก ์ ๋ฐ์ดํธ | ||
this.clock += 1; | ||
|
||
return operation; | ||
} | ||
|
||
/** | ||
* ์๊ฒฉ์์ ์ฝ์ ์ฐ์ฐ์ ์์ ํ์ ๋ ์ฒ๋ฆฌํฉ๋๋ค. | ||
* @param operation ์๊ฒฉ ์ฝ์ ์ฐ์ฐ ๊ฐ์ฒด | ||
*/ | ||
remoteInsert(operation: RemoteInsertOperation): void { | ||
const newNodeId = new NodeId(operation.node.id.clock, operation.node.id.client); | ||
const newNode = new Node(operation.node.value, newNodeId); | ||
newNode.next = operation.node.next; | ||
newNode.prev = operation.node.prev; | ||
this.textLinkedList.insertById(newNode); | ||
// ๋๊ธฐํ ๋ ผ๋ฆฌ์ ์๊ฐ | ||
if (this.clock <= newNode.id.clock) { | ||
this.clock = newNode.id.clock + 1; | ||
} | ||
} | ||
|
||
/** | ||
* ์๊ฒฉ์์ ์ญ์ ์ฐ์ฐ์ ์์ ํ์๋ ์ฒ๋ฆฌํฉ๋๋ค. | ||
* @param operation ์๊ฒฉ ์ญ์ ์ฐ์ฐ ๊ฐ์ฒด | ||
*/ | ||
remoteDelete(operation: RemoteDeleteOperation): void { | ||
const { targetId, clock } = operation; | ||
if (targetId) { | ||
this.textLinkedList.deleteNode(targetId); | ||
} | ||
// ๋๊ธฐํ ๋ ผ๋ฆฌ์ ์๊ฐ | ||
if (this.clock <= clock) { | ||
this.clock = clock + 1; | ||
} | ||
} | ||
|
||
/** | ||
* ํ์ฌ ํ ์คํธ๋ฅผ ๋ฌธ์์ด๋ก ๋ฐํํฉ๋๋ค. | ||
* @returns ํ์ฌ ํ ์คํธ | ||
*/ | ||
read(): string { | ||
return this.textLinkedList.stringify(); | ||
} | ||
|
||
/** | ||
* ํ์ฌ ํ ์คํธ๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํํฉ๋๋ค. | ||
* @returns ํ์ฌ ํ ์คํธ ๋ฐฐ์ด | ||
*/ | ||
spread(): string[] { | ||
return this.textLinkedList.spread(); | ||
} | ||
|
||
/** | ||
* textLinkedList๋ฅผ ๋ฐํํ๋ getter ๋ฉ์๋ | ||
* @returns LinkedList ์ธ์คํด์ค | ||
*/ | ||
public getTextLinkedList(): LinkedList { | ||
return this.textLinkedList; | ||
} | ||
|
||
/** | ||
* CRDT์ ์ํ๋ฅผ ์ง๋ ฌํ ๊ฐ๋ฅํ ๊ฐ์ฒด๋ก ๋ฐํํฉ๋๋ค. | ||
* @returns ์ง๋ ฌํ ๊ฐ๋ฅํ CRDT ์ํ | ||
*/ | ||
serialize(): SerializedProps { | ||
return { | ||
clock: this.clock, | ||
client: this.client, | ||
textLinkedList: { | ||
head: this.textLinkedList.head, | ||
nodeMap: this.textLinkedList.nodeMap, | ||
}, | ||
}; | ||
} | ||
} |
This file contains 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,32 @@ | ||
import { NodeId, Node } from "./Node"; | ||
|
||
export interface InsertOperation { | ||
node: Node; | ||
} | ||
|
||
export interface DeleteOperation { | ||
targetId: NodeId | null; | ||
clock: number; | ||
} | ||
export interface RemoteInsertOperation { | ||
node: Node; | ||
} | ||
|
||
export interface RemoteDeleteOperation { | ||
targetId: NodeId | null; | ||
clock: number; | ||
} | ||
|
||
export interface CursorPosition { | ||
clientId: number; | ||
position: number; | ||
} | ||
|
||
export interface SerializedProps { | ||
clock: number; | ||
client: number; | ||
textLinkedList: { | ||
head: NodeId | null; | ||
nodeMap: { [key: string]: Node }; | ||
}; | ||
} |
Oops, something went wrong.