@x5e/gink
Version:
an eventually consistent database
88 lines • 3.33 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 store_utils_1 = require("./store_utils");
const builders_1 = require("./builders");
class Edge extends Addressable_1.Addressable {
constructor(database, address) {
super(address);
this.database = database;
}
static get(database, muid, data) {
const edge = new Edge(database, muid);
edge.setFromEdgeData(data);
return edge;
}
setFromEdgeData(data) {
this.source = data.source;
this.target = data.target;
this.action = data.etype ?? {
timestamp: -1,
medallion: -1,
offset: builders_1.Behavior.EDGE_TYPE,
};
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");
}
const edge = new Edge(database, address);
edge.setFromEdgeData((0, utils_1.entryToEdgeData)(entry));
return edge;
}
getSourceVertex() {
return Vertex_1.Vertex.get(this.database, this.source);
}
getTargetVertex() {
return Vertex_1.Vertex.get(this.database, this.target);
}
getEdgeType() {
return EdgeType_1.EdgeType.get(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 meta optional metadata (may contain: comment, identity, or bundler)
*/
async remove(dest, purge, meta) {
if (!(await this.isAlive()))
throw new Error("this edge is not alive.");
const bundler = await this.database.startBundle(meta);
await (0, store_utils_1.movementHelper)(bundler, this.address, this.action, dest, purge);
if (!meta?.bundler) {
await bundler.commit();
}
}
}
exports.Edge = Edge;
//# sourceMappingURL=Edge.js.map