Skip to content

Commit

Permalink
Upgraded to rdfjs
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurensRietveld committed Dec 19, 2019
1 parent b4a5ced commit 21386fd
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 230 deletions.
2 changes: 1 addition & 1 deletion bin/hdt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ hdt.fromFile(hdtFile)
results => {
process.stdout.write('# Total matches: ' + results.totalCount +
(results.exactCount ? '' : ' (estimated)') + '\n');
writer.addTriples(results.triples);
writer.addQuads(results.triples);
writer.end();
},
error => {
Expand Down
41 changes: 24 additions & 17 deletions lib/hdt.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
const N3 = require('n3');
const { stringQuadToQuad, stringToTerm, termToString } = require('rdf-string');

/* Auxiliary methods for HdtDocument */
const hdtNative = require('../build/Release/hdt');
const HdtDocumentPrototype = hdtNative.HdtDocument.prototype;

const MAX = Math.pow(2, 31) - 1;

const closedError = Promise.reject(new Error('The HDT document cannot be accessed because it is closed'));
closedError.catch(e => {});

function isValidHdtTerm(term) {
return term && term.termType && (term.termType === 'Literal' || term.termType === 'BlankNode' || term.termType === 'NamedNode');
}

// Searches the document for triples with the given subject, predicate, and object.
HdtDocumentPrototype.searchTriples = function (subject, predicate, object, options) {
if (this.closed) return closedError;
if (typeof subject !== 'string' || subject[0] === '?') subject = '';
if (typeof predicate !== 'string' || predicate[0] === '?') predicate = '';
if (typeof object !== 'string' || object[0] === '?') object = '';
if (!isValidHdtTerm(subject)) subject = null;
if (!isValidHdtTerm(predicate)) predicate = null;
if (!isValidHdtTerm(object)) object = null;
options = options || {};

const dataFactory = this.dataFactory;
return new Promise((resolve, reject) => {
this._searchTriples(subject, predicate, object,
this._searchTriples(termToString(subject) || '', termToString(predicate) || '', termToString(object) || '',
parseOffset(options), parseLimit(options),
(err, triples, totalCount, hasExactCount) =>
err ? reject(err) : resolve({ triples, totalCount, hasExactCount }));
err ? reject(err) : resolve({ triples: triples.map((t) => stringQuadToQuad(t, dataFactory)), totalCount, hasExactCount }));
});
};

Expand All @@ -32,11 +38,12 @@ HdtDocumentPrototype.countTriples = function (subject, predicate, object) {
HdtDocumentPrototype.searchLiterals = function (substring, options) {
if (this.closed) return closedError;
options = options || {};
const dataFactory = this.dataFactory;
return new Promise((resolve, reject) => {
this._searchLiterals(substring,
parseOffset(options), parseLimit(options),
(err, literals, totalCount) =>
err ? reject(err) : resolve({ literals, totalCount }));
err ? reject(err) : resolve({ literals: literals.map(l => stringToTerm(l, dataFactory)), totalCount }));
});
};

Expand All @@ -53,8 +60,8 @@ HdtDocumentPrototype.searchTerms = function (options) {
position = options.position,
posId = POSITIONS[position],
prefix = options.prefix || '',
subject = options.subject || '',
object = options.object || '';
subject = isValidHdtTerm(options.subject) ? options.subject : null,
object = isValidHdtTerm(options.object) ? options.object : null;

// Validate parameters
if (!(position in POSITIONS))
Expand All @@ -64,21 +71,21 @@ HdtDocumentPrototype.searchTerms = function (options) {

// Return predicates that connect subject and object
if (subject && object) {
return this.searchTriples(subject, undefined, object, { limit: limit })
return this.searchTriples(subject, null, object, { limit: limit })
.then(result => result.triples.map(statement => statement.predicate));
}

const dataFactory = this.dataFactory;
// Return distinct terms
return new Promise((resolve, reject) => {
if ('subject' in options || 'object' in options) {
if (!subject && !object) return resolve([]);
this._fetchDistinctTerms(subject, object, limit, posId,
(error, results) => error ? reject(error) : resolve(results));
this._fetchDistinctTerms(termToString(subject) || '', termToString(object) || '', limit, posId,
(error, results) => error ? reject(error) : resolve(results.map(t => stringToTerm(t, dataFactory))));
}
// No subject or object values specified, so assuming we're autocompleting a term
else {
this._searchTerms(prefix, limit, posId,
(error, results) => error ? reject(error) : resolve(results));
(error, results) => error ? reject(error) : resolve(results.map(t => stringToTerm(t, dataFactory))));
}
});
};
Expand Down Expand Up @@ -125,10 +132,9 @@ HdtDocumentPrototype.search = HdtDocumentPrototype.searchTriples;

module.exports = {
// Creates an HDT document for the given file.
fromFile: filename => {
fromFile: (filename, opts) => {
if (typeof filename !== 'string' || filename.length === 0)
return Promise.reject(Error('Invalid filename: ' + filename));

return new Promise((resolve, reject) => {
hdtNative.createHdtDocument(filename, (error, document) => {
// Abort the creation if any error occurred
Expand All @@ -142,6 +148,7 @@ module.exports = {
return reject(error);
}
}
document.dataFactory = opts && opts.dataFactory ? opts.dataFactory : N3.DataFactory;
// Document the features of the HDT file
document.features = Object.freeze({
searchTriples: true, // supported by default
Expand Down
35 changes: 32 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
"node": ">=6.0"
},
"scripts": {
"test": "rm test/*.hdt.index 2> /dev/null; mocha",
"test": "rm test/*.hdt.index.* 2> /dev/null; mocha",
"lint": "eslint --fix lib/*.js test/*.js bin/*",
"validate": "npm ls"
},
"dependencies": {
"minimist": "^1.1.0",
"n3": "^0.11.2",
"nan": "^2.14.0"
"n3": "^1.3.5",
"nan": "^2.14.0",
"rdf-string": "^1.3.1"
},
"devDependencies": {
"eslint": "^5.3.0",
Expand Down
Loading

0 comments on commit 21386fd

Please sign in to comment.