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 doc.getStats for debugging purpose #920

Merged
merged 1 commit into from
Oct 25, 2024
Merged
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
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;

hackerwins marked this conversation as resolved.
Show resolved Hide resolved
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
Loading