Skip to content

Commit

Permalink
Added support for batch size.
Browse files Browse the repository at this point in the history
  • Loading branch information
renctan committed Jan 9, 2011
1 parent 89de59e commit fe58192
Show file tree
Hide file tree
Showing 3 changed files with 513 additions and 23 deletions.
220 changes: 220 additions & 0 deletions integration/integration_tests.js
Expand Up @@ -1997,6 +1997,226 @@ var all_tests = {
});
},

test_batchSize_exceptions : function() {
client.createCollection('test_batchSize_exceptions', function(err, collection) {
collection.insert({'a':1}, function(err, docs) {});
collection.find(function(err, cursor) {
cursor.batchSize('not-an-integer', function(err, cursor) {
test.ok(err instanceof Error);
test.equal("batchSize requires an integer", err.message);
});
});

collection.find(function(err, cursor) {
cursor.nextObject(function(err, doc) {
cursor.nextObject(function(err, doc) {
cursor.batchSize(1, function(err, cursor) {
test.ok(err instanceof Error);
test.equal("Cursor is closed", err.message);
// Let's close the db
finished_test({test_batchSize_exceptions:'ok'});
});
});
});
});

collection.find(function(err, cursor) {
cursor.close(function(err, cursor) {
cursor.batchSize(1, function(err, cursor) {
test.ok(err instanceof Error);
test.equal("Cursor is closed", err.message);
});
});
});
});
},

test_not_multiple_batch_size : function() {
client.createCollection('test_not_multiple_batch_size', function(err, collection) {
var records = 6;
var batchSize = 2;
var docs = [];
for(var i = 0; i < records; i++) {
docs.push({'a':i});
}

collection.insert(docs, function() {
collection.find({}, {batchSize : batchSize}, function(err, cursor) {
//1st
cursor.nextObject(function(err, items) {
//cursor.items should contain 1 since nextObject already popped one
test.equal(1, cursor.items.length);
test.ok(items != null);

//2nd
cursor.nextObject(function(err, items) {
test.equal(0, cursor.items.length);
test.ok(items != null);

//test batch size modification on the fly
batchSize = 3;
cursor.batchSize(batchSize);

//3rd
cursor.nextObject(function(err, items) {
test.equal(2, cursor.items.length);
test.ok(items != null);

//4th
cursor.nextObject(function(err, items) {
test.equal(1, cursor.items.length);
test.ok(items != null);

//5th
cursor.nextObject(function(err, items) {
test.equal(0, cursor.items.length);
test.ok(items != null);

//6th
cursor.nextObject(function(err, items) {
test.equal(0, cursor.items.length);
test.ok(items != null);

//No more
cursor.nextObject(function(err, items) {
test.ok(items == null);
test.ok(cursor.isClosed());
finished_test({test_not_multiple_batch_size:'ok'});
});
});
});
});
});
});
});
});
});
});
},

test_multiple_batch_size : function() {
client.createCollection('test_multiple_batch_size', function(err, collection) {
//test with the last batch that is a multiple of batchSize
var records = 4;
var batchSize = 2;
var docs = [];
for(var i = 0; i < records; i++) {
docs.push({'a':i});
}

collection.insert(docs, function() {
collection.find({}, {batchSize : batchSize}, function(err, cursor) {
//1st
cursor.nextObject(function(err, items) {
test.equal(1, cursor.items.length);
test.ok(items != null);

//2nd
cursor.nextObject(function(err, items) {
test.equal(0, cursor.items.length);
test.ok(items != null);

//3rd
cursor.nextObject(function(err, items) {
test.equal(1, cursor.items.length);
test.ok(items != null);

//4th
cursor.nextObject(function(err, items) {
test.equal(0, cursor.items.length);
test.ok(items != null);

//No more
cursor.nextObject(function(err, items) {
test.ok(items == null);
test.ok(cursor.isClosed());
finished_test({test_multiple_batch_size:'ok'});
});
});
});
});
});
});
});
});
},

test_limit_greater_than_batch_size : function() {
client.createCollection('test_limit_greater_than_batch_size', function(err, collection) {
var limit = 4;
var records = 10;
var batchSize = 3;
var docs = [];
for(var i = 0; i < records; i++) {
docs.push({'a':i});
}

collection.insert(docs, function() {
collection.find({}, {batchSize : batchSize, limit : limit}, function(err, cursor) {
//1st
cursor.nextObject(function(err, items) {
test.equal(2, cursor.items.length);

//2nd
cursor.nextObject(function(err, items) {
test.equal(1, cursor.items.length);

//3rd
cursor.nextObject(function(err, items) {
test.equal(0, cursor.items.length);

//4th
cursor.nextObject(function(err, items) {
test.equal(0, cursor.items.length);

//No more
cursor.nextObject(function(err, items) {
test.ok(items == null);
test.ok(cursor.isClosed());
finished_test({test_limit_greater_than_batch_size:'ok'});
});
});
});
});
});
});
});
});
},

test_limit_less_than_batch_size : function () {
client.createCollection('test_limit_less_than_batch_size', function(err, collection) {
var limit = 2;
var records = 10;
var batchSize = 4;
var docs = [];
for(var i = 0; i < records; i++) {
docs.push({'a':i});
}

collection.insert(docs, function() {
collection.find({}, {batchSize : batchSize, limit : limit}, function(err, cursor) {
//1st
cursor.nextObject(function(err, items) {
test.equal(1, cursor.items.length);

//2nd
cursor.nextObject(function(err, items) {
test.equal(0, cursor.items.length);

//No more
cursor.nextObject(function(err, items) {
test.ok(items == null);
test.ok(cursor.isClosed());
finished_test({test_limit_less_than_batch_size:'ok'});
});
});
});
});
});
});
},

test_limit_skip_chaining : function() {
client.createCollection('test_limit_skip_chaining', function(err, collection) {
for(var i = 0; i < 10; i++) { collection.insert({'x':1}); }
Expand Down
29 changes: 16 additions & 13 deletions lib/mongodb/collection.js
Expand Up @@ -344,17 +344,20 @@ Collection.prototype.findAndModify = function(query, sort, update, options, call
});
}

/*
various argument possibilities
1 callback
2 selector, callback,
2 callback, options // really?!
3 selector, fields, callback
3 selector, options, callback
4,selector, fields, options, callback
5 selector, fields, skip, limit, callback
6 selector, fields, skip, limit, timeout, callback
*/
/**
* Various argument possibilities
* 1 callback
* 2 selector, callback,
* 2 callback, options // really?!
* 3 selector, fields, callback
* 3 selector, options, callback
* 4,selector, fields, options, callback
* 5 selector, fields, skip, limit, callback
* 6 selector, fields, skip, limit, timeout, callback
*
* Available options:
* limit, sort, fields, skip, hint, explain, snapshot, timeout, tailable, batchSize
*/
Collection.prototype.find = function() {
var options,
len = arguments.length,
Expand All @@ -367,7 +370,7 @@ Collection.prototype.find = function() {
}

if(len == 3){ // backwards compat for options object
var test = ['limit','sort','fields','skip','hint','explain','snapshot','timeout','tailable'],
var test = ['limit','sort','fields','skip','hint','explain','snapshot','timeout','tailable', 'batchSize'],
idx = 0, l = test.length, is_option = false;
while(!is_option && idx < l) if(test[idx] in fields ) is_option = true; else idx++;
options = is_option ? fields : {};
Expand All @@ -391,7 +394,7 @@ Collection.prototype.find = function() {
options.timeout = len == 6 ? arguments[4] : options.timeout ? options.timeout : undefined;

var o = options;
callback(null, new Cursor(this.db, this, selector, fields, o.skip, o.limit, o.sort, o.hint, o.explain, o.snapshot, o.timeout, o.tailable));
callback(null, new Cursor(this.db, this, selector, fields, o.skip, o.limit, o.sort, o.hint, o.explain, o.snapshot, o.timeout, o.tailable, o.batchSize));
};

Collection.prototype.normalizeHintField = function(hint) {
Expand Down

0 comments on commit fe58192

Please sign in to comment.