levelgraph
Version:
A graph database for Node.js and the browser built on top of LevelUp
284 lines (227 loc) • 6.97 kB
JavaScript
/*
Copyright (c) 2013-2016 Matteo Collina and LevelGraph Contributors
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
var CallbackStream = require('callback-stream')
, Variable = require('./variable')
, lodashKeys = require('lodash.keys')
, defs = {
spo: ['subject', 'predicate', 'object'],
sop: ['subject', 'object', 'predicate'],
pos: ['predicate', 'object', 'subject'],
pso: ['predicate', 'subject', 'object'],
ops: ['object', 'predicate', 'subject'],
osp: ['object', 'subject', 'predicate']
};
var upperBoundChar = '\udbff\udfff';
module.exports.defs = defs;
function wrapCallback(method) {
return function(query, cb) {
var args = Array.prototype.slice.call(arguments, 0)
, callback = args.pop()
, stream = null;
stream = this[method].apply(this, args);
stream.pipe(new CallbackStream({ objectMode: true }, callback));
};
}
module.exports.wrapCallback = wrapCallback;
function escape(value) {
if (typeof value === 'string' || value instanceof String) {
return value.replace(/(\\|:)/g,'\\$&');
}
return value;
}
module.exports.escape = escape;
function genKey(key, triple) {
var result = key, def = defs[key], value, i;
for (i = 0; (value = triple[def[i]]) !== null &&
value !== undefined; i++) {
result += '::' + escape(value);
}
if (i < 3) {
result += '::';
}
return result;
}
module.exports.genKey = genKey;
var defKeys = Object.keys(defs);
function genKeys(triple) {
var i, result = [];
for (i = 0; i < defKeys.length; i++) {
result.push(genKey(defKeys[i], triple));
}
return result;
}
module.exports.genKeys = genKeys;
function possibleIndexes(types) {
var result = Object.keys(defs).filter(function(key) {
var matches = 0;
return defs[key].every(function (e, i) {
if (types.indexOf(e) >= 0) {
matches++;
return true;
}
if (matches === types.length) {
return true;
}
});
});
result.sort();
return result;
}
module.exports.possibleIndexes = possibleIndexes;
function findIndex(types, preferiteIndex) {
var result = possibleIndexes(types)
, index
, there;
there = result.some(function(r) {
return r === preferiteIndex;
});
if (preferiteIndex && there) {
index = preferiteIndex;
} else {
index = result[0];
}
return index;
}
module.exports.findIndex = findIndex;
function typesFromPattern(pattern) {
return Object.keys(pattern).filter(function(key) {
switch(key) {
case 'subject':
return !!pattern.subject;
case 'predicate':
return !!pattern.predicate;
case 'object':
return !!pattern.object;
default:
return false;
}
});
}
function applyUpperBoundChar(key) {
var parts = key.split('::');
var len = parts.length;
return len === 4 && parts[len-1] !== '' ? key : key + upperBoundChar;
}
function createQuery(pattern, options) {
var types = typesFromPattern(pattern)
, preferiteIndex = options && options.index
, index = findIndex(types, preferiteIndex)
, key = genKey(index, pattern, '')
, limit = pattern.limit
, reverse = pattern.reverse || false
, start = reverse ? applyUpperBoundChar(key) : key
, end = reverse ? key : applyUpperBoundChar(key)
, query = {
start: start
, end: end
, reverse: reverse
, fillCache: true
, limit: typeof limit === 'number' && limit || -1
, highWaterMark: 16
, valueEncoding: 'json'
};
return query;
}
module.exports.createQuery = createQuery;
function generateBatch(triple, action) {
if (!action) {
action = 'put';
}
var json = JSON.stringify(triple);
return genKeys(triple).map(function(key) {
return { type: action, key: key, value: json };
});
}
module.exports.generateBatch = generateBatch;
function materializer(pattern, data) {
return Object.keys(pattern)
.reduce(function(result, key) {
if (pattern[key] instanceof Variable) {
result[key] = data[pattern[key].name];
} else {
result[key] = pattern[key];
}
return result;
}, {});
}
module.exports.materializer = materializer;
(function() {
var a, b, c;
a = function(key) {
return defs.spo.indexOf(key) >= 0;
};
b = function(triple, key) {
return typeof triple[key] !== 'object';
};
c = function(triple, key) {
return triple[key] instanceof Variable;
};
var objectMask = function objectMask(criteria, object) {
return lodashKeys(object).
filter(a).
filter(function(key) {
return criteria(object, key);
}).
reduce(function(acc, key) {
acc[key] = object[key];
return acc;
},
{});
};
module.exports.queryMask = function queryMask(object) {
return objectMask(b, object);
};
module.exports.variablesMask = function variablesMask(object) {
return objectMask(c, object);
};
})();
var variablesMask = module.exports.variablesMask;
function maskUpdater(pattern) {
var variables = variablesMask(pattern);
return function(solution, mask) {
return Object.keys(variables).reduce(function(newMask, key) {
var variable = variables[key];
if (variable.isBound(solution)) {
newMask[key] = solution[variable.name];
}
newMask.filter = pattern.filter;
return newMask;
}, Object.keys(mask).reduce(function(acc, key) {
acc[key] = mask[key];
return acc;
}, {}));
};
}
module.exports.maskUpdater = maskUpdater;
function matcher(pattern) {
var variables = variablesMask(pattern);
return function realMatcher(solution, triple) {
var key, bindable = true, newsolution = solution;
for (key in variables) {
if (newsolution && variables.hasOwnProperty(key)) {
newsolution = variables[key].bind(newsolution, triple[key]);
}
}
return newsolution;
};
}
module.exports.matcher = matcher;