UNPKG

rdf-stores

Version:

A TypeScript/JavaScript implementation of the RDF/JS store interface with support for quoted triples.

51 lines 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TermDictionaryNumberRecord = void 0; const rdf_data_factory_1 = require("rdf-data-factory"); const rdf_string_1 = require("rdf-string"); /** * A term dictionary that encodes to numbers, and stores the dictionary in memory in a Record. */ class TermDictionaryNumberRecord { constructor(dataFactory = new rdf_data_factory_1.DataFactory()) { this.lastId = 0; this.dictionary = {}; this.reverseDictionary = {}; this.features = { quotedTriples: false }; this.dataFactory = dataFactory; } encode(term) { const key = (0, rdf_string_1.termToString)(term); let encoded = this.dictionary[key]; if (encoded === undefined) { encoded = this.lastId++; this.dictionary[key] = encoded; this.reverseDictionary[encoded] = key; } return encoded; } encodeOptional(term) { const key = (0, rdf_string_1.termToString)(term); return this.dictionary[key]; } decode(encoding) { const string = this.reverseDictionary[encoding]; if (string === undefined) { throw new Error(`The value ${encoding} is not present in this dictionary`); } return (0, rdf_string_1.stringToTerm)(string, this.dataFactory); } *encodings() { for (const key of Object.keys(this.reverseDictionary)) { yield Number.parseInt(key, 10); } } findQuotedTriples(quotedTriplePattern) { throw new Error('findQuotedTriples is not supported'); } findQuotedTriplesEncoded(quotedTriplePattern) { throw new Error('findQuotedTriplesEncoded is not supported'); } } exports.TermDictionaryNumberRecord = TermDictionaryNumberRecord; //# sourceMappingURL=TermDictionaryNumberRecord.js.map