Skip to content

Commit

Permalink
Revise codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Jun 3, 2024
1 parent 72805b0 commit 1e9f047
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 34 deletions.
23 changes: 1 addition & 22 deletions src/api/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,6 @@ function fromTreeNodes(
nodes.push(fromTreeNode(pbTreeNode));
}

// 01. build tree structure
const root = nodes[nodes.length - 1];
for (let i = nodes.length - 2; i >= 0; i--) {
let parent: CRDTTreeNode;
Expand All @@ -1048,32 +1047,12 @@ function fromTreeNodes(
parent!.prepend(nodes[i]);
}

// 02. adjust descendant size of all nodes
dfs(root);
root.updateDescendantsSize();

// build CRDTTree from the root to construct the links between nodes.
return CRDTTree.create(root, InitialTimeTicket).getRoot();
}

/**
* `dfs` calculates the size of the given node and its descendants.
*/
function dfs(curr: CRDTTreeNode): number {
if (curr.isRemoved) {
curr.size = 0;
return 0;
}

let ret = 0;
for (const child of curr._children) {
ret += dfs(child);
}

curr.size += ret;

return curr.paddedSize;
}

/**
* `fromTreeNode` converts the given Protobuf format to model format.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/document/crdt/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1275,9 +1275,9 @@ export class CRDTTree extends CRDTElement implements GCParent {
}

/**
* `getLLRBTreeSize` returns the size of the LLRBTree.
* `getNodeSize` returns the size of the LLRBTree.
*/
public getLLRBTreeSize(): number {
public getNodeSize(): number {
return this.nodeMapByID.size();
}

Expand Down
6 changes: 3 additions & 3 deletions src/document/json/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ export class Tree {
}

/**
* `getLLRBTreeSize` returns the size of the LLRB tree of this tree.
* `getNodeSize` returns the node size of this tree.
*/
public getLLRBTreeSize(): number {
public getNodeSize(): number {
if (!this.context || !this.tree) {
throw new Error('it is not initialized yet');
}

return this.tree.getLLRBTreeSize();
return this.tree.getNodeSize();
}

/**
Expand Down
22 changes: 21 additions & 1 deletion src/util/index_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,25 @@ export abstract class IndexTreeNode<T extends IndexTreeNode<T>> {
}
}

/**
* `updateDescendantsSize` updates the size of the descendants.
*/
updateDescendantsSize(): number {
if (this.isRemoved) {
this.size = 0;
return 0;
}

let sum = 0;
for (const child of this._children) {
sum += child.updateDescendantsSize();
}

this.size += sum;

return this.paddedSize;
}

/**
* `isText` returns true if the node is a text node.
*/
Expand Down Expand Up @@ -276,7 +295,8 @@ export abstract class IndexTreeNode<T extends IndexTreeNode<T>> {
}

/**
* `prepend` prepends the given nodes to the children.
* `prepend` prepends the given nodes to the children. It is only used
* for creating a new node from snapshot.
*/
prepend(...newNode: Array<T>): void {
if (this.isText) {
Expand Down
10 changes: 5 additions & 5 deletions test/unit/api/converter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('Converter', function () {
assert.equal(doc.toSortedJSON(), obj.toSortedJSON());
});

it.skip('check size', function () {
it('should encode and decode tree properly', function () {
const doc = new Document<{
tree: Tree;
}>('test-doc');
Expand All @@ -96,16 +96,16 @@ describe('Converter', function () {
});

root.tree.editByPath([0, 1], [1, 1]);
assert.equal(root.tree.toXML(), /*html*/ `<r><p>14</p></r>`);
assert.equal(root.tree.getSize(), 4);
});
assert.equal(doc.getRoot().tree.toXML(), /*html*/ `<r><p>14</p></r>`);
assert.equal(doc.getRoot().tree.getSize(), 4);

const bytes = converter.objectToBytes(doc.getRootObject());
const obj = converter.bytesToObject(bytes);

assert.equal(
doc.getRoot().tree.getLLRBTreeSize(),
(obj.get('tree') as any).getLLRBTreeSize(),
doc.getRoot().tree.getNodeSize(),
(obj.get('tree') as any).getNodeSize(),
);

assert.equal(
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default defineConfig({
reporter: ['lcov', 'text-summary'],
},
onConsoleLog() {
// return false;
return false;
},
environment: 'custom-jsdom',
globals: true,
Expand Down

0 comments on commit 1e9f047

Please sign in to comment.