UNPKG

quadstore

Version:

Quadstore is a LevelDB-backed RDF graph database / triplestore for JavaScript runtimes (browsers, Node.js, Deno, Bun, ...) that implements the RDF/JS interfaces and supports SPARQL queries and querying across named graphs.

89 lines 3.24 kB
import { consumeOneByOne } from '../utils/consumeonebyone.js'; import { uid } from '../utils/uid.js'; import { separator, boundary } from '../utils/constants.js'; import { LevelIterator } from '../get/leveliterator.js'; export class Scope { id; blankNodes; factory; static async init(store) { return new Scope(store.dataFactory, uid(), new Map()); } static async load(store, scopeId) { const levelOpts = Scope.getLevelIteratorOpts(false, true, scopeId); const iterator = new LevelIterator(store.db.iterator(levelOpts), ([key, value]) => JSON.parse(value)); const blankNodes = new Map(); const { dataFactory: factory } = store; await consumeOneByOne(iterator, (mapping) => { blankNodes.set(mapping[0], factory.blankNode(mapping[1])); }); return new Scope(factory, scopeId, blankNodes); } static async delete(store, scopeId) { const batch = store.db.batch(); const levelOpts = Scope.getLevelIteratorOpts(true, false, scopeId); const iterator = new LevelIterator(store.db.iterator(levelOpts), ([key, value]) => key); await consumeOneByOne(iterator, (key) => { batch.del(key); }); await batch.write(); } static getLevelIteratorOpts(keys, values, scopeId) { const gte = scopeId ? `SCOPE${separator}${scopeId}${separator}` : `SCOPE${separator}`; return { keys, values, keyEncoding: 'utf8', valueEncoding: 'utf8', gte, lte: `${gte}${boundary}`, }; } static addMappingToLevelBatch(scopeId, batch, originalLabel, randomLabel) { batch.put(`SCOPE${separator}${scopeId}${separator}${originalLabel}`, JSON.stringify([originalLabel, randomLabel])); } constructor(factory, id, blankNodes) { this.blankNodes = blankNodes; this.factory = factory; this.id = id; } parseBlankNode(node, batch) { let cachedNode = this.blankNodes.get(node.value); if (!cachedNode) { cachedNode = this.factory.blankNode(uid()); this.blankNodes.set(node.value, cachedNode); Scope.addMappingToLevelBatch(this.id, batch, node.value, cachedNode.value); } return cachedNode; } parseSubject(node, batch) { switch (node.termType) { case 'BlankNode': return this.parseBlankNode(node, batch); default: return node; } } parseObject(node, batch) { switch (node.termType) { case 'BlankNode': return this.parseBlankNode(node, batch); default: return node; } } parseGraph(node, batch) { switch (node.termType) { case 'BlankNode': return this.parseBlankNode(node, batch); default: return node; } } parseQuad(quad, batch) { return this.factory.quad(this.parseSubject(quad.subject, batch), quad.predicate, this.parseObject(quad.object, batch), this.parseGraph(quad.graph, batch)); } } //# sourceMappingURL=index.js.map