Skip to content

Commit

Permalink
Refactoring to remove dependencies on Integer from all code other tha…
Browse files Browse the repository at this point in the history
…n bson.js
  • Loading branch information
Christian Amor Kvalheim committed Aug 13, 2010
1 parent e0bd20f commit b235d12
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
21 changes: 20 additions & 1 deletion external-libs/bson/bson.cc
Expand Up @@ -65,6 +65,8 @@ void BSON::Initialize(v8::Handle<v8::Object> 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());
}
Expand Down Expand Up @@ -114,9 +116,26 @@ Handle<Value> BSON::BSONSerialize(const Arguments &args) {
return bin_value;
}

Handle<Value> 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<Value> argv[] = {args[0], args[1]};
Handle<Value> long_obj = Long::constructor_template->GetFunction()->NewInstance(2, argv);
return scope.Close(long_obj);
}

Handle<Value> 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<Value> BSON::EncodeLong(const Arguments &args) {
HandleScope scope;
printf("============================================= Handle<Value> 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");
Expand Down
2 changes: 2 additions & 0 deletions external-libs/bson/bson.h
Expand Up @@ -19,6 +19,8 @@ class BSON : public EventEmitter {

// Encode functions
static Handle<Value> EncodeLong(const Arguments &args);
static Handle<Value> ToLong(const Arguments &args);
static Handle<Value> ToInt(const Arguments &args);

private:
static Handle<Value> New(const Arguments &args);
Expand Down
10 changes: 10 additions & 0 deletions lib/mongodb/bson/bson.js
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions lib/mongodb/gridfs/gridstore.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 2 additions & 7 deletions lib/mongodb/responses/mongo_reply.js
Expand Up @@ -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));
Expand Down

0 comments on commit b235d12

Please sign in to comment.