Skip to content

Commit 183940f

Browse files
authored
Merge pull request #376 from boostcampwm2023/BE-feature/crdt
์ œ๋„ค๋ฆญ ํƒ€์ž… ์ œ๊ฑฐ
2 parents c3e5a4c + e050cc3 commit 183940f

File tree

9 files changed

+111
-119
lines changed

9 files changed

+111
-119
lines changed

โ€Žnestjs-BE/server/src/board-trees/board-trees.gateway.tsโ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export class BoardTreesGateway implements OnGatewayConnection {
6060
const { boardId, operation: serializedOperation } = payloadObject;
6161

6262
const operationTypeMap = {
63-
add: OperationAdd.parse<string>,
64-
delete: OperationDelete.parse<string>,
65-
move: OperationMove.parse<string>,
66-
update: OperationUpdate.parse<string>,
63+
add: OperationAdd.parse,
64+
delete: OperationDelete.parse,
65+
move: OperationMove.parse,
66+
update: OperationUpdate.parse,
6767
};
6868

6969
const operation =

โ€Žnestjs-BE/server/src/board-trees/board-trees.service.tsโ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class BoardTreesService {
1111
@InjectModel(BoardTree.name) private boardTreeModel: Model<BoardTree>,
1212
) {}
1313

14-
private boardTrees = new Map<string, CrdtTree<string>>();
14+
private boardTrees = new Map<string, CrdtTree>();
1515

1616
async create(boardId: string, tree: string) {
1717
const createdTree = new this.boardTreeModel({
@@ -33,16 +33,16 @@ export class BoardTreesService {
3333
async initBoardTree(boardId: string, boardName: string) {
3434
const existingTree = await this.findByBoardId(boardId);
3535
if (existingTree) {
36-
this.boardTrees.set(boardId, CrdtTree.parse<string>(existingTree.tree));
36+
this.boardTrees.set(boardId, CrdtTree.parse(existingTree.tree));
3737
} else {
38-
const newTree = new CrdtTree<string>(boardId);
38+
const newTree = new CrdtTree(boardId);
3939
newTree.tree.get('root').description = boardName;
4040
this.create(boardId, JSON.stringify(newTree));
4141
this.boardTrees.set(boardId, newTree);
4242
}
4343
}
4444

45-
applyOperation(boardId: string, operation: Operation<string>) {
45+
applyOperation(boardId: string, operation: Operation) {
4646
const boardTree = this.boardTrees.get(boardId);
4747
boardTree.applyOperation(operation);
4848
}

โ€Žnestjs-BE/server/src/crdt/crdt-tree.spec.tsโ€Ž

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { CrdtTree } from './crdt-tree';
22
import { Node } from './node';
33

44
it('crdt tree ๋™๊ธฐํ™”', () => {
5-
const tree1 = new CrdtTree<string>('1');
6-
const tree2 = new CrdtTree<string>('2');
5+
const tree1 = new CrdtTree('1');
6+
const tree2 = new CrdtTree('2');
77

88
const op_1_1 = tree1.generateOperationAdd('a', 'root', 'hello');
99
const op_1_2 = tree1.generateOperationAdd('b', 'root', 'hi');
@@ -45,7 +45,7 @@ it('crdt tree ๋™๊ธฐํ™”', () => {
4545
});
4646

4747
it('crdt tree ์—ญ์ง๋ ฌํ™”', () => {
48-
const tree = new CrdtTree<string>('1');
48+
const tree = new CrdtTree('1');
4949

5050
const op1 = tree.generateOperationAdd('a', 'root', 'hello');
5151
const op2 = tree.generateOperationAdd('b', 'root', 'hi');
@@ -56,13 +56,13 @@ it('crdt tree ์—ญ์ง๋ ฌํ™”', () => {
5656

5757
expect(JSON.stringify(tree));
5858

59-
const parsedTree = CrdtTree.parse<string>(JSON.stringify(tree));
59+
const parsedTree = CrdtTree.parse(JSON.stringify(tree));
6060

6161
expect(JSON.stringify(tree)).toEqual(JSON.stringify(parsedTree));
6262
});
6363

6464
it('crdt tree ์ˆœํ™˜', () => {
65-
const tree = new CrdtTree<string>('1');
65+
const tree = new CrdtTree('1');
6666

6767
const op1 = tree.generateOperationAdd('a', 'root', 'hello');
6868
const op2 = tree.generateOperationAdd('b', 'root', 'hi');
@@ -72,5 +72,5 @@ it('crdt tree ์ˆœํ™˜', () => {
7272
const op6 = tree.generateOperationMove('b', 'a');
7373

7474
tree.applyOperations([op1, op2, op3, op4, op5, op6]);
75-
expect((tree.tree.get('b') as Node<string>).parentId).toEqual('root');
75+
expect((tree.tree.get('b') as Node).parentId).toEqual('root');
7676
});

โ€Žnestjs-BE/server/src/crdt/crdt-tree.tsโ€Ž

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,75 +14,75 @@ import {
1414
import { Tree } from './tree';
1515
import { Node } from './node';
1616

17-
export class CrdtTree<T> {
18-
operationLogs: OperationLog<T>[] = [];
17+
export class CrdtTree {
18+
operationLogs: OperationLog[] = [];
1919
clock: Clock;
20-
tree = new Tree<T>();
20+
tree = new Tree();
2121

2222
constructor(id: string) {
2323
this.clock = new Clock(id);
2424
}
2525

26-
get(id: string): Node<T> | undefined {
26+
get(id: string): Node | undefined {
2727
return this.tree.get(id);
2828
}
2929

30-
addLog(log: OperationLog<T>) {
30+
addLog(log: OperationLog) {
3131
this.operationLogs.push(log);
3232
}
3333

3434
generateOperationAdd(
3535
targetId: string,
3636
parentId: string,
37-
description: T,
38-
): OperationAdd<T> {
37+
description: string,
38+
): OperationAdd {
3939
this.clock.increment();
4040
const clock = this.clock.copy();
41-
const input: OperationAddInput<T> = {
41+
const input: OperationAddInput = {
4242
id: targetId,
4343
parentId,
4444
description,
4545
clock,
4646
};
47-
return new OperationAdd<T>(input);
47+
return new OperationAdd(input);
4848
}
4949

50-
generateOperationDelete(targetId: string): OperationDelete<T> {
50+
generateOperationDelete(targetId: string): OperationDelete {
5151
this.clock.increment();
5252
const clock = this.clock.copy();
5353
const input: OperationInput = {
5454
id: targetId,
5555
clock,
5656
};
57-
return new OperationDelete<T>(input);
57+
return new OperationDelete(input);
5858
}
5959

60-
generateOperationMove(targetId: string, parentId: string): OperationMove<T> {
60+
generateOperationMove(targetId: string, parentId: string): OperationMove {
6161
this.clock.increment();
6262
const clock = this.clock.copy();
6363
const input: OperationMoveInput = {
6464
id: targetId,
6565
parentId,
6666
clock,
6767
};
68-
return new OperationMove<T>(input);
68+
return new OperationMove(input);
6969
}
7070

7171
generateOperationUpdate(
7272
targetId: string,
73-
description: T,
74-
): OperationUpdate<T> {
73+
description: string,
74+
): OperationUpdate {
7575
this.clock.increment();
7676
const clock = this.clock.copy();
77-
const input: OperationUpdateInput<T> = {
77+
const input: OperationUpdateInput = {
7878
id: targetId,
7979
description,
8080
clock,
8181
};
82-
return new OperationUpdate<T>(input);
82+
return new OperationUpdate(input);
8383
}
8484

85-
applyOperation(operation: Operation<T>) {
85+
applyOperation(operation: Operation) {
8686
this.clock = this.clock.merge(operation.clock);
8787

8888
if (this.operationLogs.length === 0) {
@@ -94,7 +94,7 @@ export class CrdtTree<T> {
9494
const lastOperation =
9595
this.operationLogs[this.operationLogs.length - 1].operation;
9696
if (operation.clock.compare(lastOperation.clock) === COMPARE.LESS) {
97-
const prevLog = this.operationLogs.pop() as OperationLog<T>;
97+
const prevLog = this.operationLogs.pop() as OperationLog;
9898
prevLog.operation.undoOperation(this.tree, prevLog);
9999
this.applyOperation(operation);
100100
const redoLog = prevLog.operation.redoOperation(this.tree, prevLog);
@@ -105,25 +105,25 @@ export class CrdtTree<T> {
105105
}
106106
}
107107

108-
applyOperations(operations: Operation<T>[]) {
108+
applyOperations(operations: Operation[]) {
109109
for (const operation of operations) this.applyOperation(operation);
110110
}
111111

112-
static parse<T>(json: string) {
112+
static parse(json: string) {
113113
const parsedJson = JSON.parse(json);
114-
const crdtTree = new CrdtTree<T>('0');
114+
const crdtTree = new CrdtTree('0');
115115
crdtTree.clock = Clock.parse(JSON.stringify(parsedJson.clock));
116-
crdtTree.tree = Tree.parse<T>(JSON.stringify(parsedJson.tree));
116+
crdtTree.tree = Tree.parse(JSON.stringify(parsedJson.tree));
117117

118118
const operationTypeMap = {
119-
add: OperationAdd.parse<T>,
120-
delete: OperationDelete.parse<T>,
121-
move: OperationMove.parse<T>,
122-
update: OperationUpdate.parse<T>,
119+
add: OperationAdd.parse,
120+
delete: OperationDelete.parse,
121+
move: OperationMove.parse,
122+
update: OperationUpdate.parse,
123123
};
124124

125125
const parsedOperationLogs = parsedJson.operationLogs.map(
126-
(operationLog: OperationLog<T>) => {
126+
(operationLog: OperationLog) => {
127127
const operationType = operationLog.operation.operationType;
128128
operationLog.operation = operationTypeMap[operationType](
129129
operationLog.operation,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Node } from './node';
22

33
it('node ์—ญ์ง๋ ฌํ™”', () => {
4-
const node = new Node<string>('1', 'root', 'hello');
5-
const parsedNode = Node.parse<string>(JSON.stringify(node));
4+
const node = new Node('1', 'root', 'hello');
5+
const parsedNode = Node.parse(JSON.stringify(node));
66

77
expect(JSON.stringify(node)).toEqual(JSON.stringify(parsedNode));
88
});

โ€Žnestjs-BE/server/src/crdt/node.tsโ€Ž

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
export class Node<T> {
1+
export class Node {
22
targetId: string;
33
parentId: string;
4-
description: T | null;
4+
description?: string;
55
children = new Array<string>();
66

77
constructor(
88
targetId: string,
99
parentId: string = '0',
10-
description: T | null = null,
10+
description: string = null,
1111
) {
1212
this.targetId = targetId;
1313
this.parentId = parentId;
1414
this.description = description;
1515
}
1616

17-
static parse<T>(json: string) {
17+
static parse(json: string) {
1818
const parsedJson = JSON.parse(json);
19-
const node = new Node<T>(
19+
const node = new Node(
2020
parsedJson.targetId,
2121
parsedJson.parentId,
2222
parsedJson.description,

0 commit comments

Comments
ย (0)