summaryrefslogtreecommitdiff
path: root/node_modules/mongodb/lib/mongodb/commands/delete_command.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/mongodb/lib/mongodb/commands/delete_command.js')
-rw-r--r--node_modules/mongodb/lib/mongodb/commands/delete_command.js111
1 files changed, 111 insertions, 0 deletions
diff --git a/node_modules/mongodb/lib/mongodb/commands/delete_command.js b/node_modules/mongodb/lib/mongodb/commands/delete_command.js
new file mode 100644
index 0000000..1adad07
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/delete_command.js
@@ -0,0 +1,111 @@
+var BaseCommand = require('./base_command').BaseCommand,
+ inherits = require('util').inherits;
+
+/**
+ Insert Document Command
+**/
+var DeleteCommand = exports.DeleteCommand = function(db, collectionName, selector) {
+ BaseCommand.call(this);
+
+ // Validate correctness off the selector
+ var object = selector;
+ if(Buffer.isBuffer(object)) {
+ var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
+ if(object_size != object.length) {
+ var error = new Error("delete raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
+ error.name = 'MongoError';
+ throw error;
+ }
+ }
+
+ this.collectionName = collectionName;
+ this.selector = selector;
+ this.db = db;
+};
+
+inherits(DeleteCommand, BaseCommand);
+
+DeleteCommand.OP_DELETE = 2006;
+
+/*
+struct {
+ MsgHeader header; // standard message header
+ int32 ZERO; // 0 - reserved for future use
+ cstring fullCollectionName; // "dbname.collectionname"
+ int32 ZERO; // 0 - reserved for future use
+ mongo.BSON selector; // query object. See below for details.
+}
+*/
+DeleteCommand.prototype.toBinary = function() {
+ // Calculate total length of the document
+ var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + this.db.bson.calculateObjectSize(this.selector, false, true) + (4 * 4);
+ // Let's build the single pass buffer command
+ var _index = 0;
+ var _command = new Buffer(totalLengthOfCommand);
+ // Write the header information to the buffer
+ _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
+ _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
+ _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
+ _command[_index] = totalLengthOfCommand & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write the request ID
+ _command[_index + 3] = (this.requestId >> 24) & 0xff;
+ _command[_index + 2] = (this.requestId >> 16) & 0xff;
+ _command[_index + 1] = (this.requestId >> 8) & 0xff;
+ _command[_index] = this.requestId & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ // Write the op_code for the command
+ _command[_index + 3] = (DeleteCommand.OP_DELETE >> 24) & 0xff;
+ _command[_index + 2] = (DeleteCommand.OP_DELETE >> 16) & 0xff;
+ _command[_index + 1] = (DeleteCommand.OP_DELETE >> 8) & 0xff;
+ _command[_index] = DeleteCommand.OP_DELETE & 0xff;
+ // Adjust index
+ _index = _index + 4;
+
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+
+ // Write the collection name to the command
+ _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
+ _command[_index - 1] = 0;
+
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+
+ // Document binary length
+ var documentLength = 0
+
+ // Serialize the selector
+ // If we are passing a raw buffer, do minimal validation
+ if(Buffer.isBuffer(this.selector)) {
+ documentLength = this.selector.length;
+ // Copy the data into the current buffer
+ this.selector.copy(_command, _index);
+ } else {
+ documentLength = this.db.bson.serializeWithBufferAndIndex(this.selector, this.checkKeys, _command, _index) - _index + 1;
+ }
+
+ // Write the length to the document
+ _command[_index + 3] = (documentLength >> 24) & 0xff;
+ _command[_index + 2] = (documentLength >> 16) & 0xff;
+ _command[_index + 1] = (documentLength >> 8) & 0xff;
+ _command[_index] = documentLength & 0xff;
+ // Update index in buffer
+ _index = _index + documentLength;
+ // Add terminating 0 for the object
+ _command[_index - 1] = 0;
+ return _command;
+}; \ No newline at end of file