Skip to content

Commit

Permalink
Small refactorings and more store specs
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniogarrote committed Mar 8, 2015
1 parent bbafe34 commit a5f6d83
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 84 deletions.
8 changes: 5 additions & 3 deletions spec/store_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ describe("Store", function () {
});
});

/*

it("Should pass test Load1", function (done) {
Store.create({name: 'test', overwrite: true}, function (err, store) {

Expand Down Expand Up @@ -518,6 +518,7 @@ describe("Store", function () {
});
});

/*
it("Should pass test Load2", function (done) {
Store.create(function (err, store) {
store.load('remote', 'http://dbpedia.org/resource/Tim_Berners-Lee', function (err, result) {
Expand All @@ -536,7 +537,7 @@ describe("Store", function () {
});
});
});
*/
it("Should pass test Load3", function (done) {
Store.create({name: 'test', overwrite: true}, function (err, store) {

Expand Down Expand Up @@ -569,6 +570,7 @@ describe("Store", function () {
});
});


it("Should pass test Load5", function (done) {
Store.create({name: 'test', overwrite: true}, function (err, store) {

Expand Down Expand Up @@ -820,7 +822,7 @@ describe("Store", function () {
});
});
});
/*
//it("Should pass test Export1", function(done) {
// Store.create(function(err,store) {
// store.load('remote', 'http://dbpedia.org/resource/Tim_Berners-Lee', 'http://test.com/graph-to-export', function(err, result) {
Expand Down
68 changes: 35 additions & 33 deletions src/graph_callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var _ = require('lodash');
var async = require('async');
var QuadIndex = require('./quad_index').QuadIndex;
var Pattern = require('./quad_index').Pattern;
var RDFModel = require('./rdf_model');
var AbstractQueryTree = require('./abstract_query_tree').AbstractQueryTree;

Expand Down Expand Up @@ -194,38 +195,39 @@ Callbacks.CallbacksBackend.prototype.subscribe = function(s,p,o,g,callback, done
var queryEnv = {blanks:{}, outCache:{}};
this.engine.registerNsInEnvironment(null, queryEnv);
var that = this;
var normalized = this.engine.normalizeQuad(quad, queryEnv, true);
var pattern = new QuadIndexCommon.Pattern(normalized);
var indexKey = that._indexForPattern(pattern);
var indexOrder = that.componentOrders[indexKey];
var index = that.indexMap[indexKey];
for(var i=0; i<indexOrder.length; i++) {
var component = indexOrder[i];
var quadValue = normalized[component];
if(quadValue === '_') {
if(index['_'] == null) {
index['_'] = [];
}
that.callbackCounter++;
index['_'].push(that.callbackCounter);
that.callbacksMap[that.callbackCounter] = callback;
that.callbacksInverseMap[callback] = that.callbackCounter;
break;
} else {
if(i===indexOrder.length-1) {
index[quadValue] = index[quadValue] || {'_':[]};
this.engine.normalizeQuad(quad, queryEnv, true, function(normalized){
var pattern = new Pattern(normalized);
var indexKey = that._indexForPattern(pattern);
var indexOrder = that.componentOrders[indexKey];
var index = that.indexMap[indexKey];
for(var i=0; i<indexOrder.length; i++) {
var component = indexOrder[i];
var quadValue = normalized[component];
if(quadValue === '_') {
if(index['_'] == null) {
index['_'] = [];
}
that.callbackCounter++;
index[quadValue]['_'].push(that.callbackCounter);
index['_'].push(that.callbackCounter);
that.callbacksMap[that.callbackCounter] = callback;
that.callbacksInverseMap[callback] = that.callbackCounter;
break;
} else {
index[quadValue] = index[quadValue] || {};
index = index[quadValue];
if(i===indexOrder.length-1) {
index[quadValue] = index[quadValue] || {'_':[]};
that.callbackCounter++;
index[quadValue]['_'].push(that.callbackCounter);
that.callbacksMap[that.callbackCounter] = callback;
that.callbacksInverseMap[callback] = that.callbackCounter;
} else {
index[quadValue] = index[quadValue] || {};
index = index[quadValue];
}
}
}
}
if(doneCallback != null)
doneCallback(true);
if(doneCallback != null)
doneCallback(true);
});
};

Callbacks.CallbacksBackend.prototype.unsubscribe = function(callback) {
Expand Down Expand Up @@ -309,8 +311,8 @@ Callbacks.CallbacksBackend.prototype.observeNode = function() {
var queryEnv = {blanks:{}, outCache:{}};
this.engine.registerNsInEnvironment(null, queryEnv);
var bindings = [];
this.engine.execute(query, function(success, graph){
if(success) {
this.engine.execute(query, function(err, graph){
if(!err) {
var node = graph;
var mustFlush = false;
var observer = function(event, triples){
Expand Down Expand Up @@ -390,7 +392,7 @@ Callbacks.CallbacksBackend.prototype.observeQuery = function(query, callback, en
}

var normalized = that.engine.normalizeQuad(quad, queryEnv, true);
pattern = new QuadIndexCommon.Pattern(normalized);
pattern = new Pattern(normalized);
indexKey = that._indexForPattern(pattern);
indexOrder = that.componentOrders[indexKey];
index = that.queriesIndexMap[indexKey];
Expand All @@ -417,8 +419,8 @@ Callbacks.CallbacksBackend.prototype.observeQuery = function(query, callback, en

}

this.engine.execute(query, function(success, results){
if(success){
this.engine.execute(query, function(err, results){
if(!err){
callback(results);
} else {
console.log("ERROR in query callback "+results);
Expand Down Expand Up @@ -481,8 +483,8 @@ Callbacks.CallbacksBackend.prototype.dispatchQueries = function(callback) {
queryCallback = that.queriesCallbacksMap[queryId];

that.engine.execute(query,
function(success, results){
if(success) {
function(err, results){
if(!err) {
try{
queryCallback(results);
}catch(e){}
Expand Down
2 changes: 1 addition & 1 deletion src/lexicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Lexicon.prototype.registeredGraphs = function(returnUris, callback) {
var graphs = [];
this.knownGraphs.walk(function(node){
if(returnUris === true) {
graphs.push(node.value);
graphs.push(node.data);
} else {
graphs.push(node.key);
}
Expand Down
21 changes: 11 additions & 10 deletions src/query_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,12 @@ QueryEngine.prototype.normalizeTerm = function(term, env, shouldIndex, callback)
callback(oid);
} else {
if(shouldIndex) {
this.lexicon.registerBlank(label, function(oid){
this.lexicon.registerBlank(function(oid){
env.blanks[label] = oid;
callback(oid);
});
} else {
this.lexicon.resolveBlank(label, function(oid) {
this.lexicon.resolveBlank(function(oid) {
env.blanks[label] = oid;
callback(oid);
});
Expand Down Expand Up @@ -403,15 +403,16 @@ QueryEngine.prototype.normalizeQuad = function(quad, queryEnv, shouldIndex, call
var error = false;

async.seq(function(k){
if(quad.graph == null) {
if(quad.graph == null || quad.graph.value === that.lexicon.defaultGraphUri) {
graph = 0; // default graph
k();
} else {
var graphUriValue = Utils.lexicalFormBaseUri(quad.graph, queryEnv);
that.normalizeTerm(quad.graph, queryEnv, shouldIndex, function(oid){
if(oid != null) {
graph = oid;
if(shouldIndex === true && quad.graph.token!='var') {
that.lexicon.registerGraph(oid, quad.graph.value, function(){
that.lexicon.registerGraph(oid, graphUriValue, function(){
k();
});
} else {
Expand Down Expand Up @@ -1359,13 +1360,13 @@ QueryEngine.prototype.executeUpdate = function(syntaxTree, callback) {
graph = {'uri': Utils.lexicalFormBaseUri(aqt.destinyGraph, queryEnv)};
}
var that = this;
this.rdfLoader.load(aqt.sourceGraph.value, graph, function(success, result){
if(success == false) {
this.rdfLoader.load(aqt.sourceGraph.value, graph, function(err, result){
if(err) {
console.log("Error loading graph");
console.log(result);
callback(false, "error batch loading quads");
} else {
var result = that.batchLoad(result,function(result){
that.batchLoad(result,function(result){
callback(result!=null, result||"error batch loading quads");
});
}
Expand Down Expand Up @@ -1413,7 +1414,7 @@ QueryEngine.prototype.batchLoad = function(quads, callback) {
} else {
maybeBlankOid = blanks[quad[component].blank || quad[component].value];
if (maybeBlankOid == null) {
that.lexicon.registerBlank(quad[component].blank || quad[component].value, function(maybeBlankOid){
that.lexicon.registerBlank(function(maybeBlankOid){
blanks[(quad[component].blank || quad[component].value)] = maybeBlankOid;

if (quad[component].token == null) {
Expand Down Expand Up @@ -1731,8 +1732,8 @@ QueryEngine.prototype._executeClearGraph = function(destinyGraph, queryEnv, call
}
} else if(destinyGraph === 'all') {
var that = this;
this.execute("CLEAR DEFAULT", function(success, result) {
if(success) {
this.execute("CLEAR DEFAULT", function(err, result) {
if(!err) {
that.execute("CLEAR NAMED", callback);
} else {
callback(false,result);
Expand Down
2 changes: 1 addition & 1 deletion src/rdf_loader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var NetworkTransport = require("./network_transport").NetworkTransport;
var RVN3Parser = require("./rvn3_parser").RVN3Parser;
var JSONLDParser = require("./jsonld_parser").JSONLDParser;
var Utils = require("./utils").Utils;
var Utils = require("./utils");

var RDFLoader = function (params) {

Expand Down
53 changes: 17 additions & 36 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var QueryEngine = require("./query_engine").QueryEngine;
var QuadBackend = require("./quad_backend").QuadBackend;
var Lexicon = require("./lexicon").Lexicon;
var RDFModel = require("./rdf_model");
var _ = require("lodash");

/**
* Version of the store
Expand Down Expand Up @@ -106,10 +107,6 @@ Store.create = function(){
* <li> name: when using persistence, the name for this store. In the MongoDB backed version, name of the DB used by the store. By default <code>'rdfstore_js'</code> is used</li>
* <li> overwrite: clears the persistent storage </li>
* <li> maxCacheSize: if using persistence, maximum size of the index cache </li>
* <li> engine: the persistent storage to use, a value <code>mongodb</code> selects the MongoDB engine</li>
* <li> mongoDomain: when <code>engine=mongodb</code>, server domain name or IP address where the MongoDB server backing the store is running. By default <code>'127.0.0.1'</code> is used</li>
* <li> mongoPort: when <code>engine=mongodb</code>, port where the MongoDB server is running. By default <code>27017</code> is used</li>
* <li> mongoOptions: when <code>engine=mongodb</code>, additional options for the MongoDB driver. By default <code>{}</code> is used</li>
* </ul>
*/
Store.Store = function(arg1, arg2) {
Expand Down Expand Up @@ -281,15 +278,15 @@ Store.Store.prototype.executeWithEnvironment = function() {
defaultGraphs = arguments[1];
namedGraphs = arguments[2];
}
var defaultGraphsNorm = [];
var namedGraphsNorm = [];
for(var i=0; i<defaultGraphs.length; i++) {
defaultGraphsNorm.push({'token':'uri','value':defaultGraphs[i]})
}
for(var i=0; i<namedGraphs.length; i++) {
namedGraphsNorm.push({'token':'uri','value':namedGraphs[i]})
}
this.engine.execute(queryString, callback, defaultGraphsNorm, namedGraphsNorm);

defaultGraphs = _.map(defaultGraphs, function(graph){
return {'token':'uri','value':graph};
});
namedGraphs = _.map(namedGraphs, function(graph){
return {'token':'uri','value':graph};
});

this.engine.execute(queryString, callback, defaultGraphs, namedGraphs);
};

/**
Expand Down Expand Up @@ -497,16 +494,15 @@ Store.Store.prototype.subscribe = function(s, p, o, g, callback) {
var queryEnv = {blanks:{}, outCache:{}};
var bindings = [];

for(var i=0; i<triples.length; i++) {
var triple = triples[i];
_.each(triples, function(triple){
var s = RDFModel.buildRDFResource(triple.subject,bindings,that.engine,queryEnv);
var p = RDFModel.buildRDFResource(triple.predicate,bindings,that.engine,queryEnv);
var o = RDFModel.buildRDFResource(triple.object,bindings,that.engine,queryEnv);
if(s!=null && p!=null && o!=null) {
triple = new RDFModel.Triple(s,p,o);
acum.push(triple);
}
}
});

callback(event,acum);
};
Expand Down Expand Up @@ -866,28 +862,13 @@ Store.Store.prototype.registerParser = function(mediaType, parser) {
* @param {Function} callback function that will receive a success notification and the array of graph URIs
*/
Store.Store.prototype.registeredGraphs = function(callback) {
if(this.isMongodb) {
this.engine.registeredGraphs(true, function(graphs){
var acum = [];
for(var i=0; i<graphs.length; i++) {
var graph = graphs[i];
var uri = new RDFModel.NamedNode(graph);
acum.push(uri);
}

return callback(null, acum);
this.engine.lexicon.registeredGraphs(true, function(graphs){
var graphNodes = _.map(graphs, function(graph){
return new RDFModel.NamedNode(graph);
});
} else {
var graphs = this.engine.lexicon.registeredGraphs(true);
var acum = [];
for(var i=0; i<graphs.length; i++) {
var graph = graphs[i];
var uri = new RDFModel.NamedNode(graph);
acum.push(uri);
}

return callback(null, acum);
}
callback(null, graphNodes);
});
};

/**
Expand Down

0 comments on commit a5f6d83

Please sign in to comment.