Skip to content

Commit

Permalink
Fix missing removed elements collecting from root
Browse files Browse the repository at this point in the history
Co-authored-by: Yourim Cha <dkwkdnwk912@gmail.com>
  • Loading branch information
hackerwins and chacha912 committed Oct 27, 2023
1 parent 356fd7a commit c274c5f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,13 @@ export class Document<T, P extends Indexable = Indexable> {
return this.root.getGarbageLen();
}

/**
* `getGarbageLenFromClone` returns the length of elements should be purged from clone.
*/
public getGarbageLenFromClone(): number {
return this.clone!.root.getGarbageLen();
}

/**
* `toJSON` returns the JSON encoding of this document.
*/
Expand Down
6 changes: 5 additions & 1 deletion src/document/operation/set_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ export class SetOperation extends Operation {
}
const obj = parentObject as CRDTObject;
const value = this.value.deepcopy();
obj.set(this.key, value);
const removed = obj.set(this.key, value);
root.registerElement(value, obj);
if (removed) {
root.registerRemovedElement(removed);
}

return {
opInfos: [
{
Expand Down
23 changes: 23 additions & 0 deletions test/integration/gc_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,27 @@ describe('Garbage Collection', function () {
await client1.deactivate();
await client2.deactivate();
});

it('Can collect removed elements from both root and clone', async function ({
task,
}) {
type TestDoc = { point: { x: number; y: number } };
const docKey = toDocKey(`${task.name}-${new Date().getTime()}`);
const doc = new yorkie.Document<TestDoc>(docKey);
const cli = new yorkie.Client(testRPCAddr);
await cli.activate();

await cli.attach(doc, { isRealtimeSync: false });
doc.update((root) => {
root.point = { x: 0, y: 0 };
});
doc.update((root) => {
root.point = { x: 1, y: 1 };
});
doc.update((root) => {
root.point = { x: 2, y: 2 };
});
assert.equal(doc.getGarbageLen(), 6);
assert.equal(doc.getGarbageLenFromClone(), 6);
});
});
2 changes: 1 addition & 1 deletion test/unit/document/document_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ describe.sequential('Document', function () {
delete root.a;
});
assert.equal('{}', doc.toSortedJSON());
assert.equal(1, doc.getGarbageLen());
assert.equal(2, doc.getGarbageLen());

doc.garbageCollect(MaxTimeTicket);
assert.equal('{}', doc.toSortedJSON());
Expand Down

0 comments on commit c274c5f

Please sign in to comment.