@x5e/gink
Version:
an eventually consistent database
92 lines • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Vertex = void 0;
const Container_1 = require("./Container");
const builders_1 = require("./builders");
const Bundler_1 = require("./Bundler");
const utils_1 = require("./utils");
const Edge_1 = require("./Edge");
class Vertex extends Container_1.Container {
constructor(database, address, containerBuilder) {
super(database, address, builders_1.Behavior.VERTEX);
if (this.address.timestamp < 0) {
(0, utils_1.ensure)(address.offset === builders_1.Behavior.VERTEX);
}
else if (containerBuilder) {
(0, utils_1.ensure)(containerBuilder.getBehavior() === builders_1.Behavior.VERTEX);
}
}
/**
* Returns a promise that resolves to true showing if this placeholder is/was visible at the
* specified time (default now), or false if it was softly deleted.
* @returns undefined, a basic value, or a container
*/
async isAlive(asOf) {
const entry = await this.database.store.getEntryByKey(this.address, undefined, asOf);
return entry === undefined || !entry.deletion;
}
/**
* Performs a soft delete of this graph node.
*/
async remove(change) {
return this.addEntry(undefined, Container_1.Container.DELETION, change);
}
async revive(change) {
return this.addEntry(undefined, Container_1.Container.INCLUSION, change);
}
async reset(args) {
const toTime = args === null || args === void 0 ? void 0 : args.toTime;
const bundlerOrComment = args === null || args === void 0 ? void 0 : args.bundlerOrComment;
const skipProperties = args === null || args === void 0 ? void 0 : args.skipProperties;
let immediate = false;
let bundler;
if (bundlerOrComment instanceof Bundler_1.Bundler) {
bundler = bundlerOrComment;
}
else {
immediate = true;
bundler = new Bundler_1.Bundler(bundlerOrComment);
}
if (!toTime) {
await this.remove(bundlerOrComment);
}
else {
const aliveThen = await this.isAlive(toTime);
const aliveNow = await this.isAlive();
if (aliveThen !== aliveNow) {
if (aliveThen) {
await this.revive(bundlerOrComment);
}
else {
await this.remove(bundlerOrComment);
}
}
}
if (!skipProperties) {
await this.resetProperties(toTime, bundler);
}
if (immediate) {
await this.database.addBundler(bundler);
}
}
async getEdgesFrom(asOf) {
return this.getEdges(true, asOf);
}
async getEdgesTo(asOf) {
return this.getEdges(false, asOf);
}
async getEdges(source, asOf) {
const entries = await this.database.store.getEntriesBySourceOrTarget(this.address, source, asOf);
const edges = [];
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
if (entry.behavior !== builders_1.Behavior.EDGE_TYPE)
continue;
const edge = new Edge_1.Edge(this.database, (0, utils_1.muidTupleToMuid)(entry.entryId), (0, utils_1.entryToEdgeData)(entry));
edges.push(edge);
}
return edges;
}
}
exports.Vertex = Vertex;
//# sourceMappingURL=Vertex.js.map