Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/boostcampwm-2024/web33-Nocta โ€ฆ
Browse files Browse the repository at this point in the history
โ€ฆinto Feature/#104_ํƒญ_๋ธŒ๋ผ์šฐ์ง•_๋ฐ˜์‘ํ˜•_๊ตฌํ˜„
  • Loading branch information
pipisebastian committed Nov 13, 2024
2 parents db0ded3 + 496c2bd commit c0ef9b3
Show file tree
Hide file tree
Showing 5 changed files with 444 additions and 1 deletion.
129 changes: 129 additions & 0 deletions @noctaCrdt/Crdt.ts
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,
},
};
}
}
32 changes: 32 additions & 0 deletions @noctaCrdt/Interfaces.ts
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 };
};
}
Loading

0 comments on commit c0ef9b3

Please sign in to comment.