Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add root.getStatus() #919

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/sdk/src/document/crdt/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ interface CRDTElementPair {
parent?: CRDTContainer;
}

/**
* `RootStatus` is a structure that represents TODO(raa).
*/
interface RootStatus {
elements?: number;
textNodes?: number;
gcPairs?: number;
gcElements?: number;
maxSplayTreeHeight?: number;
}

/**
* `CRDTRoot` is a structure that represents the root. It has a hash table of
* all elements to find a specific element when applying remote changes
Expand Down Expand Up @@ -308,4 +319,18 @@ export class CRDTRoot {
public toSortedJSON(): string {
return this.rootObject.toSortedJSON();
}

/**
* `getStatus` returns the current status of root.
* for debugging purpose.
*/
public getStatus(): RootStatus {
const text = this.rootObject.get('content') as CRDTText;
return {
elements: this.getElementMapSize(),
gcPairs: this.gcPairMap.size,
gcElements: this.getGarbageElementSetSize(),
maxSplayTreeHeight: text.getTreeByIndex().getHeight(),
};
}
}
56 changes: 54 additions & 2 deletions packages/sdk/src/util/splay_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ export abstract class SplayNode<V> {
private right?: SplayNode<V>;
private parent?: SplayNode<V>;
private weight!: number;
public height!: number;

constructor(value: V) {
this.value = value;
this.height = 1;
this.initWeight();
}

Expand Down Expand Up @@ -62,6 +64,20 @@ export abstract class SplayNode<V> {
return !this.hasRight() ? 0 : this.right!.getWeight();
}

/**
* `getLeftHeight` returns left height of this node.
*/
public getLeftHeight(): number {
return !this.hasLeft() ? 0 : this.left!.height;
}

/**
* `getRightHeight` returns left height of this node.
*/
public getRightHeight(): number {
return !this.hasRight() ? 0 : this.right!.height;
}

/**
* `getWeight` returns weight of this node.
*/
Expand Down Expand Up @@ -161,6 +177,13 @@ export abstract class SplayNode<V> {
public initWeight(): void {
this.weight = this.getLength();
}

/**
* `initHeight` sets initial height of this node.
*/
public initHeight() {
this.height = 1;
}
}

/**
Expand Down Expand Up @@ -211,7 +234,7 @@ export class SplayTree<V> {
`out of index range: pos: ${pos} > node.length: ${node.getLength()}`,
);
}
this.splayNode(node)
this.splayNode(node);
return [node, pos];
}

Expand All @@ -226,7 +249,7 @@ export class SplayTree<V> {
return -1;
}

this.splayNode(node)
this.splayNode(node);
return this.root!.getLeftWeight();
}

Expand Down Expand Up @@ -268,6 +291,8 @@ export class SplayTree<V> {
target.setRight();
this.updateWeight(target);
this.updateWeight(newNode);
this.updateHeight(target);
this.updateHeight(newNode);

return newNode;
}
Expand All @@ -289,10 +314,23 @@ export class SplayTree<V> {
private updateTreeWeight(node: SplayNode<V>): void {
while (node) {
this.updateWeight(node);
this.updateHeight(node);
node = node.getParent()!;
}
}

private updateHeight(node: SplayNode<V>): void {
node.initHeight();

if (node.height < node.getLeftHeight() + 1) {
node.height = node.getLeftHeight() + 1;
}

if (node.height < node.getRightHeight() + 1) {
node.height = node.getRightHeight() + 1;
}
}

/**
* `splayNode` moves the given node to the root.
*/
Expand Down Expand Up @@ -332,6 +370,7 @@ export class SplayTree<V> {
this.rotateLeft(node);
}
this.updateWeight(node);
this.updateHeight(node);
return;
}
}
Expand Down Expand Up @@ -368,6 +407,7 @@ export class SplayTree<V> {
node.unlink();
if (this.root) {
this.updateWeight(this.root);
this.updateHeight(this.root);
}
}

Expand Down Expand Up @@ -435,6 +475,14 @@ export class SplayTree<V> {
return true;
}

/**
* `getHeight` returns the height of this node.
* for debugging.
*/
public getHeight(): number {
return this.root!.height;
}

private getRightmost(): SplayNode<V> {
let node = this.root!;
while (node.hasRight()) {
Expand Down Expand Up @@ -492,6 +540,8 @@ export class SplayTree<V> {

this.updateWeight(root);
this.updateWeight(pivot);
this.updateHeight(root);
this.updateHeight(pivot);
}

private rotateRight(pivot: SplayNode<V>): void {
Expand All @@ -517,6 +567,8 @@ export class SplayTree<V> {

this.updateWeight(root);
this.updateWeight(pivot);
this.updateHeight(root);
this.updateHeight(pivot);
}

private isLeftChild(node?: SplayNode<V>): boolean {
Expand Down
Loading