UNPKG

mute-structs

Version:

NodeJS module providing an implementation of the LogootSplit CRDT algorithm

107 lines (103 loc) 4.26 kB
/* 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/>. */ import { isObject } from "./data-validation"; import { isInt32 } from "./int32"; var IdentifierTuple = /** @class */ (function () { function IdentifierTuple(random, replicaNumber, clock, offset) { console.assert([random, replicaNumber, clock, offset].every(isInt32), "each value ∈ int32"); this.random = random; this.replicaNumber = replicaNumber; this.clock = clock; this.offset = offset; } IdentifierTuple.fromPlain = function (o) { if (isObject(o) && isInt32(o.random) && isInt32(o.replicaNumber) && isInt32(o.clock) && isInt32(o.offset)) { return new IdentifierTuple(o.random, o.replicaNumber, o.clock, o.offset); } return null; }; /** * Generate a new IdentifierTuple with the same base as the provided one but with a different offset * * @param {tuple} IdentifierTuple The tuple to partly copy * @param {number} offset The offset of the new IdentifierTuple * @return {IdentifierTuple} The generated IdentifierTuple */ IdentifierTuple.fromBase = function (tuple, offset) { console.assert(isInt32(offset), "offset ∈ int32"); return new IdentifierTuple(tuple.random, tuple.replicaNumber, tuple.clock, offset); }; /** * Compare this tuple to another one to order them * Ordering.Less means that this is less than other * Ordering.Greater means that this is greater than other * Ordering.Equal means that this is equals to other * * @param {IdentifierTuple} other The tuple to compare * @return {Ordering} The order of the two tuples */ IdentifierTuple.prototype.compareTo = function (other) { var array = this.asArray(); var otherArray = other.asArray(); var i = 0; while (i < array.length && array[i] === otherArray[i]) { i++; } if (i === array.length) { return 0 /* Equal */; } else if (array[i] < otherArray[i]) { return -1 /* Less */; } else { return 1 /* Greater */; } }; IdentifierTuple.prototype.equals = function (other) { return this.equalsBase(other) && this.offset === other.offset; }; /** * Check if this tuple and another one share the same base * The base is composed of a random number, a replicaNumber and a clock * * @param {IdentifierTuple} other The tuple to compare * @return {boolean} Are the two tuple sharing the same base */ IdentifierTuple.prototype.equalsBase = function (other) { return this.random === other.random && this.replicaNumber === other.replicaNumber && this.clock === other.clock; }; /** * Map the tuple to an array, making it easier to browse * * @return {number[]} The tuple as an array */ IdentifierTuple.prototype.asArray = function () { return [this.random, this.replicaNumber, this.clock, this.offset]; }; IdentifierTuple.prototype.digest = function () { return this.asArray().reduce(function (prev, v) { return (prev * 17 + v) | 0; }, 0); }; IdentifierTuple.prototype.toString = function () { return this.random + "," + this.replicaNumber + "," + this.clock + "," + this.offset; }; return IdentifierTuple; }()); export { IdentifierTuple }; //# sourceMappingURL=identifiertuple.js.map