Skip to content

Commit

Permalink
chainable find api
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Dec 31, 2010
1 parent f064307 commit ce8d44a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -18,7 +18,7 @@ test: mkdirtmp
test_nulls_on_unordered_indexes test_nulls_on_ordered_indexes test_false_on_indexes test_ranges_on_ordered_indexes test_meta test_meta_indexes \
test_file_close test_stream \
operators/test_gte operators/test_gt operators/test_lte operators/test_lt operators/test_eq operators/test_composed operators/test_optimization operators/test_in \
operators/test_nin operators/test_neq operators/test_or operators/test_global_or operators/test_order operators/test_desc_order \
operators/test_nin operators/test_neq operators/test_or operators/test_global_or operators/test_order operators/test_desc_order operators/test_chainable \
replication/test_master replication/test_slave replication/test_slave_reconnect

benchmark: mkdirtmp mkdirresults
Expand Down
51 changes: 45 additions & 6 deletions lib/alfred/meta/finder.js
Expand Up @@ -94,7 +94,7 @@ Finder.prototype.executeAndJustReturnKeys = function(query) {
return keys;
}

Finder.prototype.execute = function(query, callback) {
Finder.prototype.executeAllAndJustReturnKeysInOrder = function(query, callback) {
var self = this;
var keys;
if (!query) {
Expand Down Expand Up @@ -150,22 +150,56 @@ Finder.prototype.execute = function(query, callback) {
keys = ordered_keys;
}

// transform the keys into real objects
callback(null, keys);

} catch(excep) {
callback(excep);
}
};

Finder.prototype.execute = function(callback) {
var self = this;
// transform the keys into real objects
this.executeAllAndJustReturnKeysInOrder(this.query, function(err, keys) {
if (err) {
callback(err);
return;
}
keys.forEach(function(key, index) {
self.key_map.getAtPos(key.p, key.l, function(err, key, value) {
if (err) { callback(err); return; }
callback(null, key, value);
});
});
} catch(excep) {
callback(excep);
}
});
};

Finder.prototype.executeBulk = function(callback) {
var self = this;
this.executeAllAndJustReturnKeysInOrder(this.query, function(err, keys) {
if (err) {
callback(err);
return;
}
var results = [];
var got = 0;
keys.forEach(function(key, index) {
self.key_map.getAtPos(key.p, key.l, function(err, key, value) {
if (err) { callback(err); return; }
results.push({key: key, value: value});
got ++;
if (got == keys.length) {
callback(null, results);
}
});
});
});
};

module.exports.create = function(key_map, query) {
var finder = new Finder(key_map, query);
var curry = function(callback) {
finder.execute(finder.query, callback);
finder.execute(callback);
return curry;
};

Expand All @@ -192,6 +226,11 @@ module.exports.create = function(key_map, query) {
return curry;
};

curry.bulk = function(callback) {
finder.executeBulk(callback);
return curry;
}

return curry;
};

12 changes: 11 additions & 1 deletion test/operators/test_chainable.js
Expand Up @@ -59,9 +59,10 @@ module.exports.run = function(next) {
var users_1_found = 0;
var users_2_found = 0;
var users_3_found = 0;
var users_4 = false

var end_test = function() {
if (users_1_found == 2 && users_2_found == 4 && users_3_found == 6) {
if (users_1_found == 2 && users_2_found == 4 && users_3_found == 6 && users_4) {
db.close(function(err) {
if (err) { throw err; }
clearTimeout(timeout);
Expand Down Expand Up @@ -99,6 +100,15 @@ module.exports.run = function(next) {
users_3_found ++;
assert.ok(users_3_found <= 6, 'already found ' + users_3_found + ' users');
end_test();
})
.bulk(function(err, records) {
if (err) { throw err; }
assert.equal(6, records.length);
records.forEach(function(record) {
assert.deepEqual(record.value, USERS[record.key]);
});
users_4 = true;
end_test();
});
}
});
Expand Down

0 comments on commit ce8d44a

Please sign in to comment.