delete_command.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var BaseCommand = require('./base_command').BaseCommand,
  2. inherits = require('util').inherits;
  3. /**
  4. Insert Document Command
  5. **/
  6. var DeleteCommand = exports.DeleteCommand = function(db, collectionName, selector, flags) {
  7. BaseCommand.call(this);
  8. // Validate correctness off the selector
  9. var object = selector;
  10. if(Buffer.isBuffer(object)) {
  11. var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
  12. if(object_size != object.length) {
  13. var error = new Error("delete raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
  14. error.name = 'MongoError';
  15. throw error;
  16. }
  17. }
  18. this.flags = flags;
  19. this.collectionName = collectionName;
  20. this.selector = selector;
  21. this.db = db;
  22. };
  23. inherits(DeleteCommand, BaseCommand);
  24. DeleteCommand.OP_DELETE = 2006;
  25. /*
  26. struct {
  27. MsgHeader header; // standard message header
  28. int32 ZERO; // 0 - reserved for future use
  29. cstring fullCollectionName; // "dbname.collectionname"
  30. int32 ZERO; // 0 - reserved for future use
  31. mongo.BSON selector; // query object. See below for details.
  32. }
  33. */
  34. DeleteCommand.prototype.toBinary = function() {
  35. // Calculate total length of the document
  36. var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + this.db.bson.calculateObjectSize(this.selector, false, true) + (4 * 4);
  37. // Let's build the single pass buffer command
  38. var _index = 0;
  39. var _command = new Buffer(totalLengthOfCommand);
  40. // Write the header information to the buffer
  41. _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
  42. _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
  43. _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
  44. _command[_index] = totalLengthOfCommand & 0xff;
  45. // Adjust index
  46. _index = _index + 4;
  47. // Write the request ID
  48. _command[_index + 3] = (this.requestId >> 24) & 0xff;
  49. _command[_index + 2] = (this.requestId >> 16) & 0xff;
  50. _command[_index + 1] = (this.requestId >> 8) & 0xff;
  51. _command[_index] = this.requestId & 0xff;
  52. // Adjust index
  53. _index = _index + 4;
  54. // Write zero
  55. _command[_index++] = 0;
  56. _command[_index++] = 0;
  57. _command[_index++] = 0;
  58. _command[_index++] = 0;
  59. // Write the op_code for the command
  60. _command[_index + 3] = (DeleteCommand.OP_DELETE >> 24) & 0xff;
  61. _command[_index + 2] = (DeleteCommand.OP_DELETE >> 16) & 0xff;
  62. _command[_index + 1] = (DeleteCommand.OP_DELETE >> 8) & 0xff;
  63. _command[_index] = DeleteCommand.OP_DELETE & 0xff;
  64. // Adjust index
  65. _index = _index + 4;
  66. // Write zero
  67. _command[_index++] = 0;
  68. _command[_index++] = 0;
  69. _command[_index++] = 0;
  70. _command[_index++] = 0;
  71. // Write the collection name to the command
  72. _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
  73. _command[_index - 1] = 0;
  74. // Write the flags
  75. _command[_index + 3] = (this.flags >> 24) & 0xff;
  76. _command[_index + 2] = (this.flags >> 16) & 0xff;
  77. _command[_index + 1] = (this.flags >> 8) & 0xff;
  78. _command[_index] = this.flags & 0xff;
  79. // Adjust index
  80. _index = _index + 4;
  81. // Document binary length
  82. var documentLength = 0
  83. // Serialize the selector
  84. // If we are passing a raw buffer, do minimal validation
  85. if(Buffer.isBuffer(this.selector)) {
  86. documentLength = this.selector.length;
  87. // Copy the data into the current buffer
  88. this.selector.copy(_command, _index);
  89. } else {
  90. documentLength = this.db.bson.serializeWithBufferAndIndex(this.selector, this.checkKeys, _command, _index) - _index + 1;
  91. }
  92. // Write the length to the document
  93. _command[_index + 3] = (documentLength >> 24) & 0xff;
  94. _command[_index + 2] = (documentLength >> 16) & 0xff;
  95. _command[_index + 1] = (documentLength >> 8) & 0xff;
  96. _command[_index] = documentLength & 0xff;
  97. // Update index in buffer
  98. _index = _index + documentLength;
  99. // Add terminating 0 for the object
  100. _command[_index - 1] = 0;
  101. return _command;
  102. };