Permalink
Browse files

Working on 0.4.X and removed simple_emitter

  • Loading branch information...
christkv committed Mar 12, 2012
1 parent e57b585 commit a86cc88a24abf9c22ee200b14c7d9d3485542077
@@ -102,7 +102,7 @@ var serializedDoc = BSONPure.serialize(object, null, true);
// Read a test doc
// var bufferData = require('fs').readFileSync("/Users/christiankvalheim/coding/projects/node-mongodb-native/1325633340440_18.txt", 'ascii');
// Serialized doc
-// var serializedDoc = new Buffer(bufferData, 'hex');
+// var serializedDoc = new Buffer(bufferData, 'base64');
// console.dir(serializedDoc)
// var index = 0;
@@ -193,14 +193,14 @@ for(var i = 0 ; i < COUNT; i++) {
// console.log("========================================================== serialize")
// console.log("BSON.serialize :: ")
-// console.dir(BSON.serialize(object).toString('hex'))
+// console.dir(BSON.serialize(object).toString('base64'))
// console.log("BSONPure.serialize2 :: ")
-// console.dir(BSONPure.serialize2(object2, null, true).toString('hex'))
+// console.dir(BSONPure.serialize2(object2, null, true).toString('base64'))
// console.dir(BSONPure.serialize2(object2, null, true).toString('ascii'))
// console.log("BSONPure.serialize :: ")
-// console.dir(BSONPure.serialize(object, null, true).toString('hex'))
+// console.dir(BSONPure.serialize(object, null, true).toString('base64'))
// console.log("BSONNative.serialize :: ")
-// console.dir(BSONNative.serialize(object2, null, true).toString('hex'))
+// console.dir(BSONNative.serialize(object2, null, true).toString('base64'))
// console.dir(BSONNative.serialize(object2, null, true).toString('ascii'))
// // Serialize
@@ -212,8 +212,8 @@ for(var i = 0 ; i < COUNT; i++) {
// console.log("[" + a[i] + "] = [" + b[i] + "] :: " + (a[i] === b[i] ? 'true' : "FALSE FALSE FALSE"));
// }
//
-// assert.equal(BSONNative.serialize(object2, null, true).toString('hex'),
-// BSONPure.serialize2(object2, null, true).toString('hex'))
+// assert.equal(BSONNative.serialize(object2, null, true).toString('base64'),
+// BSONPure.serialize2(object2, null, true).toString('base64'))
//
//
// start = new Date
@@ -97,9 +97,9 @@ console.log(COUNT + "x BSONNative.calculateObjectSize2(object) time = ", end - s
// console.log("calculate3 = " + calculate3);
console.log("----------------------------------------------------------------------- serialize");
-console.log(serialize1.toString('hex'))
-console.log(serialize2.toString('hex'))
-console.log(serialize3.toString('hex'))
+console.log(serialize1.toString('base64'))
+console.log(serialize2.toString('base64'))
+console.log(serialize3.toString('base64'))
function compare(b1, b2) {
try {
@@ -310,7 +310,7 @@ 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'))
+assert.equal(simple_string_serialized_2.toString('base64'), simple_string_serialized.toString('base64'))
var doc1 = bsonJS.deserialize(simple_string_serialized_2);
var doc2 = bsonC.deserialize(simple_string_serialized);
assert.deepEqual(doc1, doc2)
@@ -321,7 +321,7 @@ var docJS = {_id: ObjectID.createFromHexString(hexId), 'funds.remaining': {$gte:
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'));
+assert.equal(docCBin.toString('base64'), docJSBin.toString('base64'));
// // Complex document serialization
// 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")}
@@ -85,7 +85,7 @@ console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/
// var _serializedDoc2 = bsonC.serialize2(doc, true, false);
// console.dir(_serializedDoc1);
// console.dir(_serializedDoc2);
-// assert.equal(_serializedDoc1.toString('hex'), _serializedDoc2.toString('hex'))
+// assert.equal(_serializedDoc1.toString('base64'), _serializedDoc2.toString('base64'))
//
//
// // Benchmark 1
@@ -40,7 +40,6 @@ var Connection = exports.Connection = function(id, socketOptions) {
Connection.DEFAULT_MAX_BSON_SIZE = 4 * 1024 * 1024 * 4 * 3;
// Inherit event emitter so we can emit stuff wohoo
-// inherits(Connection, SimpleEmitter);
inherits(Connection, EventEmitter);
Connection.prototype.start = function() {
@@ -2,11 +2,10 @@ var Connection = require('./connection').Connection,
DbCommand = require('../commands/db_command').DbCommand,
MongoReply = require('../responses/mongo_reply').MongoReply,
debug = require('util').debug,
- SimpleEmitter = require('./simple_emitter').SimpleEmitter,
+ EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits,
inspect = require('util').inspect,
Server = require('./server').Server,
- format = require('util').format,
PingStrategy = require('./strategies/ping_strategy').PingStrategy,
StatisticsStrategy = require('./strategies/statistics_strategy').StatisticsStrategy;
@@ -29,6 +28,9 @@ const STATE_ROLLBACK = 9;
*
*/
var ReplSetServers = exports.ReplSetServers = function(servers, options) {
+ // Set up event emitter
+ EventEmitter.call(this);
+ // Set up basic
if(!(this instanceof ReplSetServers)) return new ReplSetServers(server, options);
var self = this;
@@ -218,7 +220,7 @@ var ReplSetServers = exports.ReplSetServers = function(servers, options) {
});
};
-inherits(ReplSetServers, SimpleEmitter);
+inherits(ReplSetServers, EventEmitter);
// Allow setting the read preference at the replicaset level
ReplSetServers.prototype.setReadPreference = function(preference) {
@@ -399,7 +401,7 @@ ReplSetServers.prototype.connect = function(parent, options, callback) {
// Handle a closed connection
var closeHandler = function(err, server) {
var closeServers = function() {
- // Set the state to disconnected
+ // Set the state to disconnected
parent._state = 'disconnected';
// Shut down the replicaset for now and Fire off all the callbacks sitting with no reply
if(replSetSelf._serverState == 'connected') {
@@ -425,8 +427,8 @@ ReplSetServers.prototype.connect = function(parent, options, callback) {
// Check if this is the primary server, then disconnect otherwise keep going
if(replSetSelf._state.master != null) {
- var primaryAddress = format("%s:%s", replSetSelf._state.master.host, replSetSelf._state.master.port);
- var errorServerAddress = format("%s:%s", server.host, server.port);
+ var primaryAddress = replSetSelf._state.master.host + ":" + replSetSelf._state.master.port;
+ var errorServerAddress = server.host + ":" + server.port;
// Only shut down the set if we have a primary server error
if(primaryAddress == errorServerAddress) {
@@ -482,8 +484,8 @@ ReplSetServers.prototype.connect = function(parent, options, callback) {
// Check if this is the primary server, then disconnect otherwise keep going
if(replSetSelf._state.master != null) {
- var primaryAddress = format("%s:%s", replSetSelf._state.master.host, replSetSelf._state.master.port);
- var errorServerAddress = format("%s:%s", server.host, server.port);
+ var primaryAddress = replSetSelf._state.master.host + ":" + replSetSelf._state.master.port;
+ var errorServerAddress = server.host + ":" + server.port;
// Only shut down the set if we have a primary server error
if(primaryAddress == errorServerAddress) {
@@ -539,8 +541,8 @@ ReplSetServers.prototype.connect = function(parent, options, callback) {
// Check if this is the primary server, then disconnect otherwise keep going
if(replSetSelf._state.master != null) {
- var primaryAddress = format("%s:%s", replSetSelf._state.master.host, replSetSelf._state.master.port);
- var errorServerAddress = format("%s:%s", server.host, server.port);
+ var primaryAddress = replSetSelf._state.master.host + ":" + replSetSelf._state.master.port;
+ var errorServerAddress = server.host + ":" + server.port;
// Only shut down the set if we have a primary server error
if(primaryAddress == errorServerAddress) {
@@ -2,11 +2,14 @@ var Connection = require('./connection').Connection,
DbCommand = require('../commands/db_command').DbCommand,
MongoReply = require('../responses/mongo_reply').MongoReply,
ConnectionPool = require('./connection_pool').ConnectionPool,
- SimpleEmitter = require('./simple_emitter').SimpleEmitter,
+ EventEmitter = require('events').EventEmitter,
MongoReply = require("../responses/mongo_reply").MongoReply,
inherits = require('util').inherits;
var Server = exports.Server = function(host, port, options) {
+ // Set up event emitter
+ EventEmitter.call(this);
+ // Set up Server instance
if(!(this instanceof Server)) return new Server(host, port, options);
var self = this;
@@ -122,7 +125,7 @@ var Server = exports.Server = function(host, port, options) {
};
// Inherit simple event emitter
-inherits(Server, SimpleEmitter);
+inherits(Server, EventEmitter);
// Read Preferences
Server.READ_PRIMARY = 'primary';
Server.READ_SECONDARY = 'secondary';
@@ -424,7 +427,7 @@ Server.prototype.connect = function(dbInstance, options, callback) {
// Close the pool
connectionPool.stop(true);
// If we have a callback return the error
- if(typeof callback === 'function') {
+ if(typeof callback == 'function') {
// ensure no callbacks get called twice
var internalCallback = callback;
callback = null;
@@ -1,48 +0,0 @@
-var SimpleEmitter = exports.SimpleEmitter = function() {}
-//
-// My own simple synchronous emit support, We don't need the overhead of the built in flexible node.js
-// event emitter as we are looking for as low latency as possible.
-//
-SimpleEmitter.prototype.on = function(event, callback) {
- if(this.eventHandlers[event] == null) throw "Event handler only accepts values of " + Object.keys(this.eventHandlers);
- // Just add callback to our event handler (avoiding the cost of the node.js event handler)
- this.eventHandlers[event].push(callback);
-}
-
-SimpleEmitter.prototype.emit = function(event, err, object) {
- if(this.eventHandlers[event] == null) throw "Event handler only accepts values of " + Object.keys(this.eventHandlers);
- // Fire off all the callbacks
- var callbacks = this.eventHandlers[event];
- // Attemp to emit
- try {
- // Perform a callback on all the registered callback handlers
- for(var i = 0; i < callbacks.length; i++) {
- callbacks[i](err, object);
- }
- } catch (err) {
- this.emit("error", err);
- }
-}
-
-SimpleEmitter.prototype.listeners = function(event) {
- return this.eventHandlers[event];
-}
-
-SimpleEmitter.prototype.overrideListeners = function(event, listeners) {
- this.eventHandlers[event] = listeners;
-}
-
-SimpleEmitter.prototype.removeListeners = function(event) {
- if(this.eventHandlers[event] == null) throw "Event handler only accepts values of " + Object.keys(this.eventHandlers);
- // Throw away all handlers
- this.eventHandlers[event] = [];
-}
-
-SimpleEmitter.prototype.removeAllListeners = function() {
- // Fetch all the keys of handlers
- var keys = Object.keys(this.eventHandlers);
- // Remove all handlers
- for(var i = 0; i < keys.length; i++) {
- this.eventHandlers[keys[i]] = [];
- }
-}
View
@@ -89,7 +89,6 @@ function Db(databaseName, serverConfig, options) {
this.bson_deserializer = bsonLib;
this.bson_serializer = bsonLib;
} catch (err) {
- console.dir(err)
// If we tried to instantiate the native driver
throw "Native bson parser not compiled, please compile or avoid using native_parser=true";
}
@@ -713,7 +713,11 @@ exports.shouldCorrectlyCalculateMD5 = function(test) {
var gridStore2 = new GridStore(client, "new-file", "r");
gridStore2.open(function(err, gridStore) {
test.equal("6f5902ac237024bdd0c176cb93063dc4", gridStore.md5);
- gridStore.md5 = "can't do this";
+ try {
+ gridStore.md5 = "can't do this";
+ } catch(err) {
+ test.ok(err != null);
+ }
test.equal("6f5902ac237024bdd0c176cb93063dc4", gridStore.md5);
var gridStore2 = new GridStore(client, "new-file", "w");
@@ -530,7 +530,7 @@ exports.shouldCorrectlyWriteFileToGridStore= function(test) {
gridStore.open(function(err, gridStore) {
gridStore.writeFile('./test/gridstore/test_gs_weird_bug.png', function(err, doc) {
GridStore.read(client, 'test_gs_writing_file', function(err, fileData) {
- test.equal(data.toString('hex'), fileData.toString('hex'));
+ test.equal(data.toString('base64'), fileData.toString('base64'));
test.equal(fileSize, fileData.length);
// Ensure we have a md5
@@ -556,7 +556,7 @@ exports.shouldCorrectlyWriteFileToGridStoreUsingObjectId= function(test) {
gridStore.writeFile('./test/gridstore/test_gs_weird_bug.png', function(err, doc) {
GridStore.read(client, doc._id, function(err, fileData) {
- test.equal(data.toString('hex'), fileData.toString('hex'));
+ test.equal(data.toString('base64'), fileData.toString('base64'));
test.equal(fileSize, fileData.length);
// Ensure we have a md5
@@ -644,7 +644,7 @@ exports.shouldCorrectlyWriteAndReadJpgImage = function(test) {
// Read the entire file
gs2.read(function(err, data2) {
// Compare the file content against the orgiinal
- test.equal(data.toString('hex'), data2.toString('hex'));
+ test.equal(data.toString('base64'), data2.toString('base64'));
db.close();
test.done();
@@ -930,7 +930,7 @@ exports.shouldCorrectlySaveSimpleFileToGridStoreUsingWriteFile = function(test)
// Read back all the written content and verify the correctness
GridStore.read(db, fileId, function(err, fileData) {
- test.equal(data.toString('hex'), fileData.toString('hex'))
+ test.equal(data.toString('base64'), fileData.toString('base64'))
test.equal(fileSize, fileData.length);
db.close();
@@ -976,7 +976,7 @@ exports.shouldCorrectlySaveSimpleFileToGridStoreUsingWriteFileWithHandle = funct
// Read back all the written content and verify the correctness
GridStore.read(db, fileId, function(err, fileData) {
- test.equal(data.toString('hex'), fileData.toString('hex'));
+ test.equal(data.toString('base64'), fileData.toString('base64'));
test.equal(fileSize, fileData.length);
db.close();
@@ -73,7 +73,7 @@ exports.shouldPutFileCorrectlyToGridUsingObjectId = function(test) {
grid.put(originalData, {}, function(err, result) {
// Fetch the content
grid.get(result._id, function(err, data) {
- test.deepEqual(originalData.toString('hex'), data.toString('hex'));
+ test.deepEqual(originalData.toString('base64'), data.toString('base64'));
db.close();
test.done();
Oops, something went wrong.

0 comments on commit a86cc88

Please sign in to comment.