Skip to content

Commit

Permalink
Add RHTNode removal to converter
Browse files Browse the repository at this point in the history
  • Loading branch information
raararaara committed Jun 4, 2024
1 parent 2c3e7ef commit 85473c8
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
46 changes: 35 additions & 11 deletions src/api/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,22 @@ function toTreeNodesWhenEdit(nodes: Array<CRDTTreeNode>): Array<PbTreeNodes> {
return pbTreeNodesList;
}

/**
* `toRHT` converts the given model to Protobuf format.
*/
function toRHT(rht: RHT): { [key: string]: PbNodeAttr } {
const pbAttrs: { [key: string]: PbNodeAttr } = {};
for (const [key, rhtNode] of rht.getNodeMapByKey()) {
pbAttrs[key] = new PbNodeAttr({
value: rhtNode.getValue(),
updatedAt: toTimeTicket(rhtNode.getUpdatedAt()),
isRemoved: rhtNode.isRemoved(),
});
}

return pbAttrs;
}

/**
* `toTreeNodes` converts the given model to Protobuf format.
*/
Expand Down Expand Up @@ -620,12 +636,7 @@ function toTreeNodes(node: CRDTTreeNode): Array<PbTreeNode> {
}

if (n.attrs) {
for (const attr of n.attrs) {
pbTreeNode.attributes[attr.getKey()] = new PbNodeAttr({
value: attr.getValue(),
updatedAt: toTimeTicket(attr.getUpdatedAt()),
});
}
pbTreeNode.attributes = toRHT(n.attrs);
}

pbTreeNodes.push(pbTreeNode);
Expand Down Expand Up @@ -1053,6 +1064,23 @@ function fromTreeNodes(
return CRDTTree.create(root, InitialTimeTicket).getRoot();
}

/**
* `fromRHT` converts the given Protobuf format to model format.
*/
function fromRHT(pbRHT: { [key: string]: PbNodeAttr }): RHT {
const rht = RHT.create();
Object.entries(pbRHT).forEach(([key, rhtNode]) => {
rht.setInternal(
key,
rhtNode.value,
fromTimeTicket(rhtNode.updatedAt)!,
rhtNode.isRemoved,
);
});

return rht;
}

/**
* `fromTreeNode` converts the given Protobuf format to model format.
*/
Expand All @@ -1063,11 +1091,7 @@ function fromTreeNode(pbTreeNode: PbTreeNode): CRDTTreeNode {
if (node.isText) {
node.value = pbTreeNode.value;
} else if (pbAttrs.length) {
const attrs = RHT.create();
for (const [key, value] of pbAttrs) {
attrs.set(key, value.value, fromTimeTicket(value.updatedAt)!);
}
node.attrs = attrs;
node.attrs = fromRHT(pbTreeNode.attributes);
}

if (pbTreeNode.insPrevId) {
Expand Down
1 change: 1 addition & 0 deletions src/api/yorkie/v1/resources.proto
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ message RGANode {
message NodeAttr {
string value = 1;
TimeTicket updated_at = 2;
bool is_removed = 3;
}

message TextNode {
Expand Down
5 changes: 5 additions & 0 deletions src/api/yorkie/v1/resources_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,11 @@ export declare class NodeAttr extends Message<NodeAttr> {
*/
updatedAt?: TimeTicket;

/**
* @generated from field: bool is_removed = 3;
*/
isRemoved: boolean;

constructor(data?: PartialMessage<NodeAttr>);

static readonly runtime: typeof proto3;
Expand Down
1 change: 1 addition & 0 deletions src/api/yorkie/v1/resources_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ const NodeAttr = proto3.makeMessageType(
() => [
{ no: 1, name: "value", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 2, name: "updated_at", kind: "message", T: TimeTicket },
{ no: 3, name: "is_removed", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
],
);

Expand Down
7 changes: 7 additions & 0 deletions src/document/crdt/rht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ export class RHT {
return new RHT();
}

/**
* `getNodeMapByKey` returns the hashtable of RHT.
*/
public getNodeMapByKey(): Map<string, RHTNode> {
return this.nodeMapByKey;
}

/**
* `set` sets the value of the given key.
*/
Expand Down
11 changes: 10 additions & 1 deletion test/unit/api/converter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ describe('Converter', function () {
});

root.tree.editByPath([0, 1], [1, 1]);

root.tree.style(0, 1, { b: 't', i: 't' });
assert.equal(root.tree.toXML(), '<r><p b="t" i="t">14</p></r>');

root.tree.removeStyle(0, 1, ['i']);
});
assert.equal(doc.getRoot().tree.toXML(), /*html*/ `<r><p>14</p></r>`);
assert.equal(doc.getRoot().tree.toXML(), /*html*/ `<r><p b="t">14</p></r>`);
assert.equal(doc.getRoot().tree.getSize(), 4);

const bytes = converter.objectToBytes(doc.getRootObject());
Expand All @@ -119,5 +124,9 @@ describe('Converter', function () {
doc.getRoot().tree.getSize(),
(obj.get('tree') as unknown as Tree).getSize(),
);
assert.equal(
doc.getRoot().tree.toXML(),
(obj.get('tree') as unknown as Tree).toXML(),
);
});
});

0 comments on commit 85473c8

Please sign in to comment.