@@ -17,8 +17,6 @@ var InsertCommand = exports.InsertCommand = function(db, collectionName, checkKe
1717// OpCodes
1818InsertCommand . OP_INSERT = 2002 ;
1919
20- // inherits(InsertCommand, InsertCommand);
21-
2220InsertCommand . prototype . add = function ( document ) {
2321 this . documents . push ( document ) ;
2422 return this ;
@@ -32,37 +30,16 @@ struct {
3230 BSON[] documents; // one or more documents to insert into the collection
3331}
3432*/
35- InsertCommand . prototype . getCommandAsBuffers = function ( buffers ) {
36- var collectionNameBuffers = InsertCommand . encodeCString ( this . collectionName ) ;
37- // Add command to buffers
38- buffers . push ( InsertCommand . encodeInt ( 0 ) , collectionNameBuffers [ 0 ] , collectionNameBuffers [ 1 ] ) ;
39- // Basic command length
40- var commandLength = 4 + collectionNameBuffers [ 0 ] . length + 1 ;
41-
42- for ( var i = 0 ; i < this . documents . length ; i ++ ) {
43- var command = this . db . bson_serializer . BSON . serialize ( this . documents [ i ] , this . checkKeys , true ) ;
44- commandLength += command . length ;
45- buffers . push ( command ) ;
46- }
47-
48- return commandLength ;
49- }
50-
5133InsertCommand . prototype . toBinary = function ( ) {
34+ //////////////////////////////////////////////////////////////////////////////////////
5235 // Calculate total length of the document
5336 var totalLengthOfCommand = 4 + Buffer . byteLength ( this . collectionName ) + 1 + ( 4 * 4 ) ;
54- var documentLengths = [ ] ;
5537 // var docLength = 0
5638 for ( var i = 0 ; i < this . documents . length ; i ++ ) {
5739 // Calculate size of document
58- var documentLength = this . db . bson_serializer . BSON . calculateObjectSize ( this . documents [ i ] ) ;
59- // Save the size for writing the command
60- documentLengths . push ( documentLength ) ;
61- // Add to total of command
62- totalLengthOfCommand += documentLength
40+ totalLengthOfCommand += this . db . bson_serializer . BSON . calculateObjectSize ( this . documents [ i ] ) ;
6341 }
64-
65- //////////////////////////////////////////////////////////////////////////////////////
42+
6643 // Let's build the single pass buffer command
6744 var _index = 0 ;
6845 var _command = new Buffer ( totalLengthOfCommand ) ;
@@ -99,13 +76,12 @@ InsertCommand.prototype.toBinary = function() {
9976 _command [ _index ++ ] = 0 ;
10077 // Write the collection name to the command
10178 _index = _index + _command . write ( this . collectionName , _index , 'utf8' ) + 1 ;
79+ _command [ _index - 1 ] = 0 ;
10280
10381 // Write all the bson documents to the buffer at the index offset
10482 for ( var i = 0 ; i < this . documents . length ; i ++ ) {
10583 // Serialize the document straight to the buffer
10684 var documentLength = this . db . bson_serializer . BSON . serializeWithBufferAndIndex ( this . documents [ i ] , this . checkKeys , _command , _index ) - _index + 1 ;
107- // debug("============= documentLength = " + (documentLength - _index))
108-
10985 // Write the length to the document
11086 _command [ _index + 3 ] = ( documentLength >> 24 ) & 0xff ;
11187 _command [ _index + 2 ] = ( documentLength >> 16 ) & 0xff ;
@@ -117,71 +93,71 @@ InsertCommand.prototype.toBinary = function() {
11793 _command [ _index - 1 ] = 0 ;
11894 }
11995
120- // return _command;
96+ return _command ;
12197
12298 //////////////////////////////////////////////////////////////////////////////////////
12399
124- // Build list of Buffer objects to write out
125- var buffers = [ ] ;
126-
127- // Get the command op code
128- var op_code = InsertCommand . OP_INSERT ;
129- var commandBuffers = [ ] ;
130-
131- // Get the command data structure
132- var commandLength = this . getCommandAsBuffers ( commandBuffers ) ;
133-
134-
135- // Total Size of command
136- var totalSize = 4 * 4 + commandLength ;
137- // Encode totalSize, requestId, responseId and opcode
138- buffers . push ( InsertCommand . encodeInt ( totalSize ) , InsertCommand . encodeInt ( this . requestId ) , InsertCommand . encodeInt ( 0 ) , InsertCommand . encodeInt ( op_code ) ) ;
139-
140- // Add the command items
141- buffers = buffers . concat ( commandBuffers ) ;
142- // Allocate single buffer for write
143- var finalBuffer = new Buffer ( totalSize ) ;
144-
145- var index = 0 ;
146-
147- for ( var i = 0 ; i < buffers . length ; i ++ ) {
148- buffers [ i ] . copy ( finalBuffer , index ) ;
149- index = index + buffers [ i ] . length ;
150- }
151-
152- // debug("==================== finalBuffer.length = " + finalBuffer.length)
153- // debug("==================== totalLengthOfCommand = " + totalLengthOfCommand)
154- // debug("==================== totalSize = " + totalSize)
155- // debug("==================== docLength = " + docLength)
156- // debug("==================== commandLength = " + commandLength)
100+ // // Build list of Buffer objects to write out
101+ // var buffers = [];
102+ //
103+ // // Get the command op code
104+ // var op_code = InsertCommand.OP_INSERT;
105+ // var commandBuffers = [];
106+ //
107+ // // Get the command data structure
108+ // var commandLength = this.getCommandAsBuffers(commandBuffers);
109+ //
110+ //
111+ // // Total Size of command
112+ // var totalSize = 4 * 4 + commandLength;
113+ // // Encode totalSize, requestId, responseId and opcode
114+ // buffers.push(InsertCommand.encodeInt(totalSize), InsertCommand.encodeInt(this.requestId), InsertCommand.encodeInt(0), InsertCommand.encodeInt(op_code));
115+ //
116+ // // Add the command items
117+ // buffers = buffers.concat(commandBuffers);
118+ // // Allocate single buffer for write
119+ // var finalBuffer = new Buffer(totalSize);
120+ //
121+ // var index = 0;
157122 //
158- // for(var i = 0; i < finalBuffer.length; i++) {
159- // debug(i + " :: [" + _command[i] + "] = [" + finalBuffer[i] + "]" + (_command[i] != finalBuffer[i] ? " = FALSE" : ""))
123+ // for(var i = 0; i < buffers.length; i++) {
124+ // buffers[i].copy(finalBuffer, index);
125+ // index = index + buffers[i].length;
160126 // }
161-
162- return finalBuffer ;
127+ //
128+ // // debug("==================== finalBuffer.length = " + finalBuffer.length)
129+ // // debug("==================== totalLengthOfCommand = " + totalLengthOfCommand)
130+ // // debug("==================== totalSize = " + totalSize)
131+ // // debug("==================== docLength = " + docLength)
132+ // // debug("==================== commandLength = " + commandLength)
133+ // //
134+ // // for(var i = 0; i < finalBuffer.length; i++) {
135+ // // debug(i + " :: [" + _command[i] + "] = [" + finalBuffer[i] + "]" + (_command[i] != finalBuffer[i] ? " = FALSE" : ""))
136+ // // }
137+ //
138+ // return finalBuffer;
163139} ;
164-
165- InsertCommand . encodeInt = function ( value ) {
166- var buffer = new Buffer ( 4 ) ;
167- buffer [ 3 ] = ( value >> 24 ) & 0xff ;
168- buffer [ 2 ] = ( value >> 16 ) & 0xff ;
169- buffer [ 1 ] = ( value >> 8 ) & 0xff ;
170- buffer [ 0 ] = value & 0xff ;
171- return buffer ;
172- }
173-
174- InsertCommand . encodeIntInPlace = function ( value , buffer , index ) {
175- buffer [ index + 3 ] = ( value >> 24 ) & 0xff ;
176- buffer [ index + 2 ] = ( value >> 16 ) & 0xff ;
177- buffer [ index + 1 ] = ( value >> 8 ) & 0xff ;
178- buffer [ index ] = value & 0xff ;
179- }
180-
181- InsertCommand . encodeCString = function ( string ) {
182- var buf = new Buffer ( string , 'utf8' ) ;
183- return [ buf , new Buffer ( [ 0 ] ) ] ;
184- }
140+ //
141+ // InsertCommand.encodeInt = function(value) {
142+ // var buffer = new Buffer(4);
143+ // buffer[3] = (value >> 24) & 0xff;
144+ // buffer[2] = (value >> 16) & 0xff;
145+ // buffer[1] = (value >> 8) & 0xff;
146+ // buffer[0] = value & 0xff;
147+ // return buffer;
148+ // }
149+ //
150+ // InsertCommand.encodeIntInPlace = function(value, buffer, index) {
151+ // buffer[index + 3] = (value >> 24) & 0xff;
152+ // buffer[index + 2] = (value >> 16) & 0xff;
153+ // buffer[index + 1] = (value >> 8) & 0xff;
154+ // buffer[index] = value & 0xff;
155+ // }
156+ //
157+ // InsertCommand.encodeCString = function(string) {
158+ // var buf = new Buffer(string, 'utf8');
159+ // return [buf, new Buffer([0])];
160+ // }
185161
186162
187163
0 commit comments