mute-structs
Version:
NodeJS module providing an implementation of the LogootSplit CRDT algorithm
167 lines (163 loc) • 7.39 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/>.
*/
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { Identifier } from "./identifier";
import { IdentifierTuple } from "./identifiertuple";
import { INT32_BOTTOM, INT32_TOP, isInt32, randomInt32, } from "./int32";
export var INT32_BOTTOM_USER = INT32_BOTTOM + 1;
export var INT32_TOP_USER = INT32_TOP - 1;
export var MIN_TUPLE = new IdentifierTuple(INT32_BOTTOM, 0, 0, 0);
export var MIN_TUPLE_USER = new IdentifierTuple(INT32_BOTTOM_USER, 0, 0, 0);
export var MAX_TUPLE_USER = new IdentifierTuple(INT32_TOP_USER, 0, 0, 0);
export var MAX_TUPLE = new IdentifierTuple(INT32_TOP, 0, 0, 0);
export function createBetweenPosition(id1, id2, replicaNumber, clock) {
console.assert(id1 === null || id2 === null ||
id1.compareTo(id2) === -1 /* Less */, "id1 < id2");
console.assert(isInt32(replicaNumber), "replicaNumber is an int32");
console.assert(isInt32(clock), "clock is an int32");
var seq1 = infiniteSequence(tuplesOf(id1), MIN_TUPLE_USER);
var seq2 = infiniteSequence(tuplesOf(id2), MAX_TUPLE_USER);
var tuples = [];
var tuple1 = seq1.next().value;
var tuple2 = seq2.next().value;
while (tuple1.compareTo(tuple2) === 0 /* Equal */) {
// Cannot insert a new tuple between tuple1 and tuple2
tuples.push(tuple1);
tuple1 = seq1.next().value;
tuple2 = seq2.next().value;
}
if (tuple1.random === INT32_BOTTOM && tuple2.random === INT32_BOTTOM_USER) {
// Special case to avoid problematic scenarios with renaming mechanism
tuples.push(tuple2);
while (tuple1.compareTo(MIN_TUPLE_USER) !== 0 /* Equal */) {
tuple1 = seq1.next().value;
}
tuple2 = seq2.next().value;
}
else if (tuple2.random - tuple1.random <= 1) {
tuples.push(tuple1);
tuple1 = seq1.next().value;
while (tuple2.compareTo(MAX_TUPLE_USER) !== 0 /* Equal */) {
tuple2 = seq2.next().value;
}
}
if (tuple1.random + 1 === INT32_TOP_USER) {
tuples.push(tuple1);
tuple1 = seq1.next().value;
}
var random = randomInt32(tuple1.random + 1, tuple2.random);
// random ∈ ]tuple1.random, tuple2.random[
// tuple1.random exclusion ensures a dense set
// tuple2.random exclusion ensures that newTuple < tuple2
// and thus that newId < id2
tuples.push(new IdentifierTuple(random, replicaNumber, clock, 0));
return new Identifier(tuples);
}
export function createAtPosition(replicaNumber, clock, position, offset) {
console.assert([position, replicaNumber, clock, offset].every(isInt32), "each value ∈ int32");
var tuple = new IdentifierTuple(position, replicaNumber, clock, offset);
return new Identifier([tuple]);
}
/**
* Generate an infinite sequence of tuples
*
* @param values
* @param defaultValue
*/
function infiniteSequence(values, defaultValue) {
var values_1, values_1_1, v, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, 6, 7]);
values_1 = __values(values), values_1_1 = values_1.next();
_b.label = 1;
case 1:
if (!!values_1_1.done) return [3 /*break*/, 4];
v = values_1_1.value;
return [4 /*yield*/, v];
case 2:
_b.sent();
_b.label = 3;
case 3:
values_1_1 = values_1.next();
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 7];
case 5:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 7];
case 6:
try {
if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 7:
if (!true) return [3 /*break*/, 9];
return [4 /*yield*/, defaultValue];
case 8:
_b.sent();
return [3 /*break*/, 7];
case 9: return [2 /*return*/];
}
});
}
/**
* @param id
* @return Tuples of `a' or an empty array if none.
*/
function tuplesOf(id) {
return (id !== null) ? id.tuples : [];
}
//# sourceMappingURL=idfactory.js.map