Comparing version 0.9.7-3-1 to 0.9.7-3-2
@@ -63,4 +63,2 @@ Database | ||
* `serverConfig` includes information about the server (`serverConfig.host', `serverConfig.port` etc.) | ||
* `bson_serializer` points to the correct BSON serializer (native or non native, depending on the initial setup) for this connection | ||
* `bson_deserializer` points to the correct BSON deserializer | ||
* `state` indicates if the database is connected or not | ||
@@ -67,0 +65,0 @@ * `strict` indicates if *strict mode* is on (true) or off (false, default) |
@@ -62,5 +62,5 @@ Queries | ||
Hex strings can be reverted back to binary (for example to perform queries) with `db.bson_serializer.ObjectID.createFromHexString` | ||
Hex strings can be reverted back to binary (for example to perform queries) with `ObjectID.createFromHexString` | ||
{_id: db.bson_serializer.ObjectID.createFromHexString(idHex)} | ||
{_id: ObjectID.createFromHexString(idHex)} | ||
@@ -67,0 +67,0 @@ When inserting new records it is possible to use custom `_id` values as well which do not need to be binary hashes, for example strings. |
@@ -16,14 +16,7 @@ var sys = require('util'), | ||
Double = require('../../../lib/mongodb/bson/bson').Double, | ||
MaxKey = require('../../../lib/mongodb/bson/bson').MaxKey, | ||
MinKey = require('../../../lib/mongodb/bson/bson').MinKey, | ||
Timestamp = require('../../../lib/mongodb/bson/bson').Timestamp, | ||
assert = require('assert'); | ||
var Long2 = require('../bson').Long, | ||
ObjectID2 = require('../bson').ObjectID, | ||
Binary2 = require('../bson').Binary, | ||
Code2 = require('../bson').Code, | ||
Symbol2 = require('../bson').Symbol, | ||
Double2 = require('../bson').Double, | ||
Timestamp2 = require('../bson').Timestamp, | ||
DBRef2 = require('../bson').DBRef; | ||
sys.puts("=== EXECUTING TEST_BSON ==="); | ||
@@ -33,73 +26,15 @@ | ||
assert.throws(function() { new ObjectID('foo'); }) | ||
assert.throws(function() { new ObjectID2('foo'); }) | ||
assert.throws(function() { new ObjectID('foo'); }) | ||
// Parsers | ||
var bsonC = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); | ||
var bsonJS = new BSONJS([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); | ||
// | ||
// Assert correct toJSON | ||
// | ||
var binary1 = new Binary(new Buffer('00000000000000000000000000000000')); | ||
var binary2 = new Binary2(new Buffer('00000000000000000000000000000000')); | ||
assert.equal(JSON.stringify(binary1), JSON.stringify(binary2)); | ||
var objectId = new ObjectID(); | ||
var dbref1 = new DBRef('test', objectId, 'db'); | ||
var dbref2 = new DBRef2('test', ObjectID2.createFromHexString(objectId.toHexString()), 'db'); | ||
assert.equal(JSON.stringify(dbref1), JSON.stringify(dbref2)); | ||
var symbol1 = new Symbol('hello'); | ||
var symbol2 = new Symbol2('hello'); | ||
assert.equal(JSON.stringify(symbol1), JSON.stringify(symbol2)); | ||
var double1 = new Double(3.232); | ||
var double2 = new Double2(3.232); | ||
assert.equal(JSON.stringify(double1), JSON.stringify(double2)); | ||
var code1 = new Code('hello', {a:1}) | ||
var code2 = new Code2('hello', {a:1}) | ||
assert.equal(JSON.stringify(code1), JSON.stringify(code2)); | ||
var long1 = Long.fromNumber(1000); | ||
var long2 = Long2.fromNumber(1000); | ||
assert.equal(JSON.stringify(long1), JSON.stringify(long2)); | ||
var timestamp1 = Timestamp.fromNumber(1000); | ||
var timestamp2 = Timestamp2.fromNumber(1000); | ||
assert.equal(JSON.stringify(timestamp1), JSON.stringify(timestamp2)); | ||
// Long data type tests | ||
var l2_string = Long2.fromNumber(100); | ||
var l_string = Long.fromNumber(100); | ||
assert.equal(l_string.toNumber(), l2_string.toNumber()); | ||
var l2_string = Long2.fromNumber(9223372036854775807).toString(); | ||
var l_string = Long.fromNumber(9223372036854775807).toString(); | ||
assert.equal(l_string, l2_string); | ||
l2_string = Long2.fromNumber(9223372036800).toString(); | ||
l_string = Long.fromNumber(9223372036800).toString(); | ||
assert.equal(l_string, l2_string); | ||
l2_string = Long2.fromNumber(2355).toString(); | ||
l_string = Long.fromNumber(2355).toString(); | ||
assert.equal(l_string, l2_string); | ||
l_string = Long.fromNumber(-9223372036854775807).toString(); | ||
l2_string = Long2.fromNumber(-9223372036854775807).toString(); | ||
assert.equal(l_string, l2_string); | ||
l2_string = Long2.fromNumber(-2355).toString(); | ||
l_string = Long.fromNumber(-2355).toString(); | ||
assert.equal(l_string, l2_string); | ||
l2_string = Long2.fromNumber(-1).toString(); | ||
l_string = Long.fromNumber(-1).toString(); | ||
assert.equal(l_string, l2_string); | ||
l2_string = Long2.fromNumber(1).toString(); | ||
l_string = Long.fromNumber(1).toString(); | ||
assert.equal(l_string, l2_string); | ||
var a = Long2.fromNumber(10); | ||
var a = Long.fromNumber(10); | ||
assert.equal(10, a); | ||
var a = Long2.fromNumber(9223372036854775807); | ||
var a = Long.fromNumber(9223372036854775807); | ||
assert.equal(9223372036854775807, a); | ||
@@ -109,62 +44,62 @@ | ||
var doc = {doc:'Serialize'}; | ||
var simple_string_serialized = BSON.serialize(doc, true, false); | ||
var simple_string_serialized = bsonC.serialize(doc, true, false); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
// Nested doc | ||
var doc = {a:{b:{c:1}}}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
// Simple integer serialization/deserialization test, including testing boundary conditions | ||
var doc = {doc:-1}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
var doc = {doc:2147483648}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
var doc = {doc:-2147483648}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
// Simple serialization and deserialization test for a Long value | ||
var doc = {doc:Long2.fromNumber(9223372036854775807)}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize({doc:Long.fromNumber(9223372036854775807)}, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
var doc = {doc:Long.fromNumber(9223372036854775807)}; | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:Long.fromNumber(9223372036854775807)}, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
var doc = {doc:Long2.fromNumber(-9223372036854775807)}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize({doc:Long.fromNumber(-9223372036854775807)}, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
var doc = {doc:Long.fromNumber(-9223372036854775807)}; | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:Long.fromNumber(-9223372036854775807)}, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
// Simple serialization and deserialization for a Float value | ||
var doc = {doc:2222.3333}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
var doc = {doc:-2222.3333}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
// Simple serialization and deserialization for a null value | ||
var doc = {doc:null}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
// Simple serialization and deserialization for a boolean value | ||
var doc = {doc:true}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
@@ -174,70 +109,70 @@ // Simple serialization and deserialization for a date value | ||
var doc = {doc:date}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')), BSON.deserialize(simple_string_serialized)); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); | ||
// Simple serialization and deserialization for a boolean value | ||
var doc = {doc:/abcd/mi}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc, false, true)); | ||
assert.equal(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), BSON.deserialize(simple_string_serialized).doc.toString()); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); | ||
assert.equal(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString()); | ||
var doc = {doc:/abcd/}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc, false, true)); | ||
assert.equal(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), BSON.deserialize(simple_string_serialized).doc.toString()); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); | ||
assert.equal(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString()); | ||
// Simple serialization and deserialization for a objectId value | ||
var doc = {doc:new ObjectID2()}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
var doc = {doc:new ObjectID()}; | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
var doc2 = {doc:ObjectID.createFromHexString(doc.doc.toHexString())}; | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize(doc2, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), BSON.deserialize(simple_string_serialized).doc.toString()); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc2, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString()); | ||
// Simple serialization and deserialization for a Binary value | ||
var binary = new Binary2(); | ||
var binary = new Binary(); | ||
var string = 'binstring' | ||
for(var index = 0; index < string.length; index++) { binary.put(string.charAt(index)); } | ||
var binary2 = new Binary(); | ||
var Binary = new Binary(); | ||
var string = 'binstring' | ||
for(var index = 0; index < string.length; index++) { binary2.put(string.charAt(index)); } | ||
for(var index = 0; index < string.length; index++) { Binary.put(string.charAt(index)); } | ||
var simple_string_serialized = BSON.serialize({doc:binary}, false, true); | ||
assert.deepEqual(simple_string_serialized, BSONJS.serialize({doc:binary2}, false, true)); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.value(), BSON.deserialize(simple_string_serialized).doc.value()); | ||
var simple_string_serialized = bsonC.serialize({doc:binary}, false, true); | ||
assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:Binary}, false, true)); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.value(), bsonC.deserialize(simple_string_serialized).doc.value()); | ||
// Simple serialization and deserialization for a Code value | ||
var code = new Code2('this.a > i', {'i': 1}); | ||
var code2 = new Code('this.a > i', {'i': 1}); | ||
var simple_string_serialized_2 = BSONJS.serialize({doc:code2}, false, true); | ||
var simple_string_serialized = BSON.serialize({doc:code}, false, true); | ||
var code = new Code('this.a > i', {'i': 1}); | ||
var Code = new Code('this.a > i', {'i': 1}); | ||
var simple_string_serialized_2 = bsonJS.serialize({doc:Code}, false, true); | ||
var simple_string_serialized = bsonC.serialize({doc:code}, false, true); | ||
assert.deepEqual(simple_string_serialized, simple_string_serialized_2); | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc.scope, BSON.deserialize(simple_string_serialized).doc.scope); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc.scope, bsonC.deserialize(simple_string_serialized).doc.scope); | ||
// Simple serialization and deserialization for an Object | ||
var simple_string_serialized = BSON.serialize({doc:{a:1, b:{c:2}}}, false, true); | ||
var simple_string_serialized_2 = BSONJS.serialize({doc:{a:1, b:{c:2}}}, false, true); | ||
var simple_string_serialized = bsonC.serialize({doc:{a:1, b:{c:2}}}, false, true); | ||
var simple_string_serialized_2 = bsonJS.serialize({doc:{a:1, b:{c:2}}}, false, true); | ||
assert.deepEqual(simple_string_serialized, simple_string_serialized_2) | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc, BSON.deserialize(simple_string_serialized).doc); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc, bsonC.deserialize(simple_string_serialized).doc); | ||
// Simple serialization and deserialization for an Array | ||
var simple_string_serialized = BSON.serialize({doc:[9, 9, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1]}, false, true); | ||
var simple_string_serialized_2 = BSONJS.serialize({doc:[9, 9, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1]}, false, true); | ||
var simple_string_serialized = bsonC.serialize({doc:[9, 9, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1]}, false, true); | ||
var simple_string_serialized_2 = bsonJS.serialize({doc:[9, 9, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1]}, false, true); | ||
assert.deepEqual(simple_string_serialized, simple_string_serialized_2) | ||
assert.deepEqual(BSONJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc, BSON.deserialize(simple_string_serialized).doc); | ||
assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc, bsonC.deserialize(simple_string_serialized).doc); | ||
// Simple serialization and deserialization for a DBRef | ||
var oid = new ObjectID2() | ||
var oid = new ObjectID() | ||
var oid2 = new ObjectID.createFromHexString(oid.toHexString()) | ||
var simple_string_serialized = BSONJS.serialize({doc:new DBRef('namespace', oid2, 'integration_tests_')}, false, true); | ||
var simple_string_serialized_2 = BSON.serialize({doc:new DBRef2('namespace', oid, 'integration_tests_')}, false, true); | ||
var simple_string_serialized = bsonJS.serialize({doc:new DBRef('namespace', oid2, 'integration_tests_')}, false, true); | ||
var simple_string_serialized_2 = bsonC.serialize({doc:new DBRef('namespace', oid, 'integration_tests_')}, false, true); | ||
assert.deepEqual(simple_string_serialized, simple_string_serialized_2) | ||
// Ensure we have the same values for the dbref | ||
var object_js = BSONJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')); | ||
var object_c = BSON.deserialize(simple_string_serialized); | ||
var object_js = bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')); | ||
var object_c = bsonC.deserialize(simple_string_serialized); | ||
@@ -255,3 +190,3 @@ assert.equal(object_js.doc.namespace, object_c.doc.namespace); | ||
} | ||
var object = BSON.deserialize(new Buffer(serialized_data, 'binary')); | ||
var object = bsonC.deserialize(new Buffer(serialized_data, 'binary')); | ||
assert.equal('Patty', object.name) | ||
@@ -263,7 +198,7 @@ assert.equal(34, object.age) | ||
var doc = { "name" : "本荘由利地域に洪水警報", "name1" : "öüóőúéáűíÖÜÓŐÚÉÁŰÍ", "name2" : "abcdedede"}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
var simple_string_serialized2 = BSONJS.serialize(doc, false, true); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
var simple_string_serialized2 = bsonJS.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, simple_string_serialized2) | ||
var object = BSON.deserialize(simple_string_serialized); | ||
var object = bsonC.deserialize(simple_string_serialized); | ||
assert.equal(doc.name, object.name) | ||
@@ -275,12 +210,12 @@ assert.equal(doc.name1, object.name1) | ||
var doc = {b:[1, 2, 3]}; | ||
var simple_string_serialized = BSON.serialize(doc, false, true); | ||
var simple_string_serialized_2 = BSONJS.serialize(doc, false, true); | ||
var simple_string_serialized = bsonC.serialize(doc, false, true); | ||
var simple_string_serialized_2 = bsonJS.serialize(doc, false, true); | ||
assert.deepEqual(simple_string_serialized, simple_string_serialized_2) | ||
var object = BSON.deserialize(simple_string_serialized); | ||
var object = bsonC.deserialize(simple_string_serialized); | ||
assert.deepEqual(doc, object) | ||
// Test equality of an object ID | ||
var object_id = new ObjectID2(); | ||
var object_id_2 = new ObjectID2(); | ||
var object_id = new ObjectID(); | ||
var object_id_2 = new ObjectID(); | ||
assert.ok(object_id.equals(object_id)); | ||
@@ -291,10 +226,10 @@ assert.ok(!(object_id.equals(object_id_2))) | ||
var object_id = new ObjectID(); | ||
var object_id2 = ObjectID2.createFromHexString(object_id.toString()) | ||
var simple_string_serialized = BSONJS.serialize({doc:object_id}, false, true); | ||
var simple_string_serialized_2 = BSON.serialize({doc:object_id2}, false, true); | ||
var object_id2 = ObjectID.createFromHexString(object_id.toString()) | ||
var simple_string_serialized = bsonJS.serialize({doc:object_id}, false, true); | ||
var simple_string_serialized_2 = bsonC.serialize({doc:object_id2}, false, true); | ||
assert.equal(simple_string_serialized_2.length, simple_string_serialized.length); | ||
assert.deepEqual(simple_string_serialized, simple_string_serialized_2) | ||
var object = BSONJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')); | ||
var object2 = BSON.deserialize(simple_string_serialized); | ||
var object = bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')); | ||
var object2 = bsonC.deserialize(simple_string_serialized); | ||
assert.equal(object.doc.id, object2.doc.id) | ||
@@ -312,7 +247,7 @@ | ||
var simple_string_serialized = BSONJS.serialize(doc, false, true); | ||
var simple_string_serialized = bsonJS.serialize(doc, false, true); | ||
// C++ Object | ||
var c1 = { _id: ObjectID2.createFromHexString(c1._id.toHexString()), comments: [], title: 'number 1' }; | ||
var c2 = { _id: ObjectID2.createFromHexString(c2._id.toHexString()), comments: [], title: 'number 2' }; | ||
var c1 = { _id: ObjectID.createFromHexString(c1._id.toHexString()), comments: [], title: 'number 1' }; | ||
var c2 = { _id: ObjectID.createFromHexString(c2._id.toHexString()), comments: [], title: 'number 2' }; | ||
var doc = { | ||
@@ -322,6 +257,6 @@ numbers: [] | ||
, comments: [c1, c2] | ||
, _id: ObjectID2.createFromHexString(doc._id.toHexString()) | ||
, _id: ObjectID.createFromHexString(doc._id.toHexString()) | ||
}; | ||
var simple_string_serialized_2 = BSON.serialize(doc, false, true); | ||
var simple_string_serialized_2 = bsonC.serialize(doc, false, true); | ||
@@ -334,4 +269,4 @@ for(var i = 0; i < simple_string_serialized_2.length; i++) { | ||
// Deserialize the string | ||
var doc1 = BSONJS.deserialize(new Buffer(simple_string_serialized_2)); | ||
var doc2 = BSON.deserialize(new Buffer(simple_string_serialized_2)); | ||
var doc1 = bsonJS.deserialize(new Buffer(simple_string_serialized_2)); | ||
var doc2 = bsonC.deserialize(new Buffer(simple_string_serialized_2)); | ||
assert.equal(doc._id.id, doc1._id.id) | ||
@@ -347,8 +282,8 @@ assert.equal(doc._id.id, doc2._id.id) | ||
var simple_string_serialized = BSONJS.serialize(doc, false, true); | ||
var simple_string_serialized_2 = BSON.serialize(doc, false, true); | ||
var simple_string_serialized = bsonJS.serialize(doc, false, true); | ||
var simple_string_serialized_2 = bsonC.serialize(doc, false, true); | ||
// Deserialize the string | ||
var doc1 = BSONJS.deserialize(new Buffer(simple_string_serialized_2)); | ||
var doc2 = BSON.deserialize(new Buffer(simple_string_serialized_2)); | ||
var doc1 = bsonJS.deserialize(new Buffer(simple_string_serialized_2)); | ||
var doc2 = bsonC.deserialize(new Buffer(simple_string_serialized_2)); | ||
assert.deepEqual(doc2, doc1) | ||
@@ -364,18 +299,18 @@ assert.deepEqual(doc, doc2) | ||
var simple_string_serialized = BSONJS.serialize(doc, false, true, true); | ||
var simple_string_serialized_2 = BSON.serialize(doc, false, true, true); | ||
var simple_string_serialized = bsonJS.serialize(doc, false, true, true); | ||
var simple_string_serialized_2 = bsonC.serialize(doc, false, true, true); | ||
// Deserialize the string | ||
var doc1 = BSONJS.deserialize(new Buffer(simple_string_serialized_2)); | ||
var doc2 = BSON.deserialize(new Buffer(simple_string_serialized_2)); | ||
var doc1 = bsonJS.deserialize(new Buffer(simple_string_serialized_2)); | ||
var doc2 = bsonC.deserialize(new Buffer(simple_string_serialized_2)); | ||
assert.equal(doc1.key1.code.toString(), doc2.key1.code.toString()) | ||
var doc = {"user_id":"4e9fc8d55883d90100000003","lc_status":{"$ne":"deleted"},"owner_rating":{"$exists":false}}; | ||
var simple_string_serialized = BSONJS.serialize(doc, false, true, true); | ||
var simple_string_serialized_2 = BSON.serialize(doc, false, true, true); | ||
var simple_string_serialized = bsonJS.serialize(doc, false, true, true); | ||
var simple_string_serialized_2 = bsonC.serialize(doc, false, true, true); | ||
// Should serialize to the same value | ||
assert.equal(simple_string_serialized_2.toString('hex'), simple_string_serialized.toString('hex')) | ||
var doc1 = BSONJS.deserialize(simple_string_serialized_2); | ||
var doc2 = BSON.deserialize(simple_string_serialized); | ||
var doc1 = bsonJS.deserialize(simple_string_serialized_2); | ||
var doc2 = bsonC.deserialize(simple_string_serialized); | ||
assert.deepEqual(doc1, doc2) | ||
@@ -386,5 +321,5 @@ | ||
var docJS = {_id: ObjectID.createFromHexString(hexId), 'funds.remaining': {$gte: 1.222}, 'transactions.id': {$ne: ObjectID.createFromHexString(hexId)}}; | ||
var docC = {_id: ObjectID2.createFromHexString(hexId), 'funds.remaining': {$gte: 1.222}, 'transactions.id': {$ne: ObjectID2.createFromHexString(hexId)}}; | ||
var docJSBin = BSONJS.serialize(docJS, false, true, true); | ||
var docCBin = BSON.serialize(docC, false, true, true); | ||
var docC = {_id: ObjectID.createFromHexString(hexId), 'funds.remaining': {$gte: 1.222}, 'transactions.id': {$ne: ObjectID.createFromHexString(hexId)}}; | ||
var docJSBin = bsonJS.serialize(docJS, false, true, true); | ||
var docCBin = bsonC.serialize(docC, false, true, true); | ||
assert.equal(docCBin.toString('hex'), docJSBin.toString('hex')); | ||
@@ -394,9 +329,9 @@ | ||
// doc = {"DateTime": "Tue Nov 40 2011 17:27:55 GMT+0000 (WEST)","isActive": true,"Media": {"URL": "http://videos.sapo.pt/Tc85NsjaKjj8o5aV7Ubb"},"Title": "Lisboa fecha a ganhar 0.19%","SetPosition": 60,"Type": "videos","Thumbnail": [{"URL": "http://rd3.videos.sapo.pt/Tc85NsjaKjj8o5aV7Ubb/pic/320x240","Dimensions": {"Height": 240,"Width": 320}}],"Source": {"URL": "http://videos.sapo.pt","SetID": "1288","SourceID": "http://videos.sapo.pt/tvnet/rss2","SetURL": "http://noticias.sapo.pt/videos/tv-net_1288/","ItemID": "Tc85NsjaKjj8o5aV7Ubb","Name": "SAPO VÃdeos"},"Category": "Tec_ciencia","Description": "Lisboa fecha a ganhar 0.19%","GalleryID": new ObjectID("4eea2a634ce8573200000000"),"InternalRefs": {"RegisterDate": "Thu Dec 15 2011 17:12:51 GMT+0000 (WEST)","ChangeDate": "Thu Dec 15 2011 17:12:51 GMT+0000 (WEST)","Hash": 332279244514},"_id": new ObjectID("4eea2a96e52778160000003a")} | ||
// var docJSBin = BSONJS.serialize(docJS, false, true, true); | ||
// var docCBin = BSON.serialize(docC, false, true, true); | ||
// var docJSBin = bsonJS.serialize(docJS, false, true, true); | ||
// var docCBin = bsonC.serialize(docC, false, true, true); | ||
// | ||
// | ||
// Force garbage collect | ||
global.gc(); | ||
// // Force garbage collect | ||
// global.gc(); | ||
@@ -403,0 +338,0 @@ |
@@ -7,12 +7,21 @@ var sys = require('util'), | ||
assert = require('assert'), | ||
Long = require('../bson').Long, | ||
ObjectID = require('../bson').ObjectID, | ||
Binary = require('../bson').Binary, | ||
Code = require('../bson').Code, | ||
BinaryParser = require('../../../lib/mongodb/bson/binary_parser').BinaryParser, | ||
BSONJS = require('../../../lib/mongodb/bson/bson').BSON, | ||
Binary2 = require('../../../lib/mongodb/bson/bson').Binary; | ||
Long = require('../../../lib/mongodb/goog/math/long').Long, | ||
ObjectID = require('../../../lib/mongodb/bson/bson').ObjectID, | ||
Binary = require('../../../lib/mongodb/bson/bson').Binary, | ||
Code = require('../../../lib/mongodb/bson/bson').Code, | ||
DBRef = require('../../../lib/mongodb/bson/bson').DBRef, | ||
Symbol = require('../../../lib/mongodb/bson/bson').Symbol, | ||
Double = require('../../../lib/mongodb/bson/bson').Double, | ||
MaxKey = require('../../../lib/mongodb/bson/bson').MaxKey, | ||
MinKey = require('../../../lib/mongodb/bson/bson').MinKey, | ||
Timestamp = require('../../../lib/mongodb/bson/bson').Timestamp; | ||
sys.puts("=== EXECUTING TEST_FULL_BSON ==="); | ||
// Parsers | ||
var bsonC = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); | ||
var bsonJS = new BSONJS([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); | ||
// Should Correctly Deserialize object | ||
@@ -25,3 +34,3 @@ var bytes = [95,0,0,0,2,110,115,0,42,0,0,0,105,110,116,101,103,114,97,116,105,111,110,95,116,101,115,116,115,95,46,116,101,115,116,95,105,110,100,101,120,95,105,110,102,111,114,109,97,116,105,111,110,0,8,117,110,105,113,117,101,0,0,3,107,101,121,0,12,0,0,0,16,97,0,1,0,0,0,0,2,110,97,109,101,0,4,0,0,0,97,95,49,0,0]; | ||
} | ||
var object = BSON.deserialize(serialized_data); | ||
var object = bsonC.deserialize(serialized_data); | ||
assert.equal("a_1", object.name); | ||
@@ -39,3 +48,3 @@ assert.equal(false, object.unique); | ||
var object = BSONJS.deserialize(new Buffer(serialized_data, 'binary')); | ||
var object = bsonJS.deserialize(new Buffer(serialized_data, 'binary')); | ||
assert.equal("hello", object.string); | ||
@@ -58,14 +67,14 @@ assert.deepEqual([1, 2, 3], object.array); | ||
var test_string = {hello: 'world'} | ||
var serialized_data = BSON.serialize(test_string) | ||
assert.deepEqual(test_string, BSON.deserialize(serialized_data)); | ||
var serialized_data = bsonC.serialize(test_string) | ||
assert.deepEqual(test_string, bsonC.deserialize(serialized_data)); | ||
// Should Correctly Serialize and Deserialize Integer | ||
var test_number = {doc: 5} | ||
var serialized_data = BSON.serialize(test_number) | ||
assert.deepEqual(test_number, BSON.deserialize(serialized_data)); | ||
var serialized_data = bsonC.serialize(test_number) | ||
assert.deepEqual(test_number, bsonC.deserialize(serialized_data)); | ||
// Should Correctly Serialize and Deserialize null value | ||
var test_null = {doc:null} | ||
var serialized_data = BSON.serialize(test_null) | ||
var object = BSON.deserialize(serialized_data); | ||
var serialized_data = bsonC.serialize(test_null) | ||
var object = bsonC.deserialize(serialized_data); | ||
assert.deepEqual(test_null, object); | ||
@@ -75,4 +84,4 @@ | ||
var test_undefined = {doc:undefined} | ||
var serialized_data = BSON.serialize(test_undefined) | ||
var object = BSONJS.deserialize(new Buffer(serialized_data, 'binary')); | ||
var serialized_data = bsonC.serialize(test_undefined) | ||
var object = bsonJS.deserialize(new Buffer(serialized_data, 'binary')); | ||
assert.equal(null, object.doc) | ||
@@ -82,41 +91,41 @@ | ||
var test_number = {doc: 5.5} | ||
var serialized_data = BSON.serialize(test_number) | ||
assert.deepEqual(test_number, BSON.deserialize(serialized_data)); | ||
var serialized_data = bsonC.serialize(test_number) | ||
assert.deepEqual(test_number, bsonC.deserialize(serialized_data)); | ||
// Should Correctly Serialize and Deserialize Integer | ||
var test_int = {doc: 42} | ||
var serialized_data = BSON.serialize(test_int) | ||
assert.deepEqual(test_int, BSON.deserialize(serialized_data)); | ||
var serialized_data = bsonC.serialize(test_int) | ||
assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); | ||
test_int = {doc: -5600} | ||
serialized_data = BSON.serialize(test_int) | ||
assert.deepEqual(test_int, BSON.deserialize(serialized_data)); | ||
serialized_data = bsonC.serialize(test_int) | ||
assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); | ||
test_int = {doc: 2147483647} | ||
serialized_data = BSON.serialize(test_int) | ||
assert.deepEqual(test_int, BSON.deserialize(serialized_data)); | ||
serialized_data = bsonC.serialize(test_int) | ||
assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); | ||
test_int = {doc: -2147483648} | ||
serialized_data = BSON.serialize(test_int) | ||
assert.deepEqual(test_int, BSON.deserialize(serialized_data)); | ||
serialized_data = bsonC.serialize(test_int) | ||
assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); | ||
// Should Correctly Serialize and Deserialize Object | ||
var doc = {doc: {age: 42, name: 'Spongebob', shoe_size: 9.5}} | ||
var serialized_data = BSON.serialize(doc) | ||
assert.deepEqual(doc, BSON.deserialize(serialized_data)); | ||
var serialized_data = bsonC.serialize(doc) | ||
assert.deepEqual(doc, bsonC.deserialize(serialized_data)); | ||
// Should Correctly Serialize and Deserialize Array | ||
var doc = {doc: [1, 2, 'a', 'b']} | ||
var serialized_data = BSON.serialize(doc) | ||
assert.deepEqual(doc, BSON.deserialize(serialized_data)); | ||
var serialized_data = bsonC.serialize(doc) | ||
assert.deepEqual(doc, bsonC.deserialize(serialized_data)); | ||
// Should Correctly Serialize and Deserialize Array with added on functions | ||
var doc = {doc: [1, 2, 'a', 'b']} | ||
var serialized_data = BSON.serialize(doc) | ||
assert.deepEqual(doc, BSON.deserialize(serialized_data)); | ||
var serialized_data = bsonC.serialize(doc) | ||
assert.deepEqual(doc, bsonC.deserialize(serialized_data)); | ||
// Should Correctly Serialize and Deserialize A Boolean | ||
var doc = {doc: true} | ||
var serialized_data = BSON.serialize(doc) | ||
assert.deepEqual(doc, BSON.deserialize(serialized_data)); | ||
var serialized_data = bsonC.serialize(doc) | ||
assert.deepEqual(doc, bsonC.deserialize(serialized_data)); | ||
@@ -133,19 +142,19 @@ // Should Correctly Serialize and Deserialize a Date | ||
var doc = {doc: date} | ||
var serialized_data = BSON.serialize(doc) | ||
assert.deepEqual(doc, BSON.deserialize(serialized_data)); | ||
var serialized_data = bsonC.serialize(doc) | ||
assert.deepEqual(doc, bsonC.deserialize(serialized_data)); | ||
// // Should Correctly Serialize and Deserialize Oid | ||
var doc = {doc: new ObjectID()} | ||
var serialized_data = BSON.serialize(doc) | ||
assert.deepEqual(doc.doc.toHexString(), BSON.deserialize(serialized_data).doc.toHexString()) | ||
var serialized_data = bsonC.serialize(doc) | ||
assert.deepEqual(doc.doc.toHexString(), bsonC.deserialize(serialized_data).doc.toHexString()) | ||
// Should Correctly encode Empty Hash | ||
var test_code = {} | ||
var serialized_data = BSON.serialize(test_code) | ||
assert.deepEqual(test_code, BSON.deserialize(serialized_data)); | ||
var serialized_data = bsonC.serialize(test_code) | ||
assert.deepEqual(test_code, bsonC.deserialize(serialized_data)); | ||
// Should Correctly Serialize and Deserialize Ordered Hash | ||
var doc = {doc: {b:1, a:2, c:3, d:4}} | ||
var serialized_data = BSON.serialize(doc) | ||
var decoded_hash = BSON.deserialize(serialized_data).doc | ||
var serialized_data = bsonC.serialize(doc) | ||
var decoded_hash = bsonC.deserialize(serialized_data).doc | ||
var keys = [] | ||
@@ -158,4 +167,4 @@ for(name in decoded_hash) keys.push(name) | ||
var doc = {doc: /foobar/mi} | ||
var serialized_data = BSON.serialize(doc) | ||
var doc2 = BSON.deserialize(serialized_data); | ||
var serialized_data = bsonC.serialize(doc) | ||
var doc2 = bsonC.deserialize(serialized_data); | ||
assert.equal(doc.doc.toString(), doc2.doc.toString()) | ||
@@ -170,4 +179,4 @@ | ||
var doc = {doc: bin} | ||
var serialized_data = BSON.serialize(doc) | ||
var deserialized_data = BSON.deserialize(serialized_data); | ||
var serialized_data = bsonC.serialize(doc) | ||
var deserialized_data = bsonC.deserialize(serialized_data); | ||
assert.equal(doc.doc.value(), deserialized_data.doc.value()) | ||
@@ -180,4 +189,4 @@ | ||
var doc = {doc: bin} | ||
var serialized_data = BSON.serialize(doc) | ||
var deserialized_data = BSON.deserialize(serialized_data); | ||
var serialized_data = bsonC.serialize(doc) | ||
var deserialized_data = bsonC.deserialize(serialized_data); | ||
assert.equal(doc.doc.value(), deserialized_data.doc.value()) | ||
@@ -184,0 +193,0 @@ |
@@ -145,5 +145,5 @@ var Buffer = require('buffer').Buffer, | ||
// // Serialized document | ||
// var docBin = BSONJS.serialize(doc, false, true); | ||
// | ||
// Serialized document | ||
var docBin = BSONJS.serialize(doc, false, true); | ||
// // -------------------------------------------------------------------------------------- | ||
@@ -235,11 +235,2 @@ // // -------------------------------------------------------------------------------------- | ||
// | ||
// var startTimeD2 = new Date().getTime(); | ||
// | ||
// for(var i = 0; i < COUNT; i++) { | ||
// BSON.deserialize(docBin); | ||
// } | ||
// | ||
// var deltaTimeD2 = new Date().getTime() - startTimeD2; | ||
// console.log("C++ with c++ objects = " + (deltaTimeD2)); | ||
// | ||
// var startTimeD3 = new Date().getTime(); | ||
@@ -246,0 +237,0 @@ // |
@@ -13,33 +13,73 @@ var Buffer = require('buffer').Buffer, | ||
Double = require('../../../lib/mongodb/bson/bson').Double, | ||
Timestamp = require('../../../lib/mongodb/bson/bson').Timestamp, | ||
MaxKey = require('../../../lib/mongodb/bson/bson').MaxKey, | ||
MinKey = require('../../../lib/mongodb/bson/bson').MinKey, | ||
Timestamp = require('../../../lib/mongodb/bson/bson').Timestamp; | ||
assert = require('assert'); | ||
var Long2 = require('../bson').Long, | ||
ObjectID2 = require('../bson').ObjectID, | ||
Binary2 = require('../bson').Binary, | ||
Code2 = require('../bson').Code, | ||
Symbol2 = require('../bson').Symbol, | ||
Double2 = require('../bson').Double, | ||
Timestamp2 = require('../bson').Timestamp, | ||
DBRef2 = require('../bson').DBRef; | ||
console.log("=== EXECUTING TEST_STACKLESS_BSON ==="); | ||
// // Number of iterations for the benchmark | ||
// var COUNT = 100000; | ||
// // Sample simple doc | ||
// // var doc = {key:"Hello world"}; | ||
// Parsers | ||
var bsonC = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); | ||
var bsonJS = new BSONJS([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); | ||
// Number of iterations for the benchmark | ||
var COUNT = 10000; | ||
// var COUNT = 1; | ||
// Sample simple doc | ||
var doc = {key:"Hello world", key2:"šđžčćŠĐŽČĆ", key3:'客家话', key4:'how are you doing dog!!'}; | ||
// var doc = {}; | ||
// for(var i = 0; i < 100; i++) { | ||
// doc['string' + i] = "dumdyms fsdfdsfdsfdsfsdfdsfsdfsdfsdfsdfsdfsdfsdffsfsdfs"; | ||
// doc['string' + i] = "dumdyms fsdfdsfdsfdsfsdfdsfsdfsdfsdfsdfsdfsdfsdffsfsdfs"; | ||
// } | ||
// | ||
// // Calculate size | ||
// console.log(BSON.calculateObjectSize2(doc)); | ||
// console.log(BSONJS.calculateObjectSize(doc)); | ||
// assert.equal(BSONJS.calculateObjectSize(doc), BSON.calculateObjectSize2(doc)); | ||
// | ||
console.log(bsonC.calculateObjectSize2(doc)); | ||
console.log(bsonJS.calculateObjectSize(doc)); | ||
// assert.equal(bsonJS.calculateObjectSize(doc), bsonC.calculateObjectSize2(doc)); | ||
// ---------------------------------------------------------------------------- | ||
// ---------------------------------------------------------------------------- | ||
// Benchmark calculateObjectSize | ||
// ---------------------------------------------------------------------------- | ||
// ---------------------------------------------------------------------------- | ||
// Benchmark 1 JS BSON | ||
console.log(COUNT + "x (objectBSON = bsonC.calculateObjectSize(object))") | ||
start = new Date | ||
for (j=COUNT; --j>=0; ) { | ||
var objectBSON = bsonJS.calculateObjectSize(doc); | ||
} | ||
end = new Date | ||
var opsprsecond = COUNT / ((end - start)/1000); | ||
console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); | ||
// Benchmark 2 C++ BSON calculateObjectSize | ||
console.log(COUNT + "x (objectBSON = bsonC.calculateObjectSize(object))") | ||
start = new Date | ||
for (j=COUNT; --j>=0; ) { | ||
var objectBSON = bsonC.calculateObjectSize(doc); | ||
} | ||
end = new Date | ||
var opsprsecond = COUNT / ((end - start)/1000); | ||
console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); | ||
// Benchmark 3 C++ BSON calculateObjectSize2 | ||
console.log(COUNT + "x (objectBSON = bsonC.calculateObjectSize2(object))") | ||
start = new Date | ||
for (j=COUNT; --j>=0; ) { | ||
var objectBSON = bsonC.calculateObjectSize2(doc); | ||
} | ||
end = new Date | ||
var opsprsecond = COUNT / ((end - start)/1000); | ||
console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); | ||
// // Serialize the content | ||
// var _serializedDoc1 = BSONJS.serialize(doc, true, false); | ||
// var _serializedDoc2 = BSON.serialize2(doc, true, false); | ||
// var _serializedDoc1 = bsonJS.serialize(doc, true, false); | ||
// var _serializedDoc2 = bsonC.serialize2(doc, true, false); | ||
// console.dir(_serializedDoc1); | ||
@@ -51,8 +91,8 @@ // console.dir(_serializedDoc2); | ||
// // Benchmark 1 | ||
// console.log(COUNT + "x (objectBSON = BSON.serialize(object))") | ||
// console.log(COUNT + "x (objectBSON = bsonC.serialize(object))") | ||
// start = new Date | ||
// | ||
// for (j=COUNT; --j>=0; ) { | ||
// // var objectBSON = BSON.serialize2(doc, true, false); | ||
// var objectBSON = BSONJS.serialize(doc, true, false); | ||
// // var objectBSON = bsonC.serialize2(doc, true, false); | ||
// var objectBSON = bsonJS.serialize(doc, true, false); | ||
// } | ||
@@ -62,12 +102,12 @@ // | ||
// var opsprsecond = COUNT / ((end - start)/1000); | ||
// console.log("bson size (bytes): ", objectBSON.length); | ||
// console.log("bson size (bytes): ", objectbsonC.length); | ||
// console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); | ||
// console.log("MB/s = " + ((opsprsecond*objectBSON.length)/1024)); | ||
// console.log("MB/s = " + ((opsprsecond*objectbsonC.length)/1024)); | ||
// | ||
// // Benchmark 2 | ||
// console.log(COUNT + "x (objectBSON = BSON.serialize(object))") | ||
// console.log(COUNT + "x (objectBSON = bsonC.serialize(object))") | ||
// start = new Date | ||
// | ||
// for (j=COUNT; --j>=0; ) { | ||
// var objectBSON = BSON.serialize2(doc, true, false); | ||
// var objectBSON = bsonC.serialize2(doc, true, false); | ||
// } | ||
@@ -77,12 +117,12 @@ // | ||
// var opsprsecond = COUNT / ((end - start)/1000); | ||
// console.log("bson size (bytes): ", objectBSON.length); | ||
// console.log("bson size (bytes): ", objectbsonC.length); | ||
// console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); | ||
// console.log("MB/s = " + ((opsprsecond*objectBSON.length)/1024)); | ||
// console.log("MB/s = " + ((opsprsecond*objectbsonC.length)/1024)); | ||
// | ||
// // Benchmark 3 | ||
// console.log(COUNT + "x (objectBSON = BSON.serialize(object))") | ||
// console.log(COUNT + "x (objectBSON = bsonC.serialize(object))") | ||
// start = new Date | ||
// | ||
// for (j=COUNT; --j>=0; ) { | ||
// var objectBSON = BSON.serialize(doc, true, false); | ||
// var objectBSON = bsonC.serialize(doc, true, false); | ||
// } | ||
@@ -92,4 +132,4 @@ // | ||
// var opsprsecond = COUNT / ((end - start)/1000); | ||
// console.log("bson size (bytes): ", objectBSON.length); | ||
// console.log("bson size (bytes): ", objectbsonC.length); | ||
// console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); | ||
// console.log("MB/s = " + ((opsprsecond*objectBSON.length)/1024)); | ||
// console.log("MB/s = " + ((opsprsecond*objectbsonC.length)/1024)); |
@@ -26,4 +26,4 @@ var Collection = require('./collection').Collection, | ||
doc = doc.documents[0]; | ||
if(err == null && (doc.ok == 1 || doc.was.constructor == Numeric)) { | ||
if(err == null && (doc.ok == 1 || typeof doc.was === 'number')) { | ||
var was = doc.was; | ||
@@ -105,8 +105,10 @@ if(was == 0) { | ||
} | ||
command['profile'] = profile; | ||
// Set up the profile number | ||
command['profile'] = profile; | ||
// Execute the command to set the profiling level | ||
this.command(command, function(err, doc) { | ||
doc = doc.documents[0]; | ||
if(err == null && (doc.ok == 1 || doc.was.constructor == Numeric)) { | ||
if(err == null && (doc.ok == 1 || typeof doc.was === 'number')) { | ||
return callback(null, level); | ||
@@ -113,0 +115,0 @@ } else { |
@@ -22,4 +22,6 @@ | ||
this.sub_type = buffer; | ||
this.position = 0; | ||
} else { | ||
this.sub_type = subType == null ? bson.BSON.BSON_BINARY_SUBTYPE_DEFAULT : subType; | ||
this.position = 0; | ||
} | ||
@@ -26,0 +28,0 @@ |
@@ -29,3 +29,3 @@ /** | ||
BSON.BSON_INT32_MAX = 0x80000000; | ||
BSON.BSON_INT32_MIN = -0x80000000; | ||
BSON.BSON_INT32_MAX = -0x80000000; | ||
@@ -75,89 +75,62 @@ // JS MAX PRECISE VALUES | ||
*/ | ||
// Does not do recursion, uses a stack to handle depth | ||
// Experiment for performance | ||
BSON.calculateObjectSize = function(object, serializeFunctions) { | ||
var calculateObjectSize = BSON.calculateObjectSize = function(object, serializeFunctions) { | ||
var totalLength = (4 + 1); | ||
var done = false; | ||
var stack = new Array(32); | ||
var currentObject = object; | ||
var keys = null; | ||
var keysIndex = 0; | ||
var stackIndex = 0; | ||
var keyLength = Object.keys(object).length; | ||
// Controls the flow | ||
var finished = false; | ||
// Ensure serialized functions set | ||
serializeFunctions = serializeFunctions == null ? false : serializeFunctions; | ||
if(Array.isArray(object)) { | ||
for(var i = 0; i < object.length; i++) { | ||
totalLength += calculateElement(i.toString(), object[i], serializeFunctions) | ||
} | ||
} else { | ||
for(var key in object) { | ||
totalLength += calculateElement(key, object[key], serializeFunctions) | ||
} | ||
} | ||
while(!done) { | ||
// Only get keys if we have a new object | ||
keys = keys == null ? Object.keys(currentObject) : keys; | ||
return totalLength; | ||
} | ||
// Let's process all the elements | ||
while(keysIndex < keyLength) { | ||
var name = keys[keysIndex++]; | ||
var value = currentObject[name]; | ||
if(value == null | value instanceof MinKey || value instanceof MaxKey) { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (1); | ||
} else if(typeof value == 'string') { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (Buffer.byteLength(value, 'utf8') + 4 + 1 + 1); | ||
} else if(Array.isArray(value)) { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (4 + 1) + 1; | ||
stack[stackIndex++] = ({keys:keys, object:currentObject, keysIndex:keysIndex}); | ||
currentObject = value; | ||
keys = Object.keys(value) | ||
keysIndex = 0; | ||
keyLength = keys.length; | ||
} else if((typeof value == 'number' || toString.call(value) === '[object Number]') && | ||
value === parseInt(value, 10) && | ||
value.toString().match(/\./) == null) { | ||
// Write the type | ||
var int64 = value >= BSON.BSON_INT32_MAX || value < BSON.BSON_INT32_MIN; | ||
if(int64) { | ||
// Write the number | ||
var long = Long.fromNumber(value); | ||
// If the number is the same after long conversion force double | ||
if(value.toString() == long.toNumber().toString() || value >= Long.MAX_VALUE || value <= Long.MIN_VALUE) { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1); | ||
} else { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1); | ||
} | ||
} else { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (4 + 1); | ||
var calculateElement = function(name, value, serializeFunctions) { | ||
switch(typeof value) { | ||
case 'string': | ||
return 1 + Buffer.byteLength(name, 'utf8') + 1 + 4 + Buffer.byteLength(value, 'utf8') + 1; | ||
case 'number': | ||
if(~~value === value) { // 32 bit | ||
if(value >= BSON.BSON_INT32_MAX || value < BSON.BSON_INT32_MAX) { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (4 + 1); | ||
} else if(value >= BSON.JS_INT_MAX || value < BSON.JS_INT_MIN) { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1); | ||
} else { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1); | ||
} | ||
} else if(typeof value == 'number' || toString.call(value) === '[object Number]' || | ||
value instanceof Double) { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1); | ||
} else if(typeof value == 'boolean' || toString.call(value) === '[object Boolean]') { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (1 + 1); | ||
} else if(value instanceof RegExp || toString.call(value) === '[object RegExp]') { | ||
// Keep list of valid options | ||
var options_array = []; | ||
var str = value.toString(); | ||
var clean_regexp = str.match(/\/.*\//, ''); | ||
clean_regexp = clean_regexp[0].substring(1, clean_regexp[0].length - 1); | ||
var options = str.substr(clean_regexp.length + 2); | ||
// Extract all options that are legal and sort them alphabetically | ||
for(var index = 0, len = options.length; index < len; ++index) { | ||
var chr = options.charAt(index); | ||
if('i' == chr || 'm' == chr || 'x' == chr) { | ||
options_array.push(chr); | ||
} | ||
} | ||
// Calculate the total length | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (Buffer.byteLength(clean_regexp) + 1 + options_array.length + 1 + 1); | ||
} else if(value instanceof Date || toString.call(value) === '[object Date]') { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1); | ||
} else if(value instanceof ObjectID || (value.id && value.toHexString)) { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (12 + 1); | ||
} else { // 64 bit | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1); | ||
} | ||
case 'undefined': | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (1); | ||
case 'boolean': | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (1 + 1); | ||
case 'object': | ||
if(value instanceof ObjectID) { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (12 + 1); | ||
} else if(value instanceof Date) { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1); | ||
} else if(Buffer.isBuffer(value)) { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (1 + 4 + 1) + value.length; | ||
} else if(value instanceof Long || value instanceof Double || value instanceof Timestamp) { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1); | ||
} else if(value instanceof Code) { | ||
// Calculate size depending on the availability of a scope | ||
if(value.scope != null && Object.keys(value.scope).length > 0) { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + 1 + 4 + 4 + Buffer.byteLength(value.code.toString(), 'utf8') + 1 + calculateObjectSize(value.scope); | ||
} else { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + 1 + 4 + Buffer.byteLength(value.code.toString(), 'utf8') + 1; | ||
} | ||
} else if(value == null || value instanceof MinKey || value instanceof MaxKey) { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (1); | ||
} else if(value instanceof Binary) { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (value.position + 1 + 4 + 1); | ||
} else if(value instanceof Long || value instanceof Double || value instanceof Timestamp) { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (8 + 1); | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (value.position + 1 + 4 + 1); | ||
} else if(value instanceof Symbol) { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + (Buffer.byteLength(value.value, 'utf8') + 4 + 1 + 1); | ||
} else if(value instanceof DBRef) { | ||
// Set up correct object for serialization | ||
var ordered_values = { | ||
@@ -168,611 +141,530 @@ '$ref': value.namespace | ||
// Add db reference if it exists | ||
if(null != value.db) { | ||
ordered_values['$db'] = value.db; | ||
} | ||
// Calculate the object | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (4 + 1 + 1); | ||
stack[stackIndex++] = ({keys:keys, object:currentObject, keysIndex:keysIndex}); | ||
currentObject = ordered_values; | ||
keys = Object.keys(ordered_values) | ||
keysIndex = 0; | ||
keyLength = keys.length; | ||
} else if(value instanceof Code && Object.keys(value.scope).length == 0) { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (Buffer.byteLength(value.code.toString(), 'utf8') + 4 + 1 + 1); | ||
} else if(value instanceof Code) { | ||
// Calculate the length of the code string | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + 4 + (Buffer.byteLength(value.code.toString(), 'utf8') + 1) + 4; | ||
totalLength += (4 + 1 + 1); | ||
// Push the current object | ||
stack[stackIndex++] = ({keys:keys, object:currentObject, keysIndex:keysIndex}); | ||
currentObject = value.scope; | ||
keys = Object.keys(value.scope) | ||
keysIndex = 0; | ||
keyLength = keys.length; | ||
} else if(value instanceof Symbol) { | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (Buffer.byteLength(value.value, 'utf8') + 4 + 1 + 1); | ||
} else if(typeof value == 'function' && serializeFunctions) { | ||
// Calculate the length of the code string | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + 4 + (Buffer.byteLength(value.toString(), 'utf8') + 1 + 1); | ||
} else if(typeof value == 'object') { | ||
// Calculate the object | ||
totalLength += (name != null ? (Buffer.byteLength(name) + 1) : 0) + (4 + 1 + 1); | ||
// Otherwise handle keys | ||
stack[stackIndex++] = ({keys:keys, object:currentObject, keysIndex:keysIndex}); | ||
currentObject = value; | ||
keys = Object.keys(value) | ||
keysIndex = 0; | ||
keyLength = keys.length; | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + 1 + calculateObjectSize(ordered_values, serializeFunctions); | ||
} else if(value instanceof RegExp || toString.call(value) === '[object RegExp]') { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + 1 + Buffer.byteLength(value.source, 'utf8') + 1 | ||
+ (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1 | ||
} else { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + calculateObjectSize(value, serializeFunctions) + 1; | ||
} | ||
case 'function': | ||
if(serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + 1 + 4 + 4 + Buffer.byteLength(value.toString(), 'utf8') + 1 + calculateObjectSize(value.scope); | ||
} else if(serializeFunctions) { | ||
return (name != null ? (Buffer.byteLength(name) + 1) : 0) + 1 + 4 + Buffer.byteLength(value.toString(), 'utf8') + 1; | ||
} | ||
} | ||
return 0; | ||
} | ||
// Finished up the object | ||
if(keysIndex == keyLength) { | ||
finished = true; | ||
} | ||
} | ||
var serializeWithBufferAndIndex = BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object, checkKeys, buffer, index, serializeFunctions) { | ||
// Default setting false | ||
serializeFunctions = serializeFunctions == null ? false : serializeFunctions; | ||
// Write end information (length of the object) | ||
var size = buffer.length; | ||
// Write the size of the object | ||
buffer[index++] = size & 0xff; | ||
buffer[index++] = (size >> 8) & 0xff; | ||
buffer[index++] = (size >> 16) & 0xff; | ||
buffer[index++] = (size >> 24) & 0xff; | ||
return serializeObject(object, checkKeys, buffer, index, serializeFunctions) - 1; | ||
} | ||
// If the stack is empty let's finish up, otherwise pop the previous object and | ||
// continue | ||
if(stackIndex == 0) { | ||
done = true; | ||
} else if(finished || (keysIndex == keyLength)){ | ||
var currentObjectStored = stack[--stackIndex]; | ||
currentObject = currentObjectStored.object; | ||
keysIndex = currentObjectStored.keysIndex; | ||
keys = currentObjectStored.keys; | ||
keyLength = keys.length; | ||
finished = (keyLength - 1) == keysIndex; | ||
var serializeObject = function(object, checkKeys, buffer, index, serializeFunctions) { | ||
// Process the object | ||
if(Array.isArray(object)) { | ||
for(var i = 0; i < object.length; i++) { | ||
index = packElement(i.toString(), object[i], checkKeys, buffer, index, serializeFunctions); | ||
} | ||
} | ||
} else { | ||
for(var key in object) { | ||
// Check the key and throw error if it's illegal | ||
if(checkKeys == true && (key != '$db' && key != '$ref' && key != '$id')) { | ||
BSON.checkKey(key); | ||
} | ||
return totalLength; | ||
// Pack the element | ||
index = packElement(key, object[key], checkKeys, buffer, index, serializeFunctions); | ||
} | ||
} | ||
// Write zero | ||
buffer[index++] = 0; | ||
return index; | ||
} | ||
// In place serialization with index to starting point of serialization | ||
BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object, checkKeys, buffer, startIndex, serializeFunctions) { | ||
if(null != object && 'object' === typeof object) { | ||
// Encode the object using single allocated buffer and no recursion | ||
var index = startIndex == null ? 0 : startIndex; | ||
var done = false; | ||
var stack = new Array(32); | ||
var currentObject = object; | ||
var keys = null; | ||
var keysIndex = 0; | ||
var stackIndex = 0; | ||
var keyLength = Object.keys(object).length; | ||
var size = 0; | ||
var objectIndex = 0; | ||
var totalNumberOfObjects = 0; | ||
// Special index for Code objects | ||
var codeStartIndex = 0; | ||
// Signals if we are finished up | ||
var finished = false; | ||
// Ensure serialized functions set | ||
serializeFunctions = serializeFunctions == null ? false : serializeFunctions; | ||
// Current parsing object state | ||
var currentObjectStored = {object: object, index: index, endIndex: 0, keys: Object.keys(object), keysIndex:keysIndex, keyLength:keyLength}; | ||
// Adjust the index | ||
index = index + 4; | ||
// While meeting | ||
while(!done) { | ||
// While current object has keys | ||
while(keysIndex < keyLength) { | ||
var name = currentObjectStored.keys[keysIndex++]; | ||
var value = currentObjectStored.object[name]; | ||
// If we got a key check for valid type | ||
if(name != null && checkKeys == true && (name != '$db' && name != '$ref' && name != '$id')) { | ||
BSON.checkKey(name); | ||
} | ||
if(value == null) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_NULL; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
} else if(typeof value == 'string') { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_STRING; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
// Calculate size | ||
size = Buffer.byteLength(value) + 1; | ||
// Write the size of the string to buffer | ||
buffer[index + 3] = (size >> 24) & 0xff; | ||
buffer[index + 2] = (size >> 16) & 0xff; | ||
buffer[index + 1] = (size >> 8) & 0xff; | ||
buffer[index] = size & 0xff; | ||
// Ajust the index | ||
index = index + 4; | ||
// Write the string | ||
buffer.write(value, index, 'utf8'); | ||
// Update index | ||
index = index + size - 1; | ||
// Write zero | ||
buffer[index++] = 0; | ||
} else if((typeof value == 'number' || toString.call(value) === '[object Number]') && | ||
value === parseInt(value, 10) && | ||
value.toString().match(/\./) == null) { | ||
// Write the type | ||
var int64 = value >= BSON.BSON_INT32_MAX || value < BSON.BSON_INT32_MIN; | ||
var startIndex = index; | ||
// Set type | ||
buffer[index++] = int64 ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_INT; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
if(int64) { | ||
// Write the number | ||
var long = Long.fromNumber(value); | ||
var lowBits = long.getLowBits(); | ||
var highBits = long.getHighBits(); | ||
// If the number is the same after long conversion force double | ||
if(value.toString() == long.toNumber().toString() || value >= Long.MAX_VALUE || value <= Long.MIN_VALUE) { | ||
buffer[startIndex] = BSON.BSON_DATA_NUMBER; | ||
// Write float | ||
ieee754.writeIEEE754(buffer, value, index, 'little', 52, 8); | ||
// Ajust index | ||
index = index + 8; | ||
} else { | ||
buffer[index + 3] = (lowBits >> 24) & 0xff; | ||
buffer[index + 2] = (lowBits >> 16) & 0xff; | ||
buffer[index + 1] = (lowBits >> 8) & 0xff; | ||
buffer[index] = lowBits & 0xff; | ||
index += 4; | ||
buffer[index + 3] = (highBits >> 24) & 0xff; | ||
buffer[index + 2] = (highBits >> 16) & 0xff; | ||
buffer[index + 1] = (highBits >> 8) & 0xff; | ||
buffer[index] = highBits & 0xff; | ||
index += 4; | ||
} | ||
} else { | ||
// Write the int value to the buffer | ||
buffer[index + 3] = (value >> 24) & 0xff; | ||
buffer[index + 2] = (value >> 16) & 0xff; | ||
buffer[index + 1] = (value >> 8) & 0xff; | ||
buffer[index] = value & 0xff; | ||
index = index + 4; | ||
} | ||
} else if(typeof value == 'number' || toString.call(value) === '[object Number]' || | ||
value instanceof Double) { | ||
// Write the type | ||
var packElement = function(name, value, checkKeys, buffer, index, serializeFunctions) { | ||
// console.log("packElement: " + name + " :: " + value) | ||
var startIndex = index; | ||
switch(typeof value) { | ||
case 'string': | ||
// Encode String type | ||
buffer[index++] = BSON.BSON_DATA_STRING; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Calculate size | ||
var size = Buffer.byteLength(value) + 1; | ||
// Write the size of the string to buffer | ||
buffer[index + 3] = (size >> 24) & 0xff; | ||
buffer[index + 2] = (size >> 16) & 0xff; | ||
buffer[index + 1] = (size >> 8) & 0xff; | ||
buffer[index] = size & 0xff; | ||
// Ajust the index | ||
index = index + 4; | ||
// Write the string | ||
buffer.write(value, index, 'utf8'); | ||
// Update index | ||
index = index + size - 1; | ||
// Write zero | ||
buffer[index++] = 0; | ||
// Return index | ||
return index; | ||
case 'number': | ||
// We have an integer value | ||
if(~~value === value) { | ||
// If the value fits in 32 bits encode as int, if it fits in a double | ||
// encode it as a double, otherwise long | ||
if(value >= BSON.BSON_INT32_MAX || value < BSON.BSON_INT32_MAX) { | ||
// Set int type 32 bits or less | ||
buffer[index++] = BSON.BSON_DATA_INT; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Write the int value | ||
buffer[index++] = value & 0xff; | ||
buffer[index++] = (value >> 8) & 0xff; | ||
buffer[index++] = (value >> 16) & 0xff; | ||
buffer[index++] = (value >> 24) & 0xff; | ||
} else if(value >= BSON.JS_INT_MAX || value < BSON.JS_INT_MIN) { | ||
// Encode as double | ||
buffer[index++] = BSON.BSON_DATA_NUMBER; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Write float | ||
ieee754.writeIEEE754(buffer, value, index, 'little', 52, 8); | ||
// Ajust index | ||
index = index + 8; | ||
} else if(typeof value == 'boolean' || toString.call(value) === '[object Boolean]') { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_BOOLEAN; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
buffer[index++] = value ? 1 : 0; | ||
} else if(value instanceof Date || toString.call(value) === '[object Date]') { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_DATE; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
// Write the date | ||
var dateInMilis = Long.fromNumber(value.getTime()); | ||
var lowBits = dateInMilis.getLowBits(); | ||
var highBits = dateInMilis.getHighBits(); | ||
buffer[index + 3] = (lowBits >> 24) & 0xff; | ||
buffer[index + 2] = (lowBits >> 16) & 0xff; | ||
buffer[index + 1] = (lowBits >> 8) & 0xff; | ||
buffer[index] = lowBits & 0xff; | ||
index = index + 4; | ||
buffer[index + 3] = (highBits >> 24) & 0xff; | ||
buffer[index + 2] = (highBits >> 16) & 0xff; | ||
buffer[index + 1] = (highBits >> 8) & 0xff; | ||
buffer[index] = highBits & 0xff; | ||
index = index + 4; | ||
} else if(value instanceof RegExp || toString.call(value) === '[object RegExp]') { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_REGEXP; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
// Keep list of valid options | ||
var options_array = []; | ||
var str = value.toString(); | ||
var clean_regexp = str.match(/\/.*\//, ''); | ||
clean_regexp = clean_regexp[0].substring(1, clean_regexp[0].length - 1); | ||
// Get options from the regular expression | ||
var options = str.substr(clean_regexp.length + 2); | ||
// Write the regexp to the buffer | ||
buffer.write(clean_regexp, index, 'utf8'); | ||
// Update the index | ||
index = index + Buffer.byteLength(clean_regexp) + 1; | ||
// Write ending cstring zero | ||
index = index + 8; | ||
} else { | ||
// Set long type | ||
buffer[index++] = BSON.BSON_DATA_LONG; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Extract all options that are legal and sort them alphabetically | ||
for(var i = 0, len = options.length; i < len; ++i) { | ||
var chr = options[i]; | ||
if('i' == chr || 'm' == chr || 'x' == chr) { | ||
buffer[index++] = chr.charCodeAt(0) | ||
} | ||
} | ||
// Write ending cstring zero | ||
buffer[index++] = 0; | ||
} else if(value instanceof Long) { | ||
// Encode low bits | ||
buffer[index++] = lowBits & 0xff; | ||
buffer[index++] = (lowBits >> 8) & 0xff; | ||
buffer[index++] = (lowBits >> 16) & 0xff; | ||
buffer[index++] = (lowBits >> 24) & 0xff; | ||
// Encode high bits | ||
buffer[index++] = highBits & 0xff; | ||
buffer[index++] = (highBits >> 8) & 0xff; | ||
buffer[index++] = (highBits >> 16) & 0xff; | ||
buffer[index++] = (highBits >> 24) & 0xff; | ||
} | ||
} else { | ||
// Encode as double | ||
buffer[index++] = BSON.BSON_DATA_NUMBER; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Write float | ||
ieee754.writeIEEE754(buffer, value, index, 'little', 52, 8); | ||
// Ajust index | ||
index = index + 8; | ||
} | ||
return index; | ||
case 'undefined': | ||
// Set long type | ||
buffer[index++] = BSON.BSON_DATA_NULL; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
return index; | ||
case 'boolean': | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_BOOLEAN; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Encode the boolean value | ||
buffer[index++] = value ? 1 : 0; | ||
return index; | ||
case 'object': | ||
if(value instanceof ObjectID) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_OID; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Write objectid | ||
buffer.write(value.id, index, 'binary'); | ||
// Ajust index | ||
index = index + 12; | ||
return index; | ||
} else if(value instanceof Date) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_DATE; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Write the date | ||
var dateInMilis = Long.fromNumber(value.getTime()); | ||
var lowBits = dateInMilis.getLowBits(); | ||
var highBits = dateInMilis.getHighBits(); | ||
// Encode low bits | ||
buffer[index++] = lowBits & 0xff; | ||
buffer[index++] = (lowBits >> 8) & 0xff; | ||
buffer[index++] = (lowBits >> 16) & 0xff; | ||
buffer[index++] = (lowBits >> 24) & 0xff; | ||
// Encode high bits | ||
buffer[index++] = highBits & 0xff; | ||
buffer[index++] = (highBits >> 8) & 0xff; | ||
buffer[index++] = (highBits >> 16) & 0xff; | ||
buffer[index++] = (highBits >> 24) & 0xff; | ||
return index; | ||
} else if(Buffer.isBuffer(value)) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_BINARY; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Get size of the buffer (current write point) | ||
var size = value.length; | ||
// Write the size of the string to buffer | ||
buffer[index++] = size & 0xff; | ||
buffer[index++] = (size >> 8) & 0xff; | ||
buffer[index++] = (size >> 16) & 0xff; | ||
buffer[index++] = (size >> 24) & 0xff; | ||
// Write the default subtype | ||
buffer[index++] = BSON.BSON_BINARY_SUBTYPE_DEFAULT; | ||
// Copy the content form the binary field to the buffer | ||
value.copy(buffer, index, 0, size); | ||
// Adjust the index | ||
index = index + size; | ||
return index; | ||
} else if(value instanceof Long || value instanceof Timestamp) { | ||
// Write the type | ||
buffer[index++] = value instanceof Long ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_TIMESTAMP; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Write the date | ||
var lowBits = value.getLowBits(); | ||
var highBits = value.getHighBits(); | ||
// Encode low bits | ||
buffer[index++] = lowBits & 0xff; | ||
buffer[index++] = (lowBits >> 8) & 0xff; | ||
buffer[index++] = (lowBits >> 16) & 0xff; | ||
buffer[index++] = (lowBits >> 24) & 0xff; | ||
// Encode high bits | ||
buffer[index++] = highBits & 0xff; | ||
buffer[index++] = (highBits >> 8) & 0xff; | ||
buffer[index++] = (highBits >> 16) & 0xff; | ||
buffer[index++] = (highBits >> 24) & 0xff; | ||
return index; | ||
} else if(value instanceof Double) { | ||
// Encode as double | ||
buffer[index++] = BSON.BSON_DATA_NUMBER; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Write float | ||
ieee754.writeIEEE754(buffer, value, index, 'little', 52, 8); | ||
// Ajust index | ||
index = index + 8; | ||
return index; | ||
} else if(value instanceof Code) { | ||
if(value.scope != null && Object.keys(value.scope).length > 0) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_LONG; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Calculate the scope size | ||
var scopeSize = BSON.calculateObjectSize(value.scope); | ||
// Function string | ||
var functionString = value.code.toString(); | ||
// Function Size | ||
var codeSize = Buffer.byteLength(functionString) + 1; | ||
// Write the date | ||
var lowBits = value.getLowBits(); | ||
var highBits = value.getHighBits(); | ||
buffer[index + 3] = (lowBits >> 24) & 0xff; | ||
buffer[index + 2] = (lowBits >> 16) & 0xff; | ||
buffer[index + 1] = (lowBits >> 8) & 0xff; | ||
buffer[index] = lowBits & 0xff; | ||
index = index + 4; | ||
// Calculate full size of the object | ||
var totalSize = 4 + codeSize + scopeSize + 4; | ||
buffer[index + 3] = (highBits >> 24) & 0xff; | ||
buffer[index + 2] = (highBits >> 16) & 0xff; | ||
buffer[index + 1] = (highBits >> 8) & 0xff; | ||
buffer[index] = highBits & 0xff; | ||
index = index + 4; | ||
} else if(value instanceof Timestamp) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_TIMESTAMP; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
// Write the total size of the object | ||
buffer[index++] = totalSize & 0xff; | ||
buffer[index++] = (totalSize >> 8) & 0xff; | ||
buffer[index++] = (totalSize >> 16) & 0xff; | ||
buffer[index++] = (totalSize >> 24) & 0xff; | ||
// Write the date | ||
var lowBits = value.getLowBits(); | ||
var highBits = value.getHighBits(); | ||
buffer[index + 3] = (lowBits >> 24) & 0xff; | ||
buffer[index + 2] = (lowBits >> 16) & 0xff; | ||
buffer[index + 1] = (lowBits >> 8) & 0xff; | ||
buffer[index] = lowBits & 0xff; | ||
index = index + 4; | ||
buffer[index + 3] = (highBits >> 24) & 0xff; | ||
buffer[index + 2] = (highBits >> 16) & 0xff; | ||
buffer[index + 1] = (highBits >> 8) & 0xff; | ||
buffer[index] = highBits & 0xff; | ||
index = index + 4; | ||
} else if(value instanceof Binary) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_BINARY; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
// Extract the buffer | ||
var data = value.value(true); | ||
// Calculate size | ||
size = data.length; | ||
// Write the size of the string to buffer | ||
buffer[index + 3] = (size >> 24) & 0xff; | ||
buffer[index + 2] = (size >> 16) & 0xff; | ||
buffer[index + 1] = (size >> 8) & 0xff; | ||
buffer[index] = size & 0xff; | ||
// Update the index | ||
index = index + 4; | ||
// Write the subtype to the buffer | ||
buffer[index++] = value.sub_type; | ||
// Write the data to the object | ||
data.copy(buffer, index, 0, data.length); | ||
// Ajust index | ||
index = index + data.length; | ||
} else if(value instanceof ObjectID) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_OID; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
buffer[index++] = codeSize & 0xff; | ||
buffer[index++] = (codeSize >> 8) & 0xff; | ||
buffer[index++] = (codeSize >> 16) & 0xff; | ||
buffer[index++] = (codeSize >> 24) & 0xff; | ||
// Write objectid | ||
buffer.write(value.id, index, 'binary'); | ||
// Ajust index | ||
index = index + 12; | ||
} else if(value instanceof DBRef) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_OBJECT; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
var ordered_values = { | ||
'$ref': value.namespace | ||
, '$id' : value.oid | ||
}; | ||
if(null != value.db) { | ||
ordered_values['$db'] = value.db; | ||
} | ||
// Update object name index | ||
currentObjectStored.keysIndex = keysIndex; | ||
// Push object on stack | ||
stack[stackIndex++] = currentObjectStored; | ||
var objKeys = Object.keys(ordered_values); | ||
// Set the new object | ||
currentObjectStored = {object: ordered_values, index: index, endIndex: 0, keys: objKeys, keysIndex: 0, keyLength: objKeys.length}; | ||
keyLength = objKeys.length; | ||
keysIndex = 0; | ||
// Adjust index | ||
index = index + 4; | ||
} else if(value instanceof Code && Object.keys(value.scope).length == 0) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_CODE; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
// Calculate size | ||
size = Buffer.byteLength(value.code.toString()) + 1; | ||
// Write the size of the string to buffer | ||
buffer[index + 3] = (size >> 24) & 0xff; | ||
buffer[index + 2] = (size >> 16) & 0xff; | ||
buffer[index + 1] = (size >> 8) & 0xff; | ||
buffer[index] = size & 0xff; | ||
// Ajust the index | ||
index = index + 4; | ||
// Write the string | ||
buffer.write(value.code.toString(), index, 'utf8'); | ||
buffer.write(functionString, index, 'utf8'); | ||
// Update index | ||
index = index + size - 1; | ||
index = index + codeSize - 1; | ||
// Write zero | ||
buffer[index++] = 0; | ||
} else if(value instanceof Code) { | ||
// Calculate the scope size | ||
var scopeSize = BSON.calculateObjectSize(value.scope); | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
// Convert value to string | ||
var codeString = value.code.toString(); | ||
var codeStringLength = Buffer.byteLength(codeString); | ||
// Calculate size | ||
size = 4 + codeStringLength + 1 + 4 + scopeSize; | ||
// Write the size of the string to buffer | ||
buffer[index + 3] = (size >> 24) & 0xff; | ||
buffer[index + 2] = (size >> 16) & 0xff; | ||
buffer[index + 1] = (size >> 8) & 0xff; | ||
buffer[index] = size & 0xff; | ||
// Update index | ||
index = index + 4; | ||
// Calculate codestring length | ||
size = codeStringLength + 1; | ||
// Write the size of the string to buffer | ||
buffer[index + 3] = (size >> 24) & 0xff; | ||
buffer[index + 2] = (size >> 16) & 0xff; | ||
buffer[index + 1] = (size >> 8) & 0xff; | ||
buffer[index] = size & 0xff; | ||
// Update index | ||
index = index + 4; | ||
// Write the string | ||
buffer.write(codeString, index, 'utf8'); | ||
// Update index | ||
index = index + codeStringLength; | ||
// Add final 0 for cstring | ||
buffer[index++] = 0; | ||
// Serialize the scope object | ||
var scopeObjectBuffer = new Buffer(scopeSize); | ||
// Execute the serialization into a seperate buffer | ||
serializeObject(value.scope, checkKeys, scopeObjectBuffer, 0, serializeFunctions); | ||
// Adjusted scope Size (removing the header) | ||
var scopeDocSize = scopeSize; | ||
// Write scope object size | ||
buffer[index++] = scopeDocSize & 0xff; | ||
buffer[index++] = (scopeDocSize >> 8) & 0xff; | ||
buffer[index++] = (scopeDocSize >> 16) & 0xff; | ||
buffer[index++] = (scopeDocSize >> 24) & 0xff; | ||
// Write the scopeObject into the buffer | ||
scopeObjectBuffer.copy(buffer, index, 0, scopeSize); | ||
// Update object name index | ||
currentObjectStored.keysIndex = keysIndex; | ||
// Push object on stack | ||
stack[stackIndex++] = currentObjectStored; | ||
var objKeys = Object.keys(value.scope); | ||
// Set the new object | ||
currentObjectStored = {object: value.scope, index: index, endIndex: 0, keys: objKeys, keysIndex: 0, keyLength: objKeys.length}; | ||
keyLength = objKeys.length; | ||
keysIndex = 0; | ||
// Adjust index | ||
index = index + 4; | ||
} else if(value instanceof Symbol) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_SYMBOL; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
// Calculate size | ||
size = Buffer.byteLength(value.value) + 1; | ||
// Write the size of the string to buffer | ||
buffer[index + 3] = (size >> 24) & 0xff; | ||
buffer[index + 2] = (size >> 16) & 0xff; | ||
buffer[index + 1] = (size >> 8) & 0xff; | ||
buffer[index] = size & 0xff; | ||
// Ajust the index | ||
index = index + 4; | ||
// Write the string | ||
buffer.write(value.value, index, 'utf8'); | ||
// Update index | ||
index = index + size - 1; | ||
// Write zero | ||
// Adjust index, removing the empty size of the doc (5 bytes 0000000005) | ||
index = index + scopeDocSize - 5; | ||
// Write trailing zero | ||
buffer[index++] = 0; | ||
} else if(typeof value == 'function' && serializeFunctions) { | ||
// Write the type | ||
return index | ||
} else { | ||
buffer[index++] = BSON.BSON_DATA_CODE; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
// Calculate size | ||
size = Buffer.byteLength(value.toString()) + 1; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Function string | ||
var functionString = value.code.toString(); | ||
// Function Size | ||
var size = Buffer.byteLength(functionString) + 1; | ||
// Write the size of the string to buffer | ||
buffer[index + 3] = (size >> 24) & 0xff; | ||
buffer[index + 2] = (size >> 16) & 0xff; | ||
buffer[index + 1] = (size >> 8) & 0xff; | ||
buffer[index] = size & 0xff; | ||
// Ajust the index | ||
index = index + 4; | ||
buffer[index++] = size & 0xff; | ||
buffer[index++] = (size >> 8) & 0xff; | ||
buffer[index++] = (size >> 16) & 0xff; | ||
buffer[index++] = (size >> 24) & 0xff; | ||
// Write the string | ||
buffer.write(value.toString(), index, 'utf8'); | ||
buffer.write(functionString, index, 'utf8'); | ||
// Update index | ||
index = index + size - 1; | ||
// Write zero | ||
buffer[index++] = 0; | ||
buffer[index++] = 0; | ||
return index; | ||
} | ||
} else if(value === null || value instanceof MinKey || value instanceof MaxKey) { | ||
// Write the type of either min or max key | ||
if(value === null) { | ||
buffer[index++] = BSON.BSON_DATA_NULL; | ||
} else if(value instanceof MinKey) { | ||
// Write the type of either Array or object | ||
buffer[index++] = BSON.BSON_DATA_MIN_KEY; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
} else if(value instanceof MaxKey) { | ||
// Write the type of either Array or object | ||
} else { | ||
buffer[index++] = BSON.BSON_DATA_MAX_KEY; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
} else if(typeof value == 'object') { | ||
// Write the type of either Array or object | ||
buffer[index++] = Array.isArray(value) ? BSON.BSON_DATA_ARRAY : BSON.BSON_DATA_OBJECT; | ||
// Write the name | ||
if(name != null) { | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
} | ||
} | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
return index; | ||
} else if(value instanceof Binary) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_BINARY; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Extract the buffer | ||
var data = value.value(true); | ||
// Calculate size | ||
var size = value.position; | ||
// Write the size of the string to buffer | ||
buffer[index++] = size & 0xff; | ||
buffer[index++] = (size >> 8) & 0xff; | ||
buffer[index++] = (size >> 16) & 0xff; | ||
buffer[index++] = (size >> 24) & 0xff; | ||
// Write the subtype to the buffer | ||
buffer[index++] = value.sub_type; | ||
// Write the data to the object | ||
data.copy(buffer, index, 0, value.position); | ||
// Ajust index | ||
index = index + value.position; | ||
return index; | ||
} else if(value instanceof Symbol) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_SYMBOL; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Calculate size | ||
size = Buffer.byteLength(value.value) + 1; | ||
// Write the size of the string to buffer | ||
buffer[index++] = size & 0xff; | ||
buffer[index++] = (size >> 8) & 0xff; | ||
buffer[index++] = (size >> 16) & 0xff; | ||
buffer[index++] = (size >> 24) & 0xff; | ||
// Write the string | ||
buffer.write(value.value, index, 'utf8'); | ||
// Update index | ||
index = index + size - 1; | ||
// Write zero | ||
buffer[index++] = 0x00; | ||
return index; | ||
} else if(value instanceof DBRef) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_OBJECT; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Set up correct object for serialization | ||
var ordered_values = { | ||
'$ref': value.namespace | ||
, '$id' : value.oid | ||
}; | ||
// Add db reference if it exists | ||
if(null != value.db) { | ||
ordered_values['$db'] = value.db; | ||
} | ||
// Update object name index | ||
currentObjectStored.keysIndex = keysIndex; | ||
// Push object on stack | ||
stack[stackIndex++] = currentObjectStored; | ||
var objKeys = Object.keys(value); | ||
// Set the new object | ||
currentObjectStored = {object: value, index: index, endIndex: 0, keys: objKeys, keysIndex: 0, keyLength: objKeys.length}; | ||
keyLength = objKeys.length; | ||
keysIndex = 0; | ||
// Message size | ||
var size = calculateObjectSize(ordered_values); | ||
// Serialize the object | ||
var endIndex = serializeWithBufferAndIndex(ordered_values, checkKeys, buffer, index, serializeFunctions); | ||
// Write the size of the string to buffer | ||
buffer[index++] = size & 0xff; | ||
buffer[index++] = (size >> 8) & 0xff; | ||
buffer[index++] = (size >> 16) & 0xff; | ||
buffer[index++] = (size >> 24) & 0xff; | ||
// Write zero for object | ||
buffer[endIndex++] = 0x00; | ||
// Return the end index | ||
return endIndex; | ||
} else if(value instanceof RegExp || toString.call(value) === '[object RegExp]') { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_REGEXP; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Adjust index | ||
index = index + 4; | ||
} | ||
// Write the regular expression string | ||
buffer.write(value.source, index, 'utf8'); | ||
// Adjust the index | ||
index = index + Buffer.byteLength(value.source); | ||
// Write zero | ||
buffer[index++] = 0x00; | ||
// Write the parameters | ||
if(value.global) buffer[index++] = 0x73; // s | ||
if(value.ignoreCase) buffer[index++] = 0x69; // i | ||
if(value.multiline) buffer[index++] = 0x6d; // m | ||
// Add ending zero | ||
buffer[index++] = 0x00; | ||
return index; | ||
} else { | ||
// Write the type | ||
buffer[index++] = Array.isArray(value) ? BSON.BSON_DATA_ARRAY : BSON.BSON_DATA_OBJECT; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Serialize the object | ||
var endIndex = serializeObject(value, checkKeys, buffer, index + 4, serializeFunctions); | ||
// Write size | ||
var size = endIndex - index; | ||
// Write the size of the string to buffer | ||
buffer[index++] = size & 0xff; | ||
buffer[index++] = (size >> 8) & 0xff; | ||
buffer[index++] = (size >> 16) & 0xff; | ||
buffer[index++] = (size >> 24) & 0xff; | ||
return endIndex; | ||
} | ||
case 'function': | ||
if(serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) { | ||
// Write the type | ||
buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Calculate the scope size | ||
var scopeSize = BSON.calculateObjectSize(value.scope); | ||
// Function string | ||
var functionString = value.toString(); | ||
// Function Size | ||
var codeSize = Buffer.byteLength(functionString) + 1; | ||
if(keysIndex == keyLength) { | ||
// Save end index | ||
currentObjectStored.endIndex = index; | ||
// Calculate full size of the object | ||
var totalSize = 4 + codeSize + scopeSize; | ||
// If we have a stack pop and finish up processing | ||
if(stackIndex > 0) { | ||
// Write the current object size out | ||
// Pack the size of the total buffer length | ||
size = currentObjectStored.endIndex - currentObjectStored.index + 1; | ||
// Write the size of the string to buffer | ||
buffer[currentObjectStored.index + 3] = (size >> 24) & 0xff; | ||
buffer[currentObjectStored.index + 2] = (size >> 16) & 0xff; | ||
buffer[currentObjectStored.index + 1] = (size >> 8) & 0xff; | ||
buffer[currentObjectStored.index] = size & 0xff; | ||
// Adjust and set null last parameter | ||
buffer[index++] = 0; | ||
// Write the total size of the object | ||
buffer[index++] = totalSize & 0xff; | ||
buffer[index++] = (totalSize >> 8) & 0xff; | ||
buffer[index++] = (totalSize >> 16) & 0xff; | ||
buffer[index++] = (totalSize >> 24) & 0xff; | ||
// Pop off the stored object | ||
// currentObjectStored = stack.pop(); | ||
currentObjectStored = stack[--stackIndex]; | ||
keysIndex = currentObjectStored.keysIndex; | ||
keyLength = currentObjectStored.keyLength; | ||
} | ||
} | ||
} | ||
// Write the size of the string to buffer | ||
buffer[index++] = codeSize & 0xff; | ||
buffer[index++] = (codeSize >> 8) & 0xff; | ||
buffer[index++] = (codeSize >> 16) & 0xff; | ||
buffer[index++] = (codeSize >> 24) & 0xff; | ||
if(stackIndex > 0) { | ||
// Write the current object size out | ||
// Pack the size of the total buffer length | ||
size = stackIndex >= 1 ? (index - currentObjectStored.index + 1) : | ||
currentObjectStored.endIndex - currentObjectStored.index + 16; | ||
// Write the string | ||
buffer.write(functionString, index, 'utf8'); | ||
// Update index | ||
index = index + codeSize - 1; | ||
// Write zero | ||
buffer[index++] = 0; | ||
// Serialize the scope object | ||
var scopeObjectBuffer = new Buffer(scopeSize); | ||
// Execute the serialization into a seperate buffer | ||
serializeObject(value.scope, checkKeys, scopeObjectBuffer, 0, serializeFunctions); | ||
// Adjusted scope Size (removing the header) | ||
var scopeDocSize = scopeSize - 4; | ||
// Write scope object size | ||
buffer[index++] = scopeDocSize & 0xff; | ||
buffer[index++] = (scopeDocSize >> 8) & 0xff; | ||
buffer[index++] = (scopeDocSize >> 16) & 0xff; | ||
buffer[index++] = (scopeDocSize >> 24) & 0xff; | ||
// Write the scopeObject into the buffer | ||
scopeObjectBuffer.copy(buffer, index, 0, scopeSize); | ||
// Adjust index, removing the empty size of the doc (5 bytes 0000000005) | ||
index = index + scopeDocSize - 5; | ||
// Write trailing zero | ||
buffer[index++] = 0; | ||
return index | ||
} else if(serializeFunctions) { | ||
buffer[index++] = BSON.BSON_DATA_CODE; | ||
// Encode the name | ||
index = index + buffer.write(name, index, 'utf8') + 1; | ||
buffer[index - 1] = 0; | ||
// Function string | ||
var functionString = value.toString(); | ||
// Function Size | ||
var size = Buffer.byteLength(functionString) + 1; | ||
// Write the size of the string to buffer | ||
buffer[currentObjectStored.index + 3] = (size >> 24) & 0xff; | ||
buffer[currentObjectStored.index + 2] = (size >> 16) & 0xff; | ||
buffer[currentObjectStored.index + 1] = (size >> 8) & 0xff; | ||
buffer[currentObjectStored.index] = size & 0xff; | ||
// Adjust and set null last parameter | ||
buffer[index++] = 0; | ||
// Pop off the stored object | ||
currentObjectStored = stack[--stackIndex]; | ||
keysIndex = currentObjectStored.keysIndex; | ||
keyLength = currentObjectStored.keyLength; | ||
} else { | ||
// Pack the size of the total buffer length | ||
size = buffer.length; | ||
// Write the size of the string to buffer | ||
buffer[3] = (size >> 24) & 0xff; | ||
buffer[2] = (size >> 16) & 0xff; | ||
buffer[1] = (size >> 8) & 0xff; | ||
buffer[0] = size & 0xff; | ||
// Set last buffer field to 0 | ||
buffer[buffer.length - 1] = 0; | ||
// return buffer; | ||
done = true; | ||
break; | ||
buffer[index++] = size & 0xff; | ||
buffer[index++] = (size >> 8) & 0xff; | ||
buffer[index++] = (size >> 16) & 0xff; | ||
buffer[index++] = (size >> 24) & 0xff; | ||
// Write the string | ||
buffer.write(functionString, index, 'utf8'); | ||
// Update index | ||
index = index + size - 1; | ||
// Write zero | ||
buffer[index++] = 0; | ||
return index; | ||
} | ||
} | ||
// If we passed in an index | ||
return index; | ||
} else { | ||
throw new Error("Not a valid object"); | ||
} | ||
// If no value to serialize | ||
return index; | ||
} | ||
@@ -779,0 +671,0 @@ |
@@ -131,2 +131,6 @@ var utils = require('./connection_utils'), | ||
var binaryCommand = command.toBinary() | ||
// console.log("=======================================================================") | ||
// console.dir(command.toBinary().toString('hex')) | ||
// console.dir(command.toBinary().toString('ascii')) | ||
if(this.logger != null && this.logger.doDebug) this.logger.debug("writing command to mongodb", binaryCommand); | ||
@@ -133,0 +137,0 @@ var r = this.writeSteam.write(binaryCommand); |
@@ -416,2 +416,6 @@ var QueryCommand = require('./commands/query_command').QueryCommand, | ||
this._executeQueryCommand(DbCommand.createGetNonceCommand(self), {onAll:true}, function(err, result, connection) { | ||
// console.log("------------------------------------------- nonce") | ||
// console.dir(err) | ||
// console.dir(result) | ||
// Execute on all the connections | ||
@@ -421,4 +425,10 @@ if(err == null) { | ||
var nonce = result.documents[0].nonce; | ||
// console.log("=============================================================") | ||
// console.dir(DbCommand.createAuthenticationCommand(self, username, password, nonce), {connection:connection}) | ||
// Execute command | ||
self._executeQueryCommand(DbCommand.createAuthenticationCommand(self, username, password, nonce), {connection:connection}, function(err, result) { | ||
// console.log("------------------------------------------- auth") | ||
// console.dir(err) | ||
// console.dir(result) | ||
// Ensure we save any error | ||
@@ -425,0 +435,0 @@ if(err) { |
@@ -10,2 +10,12 @@ | ||
[ 'bson/binary_parser' | ||
, 'bson/binary' | ||
, 'bson/code' | ||
, 'bson/db_ref' | ||
, 'bson/double' | ||
, 'bson/max_key' | ||
, 'bson/min_key' | ||
, 'bson/objectid' | ||
, 'bson/symbol' | ||
, 'bson/timestamp' | ||
, 'goog/math/long' | ||
, 'commands/base_command' | ||
@@ -12,0 +22,0 @@ , 'commands/db_command' |
{ "name" : "mongodb" | ||
, "description" : "A node.js driver for MongoDB" | ||
, "keywords" : ["mongodb", "mongo", "driver", "db"] | ||
, "version" : "0.9.7-3-1" | ||
, "version" : "0.9.7-3-2" | ||
, "author" : "Christian Amor Kvalheim <christkv@gmail.com>" | ||
@@ -6,0 +6,0 @@ , "contributors" : [ "Aaron Heckmann", |
@@ -51,8 +51,11 @@ Install | ||
To store and retrieve the non-JSON MongoDb primitives ([ObjectID](http://www.mongodb.org/display/DOCS/Object+IDs), Long, Binary, [Timestamp](http://www.mongodb.org/display/DOCS/Timestamp+data+type), [DBRef](http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-DBRef), Code), you have to use one of the types from the bson_serializer. | ||
To store and retrieve the non-JSON MongoDb primitives ([ObjectID](http://www.mongodb.org/display/DOCS/Object+IDs), Long, Binary, [Timestamp](http://www.mongodb.org/display/DOCS/Timestamp+data+type), [DBRef](http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-DBRef), Code). | ||
In particular, every document has a unique `_id` which can be almost any type, and by default a 12-byte ObjectID is created. ObjectIDs can be represented as 24-digit hexadecimal strings, but you must convert the string back into an ObjectID before you can use it in the database. For example: | ||
// Get the objectID type | ||
var ObjectID = require('mongodb').ObjectID; | ||
var idString = '4e4e1638c85e808431000003'; | ||
collection.findOne({_id: new client.bson_serializer.ObjectID(idString)}, console.log) // ok | ||
collection.findOne({_id: new ObjectID(idString)}, console.log) // ok | ||
collection.findOne({_id: idString}, console.log) // wrong! callback gets undefined | ||
@@ -62,9 +65,15 @@ | ||
var client = new Db(...); | ||
new client.bson_serializer.Long(numberString) | ||
new client.bson_serializer.ObjectID(hexString) | ||
new client.bson_serializer.Timestamp() // the actual unique number is generated on insert. | ||
new client.bson_serializer.DBRef(collectionName, id, dbName) | ||
new client.bson_serializer.Binary(buffer) // takes a string or Buffer | ||
new client.bson_serializer.Code(code, [context]) | ||
// Fetch the library | ||
var mongo = require('mongodb'); | ||
// Create new instances of BSON types | ||
new mongo.Long(numberString) | ||
new mongo.ObjectID(hexString) | ||
new mongo.Timestamp() // the actual unique number is generated on insert. | ||
new mongo.DBRef(collectionName, id, dbName) | ||
new mongo.Binary(buffer) // takes a string or Buffer | ||
new mongo.Code(code, [context]) | ||
new mongo.Symbol(string) | ||
new mongo.MinKey() | ||
new mongo.MaxKey() | ||
new mongo.Double(number) // Force double storage | ||
@@ -83,3 +92,3 @@ The C/C++ bson parser/serializer | ||
Since objects created using the C/C++ bson parser are incompatible with a client configured to use the Javascript bson parser and vice versa, you should call constructors using `client.bson_serializer` as described above (don't use `mongodb.BSONNative` and `mongodb.BSONPure` directly). | ||
The C++ parser uses the js objects both for serialization and deserialization. | ||
@@ -86,0 +95,0 @@ GitHub information |
@@ -109,3 +109,3 @@ var mongodb = process.env['TEST_NATIVE'] != null ? require('../lib/mongodb').native() : require('../lib/mongodb').pure(); | ||
fs_client.admin(function(err, adminDb) { | ||
adminDb.authenticate('admin', 'admin', function(err, replies) { | ||
adminDb.authenticate('admin', 'admin', function(err, replies) { | ||
adminDb.setProfilingLevel('slow_only', function(err, level) { | ||
@@ -112,0 +112,0 @@ adminDb.profilingLevel(function(err, level) { |
@@ -137,9 +137,2 @@ var mongodb = process.env['TEST_NATIVE'] != null ? require('../lib/mongodb').native() : require('../lib/mongodb').pure(); | ||
testAccessToSerializationObjects : function(test) { | ||
var db = new Db(MONGODB, new Server("127.0.0.1", mongodb.Connection.DEFAULT_PORT, {auto_reconnect: true, poolSize: 4, socketOptions:{keepAlive:100}, ssl:useSSL}),{native_parser: (process.env['TEST_NATIVE'] != null)}); | ||
test.ok(db.bson_serializer.ObjectID != null); | ||
test.ok(db.bson_deserializer.ObjectID != null); | ||
test.done(); | ||
}, | ||
noGlobalsLeaked : function(test) { | ||
@@ -146,0 +139,0 @@ var leaks = gleak.detectNew(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
409
233
1837323
31395