@x5e/gink
Version:
an eventually consistent database
86 lines • 3.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Edge = void 0;
const Addressable_1 = require("./Addressable");
const Vertex_1 = require("./Vertex");
const EdgeType_1 = require("./EdgeType");
const utils_1 = require("./utils");
const Bundler_1 = require("./Bundler");
const store_utils_1 = require("./store_utils");
class Edge extends Addressable_1.Addressable {
constructor(database, address, data) {
super(address);
this.database = database;
this.setFromEdgeData(data);
}
setFromEdgeData(data) {
this.source = data.source;
this.target = data.target;
this.action = data.action;
this.value = data.value;
}
static async load(database, address) {
const entry = await database.store.getEntryById(address, address.timestamp + 1);
if (!entry) {
throw new Error("edge not found");
}
return new Edge(database, address, (0, utils_1.entryToEdgeData)(entry));
}
getSourceVertex() {
return new Vertex_1.Vertex(this.database, this.source);
}
getTargetVertex() {
return new Vertex_1.Vertex(this.database, this.target);
}
getEdgeType() {
return new EdgeType_1.EdgeType(this.database, this.action);
}
getValue() {
return this.value;
}
/**
* NOTE: If this edge has been removed, or if its edgeType has been reset, this method will ALWAYS return false.
* If its edgeType has been reset, it has been replaced with a new edge that has the exact same source, target, value,
* and properties. Check getEdgesTo and getEdgesFrom on the source and target vertices to find replaced edges.
*/
async isAlive(asOf) {
return 0 !== (await this.getEffective(asOf));
}
async getEffective(asOf) {
const entry = await this.database.store.getEntryById(this.address, asOf);
if (!entry) {
return 0;
}
else {
return entry.storageKey;
}
}
/**
* If dest is not provided (or 0), the edge will be removed. This exact edge
* with the same Muid will never exist again. The only way to "revive" it is to reset
* the database or its edgeType. In that case, a new edge will be created with the same
* source, target, value, and properties.
* @param dest a timestamp to move the edge to. If 0 or not specified, the edge will be removed.
* @param purge completely remove the edge's entry from the datastore?
* @param bundlerOrComment a Bundler object or a string comment to add to the movement entry.
*/
async remove(dest, purge, bundlerOrComment) {
if (!(await this.isAlive()))
throw new Error("this edge is not alive.");
let immediate = false;
let bundler;
if (bundlerOrComment instanceof Bundler_1.Bundler) {
bundler = bundlerOrComment;
}
else {
immediate = true;
bundler = new Bundler_1.Bundler(bundlerOrComment);
}
await (0, store_utils_1.movementHelper)(bundler, this.address, this.action, dest, purge);
if (immediate) {
await this.database.addBundler(bundler);
}
}
}
exports.Edge = Edge;
//# sourceMappingURL=Edge.js.map