Skip to content

Commit

Permalink
Add doc.getStats for debugging purpose (#920)
Browse files Browse the repository at this point in the history
Co-authored-by: raararaara <[email protected]>
  • Loading branch information
hackerwins and raararaara committed Oct 25, 2024
1 parent 1d8adb3 commit cbb04b9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/sdk/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ <h4 class="title">
const doc = new yorkie.Document('codemirror', {
enableDevtools: true,
});
window.doc = doc;

doc.subscribe('connection', new Network(statusHolder).statusListener);
doc.subscribe('presence', (event) => {
if (event.type === 'presence-changed') return;
Expand Down
32 changes: 32 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,26 @@ interface CRDTElementPair {
parent?: CRDTContainer;
}

/**
* `RootStats` is a structure that represents the statistics of the root object.
*/
export interface RootStats {
/**
* `elements` is the number of elements in the root object.
*/
elements?: number;

/**
* `gcElements` is the number of elements that can be garbage collected.
*/
gcElements?: number;

/**
* `gcPairs` is the number of garbage collection pairs.
*/
gcPairs?: 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 +328,16 @@ export class CRDTRoot {
public toSortedJSON(): string {
return this.rootObject.toSortedJSON();
}

/**
* `getStats` returns the current statistics of the root object.
* This includes counts of various types of elements and structural information.
*/
public getStats(): RootStats {
return {
elements: this.getElementMapSize(),
gcPairs: this.gcPairMap.size,
gcElements: this.getGarbageElementSetSize(),
};
}
}
9 changes: 8 additions & 1 deletion packages/sdk/src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
import { ChangeContext } from '@yorkie-js-sdk/src/document/change/context';
import { converter } from '@yorkie-js-sdk/src/api/converter';
import { ChangePack } from '@yorkie-js-sdk/src/document/change/change_pack';
import { CRDTRoot } from '@yorkie-js-sdk/src/document/crdt/root';
import { CRDTRoot, RootStats } from '@yorkie-js-sdk/src/document/crdt/root';
import { CRDTObject } from '@yorkie-js-sdk/src/document/crdt/object';
import {
createJSON,
Expand Down Expand Up @@ -1388,6 +1388,13 @@ export class Document<T, P extends Indexable = Indexable> {
return this.root.toSortedJSON();
}

/**
* `getStats` returns the statistics of this document.
*/
public getStats(): RootStats {
return this.root.getStats();
}

/**
* `toJSForTest` returns value with meta data for testing.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/util/splay_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,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 +226,7 @@ export class SplayTree<V> {
return -1;
}

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

Expand Down

0 comments on commit cbb04b9

Please sign in to comment.