From 1904bb729c3d08d9b964963b3c1a284b41f309b4 Mon Sep 17 00:00:00 2001 From: raararaara Date: Fri, 25 Oct 2024 18:02:04 +0900 Subject: [PATCH] Add `root.getStats` for debugging purpose --- packages/sdk/public/index.html | 2 ++ packages/sdk/src/document/crdt/root.ts | 32 ++++++++++++++++++++++++++ packages/sdk/src/document/document.ts | 9 +++++++- packages/sdk/src/util/splay_tree.ts | 4 ++-- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/sdk/public/index.html b/packages/sdk/public/index.html index 55cbcf815..3c5131315 100644 --- a/packages/sdk/public/index.html +++ b/packages/sdk/public/index.html @@ -328,6 +328,8 @@

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; diff --git a/packages/sdk/src/document/crdt/root.ts b/packages/sdk/src/document/crdt/root.ts index 246d87d41..cfc5ed3a1 100644 --- a/packages/sdk/src/document/crdt/root.ts +++ b/packages/sdk/src/document/crdt/root.ts @@ -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 @@ -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(), + }; + } } diff --git a/packages/sdk/src/document/document.ts b/packages/sdk/src/document/document.ts index b054918d7..d4027b9fd 100644 --- a/packages/sdk/src/document/document.ts +++ b/packages/sdk/src/document/document.ts @@ -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, @@ -1388,6 +1388,13 @@ export class Document { 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. */ diff --git a/packages/sdk/src/util/splay_tree.ts b/packages/sdk/src/util/splay_tree.ts index 30d64a038..36c14b08a 100644 --- a/packages/sdk/src/util/splay_tree.ts +++ b/packages/sdk/src/util/splay_tree.ts @@ -211,7 +211,7 @@ export class SplayTree { `out of index range: pos: ${pos} > node.length: ${node.getLength()}`, ); } - this.splayNode(node) + this.splayNode(node); return [node, pos]; } @@ -226,7 +226,7 @@ export class SplayTree { return -1; } - this.splayNode(node) + this.splayNode(node); return this.root!.getLeftWeight(); }