Skip to content

Commit

Permalink
Fix copyNode in TwoPhaseCore. Fixes #1955 (#1956)
Browse files Browse the repository at this point in the history
* Fix copyNode in 2PC. Fixes #1955

* Fix failing tests
  • Loading branch information
brollb authored Oct 7, 2020
1 parent 7cba6c4 commit eac5f29
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 126 deletions.
3 changes: 2 additions & 1 deletion src/plugins/TwoPhaseCommit/CreatedNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ define([
return (typeof id === 'string') && (id.indexOf(CreatedNode.CREATE_PREFIX) === 0);
}
}
CreatedNode.CREATE_PREFIX = 'created_node_';

CreatedNode.CREATE_PREFIX = '__created_node_';

class InheritedNode extends CreatedNode {
async toGMENode (rootNode, core) {
Expand Down
61 changes: 34 additions & 27 deletions src/plugins/TwoPhaseCommit/StagedChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,19 @@ define([
assert,
) {

function StagedChanges(createdNodes, changes, deletions, idDict) {
this.createdNodes = createdNodes;
this.changes = changes;
this.deletions = deletions;
function StagedChanges(idDict, predecessor) {
assert(idDict);
this.createdNodes = [];
this.changes = {};
this.deletions = [];
this._createdGMEIds = idDict;
this.predecessor = predecessor;
}

StagedChanges.prototype.getCreatedNode = function(id) {
return this.createdNodes.find(node => node.id === id);
};

StagedChanges.prototype.onNodeCreated = function(createdNode, nodeId) {
// Update newly created node
const tmpId = createdNode.id;
if (this.changes[tmpId]) {
assert(!this.changes[nodeId],
`Newly created node cannot already have changes! (${nodeId})`);
this.changes[nodeId] = this.changes[tmpId];

delete this.changes[tmpId];
}

// Update any deletions
let index = this.deletions.indexOf(tmpId);
if (index !== -1) {
this.deletions.splice(index, 1, nodeId);
}
};

StagedChanges.prototype.resolveCreateIds = function() {
const changedIds = Object.keys(this.changes)
.filter(id => CreatedNode.isCreateId(id));
Expand Down Expand Up @@ -77,12 +61,13 @@ define([
};

StagedChanges.prototype.tryGetNodeEdits = function(id) {
id = CreatedNode.isCreateId(id) ? this.tryResolveCreateId(id) : id;
if (id) {
return null;
const ids = [id];
if (CreatedNode.isCreateId(id) && this.tryResolveCreateId(id)) {
ids.push(this.tryResolveCreateId(id));
}

return this.changes[id];
return ids
.map(id => this.changes[id])
.find(changes => changes) || null;
};

StagedChanges.prototype.getModifiedNodeIds = function() {
Expand All @@ -96,5 +81,27 @@ define([
return Promise.all(gmeNodes);
};

StagedChanges.prototype.getChangesForNode = function (nodeId) {
if (!this.changes[nodeId]) {
this.changes[nodeId] = {
attr: {},
ptr: {},
};
}

return this.changes[nodeId];
};

StagedChanges.prototype.next = function() {
return new StagedChanges(this._createdGMEIds, this);
};

StagedChanges.prototype.changesets = function() {
if (this.predecessor) {
return this.predecessor.changesets().concat([this]);
}
return [this];
};

return StagedChanges;
});
Loading

0 comments on commit eac5f29

Please sign in to comment.