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

Update removedAt for already deleted nodes during range deletions #909

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
43 changes: 23 additions & 20 deletions packages/sdk/src/document/crdt/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,36 @@
* limitations under the License.
*/

import { CRDTElement } from '@yorkie-js-sdk/src/document/crdt/element';
import {
TimeTicket,
InitialTimeTicket,
TimeTicketStruct,
MaxTimeTicket,
TimeTicket,
TimeTicketStruct,
} from '@yorkie-js-sdk/src/document/time/ticket';
import { CRDTElement } from '@yorkie-js-sdk/src/document/crdt/element';

import type * as Devtools from '@yorkie-js-sdk/src/devtools/types';
import { GCChild, GCPair, GCParent } from '@yorkie-js-sdk/src/document/crdt/gc';
import { Indexable } from '@yorkie-js-sdk/src/document/document';
import { escapeString } from '@yorkie-js-sdk/src/document/json/strings';
import { Comparator } from '@yorkie-js-sdk/src/util/comparator';
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';
import type {
DefaultTextType,
TreeNodeType,
TreeToken,
} from '@yorkie-js-sdk/src/util/index_tree';
import {
IndexTree,
TreePos,
IndexTreeNode,
traverseAll,
TokenType,
TreePos,
traverseAll,
} from '@yorkie-js-sdk/src/util/index_tree';
import { RHT, RHTNode } from './rht';
import { ActorID } from './../time/actor_id';
import { LLRBTree } from '@yorkie-js-sdk/src/util/llrb_tree';
import { Comparator } from '@yorkie-js-sdk/src/util/comparator';
import { parseObjectValues } from '@yorkie-js-sdk/src/util/object';
import type {
DefaultTextType,
TreeNodeType,
TreeToken,
} from '@yorkie-js-sdk/src/util/index_tree';
import { Indexable } from '@yorkie-js-sdk/src/document/document';
import type * as Devtools from '@yorkie-js-sdk/src/devtools/types';
import { escapeString } from '@yorkie-js-sdk/src/document/json/strings';
import { GCChild, GCPair, GCParent } from '@yorkie-js-sdk/src/document/crdt/gc';
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';
import { ActorID } from './../time/actor_id';
import { RHT, RHTNode } from './rht';

/**
* `TreeNode` represents a node in the tree.
Expand Down Expand Up @@ -1094,6 +1094,9 @@ export class CRDTTree extends CRDTElement implements GCParent {
nodesToBeRemoved.push(node);
}
tokensToBeRemoved.push([node, tokenType]);
} else if (node.isRemoved && editedAt.after(node.removedAt!)) {
node.removedAt = editedAt;
pairs.push({ parent: this, child: node });
}
},
);
Expand Down Expand Up @@ -1371,8 +1374,8 @@ export class CRDTTree extends CRDTElement implements GCParent {
const treePos = node.isText
? { node, offset: 0 }
: parentNode && leftChildNode
? this.toTreePos(parentNode, leftChildNode)
: null;
? this.toTreePos(parentNode, leftChildNode)
: null;

if (treePos) {
index = this.indexTree.indexOf(treePos);
Expand Down
9 changes: 8 additions & 1 deletion packages/sdk/test/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export async function assertThrowsAsync(
message?: string,
) {
// eslint-disable-next-line @typescript-eslint/no-empty-function
let errFn = () => {};
let errFn = () => { };
try {
await fn();
} catch (e) {
Expand Down Expand Up @@ -285,3 +285,10 @@ export function posT(offset = 0): CRDTTreeNodeID {
export function timeT(): TimeTicket {
return dummyContext.issueTimeTicket();
}

/**
* `getLTT` is a helpher function that returns the last time ticket.
*/
export function getLTT(): TimeTicket {
return dummyContext.getLastTimeTicket();
}
33 changes: 32 additions & 1 deletion packages/sdk/test/unit/document/crdt/tree_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
TreeNodeForTest,
} from '@yorkie-js-sdk/src/document/crdt/tree';
import { stringifyObjectValues } from '@yorkie-js-sdk/src/util/object';
import { idT, posT, timeT } from '@yorkie-js-sdk/test/helper/helper';
import { idT, posT, timeT, getLTT } from '@yorkie-js-sdk/test/helper/helper';

describe('CRDTTreeNode', function () {
it('Can be created', function () {
Expand Down Expand Up @@ -231,6 +231,37 @@ describe('CRDTTree.Edit', function () {
);
assert.equal(t.toIndex(parent, left), 0);
});

it('Update removedAt for already deleted nodes during range deletions', function () {
// 0
// <root> </root>
const t = new CRDTTree(new CRDTTreeNode(posT(), 'r'), timeT());
assert.equal(t.getRoot().size, 0);
assert.equal(t.toXML(), /*html*/ `<r></r>`);

// 1
// <root> <p> h e l l o </p> </root>
const p = new CRDTTreeNode(posT(), 'p', []);
const txt = new CRDTTreeNode(posT(), 'text', 'hello');
p.insertAt(txt, 0);
t.editT([0, 0], [p], 0, timeT(), timeT);
assert.equal(t.toXML(), /*html*/ `<r><p>hello</p></r>`);

// 2
// <root> <p> </p> </root>
t.editT([1, 6], undefined, 0, timeT(), timeT);
assert.equal(t.toXML(), /*html*/ `<r><p></p></r>`);
assert.isFalse(p.isRemoved);
assert.isTrue(txt.getRemovedAt()?.equals(getLTT()));

// 3
// <root> </root>
t.editT([0, 2], undefined, 0, timeT(), timeT);
assert.equal(t.toXML(), /*html*/ `<r></r>`);
assert.isTrue(p.getRemovedAt()?.equals(getLTT()));
assert.isTrue(txt.getRemovedAt()?.equals(getLTT()));
});

});

describe('CRDTTree.Split', function () {
Expand Down
Loading