Skip to content

Instantly share code, notes, and snippets.

@JerrySievert
Created August 20, 2013 20:28
Show Gist options
  • Save JerrySievert/6286832 to your computer and use it in GitHub Desktop.
Save JerrySievert/6286832 to your computer and use it in GitHub Desktop.
index logic
/*
"crime":
{
"equals": "arson"
}
index -> specific index that references the property keyword
query -> object containing the specific queries for the index
set -> object containing keys of all of the id's matching currently
callback -> object containing keys of all of the id's still matching:
{
1: true,
23: true
}
TODO: add functionality for
"crime":
{
"or": {
"equals": "arson",
"equals": "theft"
}
}
*/
function eliminateForIndex(index, query, set, callback) {
var queryKeys = Object.keys(query);
var count = 0;
for (var i = 0; i < queryKeys.length; i++) {
if (typeof index[queryKeys[i]] !== "function") {
callback("Index does not have a method matching " + queryKeys[i]);
return;
}
index[queryKeys[i]](query[i], function (err, data) {
count++;
if (err) {
callback(err);
// short-circuit the scan, we hit an error. this is fatal.
count = queryKeys.length;
return;
} else {
var setKeys = Object.keys(set);
for (var j = 0; j < setKeys.length; j++) {
if (!data[setKeys[j]]) {
delete set[setKeys[j]];
}
}
}
if (count === queryKeys.length) {
callback(null, set);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment