Skip to content

Commit 44d04bc

Browse files
committed
Fixes to calculate object size, added tests
1 parent db82e25 commit 44d04bc

File tree

5 files changed

+116
-47
lines changed

5 files changed

+116
-47
lines changed

lib/mongodb/bson/bson.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,50 @@ BSON.calculateObjectSize = function(object) {
101101
keys = Object.keys(value)
102102
keysIndex = 0;
103103
keyLength = keys.length;
104-
} else if(typeof value == 'number' || toString.call(value) === '[object Number]') {
105-
if(value >= BSON.BSON_INT32_MAX || value < BSON.BSON_INT32_MIN ||
106-
value !== parseInt(value, 10)) {
107-
// Long and Number take same number of bytes.
108-
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1);
109-
} else {
104+
} else if((typeof value == 'number' || toString.call(value) === '[object Number]') &&
105+
value === parseInt(value, 10) &&
106+
value.toString().match(/\./) == null) {
107+
// Write the type
108+
var int64 = value >= BSON.BSON_INT32_MAX || value < BSON.BSON_INT32_MIN;
109+
110+
if(int64) {
111+
// Write the number
112+
var long = Long.fromNumber(value);
113+
// If the number is the same after long conversion force double
114+
if(value.toString() == long.toNumber().toString() || value >= Long.MAX_VALUE || value <= Long.MIN_VALUE) {
115+
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1);
116+
// Ajust index
117+
index = index + 8;
118+
} else {
119+
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1);
120+
}
121+
} else {
110122
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (4 + 1);
111123
}
124+
} else if(typeof value == 'number' || toString.call(value) === '[object Number]' ||
125+
value instanceof Double) {
126+
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1);
127+
// // Write the type
128+
// buffer[index++] = BSON.BSON_DATA_NUMBER;
129+
// // Write the name
130+
// if(name != null) {
131+
// index = index + buffer.write(name, index, 'utf8') + 1;
132+
// buffer[index - 1] = 0;
133+
// }
134+
//
135+
// // Write float
136+
// ieee754.writeIEEE754(buffer, value, index, 'little', 52, 8);
137+
// // Ajust index
138+
// index = index + 8;
139+
140+
// } else if(typeof value == 'number' || toString.call(value) === '[object Number]') {
141+
// if(value >= BSON.BSON_INT32_MAX || value < BSON.BSON_INT32_MIN ||
142+
// value !== parseInt(value, 10)) {
143+
// // Long and Number take same number of bytes.
144+
// totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1);
145+
// } else {
146+
// totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (4 + 1);
147+
// }
112148
} else if(typeof value == 'boolean' || toString.call(value) === '[object Boolean]') {
113149
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (1 + 1);
114150
} else if(value instanceof Date || toString.call(value) === '[object Date]') {
@@ -275,7 +311,7 @@ BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object,
275311
// Write zero
276312
buffer[index++] = 0;
277313
} else if((typeof value == 'number' || toString.call(value) === '[object Number]') &&
278-
value == parseInt(value, 10) &&
314+
value === parseInt(value, 10) &&
279315
value.toString().match(/\./) == null) {
280316
// Write the type
281317
var int64 = value >= BSON.BSON_INT32_MAX || value < BSON.BSON_INT32_MIN;

lib/mongodb/collection.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,9 @@ Collection.prototype.insertAll = function insertAll (docs, options, callback) {
246246
// Insert options (flags for insert)
247247
var insertFlags = {};
248248
// If we have a mongodb version >= 1.9.1 support keepGoing attribute
249-
// if(this.db.versionAsNumber >= 191) {
250-
if(options['keepGoing'] != null) {
251-
insertFlags['keepGoing'] = options['keepGoing'];
252-
}
253-
// }
249+
if(options['keepGoing'] != null) {
250+
insertFlags['keepGoing'] = options['keepGoing'];
251+
}
254252

255253
// Pass in options
256254
var insertCommand = new InsertCommand(
@@ -285,16 +283,17 @@ Collection.prototype.insertAll = function insertAll (docs, options, callback) {
285283
commandOptions['read'] = false;
286284
// If we have safe set set async to false
287285
if(errorOptions == null) commandOptions['async'] = true;
286+
288287
// Set safe option
289-
commandOptions['safe'] = true;
288+
commandOptions['safe'] = errorOptions;
290289
// If we have an error option
291290
if(typeof errorOptions == 'object') {
292291
var keys = Object.keys(errorOptions);
293292
for(var i = 0; i < keys.length; i++) {
294293
commandOptions[keys[i]] = errorOptions[keys[i]];
295294
}
296295
}
297-
296+
298297
// Execute command with safe options (rolls up both command and safe command into one and executes them on the same connection)
299298
this.db.executeCommand(insertCommand, commandOptions, function (err, error) {
300299
error = error && error.documents;

test/find_test.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,18 @@ var tests = testCase({
151151
// Ensure correct insertion testing via the cursor and the count function
152152
collection.find().toArray(function(err, documents) {
153153
test.equal(2, documents.length);
154-
});
155-
collection.count(function(err, count) {
156-
test.equal(2, count);
157-
});
158-
// Fetch values by selection
159-
collection.find({'a': doc1.a}).toArray(function(err, documents) {
160-
test.equal(1, documents.length);
161-
test.equal(doc1.a, documents[0].a);
162-
// Let's close the db
163-
test.done();
154+
155+
collection.count(function(err, count) {
156+
test.equal(2, count);
157+
158+
// Fetch values by selection
159+
collection.find({'a': doc1.a}).toArray(function(err, documents) {
160+
test.equal(1, documents.length);
161+
test.equal(doc1.a, documents[0].a);
162+
// Let's close the db
163+
test.done();
164+
});
165+
});
164166
});
165167
});
166168
});

test/insert_test.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,8 @@ var tests = testCase({
273273
'dbref': new client.bson_serializer.DBRef('namespace', oid, 'integration_tests_')
274274
}
275275

276-
collection.insert(motherOfAllDocuments, {safe:true}, function(err, docs) {
277-
278-
collection.findOne(function(err, doc) {
276+
collection.insert(motherOfAllDocuments, {safe:true}, function(err, docs) {
277+
collection.findOne(function(err, doc) {
279278
// Assert correct deserialization of the values
280279
test.equal(motherOfAllDocuments.string, doc.string);
281280
test.deepEqual(motherOfAllDocuments.array, doc.array);
@@ -286,15 +285,15 @@ var tests = testCase({
286285
test.equal(date.getTime(), doc.date.getTime());
287286
test.equal(motherOfAllDocuments.oid.toHexString(), doc.oid.toHexString());
288287
test.equal(motherOfAllDocuments.binary.value(), doc.binary.value());
289-
288+
290289
test.equal(motherOfAllDocuments.int, doc.int);
291290
test.equal(motherOfAllDocuments.long, doc.long);
292291
test.equal(motherOfAllDocuments.float, doc.float);
293292
test.equal(motherOfAllDocuments.regexp.toString(), doc.regexp.toString());
294293
test.equal(motherOfAllDocuments.boolean, doc.boolean);
295294
test.equal(motherOfAllDocuments.where.code, doc.where.code);
296295
test.equal(motherOfAllDocuments.where.scope['i'], doc.where.scope.i);
297-
296+
298297
test.equal(motherOfAllDocuments.dbref.namespace, doc.dbref.namespace);
299298
test.equal(motherOfAllDocuments.dbref.oid.toHexString(), doc.dbref.oid.toHexString());
300299
test.equal(motherOfAllDocuments.dbref.db, doc.dbref.db);
@@ -690,15 +689,15 @@ var tests = testCase({
690689

691690
'Should correctly insert object and retrieve it when containing array and IsoDate' : function(test) {
692691
var doc = {
693-
"_id" : new client.bson_serializer.ObjectID("4e886e687ff7ef5e00000162"),
694-
"str" : "foreign",
695-
"type" : 2,
696-
"timestamp" : ISODate("2011-10-02T14:00:08.383Z"),
697-
"links" : [
698-
"http://www.reddit.com/r/worldnews/comments/kybm0/uk_home_secretary_calls_for_the_scrapping_of_the/"
699-
]
692+
"_id" : new client.bson_serializer.ObjectID("4e886e687ff7ef5e00000162"),
693+
"str" : "foreign",
694+
"type" : 2,
695+
"timestamp" : ISODate("2011-10-02T14:00:08.383Z"),
696+
"links" : [
697+
"http://www.reddit.com/r/worldnews/comments/kybm0/uk_home_secretary_calls_for_the_scrapping_of_the/"
698+
]
700699
}
701-
700+
702701
client.createCollection('Should_correctly_insert_object_and_retrieve_it_when_containing_array_and_IsoDate', function(err, collection) {
703702
collection.insert(doc, {safe:true}, function(err, result) {
704703
test.ok(err == null);
@@ -711,19 +710,19 @@ var tests = testCase({
711710
});
712711
});
713712
},
714-
713+
715714
'Should correctly insert object with timestamps' : function(test) {
716715
var doc = {
717-
"_id" : new client.bson_serializer.ObjectID("4e886e687ff7ef5e00000162"),
718-
"str" : "foreign",
719-
"type" : 2,
720-
"timestamp" : new client.bson_serializer.Timestamp(10000),
721-
"links" : [
722-
"http://www.reddit.com/r/worldnews/comments/kybm0/uk_home_secretary_calls_for_the_scrapping_of_the/"
723-
],
724-
"timestamp2" : new client.bson_serializer.Timestamp(33333),
716+
"_id" : new client.bson_serializer.ObjectID("4e886e687ff7ef5e00000162"),
717+
"str" : "foreign",
718+
"type" : 2,
719+
"timestamp" : new client.bson_serializer.Timestamp(10000),
720+
"links" : [
721+
"http://www.reddit.com/r/worldnews/comments/kybm0/uk_home_secretary_calls_for_the_scrapping_of_the/"
722+
],
723+
"timestamp2" : new client.bson_serializer.Timestamp(33333),
725724
}
726-
725+
727726
client.createCollection('Should_correctly_insert_object_with_timestamps', function(err, collection) {
728727
collection.insert(doc, {safe:true}, function(err, result) {
729728
test.ok(err == null);

test/replicaset/insert_test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,39 @@ module.exports = testCase({
6868
callback();
6969
})
7070
},
71+
72+
shouldCorrectlyWaitForReplicationToServersOnInserts : function(test) {
73+
// debug("=========================================== shouldWorkCorrectlyWithInserts")
74+
// Replica configuration
75+
var replSet = new ReplSetServers( [
76+
new Server( RS.host, RS.ports[1], { auto_reconnect: true } ),
77+
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
78+
new Server( RS.host, RS.ports[2], { auto_reconnect: true } )
79+
],
80+
{rs_name:RS.name}
81+
);
82+
83+
// Insert some data
84+
var db = new Db('integration_test_', replSet);
85+
db.open(function(err, p_db) {
86+
// Check if we got an error
87+
if(err != null) debug("shouldCorrectlyWaitForReplicationToServersOnInserts :: " + inspect(err));
88+
89+
// Drop collection on replicaset
90+
p_db.dropCollection('testsets', function(err, r) {
91+
if(err != null) debug("shouldCorrectlyWaitForReplicationToServersOnInserts :: " + inspect(err));
92+
// Recreate collection on replicaset
93+
p_db.createCollection('testsets', function(err, collection) {
94+
if(err != null) debug("shouldCorrectlyWaitForReplicationToServersOnInserts :: " + inspect(err));
95+
// Insert a dummy document
96+
collection.insert({a:20}, {safe: {w:2, wtimeout: 10000}}, function(err, r) {
97+
test.equal(null, err);
98+
test.done();
99+
});
100+
});
101+
});
102+
});
103+
},
71104

72105
shouldCorrectlyExecuteSafeFindAndModify : function(test) {
73106
// Replica configuration

0 commit comments

Comments
 (0)