diff --git a/external-libs/bson/bson.cc b/external-libs/bson/bson.cc index f8d4fbad39..ed1cd46516 100644 --- a/external-libs/bson/bson.cc +++ b/external-libs/bson/bson.cc @@ -65,6 +65,8 @@ void BSON::Initialize(v8::Handle target) { NODE_SET_METHOD(constructor_template->GetFunction(), "serialize", BSONSerialize); NODE_SET_METHOD(constructor_template->GetFunction(), "deserialize", BSONDeserialize); NODE_SET_METHOD(constructor_template->GetFunction(), "encodeLong", EncodeLong); + NODE_SET_METHOD(constructor_template->GetFunction(), "toLong", ToLong); + NODE_SET_METHOD(constructor_template->GetFunction(), "toInt", ToInt); target->Set(String::NewSymbol("BSON"), constructor_template->GetFunction()); } @@ -114,9 +116,26 @@ Handle BSON::BSONSerialize(const Arguments &args) { return bin_value; } +Handle BSON::ToLong(const Arguments &args) { + HandleScope scope; + + if(args.Length() != 2 && !args[0]->IsString() && !args[1]->IsString()) return VException("Two arguments of type String required"); + // Create a new Long value and return it + Local argv[] = {args[0], args[1]}; + Handle long_obj = Long::constructor_template->GetFunction()->NewInstance(2, argv); + return scope.Close(long_obj); +} + +Handle BSON::ToInt(const Arguments &args) { + HandleScope scope; + + if(args.Length() != 1 && !args[0]->IsNumber() && !args[1]->IsString()) return VException("One argument of type Number required"); + // Return int value + return scope.Close(args[0]->ToInt32()); +} + Handle BSON::EncodeLong(const Arguments &args) { HandleScope scope; - printf("============================================= Handle BSON::EncodeLong(const Arguments &args)\n"); // Encode the value if(args.Length() != 1 && !Long::HasInstance(args[0])) return VException("One argument required of type Long"); diff --git a/external-libs/bson/bson.h b/external-libs/bson/bson.h index cf409f3431..67d4080205 100644 --- a/external-libs/bson/bson.h +++ b/external-libs/bson/bson.h @@ -19,6 +19,8 @@ class BSON : public EventEmitter { // Encode functions static Handle EncodeLong(const Arguments &args); + static Handle ToLong(const Arguments &args); + static Handle ToInt(const Arguments &args); private: static Handle New(const Arguments &args); diff --git a/lib/mongodb/bson/bson.js b/lib/mongodb/bson/bson.js index 07aed54b76..e528dbc04f 100644 --- a/lib/mongodb/bson/bson.js +++ b/lib/mongodb/bson/bson.js @@ -416,6 +416,16 @@ BSON.checkKey = function(variable) { } }; +BSON.toLong = function(low_bits, high_bits) { + var low_bits = Integer.fromInt(low_bits); + var high_bits = Integer.fromInt(high_bits); + return new Long(low_bits, high_bits); +} + +BSON.toInt = function(value) { + return Integer.fromNumber(value).toInt(); +} + BSON.encodeValue = function(encoded_string, variable, value, top_level, checkKeys) { var variable_encoded = variable == null ? '' : BinaryParser.encode_cstring(variable); if(checkKeys && variable != null)BSON.checkKey(variable); diff --git a/lib/mongodb/gridfs/gridstore.js b/lib/mongodb/gridfs/gridstore.js index ac5e7bbdfd..6e958a5f59 100644 --- a/lib/mongodb/gridfs/gridstore.js +++ b/lib/mongodb/gridfs/gridstore.js @@ -213,7 +213,8 @@ GridStore.prototype.nthChunk = function(chunkNumber, callback) { }; GridStore.prototype.lastChunkNumber = function() { - return Integer.fromNumber((self.length/self.chunkSize)).toInt(); + //Integer.fromNumber((self.length/self.chunkSize)).toInt() + return this.db.bson_serializer.BSON.toInt((self.length/self.chunkSize)); }; GridStore.prototype.chunkCollection = function(callback) { @@ -331,7 +332,8 @@ GridStore.prototype.seek = function(position, seekLocation, callback) { targetPosition = finalPosition; } - var newChunkNumber = Integer.fromNumber((targetPosition/self.chunkSize)).toInt(); + // var newChunkNumber = Integer.fromNumber((targetPosition/self.chunkSize)).toInt(); + var newChunkNumber = this.db.bson_serializer.BSON.toInt((targetPosition/self.chunkSize)); if(newChunkNumber != self.currentChunk.chunkNumber) { if(self.mode[0] == 'w') { self.currentChunk.save(function(err, chunk) { diff --git a/lib/mongodb/responses/mongo_reply.js b/lib/mongodb/responses/mongo_reply.js index 766b6bb097..f424018873 100644 --- a/lib/mongodb/responses/mongo_reply.js +++ b/lib/mongodb/responses/mongo_reply.js @@ -20,14 +20,9 @@ var MongoReply = exports.MongoReply = function(db, binary_reply) { index = index + 4 + 4; // Unpack the reply message this.responseFlag = BinaryParser.toInt(binary_reply.substr(index, 4)); - index = index + 4; - - throw "===================================================================== NEED TO FAIL HERE"; - + index = index + 4; // Unpack the cursor id (a 64 bit long integer) - var low_bits = Integer.fromInt(BinaryParser.toInt(binary_reply.substr(index, 4))); - var high_bits = Integer.fromInt(BinaryParser.toInt(binary_reply.substr(index + 4, 4))); - this.cursorId = new db.bson_serializer.Long(low_bits, high_bits); + this.cursorId = new db.bson_serializer.BSON.toLong(BinaryParser.toInt(binary_reply.substr(index, 4)), BinaryParser.toInt(binary_reply.substr(index + 4, 4))); index = index + 8; // Unpack the starting from this.startingFrom = BinaryParser.toInt(binary_reply.substr(index, 4));