Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bulk divide into actions, closes #265 #297

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var each = require('each-series')

var noop = function () {}
var oid = mongodb.ObjectID.createPk
const BulkChunkSize = 1000

var Bulk = function (colName, ordered, onserver) {
this._colname = colName
Expand Down Expand Up @@ -141,6 +142,36 @@ Bulk.prototype.toString = function () {
return JSON.stringify(this.tojson())
}

function getCmdsBrokenIntoChunks (cmds, chunkSize) {
var result = cmds.map(function (cmd) {
var cmdKey
if (cmd.update) {
cmdKey = 'updates'
} else if (cmd.insert) {
cmdKey = 'documents'
} else if (cmd.delete) {
cmdKey = 'deletes'
}
if (cmd[cmdKey].length > chunkSize) {
var all = cmd[cmdKey]
delete cmd[cmdKey]
var result = []

for (var i = 0; i < all.length; i += chunkSize) {
var newCmd = JSON.parse(JSON.stringify(cmd)) // deep copy
newCmd[cmdKey] = all.slice(i, chunkSize + i)
result.push(newCmd)
}

return result
} else {
return cmd
}
})

return [].concat.apply([], result) // flatten
}

Bulk.prototype.execute = function (cb) {
if (!cb) return this.execute(noop)

Expand All @@ -162,7 +193,7 @@ Bulk.prototype.execute = function (cb) {

this._getConnection(function (err, connection) {
if (err) return cb(err)
each(self._cmds, function (cmd, i, done) {
each(getCmdsBrokenIntoChunks(self._cmds, BulkChunkSize), function (cmd, i, done) {
connection.command(cmd, function (err, res) {
if (err) return done(err)
result[cmdkeys[Object.keys(cmd)[0]]] += res.n
Expand Down
26 changes: 26 additions & 0 deletions test/test-bulk-big-execute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var insert = require('./insert')

insert('bulk', [{
name: 'Squirtle', type: 'water'
}], function (db, t, done) {
db.runCommand('serverStatus', function (err, resp) {
t.error(err)
if (parseFloat(resp.version) < 2.6) return t.end()

var bulk = db.a.initializeOrderedBulkOp()
var numberOfOp = 1066
for (var i = 0; i < numberOfOp; ++i) {
bulk.insert({name: 'Spearow', type: 'flying'})
}

bulk.execute(function (err, res) {
t.error(err)
t.ok(res.ok)
db.a.find(function (err, res) {
t.error(err)
t.equal(res.length, numberOfOp + 1) // +1 because we inserted one doc before test
t.end()
})
})
})
})
5 changes: 3 additions & 2 deletions test/test-cursor-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ insert('cursor.map', [{
return x.hello
}, function (err, res) {
t.error(err)
t.equal(res[0], 'world1')
t.equal(res[1], 'world2')
t.equal(res.length, 2)
t.notEqual(res.indexOf('world1'), -1)
t.notEqual(res.indexOf('world2'), -1)
done()
})
})
Expand Down