mute-structs
Version:
NodeJS module providing an implementation of the LogootSplit CRDT algorithm
74 lines (70 loc) • 2.86 kB
JavaScript
/*
This file is part of MUTE-structs.
Copyright (C) 2017 Matthieu Nicolas, Victorien Elvinger
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export function compareBase(idInterval1, idInterval2) {
var id1 = idInterval1.idBegin;
var begin1 = idInterval1.begin;
var end1 = idInterval1.end;
var id2 = idInterval2.idBegin;
var begin2 = idInterval2.begin;
var end2 = idInterval2.end;
if (id1.equalsBase(id2)) {
if (begin1 === begin2 && end1 === end2) {
return 6 /* B1_EQUALS_B2 */;
}
else if ((end1 + 1) === begin2) {
return 4 /* B1_CONCAT_B2 */;
}
else if (begin1 === (end2 + 1)) {
return 5 /* B2_CONCAT_B1 */;
}
else if (end1 < begin2) {
return 1 /* B1_BEFORE_B2 */;
}
else if (end2 < begin1) {
return 0 /* B1_AFTER_B2 */;
}
else {
/*
(B2 ⊂ B1) || (B1 ⊂ B2) || (B1 ∩ B2 !== {})
It happens only in the following cases:
- An already applied operation is delivered again,
but the interval has since then been updated
(append, prepend, deletion at the bounds)
- It is a malicious operation which try to insert
again some identifiers
For now, do not do anything in both cases.
*/
console.warn("Trying to duplicate existing identifiers: ", idInterval1, idInterval2);
return 6 /* B1_EQUALS_B2 */;
}
}
return compareIntervalsDifferentBases(idInterval1, idInterval2);
}
function compareIntervalsDifferentBases(idInterval1, idInterval2) {
var id1 = idInterval1.idBegin;
var id2 = idInterval2.idBegin;
console.assert(!id1.equalsBase(id2), "the bases of the ids must be different");
if (idInterval1.containsId(id2)) {
return 3 /* B2_INSIDE_B1 */;
}
if (idInterval2.containsId(id1)) {
return 2 /* B1_INSIDE_B2 */;
}
if (id1.compareTo(id2) === -1 /* Less */) {
return 1 /* B1_BEFORE_B2 */;
}
return 0 /* B1_AFTER_B2 */;
}
//# sourceMappingURL=iteratorhelperidentifier.js.map