@comunica/actor-rdf-resolve-quad-pattern-hdt
Version:
A hdt rdf-resolve-quad-pattern actor
77 lines • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActorRdfResolveQuadPatternHdt = void 0;
const bus_rdf_resolve_quad_pattern_1 = require("@comunica/bus-rdf-resolve-quad-pattern");
// eslint-disable-next-line import/namespace
const HDT = require("hdt");
const HdtQuadSource_1 = require("./HdtQuadSource");
/**
* A comunica HDT RDF Resolve Quad Pattern Actor.
*/
class ActorRdfResolveQuadPatternHdt extends bus_rdf_resolve_quad_pattern_1.ActorRdfResolveQuadPatternSource {
constructor(args) {
super(args);
this.hdtDocuments = {};
this.closed = false;
this.queries = 0;
}
initializeHdt(hdtFile) {
// eslint-disable-next-line no-return-assign,import/namespace
return this.hdtDocuments[hdtFile] = HDT.fromFile(hdtFile);
}
async initialize() {
(this.hdtFiles || []).forEach(hdtFile => this.initializeHdt(hdtFile));
return null;
}
async deinitialize() {
process.on('exit', () => this.safeClose());
process.on('SIGINT', () => this.safeClose());
return null;
}
close() {
if (this.closed) {
throw new Error('This actor can only be closed once.');
}
if (!this.queries) {
this.shouldClose = false;
Object.keys(this.hdtDocuments).forEach(async (hdtFile) => (await this.hdtDocuments[hdtFile]).close());
this.closed = true;
}
else {
this.shouldClose = true;
}
}
async test(action) {
if (!this.hasContextSingleSourceOfType('hdtFile', action.context)) {
throw new Error(`${this.name} requires a single source with a hdtFile to be present in the context.`);
}
return true;
}
safeClose() {
if (!this.closed) {
this.close();
}
}
async getSource(context) {
const hdtFile = this.getContextSource(context).value;
// eslint-disable-next-line @typescript-eslint/no-misused-promises
if (!this.hdtDocuments[hdtFile]) {
await this.initializeHdt(hdtFile);
}
return new HdtQuadSource_1.HdtQuadSource(await this.hdtDocuments[hdtFile]);
}
async getOutput(source, pattern, context) {
// Attach totalItems to the output
this.queries++;
const output = await super.getOutput(source, pattern, context);
output.data.on('end', () => {
this.queries--;
if (this.shouldClose) {
this.close();
}
});
return output;
}
}
exports.ActorRdfResolveQuadPatternHdt = ActorRdfResolveQuadPatternHdt;
//# sourceMappingURL=ActorRdfResolveQuadPatternHdt.js.map