mute-structs
Version:
NodeJS module providing an implementation of the LogootSplit CRDT algorithm
128 lines (124 loc) • 4.73 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 __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
import { isArrayFromMap, isObject } from "../data-validation";
import { Epoch } from "./epoch";
export function compareEpochFullIds(id1, id2) {
var minLength = id1.length < id2.length ? id1.length : id2.length;
for (var i = 0; i < minLength; i++) {
var value1 = id1[i];
var value2 = id2[i];
if (value1 < value2) {
return -1 /* Less */;
}
else if (value1 > value2) {
return 1 /* Greater */;
}
}
if (id1.length < id2.length) {
return -1 /* Less */;
}
else if (id1.length > id2.length) {
return 1 /* Greater */;
}
else {
return 0 /* Equal */;
}
}
var EpochStore = /** @class */ (function () {
function EpochStore(origin) {
this.epochs = new Map();
this.addEpoch(origin);
}
EpochStore.fromPlain = function (o) {
if (isObject(o) && Array.isArray(o.epochs) && o.epochs.length > 0) {
var epochs = o.epochs
.filter(isArrayFromMap)
.map(function (_a) {
var _b = __read(_a, 2), _ = _b[0], v = _b[1];
return Epoch.fromPlain(v);
})
.filter(function (epoch) { return epoch !== null; });
if (o.epochs.length === epochs.length) {
var _a = __read(epochs), origin = _a[0], rest = _a.slice(1);
var epochStore_1 = new EpochStore(origin);
rest.forEach(function (epoch) {
epochStore_1.addEpoch(epoch);
});
return epochStore_1;
}
}
return null;
};
EpochStore.prototype.addEpoch = function (epoch) {
this.epochs.set(epoch.id.asStr, epoch);
};
EpochStore.prototype.hasEpoch = function (epoch) {
return this.epochs.has(epoch.id.asStr);
};
EpochStore.prototype.getEpoch = function (epochId) {
return this.epochs.get(epochId.asStr);
};
EpochStore.prototype.getEpochFullId = function (epoch) {
console.assert(this.hasEpoch(epoch), "The epoch should have been added to the store previously");
var parentEpochFullId = [];
if (epoch.parentId !== undefined) {
var parentEpoch = this.getEpoch(epoch.parentId);
if (parentEpoch !== undefined) {
parentEpochFullId = this.getEpochFullId(parentEpoch);
}
}
return parentEpochFullId.concat(epoch.id.replicaNumber, epoch.id.epochNumber);
};
EpochStore.prototype.getEpochPath = function (epoch) {
var pathEpoch = [];
var currentEpoch = epoch;
while (currentEpoch !== undefined) {
pathEpoch.push(currentEpoch);
currentEpoch = currentEpoch.parentId !== undefined ? this.getEpoch(currentEpoch.parentId) : undefined;
}
return pathEpoch.reverse();
};
EpochStore.prototype.getPathBetweenEpochs = function (from, to) {
var fromPath = this.getEpochPath(from);
var toPath = this.getEpochPath(to);
var i = 0;
while (i < fromPath.length && i < toPath.length && fromPath[i].equals(toPath[i])) {
i++;
}
return [fromPath.slice(i).reverse(), toPath.slice(i)];
};
EpochStore.prototype.toJSON = function () {
return { epochs: Array.from(this.epochs) };
};
return EpochStore;
}());
export { EpochStore };
//# sourceMappingURL=epochstore.js.map