Skip to content

Commit

Permalink
Added test an nt import
Browse files Browse the repository at this point in the history
  • Loading branch information
crubier committed Dec 6, 2014
1 parent f5e3cac commit 9bb918f
Show file tree
Hide file tree
Showing 4 changed files with 40,241 additions and 2 deletions.
42 changes: 41 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ function Hexastore() {
this.ops = {};
}

// Export a database to import with import
Hexastore.prototype.export = function(dbname) {
fs.writeFileSync(dbname + ".json", JSON.stringify(this.spo));
}

// Export a database to import with importZip
Hexastore.prototype.exportZip = function(dbname) {
// creating archives
var zip = new AZip();
Expand All @@ -23,7 +25,7 @@ Hexastore.prototype.exportZip = function(dbname) {
zip.writeZip(dbname + ".zip");
}


// Import a database previously exported with export
Hexastore.prototype.import = function(dbname) {
try {
this.addSPO(JSON.parse(fs.readFileSync(dbname + ".json")));
Expand All @@ -32,12 +34,50 @@ Hexastore.prototype.import = function(dbname) {
}
}

// Import a database previously exported with exportZip
Hexastore.prototype.importZip = function(dbname) {
var zip = new AZip(dbname + ".zip");
// outputs the content of some_folder/my_file.txt
this.addSPO(JSON.parse(zip.readAsText("data.json")));
}

// Import a database in a NT file
Hexastore.prototype.importNt = function(ntname,callback) {
function readLines(input, func) {
var remaining = '';

input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
while (index > -1) {
var line = remaining.substring(0, index);
remaining = remaining.substring(index + 1);

func(line);

index = remaining.indexOf('\n');
}
});

input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
callback();
});
}

var that = this;

function func(line) {
var elements = line.match(/[^\s"']+|"([^"]*)"|'([^']*)'/g);
that.put(elements);
}

var input = fs.createReadStream(ntname+'.nt');
readLines(input, func);
}

// Add a single triple to the store
Hexastore.prototype.put = function(element) {
var s = element[0];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A fast, pure javascript implementation of the hexatore RDF triple store, also useful as a graph database",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node test.js"
},
"repository": {
"type": "git",
Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var Hexastore = require('../hexastore');
var assert = require('assert');

var db= new Hexastore();

db.importNt("testdataset",
function() {

assert.equal(1077,

db.search([
["<http:https://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/ProductType1>",["predicate"],["object"]],
[["similar"],["predicate"],["object"]]
]).filter(function(match){return match.object2 !== match.object}).length);


assert.equal(
db.search([
["<http:https://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/ProductType1>","<http:https://purl.org/dc/elements/1.1/publisher>",["publisher"]]
])[0].publisher,'<http:https://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/StandardizationInstitution1>' );

});
Loading

0 comments on commit 9bb918f

Please sign in to comment.