diff --git a/src/document/document.ts b/src/document/document.ts index a2246dea4..8fce96be5 100644 --- a/src/document/document.ts +++ b/src/document/document.ts @@ -66,6 +66,18 @@ import { PresenceChangeType, } from '@yorkie-js-sdk/src/document/presence/presence'; +/** + * `DocumentOptions` are the options to create a new document. + * + * @public + */ +export interface DocumentOptions { + /** + * `disableGC` disables garbage collection if true. + */ + disableGC?: boolean; +} + /** * `DocumentStatus` represents the status of the document. * @public @@ -388,6 +400,7 @@ type PathOf = PathOfInternal< export class Document { private key: DocumentKey; private status: DocumentStatus; + private opts: DocumentOptions; private changeID: ChangeID; private checkpoint: Checkpoint; @@ -412,7 +425,9 @@ export class Document { */ private presences: Map; - constructor(key: string) { + constructor(key: string, opts?: DocumentOptions) { + this.opts = opts || {}; + this.key = key; this.status = DocumentStatus.Detached; this.root = CRDTRoot.create(); @@ -888,6 +903,10 @@ export class Document { * @internal */ public garbageCollect(ticket: TimeTicket): number { + if (this.opts.disableGC) { + return 0; + } + if (this.clone) { this.clone.root.garbageCollect(ticket); } diff --git a/test/integration/gc_test.ts b/test/integration/gc_test.ts index 14ed72e51..7a36fcc2b 100644 --- a/test/integration/gc_test.ts +++ b/test/integration/gc_test.ts @@ -50,6 +50,29 @@ describe('Garbage Collection', function () { assert.equal(0, doc.getGarbageLen()); }); + it('disable GC test', function () { + const doc = new yorkie.Document<{ + 1: number; + 2?: Array; + 3: number; + }>('test-doc', { disableGC: true }); + + doc.update((root) => { + root['1'] = 1; + root['2'] = [1, 2, 3]; + root['3'] = 3; + }, 'set 1, 2, 3'); + assert.equal('{"1":1,"2":[1,2,3],"3":3}', doc.toSortedJSON()); + + doc.update((root) => { + delete root['2']; + }, 'deletes 2'); + assert.equal('{"1":1,"3":3}', doc.toSortedJSON()); + assert.equal(4, doc.getGarbageLen()); + assert.equal(0, doc.garbageCollect(MaxTimeTicket)); + assert.equal(4, doc.getGarbageLen()); + }); + it('garbage collection test2', function () { const size = 10000; const doc = new yorkie.Document<{ 1?: Array }>('test-doc');