1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-12 02:42:48 +00:00

Added files

This commit is contained in:
2015-06-25 16:28:41 -05:00
parent 656dca9289
commit eb27b55a54
5621 changed files with 1630154 additions and 0 deletions

15
mongoui/mongoui-master/node_modules/monk/.npmignore generated vendored Normal file
View File

@@ -0,0 +1,15 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
pids
logs
results
node_modules
npm-debug.log

5
mongoui/mongoui-master/node_modules/monk/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.6
- 0.8
services: mongodb

137
mongoui/mongoui-master/node_modules/monk/History.md generated vendored Normal file
View File

@@ -0,0 +1,137 @@
0.7.1 / 2013-03-03
==================
* promise: expose `query`
0.7.0 / 2012-10-30
==================
*: bumped `mongoskin` and therefore `node-mongodb-native`
0.6.0 / 2012-10-29
==================
* collection: added cursor closing support
* promise: introduce #destroy
* test: added cursor destroy test
0.5.0 / 2012-10-03
==================
* promise: added opts to constructor
* util: fix field negation
* test: added test for promise options
* collection: pass options to promises
0.4.0 / 2012-10-03
==================
* added travis
* manager: added Manager#id and Manager#oid
* collection: introduced Collection#oid
* manager: added Manager#col
0.3.0 / 2012-09-06
==================
* collection: make `findAndModify` accept an oid as the query
0.2.1 / 2012-07-14
==================
* collection: fixed streaming when options are not supplied
0.2.0 / 2012-07-14
==================
* collection: added `count`
0.1.15 / 2012-07-14
===================
* collection: avoid mongoskin warn when buffering commands
0.1.14 / 2012-07-09
===================
* Use any debug. [visionmedia]
* Use any mocha. [visionmedia]
0.1.13 / 2012-05-28
===================
* Fixed string-based field selection.
0.1.12 / 2012-05-25
===================
* Added package.json tags.
* Added support for update with ids (fixes #4)
0.1.11 / 2012-05-22
===================
* Added support for new objectids through `Collection#id`
0.1.10 / 2012-05-21
===================
* Enhanced findAndModify default behavior for upserts.
* Fixed findAndModify.
0.1.9 / 2012-05-16
==================
* Bumped mongoskin
0.1.8 / 2012-05-12
==================
* Fixed mongoskin version
* Improved options docs section.
0.1.7 / 2012-05-08
==================
* Added global and collection-level options.
* Enabled safe mode by default.
* Improved error handling in tests.
* Fixed `update` callback with safe: false.
0.1.6 / 2012-05-06
==================
* Added tests for `findById`.
0.1.5 / 2012-05-06
==================
* Added `Collection` references to `Promise`s.
* Fixed `findAndModify`.
0.1.4 / 2012-05-06
==================
* Ensured insert calls back with a single object.
* Ensured `insert` resolves promise in next tick.
0.1.3 / 2012-05-03
==================
* Exposed `util`
0.1.2 / 2012-05-03
==================
* Make `Collection` inherit from `EventEmitter`.
0.1.1 / 2012-04-27
==================
* Added `updateById`.
0.1.0 / 2012-04-23
==================
* Initial release.

14
mongoui/mongoui-master/node_modules/monk/Makefile generated vendored Normal file
View File

@@ -0,0 +1,14 @@
TESTS = test/*.js
REPORTER = dot
test:
@DEBUG=$DEBUG,monk,monk:queries ./node_modules/.bin/mocha \
--require test/common.js \
--reporter $(REPORTER) \
--growl \
--bail \
--slow 1000 \
$(TESTS)
.PHONY: test bench

224
mongoui/mongoui-master/node_modules/monk/README.md generated vendored Normal file
View File

@@ -0,0 +1,224 @@
# monk
[![build status](https://secure.travis-ci.org/LearnBoost/monk.png?branch=master)](https://secure.travis-ci.org/LearnBoost/monk)
Monk is a tiny layer that provides simple yet substantial usability
improvements for MongoDB usage within Node.JS.
```js
var db = require('monk')('localhost/mydb')
, users = db.get('users')
users.index('name last');
users.insert({ name: 'Tobi', bigdata: {} });
users.find({ name: 'Loki' }, '-bigdata', function () {
// exclude bigdata field
});
```
## Features
- Command buffering. You can start querying right away.
- Promises built-in for all queries. Easy interoperability with modules.
- Easy connections / configuration
- Well-designed signatures
- Improvements to the MongoDB APIs (eg: `findAndModify` supports the
`update` signature style)
- Auto-casting of `_id` in queries
- Builds on top of [mongoskin](http://github.com/kissjs/node-mongoskin)
- Allows to set global options or collection-level options for queries. (eg:
`safe` is `true` by default for all queries)
## How to use
### Connecting
#### Single server
```js
var db = require('monk')('localhost/mydb')
```
#### Replica set
```js
var db = require('monk')('localhost/mydb,192.168.1.1')
```
### Collections
#### Getting one
```js
var users = db.get('users')
// users.insert(), users.update() … (see below)
```
#### Dropping
```js
users.drop(fn);
```
### Signatures
- All commands accept the simple `data[, …], fn`. For example
- `find({}, fn)`
- `findOne({}, fn)`
- `update({}, {}, fn)` `findAndModify({}, {}, fn)`
- `findById('id', fn)`
- You can pass options in the middle: `data[, …], options, fn`
- You can pass fields to select as an array: `data[, …], ['field', …], fn`
- You can pass fields as a string delimited by spaces:
`data[, …], 'field1 field2', fn`
- To exclude a field, prefix the field name with '-':
`data[, …], '-field1', fn`
### Promises
All methods that perform an async action return a promise.
```js
var promise = users.insert({});
promise.type; // 'insert' in this case
promise.error(function(err){});
promise.on('error', function(err){});
promise.on('success', function(doc){});
promise.on('complete', function(err, doc){});
promise.success(function(doc){});
```
### Indexes
```js
users.index('name.first', fn);
users.index('email', { unique: true }); // unique
users.index('name.first name.last') // compound
users.index({ 'email': 1, 'password': -1 }); // compound with sort
users.index('email', { sparse: true }, fn); // with options
users.indexes(fn); // get indexes
users.dropIndex(name, fn); // drop an index
users.dropIndexes(fn); // drop all indexes
```
### Inserting
```js
users.insert({ a: 'b' }, function (err, doc) {
if (err) throw err;
});
```
### Casting
To cast to `ObjectId`:
```js
users.id() // returns new generated ObjectID
users.id('hexstring') // returns ObjectId
users.id(obj) // returns ObjectId
```
### Updating
```js
users.update({}, {}, fn);
users.updateById('id', {}, fn);
```
### Finding
#### Many
```js
users.find({}, function (err, docs){});
```
#### By ID
```js
users.findById('hex representation', function(err, doc){});
users.findById(oid, function(err, doc){});
```
#### Single doc
`findOne` also provides the `findById` functionality.
```js
users.findOne({ name: 'test' }).on('success', function (doc) {});
```
#### And modify
```js
users.findAndModify({ query: {}, update: {} });
users.findAndModify({ _id: '' }, { $set: {} });
```
#### Streaming
Note: `stream: true` is optional if you register an `each` handler in the
same tick. In the following example I just include it for extra clarity.
```js
users.find({}, { stream: true })
.each(function(doc){})
.error(function(err){})
.success(function(){});
```
##### Destroying a cursor
On the returned promise you can call `destroy()`. Upon the cursor
closing the `success` event will be emitted.
### Global options
```js
var db = require('monk')('localhost/mydb')
db.options.multi = true; // global multi-doc update
db.get('users').options.multi = false; // collection-level
```
Monk sets `safe` to `true` by default.
### Query debugging
If you wish to see what queries `monk` passes to the driver, simply leverage
[debug](http://github.com/visionmedia/debug):
```bash
DEBUG="monk:queries"
```
To see all debugging output:
```bash
DEBUG="monk:*"
```
## License
(The MIT License)
Copyright (c) 2012 Guillermo Rauch <guillermo@learnboost.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,9 @@
var monk = require('..');
var db = monk('localhost:27017/local');
var oplog = db.get('oplog.rs');
oplog.find({}, { tailable: true, awaitData: true })
.each(function(){
console.log(arguments);
})

View File

@@ -0,0 +1,526 @@
/**
* Module dependencies.
*/
var util = require('./util')
, debug = require('debug')('monk:queries')
, EventEmitter = require('events').EventEmitter
, Promise = require('./promise');
/**
* Module exports
*/
module.exports = Collection;
/**
* Collection.
*
* @api public
*/
function Collection (manager, name) {
this.manager = manager;
this.driver = manager.driver;
this.name = name;
this.col = this.driver.collection(name);
this.options = {};
this.col.emitter.setMaxListeners(Infinity);
}
/**
* Inherits from EventEmitter.
*/
Collection.prototype.__proto__ = EventEmitter.prototype;
/**
* Casts to objectid
*
* @param {Mixed} hex id or ObjectId
* @return {ObjectId}
* @api public
*/
Collection.prototype.id =
Collection.prototype.oid = function (str) {
if (null == str) return this.col.ObjectID();
return 'string' == typeof str ? this.col.id(str) : str;
};
/**
* Opts utility.
*/
Collection.prototype.opts = function (opts) {
opts = util.options(opts || {});
for (var i in this.manager.options) {
if (!(i in opts) && !(i in this.options)) {
opts[i] = this.manager.options[i];
}
}
for (var i in this.options) {
if (!(i in opts)) {
opts[i] = this.options[i];
}
}
return opts;
};
/**
* Set up indexes.
*
* @param {Object|String|Array} fields
* @param {Object|Function} optional, options or callback
* @param {Function} optional, callback
* @return {Promise}
* @api public
*/
Collection.prototype.index =
Collection.prototype.ensureIndex = function (fields, opts, fn) {
if ('function' == typeof opts) {
fn = opts;
opts = {};
}
var fields = util.fields(fields)
, opts = opts || {}
, promise = new Promise(this, 'index');
if (fn) {
promise.complete(fn);
}
// query
debug('%s ensureIndex %j (%j)', this.name, fields, opts);
this.col.ensureIndex(fields, opts, promise.fulfill);
return promise;
};
/**
* Gets all indexes.
*
* @param {Function} callback
* @return {Promise}
* @api public
*/
Collection.prototype.indexes = function (fn) {
var promise = new Promise(this, 'indexes');
if (fn) {
promise.complete(fn);
}
// query
debug('%s indexInformation', this.name);
this.col.indexInformation(promise.fulfill);
return promise;
};
/**
* update
*
* @param {Object} search query
* @param {Object} update obj
* @param {Object|String|Array} optional, options or fields
* @param {Function} callback
* @return {Promise}
* @api public
*/
Collection.prototype.update = function (search, update, opts, fn) {
if ('string' == typeof search || 'function' == typeof search.toHexString) {
return this.update({ _id: search }, update, opts, fn);
}
if ('function' == typeof opts) {
fn = opts;
opts = {};
}
opts = this.opts(opts);
var promise = new Promise(this, 'update', opts);
if (fn) {
promise.on('complete', fn);
}
// cast
search = this.cast(search);
update = this.cast(update);
// query
var callback = opts.safe ? promise.fulfill : function () {
// node-mongodb-native will send err=undefined and call the fn
// in the same tick if safe: false
var args = arguments;
args[0] = args[0] || null;
process.nextTick(function () {
promise.fulfill.apply(promise, args);
});
};
debug('%s update %j with %j', this.name, search, update);
promise.query = { query: search, update: update };
this.col.update(search, update, opts, callback);
return promise;
};
/**
* update by id helper
*
* @param {String|Object} object id
* @param {Object} update obj
* @param {Object|String|Array} optional, options or fields
* @param {Function} callback
* @return {Promise}
* @api public
*/
Collection.prototype.updateById = function (id, obj, opts, fn) {
return this.update({ _id: id }, obj, opts, fn);
};
/**
* remove
*
* @param {Object} search query
* @param {Object|Function} optional, options or callback
* @param {Function} optional, callback
* @return {Promise}
*/
Collection.prototype.remove = function (search, opts, fn) {
if ('function' == typeof opts) {
fn = opts;
opts = {};
}
opts = this.opts(opts);
var promise = new Promise(this, 'remove', opts);
if (fn) {
promise.on('complete', fn);
}
// cast
search = this.cast(search);
// query
debug('%s remove %j with %j', this.name, search, opts);
promise.query = search;
this.col.remove(search, opts, promise.fulfill);
return promise;
};
/**
* findAndModify
*
* @param {Object} search query, or { query, update } object
* @param {Object} optional, update object
* @param {Object|String|Array} optional, options or fields
* @param {Function} callback
* @return {Promise}
* @api public
*/
Collection.prototype.findAndModify = function (query, update, opts, fn) {
query = query || {};
if ('object' != typeof query.query && 'object' != typeof query.update) {
query = {
query: query
, update: update
};
} else {
opts = update;
fn = opts;
}
if ('string' == typeof query.query || 'function' == typeof query.query.toHexString) {
query.query = { _id: query.query };
}
if ('function' == typeof opts) {
fn = opts;
opts = {};
}
opts = opts || {};
// `new` defaults to `true` for upserts
if (null == opts.new && opts.upsert) {
opts.new = true;
}
var promise = new Promise(this, 'findAndModify', opts);
if (fn) {
promise.on('complete', fn);
}
// cast
query.query = this.cast(query.query);
query.update = this.cast(query.update);
// query
debug('%s findAndModify %j with %j', this.name, query.query, query.update);
promise.query = query;
this.col.findAndModify(
query.query
, []
, query.update
, this.opts(opts)
, promise.fulfill
);
return promise;
};
/**
* insert
*
* @param {Object} data
* @param {Object|String|Array} optional, options or fields
* @param {Function} callback
* @return {Promise}
* @api public
*/
Collection.prototype.insert = function (data, opts, fn) {
if ('function' == typeof opts) {
fn = opts;
opts = {};
}
opts = this.opts(opts);
var promise = new Promise(this, 'insert', opts);
if (fn) {
promise.complete(fn);
}
// cast
data = this.cast(data);
// query
debug('%s insert %j', this.name, data);
promise.query = data;
this.col.insert(data, opts, function (err, docs) {
process.nextTick(function () {
promise.fulfill.call(promise, err, docs ? docs[0] : docs);
});
});
return promise;
};
/**
* findOne by ID
*
* @param {String} hex id
* @param {Object|String|Array} optional, options or fields
* @param {Function} completion callback
* @return {Promise}
* @api public
*/
Collection.prototype.findById = function (id, opts, fn) {
return this.findOne({ _id: id }, opts, fn);
};
/**
* find
*
* @param {Object} query
* @param {Object|String|Array} optional, options or fields
* @param {Function} completion callback
* @return {Promise}
* @api public
*/
Collection.prototype.find = function (query, opts, fn) {
if ('function' == typeof opts) {
fn = opts;
opts = {};
}
// cast
query = this.cast(query);
// opts
opts = this.opts(opts);
// query
debug('%s find %j', this.name, query);
var cursor = this.col.find(query, opts);
// promise
var promise = new Promise(this, 'find', opts);
promise.query = query;
if (fn) {
promise.complete(fn);
}
if (null == opts.stream) {
process.nextTick(function () {
if (promise.listeners('each').length) {
stream();
} else {
cursor.toArray(promise.fulfill);
}
});
} else if (opts.stream) {
stream();
} else {
cursor.toArray(promise.fulfill);
}
function stream () {
var didClose = false;
cursor.each(function (err, doc) {
if (didClose && 'Cursor is closed' == err.message) {
// emit success
err = doc = null;
}
if (err) {
promise.emit('error', err);
} else if (doc) {
promise.emit('each', doc);
} else {
promise.emit('success');
}
});
promise.once('destroy', function(){
didClose = true;
cursor.cursor.close();
});
}
return promise;
};
/**
* count
*
* @param {Object} query
* @param {Function} completion callback
* @return {Promise}
* @api public
*/
Collection.prototype.count = function (query, fn) {
var promise = new Promise(this, 'find');
if (fn) {
promise.complete(fn);
}
// cast
query = this.cast(query);
// query
debug('%s count %j', this.name, query);
promise.query = query;
this.col.count(query, promise.fulfill);
return promise;
};
/**
* findOne
*
* @param {String|ObjectId|Object} query
* @param {Object} options
* @param {Function} completion callback
* @return {Promise}
* @api public
*/
Collection.prototype.findOne = function (search, opts, fn) {
search = search || {};
if ('string' == typeof search || 'function' == typeof search.toHexString) {
return this.findById(search, opts, fn);
}
if ('function' == typeof opts) {
fn = opts;
opts = {};
}
opts = this.opts(opts);
var promise = new Promise(this, 'findOne', opts);
if (fn) {
promise.complete(fn);
}
// cast
search = this.cast(search);
// query
debug('%s findOne %j', this.name, search);
promise.query = search;
this.col.findOne(search, opts, promise.fulfill);
return promise;
};
/**
* Applies ObjectId casting to _id fields.
*
* @param {Object} optional, query
* @return {Object} query
* @api private
*/
Collection.prototype.cast = function (obj) {
obj = obj || {};
if (obj._id) {
obj._id = this.id(obj._id);
}
if (obj.$set && obj.$set._id) {
obj.$set._id = this.id(obj.$set._id);
}
return obj;
};
/**
* Drops the collection.
*
* @param {Function} optional, callback
* @return {Promise}
* @api public
*/
Collection.prototype.drop = function (fn) {
var promise = new Promise(this, 'drop');
if (fn) {
promise.complete(fn);
}
debug('%s drop', this.name);
promise.query = this.name;
this.col.drop(promise.fulfill);
return promise;
};

121
mongoui/mongoui-master/node_modules/monk/lib/manager.js generated vendored Normal file
View File

@@ -0,0 +1,121 @@
/**
* Module dependencies.
*/
var mongoskin = require('mongoskin')
, debug = require('debug')('monk:manager')
, Collection = require('./collection')
, ObjectId = mongoskin.ObjectID
, EventEmitter = require('events').EventEmitter;
/**
* Module exports.
*/
module.exports = Manager;
/**
* Manager constructor.
*
* @param {Array|String} connection uri. replica sets can be an array or
* comma-separated
* @param {Object|Function} options or connect callback
* @param {Function} connect callback
*/
function Manager (uri, opts, fn) {
if (!(this instanceof Manager)) {
return new Manager(uri, opts, fn);
}
if ('function' == typeof opts) {
fn = opts;
opts = {};
}
opts = opts || {};
opts.safe = true;
if (Array.isArray(uri) || ~uri.indexOf(',')) {
if ('string' == typeof uri) {
uri = uri.split(',');
}
if (!opts.database) {
for (var i = 0, l = uri.length; i < l; i++) {
if (!opts.database) {
opts.database = uri[i].replace(/([^\/])+\/?/, '');
}
uri[i] = uri[i].replace(/\/.*/, '');
}
}
debug('repl set connection "%j" to database "%s"', uri, opts.database);
}
this.driver = mongoskin.db(uri, opts);
this.driver.open(this.onOpen.bind(this));
this.collections = {};
this.options = { safe: true };
if (fn) {
this.once('open', fn);
}
}
/**
* Inherits from EventEmitter
*/
Manager.prototype.__proto__ = EventEmitter.prototype;
/**
* Open callback.
*
* @api private
*/
Manager.prototype.onOpen = function () {
this.emit('open');
};
/**
* Closes the connection.
*
* @return {Manager} for chaining
* @api private
*/
Manager.prototype.close = function (fn) {
this.driver.close(fn);
return this;
};
/**
* Gets a collection.
*
* @return {Collection} collection to query against
* @api private
*/
Manager.prototype.col =
Manager.prototype.get = function (name) {
if (!this.collections[name]) {
this.collections[name] = new Collection(this, name);
}
return this.collections[name];
};
/**
* Casts to objectid
*
* @param {Mixed} hex id or ObjectId
* @return {ObjectId}
* @api public
*/
Manager.prototype.id =
Manager.prototype.oid = function (str) {
if (null == str) return ObjectId();
return 'string' == typeof str ? ObjectId.createFromHexString(str) : str;
};

30
mongoui/mongoui-master/node_modules/monk/lib/monk.js generated vendored Normal file
View File

@@ -0,0 +1,30 @@
/**
* Module exports.
*/
module.exports = exports = require('./manager');
/**
* Expose Collection.
*
* @api public
*/
exports.Collection = require('./collection');
/**
* Expose Promise.
*
* @api public
*/
exports.Promise = require('./promise');
/**
* Expose util.
*
* @api public
*/
exports.util = require('./util');

View File

@@ -0,0 +1,95 @@
/**
* Module dependencies.
*/
var EventEmitter = require('events').EventEmitter;
/**
* Module exports.
*/
module.exports = Promise;
/**
* Promise constructor.
*
* @param {Collection} collection
* @param {String} type
* @param {Object} query options
* @api public
*/
function Promise (col, type, opts) {
this.col = col;
this.type = type;
this.completed = false;
this.opts = opts || {};
// complete event
var self = this;
this.once('error', function (err) {
if (self.completed) return;
self.emit('complete', err);
});
this.once('success', function (data) {
if (self.completed) return;
self.emit('complete', null, data);
process.nextTick(function(){
// null the query ref
delete self.query;
});
});
// for practical purposes
this.fulfill = this.fulfill.bind(this);
}
/**
* Inherits from EventEmitter.
*/
Promise.prototype.__proto__ = EventEmitter.prototype;
/**
* Map methods to events.
*/
['each', 'error', 'success', 'complete'].forEach(function (method) {
Promise.prototype[method] = function (fn) {
if (fn) {
this.on(method, fn);
}
return this;
};
});
/**
* Fulfills the promise.
*
* @api public
*/
Promise.prototype.fulfill = function (err, data) {
this.fulfilled = true;
if (err) {
this.emit('error', err);
} else {
this.emit('success', data);
}
};
/**
* Destroys the promise.
*
* @api public
*/
Promise.prototype.destroy = function(){
this.emit('destroy');
var self = this;
process.nextTick(function(){
// null the query ref
delete self.query;
});
};

44
mongoui/mongoui-master/node_modules/monk/lib/util.js generated vendored Normal file
View File

@@ -0,0 +1,44 @@
/**
* Parses all the possible ways of expressing fields.
*
* @param {String|Object|Array} fields
* @return {Object} fields in object format
* @api public
*/
exports.fields = function (obj) {
if (!Array.isArray(obj) && 'object' == typeof obj) {
return obj;
}
var fields = {};
obj = 'string' == typeof obj ? obj.split(' ') : (obj || []);
for (var i = 0, l = obj.length; i < l; i++) {
if ('-' == obj[i][0]) {
fields[obj[i].substr(1)] = 0;
} else {
fields[obj[i]] = 1;
}
}
return fields;
};
/**
* Parses an object format.
*
* @param {String|Array|Object} fields or options
* @return {Object} options
* @api public
*/
exports.options = function (opts) {
if ('string' == typeof opts || Array.isArray(opts)) {
return { fields: exports.fields(opts) };
}
opts = opts || {};
opts.fields = exports.fields(opts.fields);
return opts;
};

View File

@@ -0,0 +1,115 @@
# debug
tiny node.js debugging utility modelled after node core's debugging technique.
## Installation
```
$ npm install debug
```
## Usage
With `debug` you simply invoke the exported function to generate your debug function, passing it a name which will determine if a noop function is returned, or a decorated `console.error`, so all of the `console` format string goodies you're used to work fine. A unique color is selected per-function for visibility.
Example _app.js_:
```js
var debug = require('debug')('http')
, http = require('http')
, name = 'My App';
// fake app
debug('booting %s', name);
http.createServer(function(req, res){
debug(req.method + ' ' + req.url);
res.end('hello\n');
}).listen(3000, function(){
debug('listening');
});
// fake worker of some kind
require('./worker');
```
Example _worker.js_:
```js
var debug = require('debug')('worker');
setInterval(function(){
debug('doing some work');
}, 1000);
```
The __DEBUG__ environment variable is then used to enable these based on space or comma-delimited names. Here are some examples:
![debug http and worker](http://f.cl.ly/items/18471z1H402O24072r1J/Screenshot.png)
![debug worker](http://f.cl.ly/items/1X413v1a3M0d3C2c1E0i/Screenshot.png)
## Millisecond diff
When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the "+NNNms" will show you how much time was spent between calls.
![](http://f.cl.ly/items/2i3h1d3t121M2Z1A3Q0N/Screenshot.png)
When stderr is not a TTY, `Date#toUTCString()` is used, making it more useful for logging the debug information as shown below:
_(NOTE: Debug now uses stderr instead of stdout, so the correct shell command for this example is actually `DEBUG=* node example/worker 2> out &`)_
![](http://f.cl.ly/items/112H3i0e0o0P0a2Q2r11/Screenshot.png)
## Conventions
If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser".
## Wildcards
The "*" character may be used as a wildcard. Suppose for example your library has debuggers named "connect:bodyParser", "connect:compress", "connect:session", instead of listing all three with `DEBUG=connect:bodyParser,connect.compress,connect:session`, you may simply do `DEBUG=connect:*`, or to run everything using this module simply use `DEBUG=*`.
You can also exclude specific debuggers by prefixing them with a "-" character. For example, `DEBUG=* -connect:*` would include all debuggers except those starting with "connect:".
## Browser support
Debug works in the browser as well, currently persisted by `localStorage`. For example if you have `worker:a` and `worker:b` as shown below, and wish to debug both type `debug.enable('worker:*')` in the console and refresh the page, this will remain until you disable with `debug.disable()`.
```js
a = debug('worker:a');
b = debug('worker:b');
setInterval(function(){
a('doing some work');
}, 1000);
setInterval(function(){
a('doing some work');
}, 1200);
```
## License
(The MIT License)
Copyright (c) 2011 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,137 @@
/**
* Expose `debug()` as the module.
*/
module.exports = debug;
/**
* Create a debugger with the given `name`.
*
* @param {String} name
* @return {Type}
* @api public
*/
function debug(name) {
if (!debug.enabled(name)) return function(){};
return function(fmt){
fmt = coerce(fmt);
var curr = new Date;
var ms = curr - (debug[name] || curr);
debug[name] = curr;
fmt = name
+ ' '
+ fmt
+ ' +' + debug.humanize(ms);
// This hackery is required for IE8
// where `console.log` doesn't have 'apply'
window.console
&& console.log
&& Function.prototype.apply.call(console.log, console, arguments);
}
}
/**
* The currently active debug mode names.
*/
debug.names = [];
debug.skips = [];
/**
* Enables a debug mode by name. This can include modes
* separated by a colon and wildcards.
*
* @param {String} name
* @api public
*/
debug.enable = function(name) {
try {
localStorage.debug = name;
} catch(e){}
var split = (name || '').split(/[\s,]+/)
, len = split.length;
for (var i = 0; i < len; i++) {
name = split[i].replace('*', '.*?');
if (name[0] === '-') {
debug.skips.push(new RegExp('^' + name.substr(1) + '$'));
}
else {
debug.names.push(new RegExp('^' + name + '$'));
}
}
};
/**
* Disable debug output.
*
* @api public
*/
debug.disable = function(){
debug.enable('');
};
/**
* Humanize the given `ms`.
*
* @param {Number} m
* @return {String}
* @api private
*/
debug.humanize = function(ms) {
var sec = 1000
, min = 60 * 1000
, hour = 60 * min;
if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
if (ms >= min) return (ms / min).toFixed(1) + 'm';
if (ms >= sec) return (ms / sec | 0) + 's';
return ms + 'ms';
};
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
debug.enabled = function(name) {
for (var i = 0, len = debug.skips.length; i < len; i++) {
if (debug.skips[i].test(name)) {
return false;
}
}
for (var i = 0, len = debug.names.length; i < len; i++) {
if (debug.names[i].test(name)) {
return true;
}
}
return false;
};
/**
* Coerce `val`.
*/
function coerce(val) {
if (val instanceof Error) return val.stack || val.message;
return val;
}
// persist
try {
if (window.localStorage) debug.enable(localStorage.debug);
} catch(e){}

View File

@@ -0,0 +1,5 @@
if ('undefined' == typeof window) {
module.exports = require('./lib/debug');
} else {
module.exports = require('./debug');
}

View File

@@ -0,0 +1,147 @@
/**
* Module dependencies.
*/
var tty = require('tty');
/**
* Expose `debug()` as the module.
*/
module.exports = debug;
/**
* Enabled debuggers.
*/
var names = []
, skips = [];
(process.env.DEBUG || '')
.split(/[\s,]+/)
.forEach(function(name){
name = name.replace('*', '.*?');
if (name[0] === '-') {
skips.push(new RegExp('^' + name.substr(1) + '$'));
} else {
names.push(new RegExp('^' + name + '$'));
}
});
/**
* Colors.
*/
var colors = [6, 2, 3, 4, 5, 1];
/**
* Previous debug() call.
*/
var prev = {};
/**
* Previously assigned color.
*/
var prevColor = 0;
/**
* Is stdout a TTY? Colored output is disabled when `true`.
*/
var isatty = tty.isatty(2);
/**
* Select a color.
*
* @return {Number}
* @api private
*/
function color() {
return colors[prevColor++ % colors.length];
}
/**
* Humanize the given `ms`.
*
* @param {Number} m
* @return {String}
* @api private
*/
function humanize(ms) {
var sec = 1000
, min = 60 * 1000
, hour = 60 * min;
if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
if (ms >= min) return (ms / min).toFixed(1) + 'm';
if (ms >= sec) return (ms / sec | 0) + 's';
return ms + 'ms';
}
/**
* Create a debugger with the given `name`.
*
* @param {String} name
* @return {Type}
* @api public
*/
function debug(name) {
function disabled(){}
disabled.enabled = false;
var match = skips.some(function(re){
return re.test(name);
});
if (match) return disabled;
match = names.some(function(re){
return re.test(name);
});
if (!match) return disabled;
var c = color();
function colored(fmt) {
fmt = coerce(fmt);
var curr = new Date;
var ms = curr - (prev[name] || curr);
prev[name] = curr;
fmt = ' \u001b[9' + c + 'm' + name + ' '
+ '\u001b[3' + c + 'm\u001b[90m'
+ fmt + '\u001b[3' + c + 'm'
+ ' +' + humanize(ms) + '\u001b[0m';
console.error.apply(this, arguments);
}
function plain(fmt) {
fmt = coerce(fmt);
fmt = new Date().toUTCString()
+ ' ' + name + ' ' + fmt;
console.error.apply(this, arguments);
}
colored.enabled = plain.enabled = true;
return isatty || process.env.DEBUG_COLORS
? colored
: plain;
}
/**
* Coerce `val`.
*/
function coerce(val) {
if (val instanceof Error) return val.stack || val.message;
return val;
}

View File

@@ -0,0 +1,49 @@
{
"name": "debug",
"version": "0.7.4",
"repository": {
"type": "git",
"url": "git://github.com/visionmedia/debug.git"
},
"description": "small debugging utility",
"keywords": [
"debug",
"log",
"debugger"
],
"author": {
"name": "TJ Holowaychuk",
"email": "tj@vision-media.ca"
},
"dependencies": {},
"devDependencies": {
"mocha": "*"
},
"main": "lib/debug.js",
"browser": "./debug.js",
"engines": {
"node": "*"
},
"files": [
"lib/debug.js",
"debug.js",
"index.js"
],
"component": {
"scripts": {
"debug/index.js": "index.js",
"debug/debug.js": "debug.js"
}
},
"readme": "# debug\n\n tiny node.js debugging utility modelled after node core's debugging technique.\n\n## Installation\n\n```\n$ npm install debug\n```\n\n## Usage\n\n With `debug` you simply invoke the exported function to generate your debug function, passing it a name which will determine if a noop function is returned, or a decorated `console.error`, so all of the `console` format string goodies you're used to work fine. A unique color is selected per-function for visibility.\n \nExample _app.js_:\n\n```js\nvar debug = require('debug')('http')\n , http = require('http')\n , name = 'My App';\n\n// fake app\n\ndebug('booting %s', name);\n\nhttp.createServer(function(req, res){\n debug(req.method + ' ' + req.url);\n res.end('hello\\n');\n}).listen(3000, function(){\n debug('listening');\n});\n\n// fake worker of some kind\n\nrequire('./worker');\n```\n\nExample _worker.js_:\n\n```js\nvar debug = require('debug')('worker');\n\nsetInterval(function(){\n debug('doing some work');\n}, 1000);\n```\n\n The __DEBUG__ environment variable is then used to enable these based on space or comma-delimited names. Here are some examples:\n\n ![debug http and worker](http://f.cl.ly/items/18471z1H402O24072r1J/Screenshot.png)\n\n ![debug worker](http://f.cl.ly/items/1X413v1a3M0d3C2c1E0i/Screenshot.png)\n\n## Millisecond diff\n\n When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the \"+NNNms\" will show you how much time was spent between calls.\n\n ![](http://f.cl.ly/items/2i3h1d3t121M2Z1A3Q0N/Screenshot.png)\n\n When stderr is not a TTY, `Date#toUTCString()` is used, making it more useful for logging the debug information as shown below:\n _(NOTE: Debug now uses stderr instead of stdout, so the correct shell command for this example is actually `DEBUG=* node example/worker 2> out &`)_\n \n ![](http://f.cl.ly/items/112H3i0e0o0P0a2Q2r11/Screenshot.png)\n \n## Conventions\n\n If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use \":\" to separate features. For example \"bodyParser\" from Connect would then be \"connect:bodyParser\". \n\n## Wildcards\n\n The \"*\" character may be used as a wildcard. Suppose for example your library has debuggers named \"connect:bodyParser\", \"connect:compress\", \"connect:session\", instead of listing all three with `DEBUG=connect:bodyParser,connect.compress,connect:session`, you may simply do `DEBUG=connect:*`, or to run everything using this module simply use `DEBUG=*`.\n\n You can also exclude specific debuggers by prefixing them with a \"-\" character. For example, `DEBUG=* -connect:*` would include all debuggers except those starting with \"connect:\".\n\n## Browser support\n\n Debug works in the browser as well, currently persisted by `localStorage`. For example if you have `worker:a` and `worker:b` as shown below, and wish to debug both type `debug.enable('worker:*')` in the console and refresh the page, this will remain until you disable with `debug.disable()`. \n\n```js\na = debug('worker:a');\nb = debug('worker:b');\n\nsetInterval(function(){\n a('doing some work');\n}, 1000);\n\nsetInterval(function(){\n a('doing some work');\n}, 1200);\n```\n\n## License \n\n(The MIT License)\n\nCopyright (c) 2011 TJ Holowaychuk &lt;tj@vision-media.ca&gt;\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n",
"readmeFilename": "Readme.md",
"bugs": {
"url": "https://github.com/visionmedia/debug/issues"
},
"_id": "debug@0.7.4",
"_from": "debug@0.7.4",
"dist": {
"shasum": "06e1ea8082c2cb14e39806e22e2f6f757f92af39"
},
"_resolved": "https://registry.npmjs.org/debug/-/debug-0.7.4.tgz"
}

View File

@@ -0,0 +1,43 @@
{
"predef": [
"phantom",
"module",
"require",
"__dirname",
"process",
"console",
"it",
"describe",
"before",
"beforeEach",
"after",
"should",
"rewire",
"$"
],
"browser": true,
"node" : true,
"es5": true,
"bitwise": true,
"curly": true,
"eqeqeq": true,
"forin": false,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"noempty": true,
"nonew": true,
"plusplus": false,
"undef": true,
"strict": true,
"trailing": false,
"globalstrict": true,
"nonstandard": true,
"white": false,
"indent": 2,
"expr": true,
"multistr": true,
"onevar": false
}

View File

@@ -0,0 +1,6 @@
data/
deps/
coverage.html
lib-cov
test/
test_results.md

View File

@@ -0,0 +1,4 @@
language: node_js
node_js:
- 0.6
- 0.8

View File

@@ -0,0 +1,16 @@
# Total 12 contributors.
# Ordered by date of first contribution.
# Auto-generated (http://github.com/dtrejo/node-authors) on Tue Aug 14 2012 02:58:57 GMT+0800 (CST).
Gui Lin <guileen@gmail.com> (https://github.com/guileen)
François de Metz <francois@2metz.fr> (https://github.com/francois2metz)
fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
Quang Van <quangvvv@gmail.com> (https://github.com/quangv)
Matt Perpick <clutchski@gmail.com> (https://github.com/clutchski)
humanchimp <morphcham@gmail.com> (https://github.com/humanchimp)
Joe Faber <joe.faber@mandiant.com> (https://github.com/jlfaber)
Harvey McQueen <hmcqueen@gmail.com> (https://github.com/hmcqueen)
Paul Gebheim <pgebheim@monkeyinferno.com> (https://github.com/paulirish)
Aneil Mallavarapu <aneil@blipboard.com> (https://github.com/amallavarapu)
wmertens <Wout.Mertens@gmail.com> (https://github.com/wmertens)
Rakshit Menpara <deltasquare4@gmail.com> (https://github.com/deltasquare4)

View File

@@ -0,0 +1,47 @@
0.3.4 / 2011-03-24
* fix global leaks
0.3.3 / 2011-03-15
==================
* Add rootCollection option to SkinGridStore.exist
0.3.2 / 2011-03-01
==================
* exports all classes of node-mongodb-native
0.3.1 / 2011-02-26
==================
* bug fix #33
0.3.0 / 2011-01-19
==================
* add ReplSet support
* bug fix
0.2.3 / 2011-01-03
==================
* add db.toObjectID
* fix #25 for node-mongodb-native update
0.2.2 / 2011-12-02
==================
* add bind support for embeded collections, e.g. db.bind('system.js')
* add method `toId` to SkinDB
* add property `ObjectID`, `bson_serializer` to SkinDB.
* SkinCollection.prototype.id is now deprecated.
0.2.1 / 2011-11-18
==================
* add ObjectId support for XXXXById
0.2.0 / 2011-11-06
==================
* add SkinDB.gridfs
0.1.3 / 2011-05-24
==================
* add SkinCollection.removeById
0.1.2 / 2011-04-30
==================
* add mongoskin.router

View File

@@ -0,0 +1,22 @@
(The MIT License)
Copyright (c) 2011 - 2012 kissjs.org
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,44 @@
TESTS = test/
TESTTIMEOUT = 60000
REPORTER = spec
MOCHA_OPTS =
PROJECT_DIR = $(shell pwd)
MONGOSKIN_REPLICASET = false
SUPPORT_VERSIONS := \
1.0.0 1.0.1 1.0.2 \
1.1.0-beta 1.1.1 1.1.2 1.1.3 1.1.4
test:
@npm install
@if ! test -d deps/mongodb; then \
git clone git://github.com/mongodb/node-mongodb-native.git deps/mongodb; \
fi
@cd deps/mongodb && npm install && git pull && cd ../..
@NODE_ENV=test MONGOSKIN_REPLICASET=$(MONGOSKIN_REPLICASET) \
./node_modules/mocha/bin/mocha --recursive \
--reporter $(REPORTER) --timeout $(TESTTIMEOUT) \
$(MOCHA_OPTS) $(TESTS)
test-debug:
@$(MAKE) test MOCHA_OPTS="--debug-brk"
test-replicaset:
@$(MAKE) test MONGOSKIN_REPLICASET=true
lib-cov:
@rm -rf ./$@
@jscoverage --encoding=utf-8 ./lib ./$@
test-cov: lib-cov
@MONGOSKIN_COV=1 $(MAKE) test REPORTER=dot
@MONGOSKIN_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
@$(MAKE) test REPORTER=markdown > test_results.md
test-version:
@for version in $(SUPPORT_VERSIONS); do \
echo "test with mongodb@$$version"; \
npm install mongodb@$$version --loglevel=warn; \
$(MAKE) test REPORTER=dot; \
done
.PHONY: test-replicaset test-version test-cov test lib-cov

View File

@@ -0,0 +1,698 @@
# mongoskin
[![Build Status](https://secure.travis-ci.org/kissjs/node-mongoskin.png)](http://travis-ci.org/kissjs/node-mongoskin)
This project is a wrapper of [node-mongodb-native](https://github.com/mongodb/node-mongodb-native).
The api is same to node-mongodb-native, please see the [document](http://mongodb.github.com/node-mongodb-native/) first.
## Test
* test results: [test_results.md](https://github.com/kissjs/node-mongoskin/blob/master/test_results.md)
* jscoverage: [**89%**](http://fengmk2.github.com/coverage/mongoskin.html)
## Test pass [mongodb] versions
* <del>>= 0.9.8 < 1.0.0</del>: mongodb have bug, it will throw a `TypeError: object is not a function`
when connection open error.
* 1.0.x
* 1.1.x
```bash
$ make test-version
```
<a name='index'>
# Mongoskin document
* [Nodejs mongodb drivers comparation](#comparation)
* [Install](#install)
* [Quick Start](#quickstart)
* [Connect easier](#quickstart-1)
* [Server options and BSON options](#quickstart-2)
* [Similar API with node-mongodb-native](#quickstart-3)
* [Cursor easier](#quickstart-4)
* [MVC helper](#quickstart-5)
* [Documentation](#documentation)
* [Module](#module)
* [SkinServer](#skinserver)
* [SkinDb](#skindb)
* [SkinCollection](#skincollection)
* [Additional methods](#additional-collection-op)
* [Collection operation](#inherit-collection-op)
* [Indexes](#inherit-indexes)
* [Querying](#inherit-query)
* [Aggregation](#inherit-aggregation)
* [Inserting](#inherit-inserting)
* [Updating](#inherit-updating)
* [Removing](#inherit-removing)
* [SkinCursor](#skincursor)
<a name='comparation'>
Nodejs Mongodb Driver Comparison
========
node-mongodb-native
--------
One of the most powerful Mongo drivers is node-mongodb-native. Most other drivers build
on top of it, including mongoskin. Unfortunately, it has an awkward interface with too many
callbacks. Also, mongoskin needs a way to hold a Collection instance as an MVC model.
See [mongodb-native](https://github.com/christkv/node-mongodb-native/tree/master/docs)
mongoose
--------
Mongoose provides an ORM way to hold Collection instance as Model,
you should define schema first. But why mongodb need schema?
Some guys like me, want to write code from application layer but not database layer,
and we can use any fields without define it before.
Mongoose provide a DAL that you can do validation, and write your middlewares.
But some guys like me would like to validate manually, I think it is the tao of mongodb.
If you don't thinks so, [Mongoose-ORM](https://github.com/LearnBoost/mongoose) is probably your choice.
mongoskin
--------
Mongoskin is an easy to use driver of mongodb for nodejs,
it is similar with mongo shell, powerful like node-mongodb-native,
and support additional javascript method binding, which make it can act as a Model(in document way).
It will provide full features of [node-mongodb-native](https://github.com/christkv/node-mongodb-native),
and make it [future](http://en.wikipedia.org/wiki/Future_%28programming%29).
If you need validation, you can use [node-iform](https://github.com/guileen/node-iform).
[Back to index](#index)
<a name='install'></a>
Install
========
```bash
$ npm install mongoskin
```
[Back to index](#index)
<a name='quickstart'></a>
Quick Start
========
**Is mongoskin synchronized?**
Nope! It is asynchronized, it use the [future pattern](http://en.wikipedia.org/wiki/Future_%28programming%29).
**Mongoskin** is the future layer above [node-mongodb-native](https://github.com/christkv/node-mongodb-native)
<a name='quickstart-1'></a>
Connect easier
--------
You can connect to mongodb easier now.
```js
var mongo = require('mongoskin');
mongo.db('localhost:27017/testdb').collection('blog').find().toArray(function (err, items) {
console.dir(items);
})
```
<a name='quickstart-2'></a>
Server options and BSON options
--------
You can also set `auto_reconnect` options querystring.
And native_parser options will automatically set if native_parser is avariable.
```js
var mongo = require('mongoskin');
var db = mongo.db('localhost:27017/test?auto_reconnect');
```
<a name='quickstart-3'></a>
Similar API with node-mongodb-native
--------
You can do everything that node-mongodb-native can do.
```js
db.createCollection(...);
db.collection('user').ensureIndex([['username', 1]], true, function (err, replies) {});
db.collection('posts').hint = 'slug';
db.collection('posts').findOne({slug: 'whats-up'}, function (err, post) {
// do something
});
```
<a name='quickstart-4'></a>
Cursor easier
--------
```js
db.collection('posts').find().toArray(function (err, posts) {
// do something
});
```
<a name='quickstart-5'></a>
MVC helper
--------
You can bind **additional methods** for collection.
It is very useful if you want to use MVC patterns with nodejs and mongodb.
You can also invoke collection by properties after bind,
it could simplfy your `require`.
```js
db.bind('posts', {
findTop10 : function (fn) {
this.find({}, {limit:10, sort:[['views', -1]]}).toArray(fn);
},
removeTagWith : function (tag, fn) {
this.remove({tags:tag},fn);
}
});
db.bind('comments');
db.collection('posts').removeTagWith('delete', function (err, replies) {
//do something
});
db.posts.findTop10(function (err, topPosts) {
//do something
});
db.comments.find().toArray(function (err, comments) {
//do something
});
```
[Back to index](#index)
<a name='documentation'>
Documentation
========
for more information, see the source.
[Back to index](#index)
<a name='module'>
Module
--------
### MongoSkin Url format
```
[*://][username:password@]host[:port][/database][?auto_reconnect[=true|false]]`
```
e.g.
```
localhost/blog
mongo://admin:pass@127.0.0.1:27017/blog?auto_reconnect
127.0.0.1?auto_reconnect=false
```
### db(serverURL[s], dbOptions, replicasetOptions)
Get or create instance of [SkinDb](#skindb).
```js
var db = mongoskin.db('localhost:27017/testdb?auto_reconnect=true&poolSize=5');
```
for ReplSet server
```js
var db = mongoskin.db([
'192.168.0.1:27017/?auto_reconnect=true',
'192.168.0.2:27017/?auto_reconnect=true',
'192.168.0.3:27017/?auto_reconnect=true'
], {
database: 'testdb'
}, {
connectArbiter: false,
socketOptions: {
timeout: 2000
}
});
```
### router(select)
select is `function(collectionName)` returns a database instance, means router collectionName to that database.
```js
var db = mongo.router(function (coll_name) {
switch(coll_name) {
case 'user':
case 'message':
return mongo.db('192.168.1.3/auth_db');
default:
return mongo.db('192.168.1.2/app_db');
}
});
db.bind('user', require('./shared-user-methods'));
var users = db.user; //auth_db.user
var messages = db.collection('message'); // auth_db.message
var products = db.collection('product'); //app_db.product
```
### classes extends frome node-mongodb-native
* BSONPure
* BSONNative
* BinaryParser
* Binary
* Code
* DBRef
* Double
* MaxKey
* MinKey
* ObjectID
* Symbol
* Timestamp
* Long
* BaseCommand
* DbCommand
* DeleteCommand
* GetMoreCommand
* InsertCommand
* KillCursorCommand
* QueryCommand
* UpdateCommand
* MongoReply
* Admin
* Collection
* Connection
* Server
* ReplSetServers
* Cursor
* Db
* connect
* Grid
* Chunk
* GridStore
* native
* pure
[Back to index](#index)
<a name='skinserver'>
SkinServer
--------
### SkinServer(server)
Construct SkinServer from native Server instance.
### db(dbname, username=null, password=null)
Construct [SkinDb](#skindb) from SkinServer.
[Back to index](#index)
<a name='skindb'>
SkinDb
--------
### SkinDb(db, username=null, password=null)
Construct SkinDb.
### open(callback)
Connect to database, retrieval native
[Db](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/db.js#L17)
instance, callback is function(err, db).
### collection(collectionName)
Retrieval [SkinCollection](#skincollection) instance of specified collection name.
<a name='skindb-bind'>
### bind(collectionName)
### bind(collectionName, SkinCollection)
### bind(collectionName, extendObject1, extendObject2 ...)
Bind [SkinCollection](#skincollection) to db properties as a shortcut to db.collection(name).
You can also bind additional methods to the SkinCollection, it is useful when
you want to reuse a complex operation. This will also affect
db.collection(name) method.
e.g.
```js
db.bind('book', {
firstBook: function (fn) {
this.findOne(fn);
}
});
db.book.firstBook(function (err, book) {});
```
### all the methods from Db.prototype
See [Db](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/db.js#L17) of node-mongodb-native for more information.
[Back to index](#index)
<a name='skincollection'>
SkinCollection
--------
See [Collection](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/collection.js#L45) of node-mongodb-native for more information.
<a name='additional-collection-op'>
### open(callback)
Retrieval native
[Collection](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/collection.js#L45)
instance, callback is function(err, collection).
### id(hex)
Equivalent to
```js
db.bson_serializer.ObjectID.createFromHexString(hex);
```
See [ObjectID.createFromHexString](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/bson/bson.js#L548)
<a name='inherit-collection-op'>
### Collection operation
```js
checkCollectionName(collectionName)
options(callback)
rename(collectionName, callback)
drop(callback)
```
<a name='inherit-indexes'>
### Indexes
```js
createIndex(fieldOrSpec, unique, callback)
ensureIndex(fieldOrSpec, unique, callback)
indexInformation(callback)
dropIndex(indexName, callback)
dropIndexes(callback)
```
See [mongodb-native indexes](https://github.com/christkv/node-mongodb-native/blob/master/docs/indexes.md)
<a name='inherit-query'>
### Queries
See [mongodb-native queries](https://github.com/christkv/node-mongodb-native/blob/master/docs/queries.md)
#### findItems(..., callback)
Equivalent to
```js
collection.find(..., function (err, cursor) {
cursor.toArray(callback);
});
```
See [Collection.find](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/collection.js#L348)
#### findEach(..., callback)
Equivalent to
```js
collection.find(..., function (err, cursor) {
cursor.each(callback);
});
```
See [Collection.find](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/collection.js#L348)
#### findById(id, ..., callback)
Equivalent to
```js
collection.findOne({_id, ObjectID.createFromHexString(id)}, ..., callback);
```
See [Collection.findOne](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/collection.js#L417)
#### find(...)
If the last parameter is function, it is equivalent to native
[Collection.find](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/collection.js#L348)
method, else it will return a future [SkinCursor](#skincursor).
e.g.
```js
// callback
db.book.find({}, function (err, cursor) {/* do something */});
// future SkinCursor
db.book.find().toArray(function (err, books) {/* do something */});
```
#### normalizeHintField(hint)
#### find
```js
/**
* Various argument possibilities
* 1 callback
* 2 selector, callback,
* 2 callback, options // really?!
* 3 selector, fields, callback
* 3 selector, options, callback
* 4,selector, fields, options, callback
* 5 selector, fields, skip, limit, callback
* 6 selector, fields, skip, limit, timeout, callback
*
* Available options:
* limit, sort, fields, skip, hint, explain, snapshot, timeout, tailable, batchSize
*/
```
#### findAndModify(query, sort, update, options, callback)
```js
/**
Fetch and update a collection
query: a filter for the query
sort: if multiple docs match, choose the first one in the specified sort order as the object to manipulate
update: an object describing the modifications to the documents selected by the query
options:
remove: set to a true to remove the object before returning
new: set to true if you want to return the modified object rather than the original. Ignored for remove.
upsert: true/false (perform upsert operation)
**/
```
#### findOne(queryObject, options, callback)
<a name='inherit-aggregation'>
### Aggregation
#### mapReduce(map, reduce, options, callback)
e.g.
```js
var map = function () {
emit(test(this.timestamp.getYear()), 1);
}
var reduce = function (k, v){
count = 0;
for (i = 0; i < v.length; i++) {
count += v[i];
}
return count;
}
var options = {scope: {test: new client.bson_serializer.Code(t.toString())}};
collection.mapReduce(map, reduce, options, function (err, collection) {
collection.find(function (err, cursor) {
cursor.toArray(function (err, results) {
test.equal(2, results[0].value)
finished_test({test_map_reduce_functions_scope:'ok'});
})
})
```
#### group(keys, condition, initial, reduce, command, callback)
e.g.
```js
collection.group([], {}, {"count":0}, "function (obj, prev) { prev.count++; }", true, function(err, results) {});
```
#### count(query, callback)
#### distinct(key, query, callback)
<a name='inherit-inserting'>
### Inserting
#### insert(docs, options, callback)
<a name='inherit-updating'>
### Updating
#### save(doc, options, callback)
```js
/**
Update a single document in this collection.
spec - a associcated array containing the fields that need to be present in
the document for the update to succeed
document - an associated array with the fields to be updated or in the case of
a upsert operation the fields to be inserted.
Options:
upsert - true/false (perform upsert operation)
multi - true/false (update all documents matching spec)
safe - true/false (perform check if the operation failed, required extra call to db)
**/
```
#### update(spec, document, options, callback)
#### updateById(_id, ..., callback)
Equivalent to
```js
collection.update({_id, ObjectID.createFromHexString(id)}, ..., callback);
```
See [Collection.update](https://github.com/christkv/node-mongodb-native/blob/master/docs/insert.md)
<a name='inherit-removing'>
### Removing
#### remove(selector, options, callback)
#### removeById(_id, options, callback)
[Back to index](#index)
<a name='skincursor'>
SkinCursor
---------
See [Cursor](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/cursor.js#L1)
of node-mongodb-native for more information.
All these methods will return the SkinCursor itself.
```js
sort(keyOrList, [direction], [callback])
limit(limit, [callback])
skip(skip, [callback])
batchSize(skip, [callback])
toArray(callback)
each(callback)
count(callback)
nextObject(callback)
getMore(callback)
explain(callback)
```
[Back to index](#index)
## How to validate input?
I wrote a middleware to validate post data, [node-iform](https://github.com/guileen/node-iform)
base on [node-validator](https://github.com/chriso/node-validator)
## Authors
Below is the output from `git-summary`.
```
project: node-mongoskin
commits: 112
active : 54 days
files : 39
authors:
49 Lin Gui 43.8%
34 guilin 桂林 30.4%
9 fengmk2 8.0%
5 guilin 4.5%
2 François de Metz 1.8%
2 Paul Gebheim 1.8%
2 Gui Lin 1.8%
1 humanchimp 0.9%
1 Aneil Mallavarapu 0.9%
1 wmertens 0.9%
1 Harvey McQueen 0.9%
1 Joe Faber 0.9%
1 Matt Perpick 0.9%
1 Quang Van 0.9%
1 Rakshit Menpara 0.9%
1 Wout Mertens 0.9%
```
## License
(The MIT License)
Copyright (c) 2011 - 2012 kissjs.org
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,9 @@
var db = require('./config').db;
db.admin.listDatabases(function(err, result){
if(err) {
console.traceError(err);
}
console.log(result);
db.close();
})

View File

@@ -0,0 +1,15 @@
var db = require('./config').db;
db.collection('test').findOne({}, function(err, data) {
if(!err) {
console.log('db has open');
console.log(data);
}
});
process.on('SIGINT', function() {
console.log('Recieve SIGINT');
db.close(function(){
console.log('database has closed');
})
})

View File

@@ -0,0 +1,5 @@
var mongoskin = require('../lib/mongoskin/');
exports.db = mongoskin.db('localhost/test');
mongoskin.db('localhost', { database: 'test' });

View File

@@ -0,0 +1,31 @@
var redis = require('redis').createClient()
, shorten = require('shorten')(redis)
, async = require('async')
, db = require('./config').db
;
db.bind('user');
function log(err) {
if(err) {
console.log(err.stack);
}
}
function createUser(user, callback) {
async.waterfall([
function(fn) {
shorten.nextId('user', fn);
}
, function(uid, fn) {
user.uid = uid;
db.user.save(user, fn);
}
], callback);
}
for(var i = 0; i<10; i++) {
createUser({name: 'user' + i}, log);
}

View File

@@ -0,0 +1,13 @@
var db = require('./config').db;
db.gridfs().open('test.txt', 'w', function(err, gs) {
gs.write('blablabla', function(err, reply) {
gs.close(function(err, reply){
db.gridfs().open('test.txt', 'r' ,function(err, gs) {
gs.read(function(err, reply){
console.log(reply.toString());
});
});
});
});
});

View File

@@ -0,0 +1,8 @@
var db = require('./config').db;
db.collection('test').insert({foo: 'bar'}, function(err, result) {
console.log(result);
db.collection('test').drop();
db.close();
});

View File

@@ -0,0 +1,45 @@
var mongo = require('../');
var conf = {
hosts: [
'127.0.0.1:27110/?auto_reconnect',
'127.0.0.1:27111/?auto_reconnect'
],
dataDB: 'test'
};
var db = exports.db = mongo.db(conf.hosts, {
database: conf.dataDB
});
var noop = function() {};
db.bind('user');
// db.user.ensureIndex({ name: 1 }, { unique: true }, noop);
// db.user.ensureIndex({ enable: 1 }, noop);
// db.user.ensureIndex({ created_at: 1, enable: 1 }, noop);
var counter = 0;
setInterval(function () {
db.user.findItems({ name: 'name_' + counter }, function (err, items) {
if (err) {
console.error('findItems user error', err);
}
if (items) {
console.log('total: %d users', items.length);
}
});
db.user.insert({
name: 'name_' + counter,
createtime: new Date()
}, function(err, user) {
if (err) {
console.error('insert user error', err);
}
if (user && user[0]) {
console.log('new: %d %s', counter, user[0]._id);
}
});
counter++;
}, 10);

View File

@@ -0,0 +1,10 @@
var mongoskin = require('../lib/mongoskin/');
var db = mongoskin.db(['127.0.0.1:27017'], {
database: 'test'
});
db.open(function(err, data) {
console.log(err && err.stack);
console.log(data);
});

View File

@@ -0,0 +1,19 @@
var db = require('./config').db;
var articles = db.collection('articles');
articles.insert({foo: 'bar', val: 'val1'}, function(err, result) {
console.log(result);
articles.update({foo:'bar'}, {foo: 'bar', val:'val2'}, {safe: true}, function(err, result) {
console.log(result);
articles.find({foo: 'bar'}).toArray(function(err, docs){
console.log(docs);
articles.drop();
db.close();
});
})
});

View File

@@ -0,0 +1 @@
module.exports = process.env.MONGOSKIN_COV ? require('./lib-cov/mongoskin') : require('./lib/mongoskin');

View File

@@ -0,0 +1,203 @@
GLOBAL.DEBUG = true;
var assert = require('assert'),
mongo = require('../lib/mongoskin');
console.log('======== test MongoSkin.db ========');
(function(){
var username = 'testuser',
password = 'password';
db = mongo.db('localhost/test');
db.open(function(err, db) {
assert.ok(!err);
assert.ok(db, err && err.stack);
db.addUser(username, password, function(err, result){
var authdb = mongo.db(username + ':' + password +'@localhost/test');
authdb.open(function(err, db){
assert.ok(!err, err && err.stack);
});
var faildb = mongo.db(username + ':wrongpassword@localhost/test');
faildb.open(function(err, db){
assert.ok(err, 'should not auth');
assert.ok(!db, 'should not return db');
});
});
});
})();
(function(){
db = mongo.db('db://admin:admin@localhost:27017/test?auto_reconnect');
db.open(function(err, db){
assert.ok(err instanceof Error);
})
})();
var bindToBlog = {
first: function(fn) {
this.findOne(fn);
}
};
console.log('======== test MongoSkin.router ========');
var testdb1 = mongo.db('localhost/test1');
var testdb2 = mongo.db('localhost/test2');
var router = mongo.router(function(name){
switch(name){
case 'user':
case 'message':
return testdb1;
default:
return testdb2;
}
});
assert.equal(router.collection('user'), testdb1.collection('user'), 'user should router to testdb1');
assert.equal(router.collection('message'), testdb1.collection('message'), 'message should router to testdb1');
assert.equal(router.collection('others'), testdb2.collection('others'), 'others should router to testdb2');
router.bind('user');
router.bind('others');
assert.equal(router.user, testdb1.user, 'user property should router to testdb1');
assert.equal(router.others, testdb2.others, 'user property should router to testdb1');
console.log('======== test MongoSkin.bind ========');
var db = mongo.db('localhost/test_mongoskin');
db.bind('blog', bindToBlog);
db.bind('users');
assert.equal(db.blog.first, bindToBlog.first);
assert.ok(db.users);
console.log('======== test SkinDb bson ========');
assert.ok(db.ObjectID.createFromHexString('a7b79d4dca9d730000000000'));
console.log('======== test SkinDb.bind ========');
db.bind('blog2', bindToBlog);
db.bind('user2');
assert.equal(db.blog2.first, bindToBlog.first);
assert.ok(db.user2);
console.log('======== test SkinDb.open ========');
(function(){
var db1, db2;
db.open(function(err, db) {
assert.ok(db, err && err.stack);
db1 = db;
assert.equal(db1.state, 'connected');
if (db2) {
assert.equal(db1, db2, 'should alwayse be the same instance in db.open.');
}
});
db.open(function(err, db) {
assert.ok(db, err && err.stack);
db2 = db;
assert.equal(db2.state, 'connected');
if (db1) {
assert.equal(db1, db2, 'should alwayse be the same instance in db.open.');
}
});
})()
console.log('======== test normal method of SkinDb ========');
db.createCollection('test_createCollection', function(err, collection) {
assert.equal(db.db.state, 'connected');
assert.ok(collection, err && err.stack);
});
console.log('======== test SkinDb.collection ========');
assert.equal(db.blog, db.collection('blog'));
console.log('======== test SkinCollection.open ========');
var coll1, coll2;
db.blog.open(function(err, coll) {
assert.ok(coll, err && err.stack);
coll1 = coll;
if (coll2) {
assert.equal(coll1, coll2, 'should be the same instance in collection.open');
}
});
db.blog.open(function(err, coll) {
assert.ok(coll, err && err.stack);
coll2 = coll;
if (coll1) {
assert.equal(coll1, coll2, 'should be the same instance in collection.open');
}
});
console.log('======== test normal method of SkinCollection ========');
db.collection('test_normal').ensureIndex([['a',1]], function(err, replies){
assert.ok(replies, err && err.stack);
});
console.log('======== test SkinCollection.drop ========');
db.collection('test_find').drop(function(err, replies){
assert.ok(!err, err && err.stack);
});
console.log('======== test SkinCollection.find ========');
collection = db.collection('test_find');
collection.insert([{a:1},{a:2},{a:3},{a:4}], function(err, replies){
assert.ok(replies, err && err.stack);
console.log('======== test SkinCollection.findById ========');
collection.findById(replies[0]._id.toString(), function(err, item){
assert.equal(item.a, 1);
console.log('======== test SkinCollection.removeById ========');
collection.removeById(replies[0]._id.toString(), function(err, reply){
assert.ok(!err, err && err.stack);
collection.findById(replies[0]._id.toString(), function(err, item){
assert.ok(!err);
assert.ok(!item);
});
});
});
});
collection.findItems(function(err, items){
assert.ok(items, err && err.stack);
console.log('found '+ items.length + ' items');
});
collection.findEach(function(err, item){
assert.ok(!err, err && err.stack);
});
collection.find(function(err, cursor){
assert.ok(cursor, err && err.stack);
});
console.log('======== test SkinCursor ========');
collection.find().toArray(function(err, items){
console.log('======== test find cursor toArray========');
assert.ok(items, err && err.stack);
});
collection.find().each(function(err, item){
console.log('======== test find cursor each========');
assert.ok(!err, err && err.stack);
});
collection.find().sort({a:-1}).limit(2).skip(1).toArray(function(err, items){
console.log('======== test cursor sort() limit() skip() ========');
assert.ok(!err, err && err.stack);
console.dir(items);
});
console.log('======== deep future test ========');
(function(){
var db2 = mongo.db('localhost/test-mongoskin01');
db2.collection('blog').find().toArray(function(err, items){
assert.ok(!err, err && err.stack);
})
})();
(function(){
var db2 = mongo.db('unknownhost/test-mongoskin01');
db2.collection('blog').find().toArray(function(err, items){
assert.ok(err);
})
})();
/*
console.log('======== test SkinDb.close ========');
db.close();
assert.equal(db.db.state, 'notConnected');
*/

View File

@@ -0,0 +1,28 @@
var mongo = require('../');
var db = mongo.db('192.168.0.103/test');
// var db = mongo.db('127.0.0.1/test');
var myconsole = require('myconsole');
var foo = db.collection('foo');
setInterval(function() {
foo.insert({foo:'foo'}, function(err, result){
if(err) return myconsole.error(err);
foo.count(function(err, count){
if(err) return myconsole.error(err);
myconsole.log('count: %d', count);
foo.find().limit(10).toArray(function(err, arr) {
if(err) return myconsole.error(err);
myconsole.log('arr: %d', arr.length);
})
})
})
}, 500);
process.on('SIGINT', function(){
myconsole.log('SIGINT')
foo.drop(function(err){
if(err) myconsole.error(err);
process.exit();
})
})

View File

@@ -0,0 +1,67 @@
/*!
* mongoskin - admin.js
*
* Copyright(c) 2011 - 2012 kissjs.org
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
var Admin = require('mongodb').Admin;
var utils = require('./utils');
var constant = require('./constant');
/**
* SkinAdmin
*
* @param {SkinDb} skinDb
* @constructor
* @api public
*/
var SkinAdmin = exports.SkinAdmin = function (skinDb) {
utils.SkinObject.call(this);
this.skinDb = skinDb;
this.admin = null;
};
utils.inherits(SkinAdmin, utils.SkinObject);
/**
* Retrieve mongodb.Admin instance.
*
* @param {Function(err, admin)} callback
* @return {SkinAdmin} this
* @api public
*/
SkinAdmin.prototype.open = function (callback) {
if (this.state === constant.STATE_OPEN) {
callback(null, this.admin);
return this;
}
this.emitter.once('open', callback);
if (this.state === constant.STATE_OPENNING) {
return this;
}
this.state = constant.STATE_OPENNING;
this.skinDb.open(function (err, db) {
if (err) {
this.admin = null;
this.state = constant.STATE_CLOSE;
} else {
this.admin = new Admin(db);
this.state = constant.STATE_OPEN;
}
this.emitter.emit('open', err, this.admin);
}.bind(this));
return this;
};
for (var key in Admin.prototype) {
var method = Admin.prototype[key];
utils.bindSkin('SkinAdmin', SkinAdmin, 'admin', key, method);
}

View File

@@ -0,0 +1,300 @@
/*!
* mongoskin - collection.js
*
* Copyright(c) 2011 - 2012 kissjs.org
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
/**
bind these methods from Collection.prototype to Provider
methods:
insert
checkCollectionName
remove
rename
save
update
distinct
count
drop
findAndModify
find
normalizeHintField
findOne
createIndex
ensureIndex
indexInformation
dropIndex
dropIndexes
mapReduce
group
options
*/
var __slice = Array.prototype.slice;
var events = require('events');
var Collection = require('mongodb').Collection;
var SkinCursor = require('./cursor').SkinCursor;
var utils = require('./utils');
var constant = require('./constant');
var STATE_CLOSE = constant.STATE_CLOSE;
var STATE_OPENNING = constant.STATE_OPENNING;
var STATE_OPEN = constant.STATE_OPEN;
/**
* Construct SkinCollection from SkinDb and collectionName
* use skinDb.collection('name') usually
*
* @param {SkinDb} skinDb
* @param {String} collectionName
* @param {Object} [options] collection options
* @constructor
* @api public
*/
var SkinCollection = exports.SkinCollection = function (skinDb, collectionName, options) {
utils.SkinObject.call(this);
this.emitter.setMaxListeners(50);
this.options = options;
this.skinDb = skinDb;
this.ObjectID = this.skinDb.ObjectID;
this.collectionName = collectionName;
this.collection = null;
this.internalHint = null;
this.__defineGetter__('hint', function () {
return this.internalHint;
});
this.__defineSetter__('hint', function (value) {
this.internalHint = value;
this.open(function (err, collection) {
collection.hint = value;
this.internalHint = collection.hint;
}.bind(this));
});
};
utils.inherits(SkinCollection, utils.SkinObject);
for (var _name in Collection.prototype) {
var method = Collection.prototype[_name];
utils.bindSkin('SkinCollection', SkinCollection, 'collection', _name, method);
}
/*
* find is a special method, because it could return a SkinCursor instance
*/
SkinCollection.prototype._find = SkinCollection.prototype.find;
/**
* Retrieve mongodb.Collection
*
* @param {Function(err, collection)} callback
* @return {SkinCollection} this
* @api public
*/
SkinCollection.prototype.open = function (callback) {
switch (this.state) {
case STATE_OPEN:
callback(null, this.collection);
break;
case STATE_OPENNING:
this.emitter.once('open', callback);
break;
// case STATE_CLOSE:
default:
this.emitter.once('open', callback);
this.state = STATE_OPENNING;
this.skinDb.open(function (err, db) {
if (err) {
this.state = STATE_CLOSE;
return this.emitter.emit('open', err, null);
}
db.collection(this.collectionName, this.options, function (err, collection) {
if (err) {
this.state = STATE_CLOSE;
} else {
this.state = STATE_OPEN;
this.collection = collection;
if (this.hint) {
this.collection.hint = this.hit;
}
}
this.emitter.emit('open', err, collection);
}.bind(this));
}.bind(this));
break;
}
return this;
};
/**
* Close current collection.
*
* @param {Function(err)} callback
* @return {SkinCollection} this
* @api public
*/
SkinCollection.prototype.close = function (callback) {
this.collection = null;
this.state = STATE_CLOSE;
return this;
};
/**
* Drop current collection.
*
* @param {Function(err)} callback
* @return {SkinCollection} this
* @api public
*/
SkinCollection.prototype.drop = function (callback) {
this.skinDb.dropCollection(this.collectionName, callback);
this.close();
return this;
};
/**
* same args as find, but use Array as callback result but not use Cursor
*
* findItems(args, function (err, items) {});
*
* same as
*
* find(args).toArray(function (err, items) {});
*
* or using `mongodb.collection.find()`
*
* find(args, function (err, cursor) {
* cursor.toArray(function (err, items) {
* });
* });
*
* @param {Object} [query]
* @param {Object} [options]
* @param {Function(err, docs)} callback
* @return {SkinCollection} this
* @api public
*/
SkinCollection.prototype.findItems = function (query, options, callback) {
var args = __slice.call(arguments);
var fn = args[args.length - 1];
args[args.length - 1] = function (err, cursor) {
if (err) {
return fn(err);
}
cursor.toArray(fn);
};
this.find.apply(this, args);
return this;
};
/**
* find and cursor.each(fn).
*
* @param {Object} [query]
* @param {Object} [options]
* @param {Function(err, item)} eachCallback
* @return {SkinCollection} this
* @api public
*/
SkinCollection.prototype.findEach = function (query, options, eachCallback) {
var args = __slice.call(arguments);
var fn = args[args.length - 1];
args[args.length - 1] = function (err, cursor) {
if (err) {
return fn(err);
}
cursor.each(fn);
};
this.find.apply(this, args);
return this;
};
/**
* @deprecated use `SkinDb.toId` instead
*
* @param {String} hex
* @return {ObjectID|String}
* @api public
*/
SkinCollection.prototype.id = function (hex) {
return this.skinDb.toId(hex);
};
/**
* Operate by object.`_id`
*
* @param {String} methodName
* @param {String|ObjectID|Number} id
* @param {Arguments|Array} args
* @return {SkinCollection} this
* @api private
*/
SkinCollection.prototype._operateById = function (methodName, id, args) {
args = __slice.call(args);
args[0] = {_id: this.skinDb.toId(id)};
this[methodName].apply(this, args);
return this;
};
/**
* Find one object by _id.
*
* @param {String|ObjectID|Number} id, doc primary key `_id`
* @param {Function(err, doc)} callback
* @return {SkinCollection} this
* @api public
*/
SkinCollection.prototype.findById = function (id, callback) {
return this._operateById('findOne', id, arguments);
};
/**
* Update doc by _id.
* @param {String|ObjectID|Number} id, doc primary key `_id`
* @param {Object} doc
* @param {Function(err)} callback
* @return {SkinCollection} this
* @api public
*/
SkinCollection.prototype.updateById = function (id, doc, callback) {
return this._operateById('update', id, arguments);
};
/**
* Remove doc by _id.
* @param {String|ObjectID|Number} id, doc primary key `_id`
* @param {Function(err)} callback
* @return {SkinCollection} this
* @api public
*/
SkinCollection.prototype.removeById = function (id, callback) {
return this._operateById('remove', id, arguments);
};
/**
* Creates a cursor for a query that can be used to iterate over results from MongoDB.
*
* @param {Object} query
* @param {Object} options
* @param {Function(err, docs)} callback
* @return {SkinCursor|SkinCollection} if last argument is not a function, then returns a SkinCursor,
* otherise return this
* @api public
*/
SkinCollection.prototype.find = function (query, options, callback) {
var args = __slice.call(arguments);
if (args.length > 0 && typeof args[args.length - 1] === 'function') {
this._find.apply(this, args);
return this;
} else {
return new SkinCursor(null, this, args);
}
};

View File

@@ -0,0 +1,15 @@
/*!
* mongoskin - constant.js
*
* Copyright(c) 2011 - 2012 kissjs.org
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
"use strict";
exports.DEFAULT_PORT = 27017;
exports.STATE_CLOSE = 0;
exports.STATE_OPENNING = 1;
exports.STATE_OPEN = 2;

View File

@@ -0,0 +1,87 @@
/*!
* mongoskin - cursor.js
*
* Copyright(c) 2011 - 2012 kissjs.org
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
var EventEmitter = require('events').EventEmitter;
var Cursor = require('mongodb').Cursor;
var utils = require('./utils');
var constant = require('./constant');
var STATE_CLOSE = constant.STATE_CLOSE;
var STATE_OPENNING = constant.STATE_OPENNING;
var STATE_OPEN = constant.STATE_OPEN;
var SkinCursor = exports.SkinCursor = function (cursor, skinCollection, args) {
utils.SkinObject.call(this);
this.cursor = cursor;
this.skinCollection = skinCollection;
this.args = args || [];
this.emitter = new EventEmitter();
if (cursor) {
this.state = STATE_OPEN;
}
};
utils.inherits(SkinCursor, utils.SkinObject);
/**
* Retrieve mongodb.Cursor instance.
*
* @param {Function(err, cursor)} callback
* @return {SkinCursor} this
* @api public
*/
SkinCursor.prototype.open = function (callback) {
switch (this.state) {
case STATE_OPEN:
callback(null, this.cursor);
break;
case STATE_OPENNING:
this.emitter.once('open', callback);
break;
// case STATE_CLOSE:
default:
this.emitter.once('open', callback);
this.state = STATE_OPENNING;
this.skinCollection.open(function (err, collection) {
if (err) {
this.state = STATE_CLOSE;
this.emitter.emit('open', err);
return;
}
// copy args
var args = this.args.slice();
args.push(function (err, cursor) {
if (cursor) {
this.state = STATE_OPEN;
this.cursor = cursor;
}
this.emitter.emit('open', err, cursor);
}.bind(this));
collection.find.apply(collection, args);
}.bind(this));
break;
}
return this;
};
[
// callbacks
'toArray', 'each', 'count', 'nextObject', 'getMore', 'explain',
// self return
'sort', 'limit', 'skip', 'batchSize',
// unsupported
//'rewind', 'close' ,...
].forEach(function (name) {
var method = Cursor.prototype[name];
utils.bindSkin('SkinCursor', SkinCursor, 'cursor', name, method);
});

View File

@@ -0,0 +1,254 @@
/*!
* mongoskin - db.js
*
* Copyright(c) 2011 - 2012 kissjs.org
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
var __slice = Array.prototype.slice;
var mongodb = require('mongodb');
var utils = require('./utils');
var SkinAdmin = require('./admin').SkinAdmin;
var SkinCollection = require('./collection').SkinCollection;
var SkinGridStore = require('./gridfs').SkinGridStore;
var Db = mongodb.Db;
var constant = require('./constant');
var STATE_CLOSE = constant.STATE_CLOSE;
var STATE_OPENNING = constant.STATE_OPENNING;
var STATE_OPEN = constant.STATE_OPEN;
/**
* SkinDb
*
* @param {Db} dbconn, mongodb.Db instance
* @param {String} [username]
* @param {String} [password]
* @constructor
* @api public
*/
var SkinDb = exports.SkinDb = function (dbconn, username, password) {
utils.SkinObject.call(this);
this.emitter.setMaxListeners(100);
this._dbconn = dbconn;
this.db = null;
this.username = username;
this.password = password;
this.admin = new SkinAdmin(this);
this._collections = {};
this.bson_serializer = dbconn.bson_serializer;
this.ObjectID = mongodb.ObjectID /* 0.9.7-3-2 */ || dbconn.bson_serializer.ObjectID /* <= 0.9.7 */;
};
utils.inherits(SkinDb, utils.SkinObject);
/**
* Convert to ObjectID.
*
* @param {String} hex
* @return {ObjectID}
*/
SkinDb.prototype.toObjectID = SkinDb.prototype.toId = function (hex) {
if (hex instanceof this.ObjectID) {
return hex;
}
if (!hex || hex.length !== 24) {
return hex;
}
return this.ObjectID.createFromHexString(hex);
};
/**
* Open the database connection.
*
* @param {Function(err, nativeDb)} [callback]
* @return {SkinDb} this
* @api public
*/
SkinDb.prototype.open = function (callback) {
switch (this.state) {
case STATE_OPEN:
callback && callback(null, this.db);
break;
case STATE_OPENNING:
// if call 'open' method multi times before opened
callback && this.emitter.once('open', callback);
break;
// case STATE_CLOSE:
default:
var onDbOpen = function (err, db) {
if (!err && db) {
this.db = db;
this.state = STATE_OPEN;
} else {
db && db.close();
// close the openning connection.
this._dbconn.close();
this.db = null;
this.state = STATE_CLOSE;
}
this.emitter.emit('open', err, this.db);
}.bind(this);
callback && this.emitter.once('open', callback);
this.state = STATE_OPENNING;
this._dbconn.open(function (err, db) {
if (db && this.username) {
// do authenticate
db.authenticate(this.username, this.password, function (err) {
onDbOpen(err, db);
});
} else {
onDbOpen(err, db);
}
}.bind(this));
break;
}
return this;
};
/**
* Close the database connection.
*
* @param {Function(err)} [callback]
* @return {SkinDb} this
* @api public
*/
SkinDb.prototype.close = function (callback) {
if (this.state === STATE_CLOSE) {
callback && callback();
} else if (this.state === STATE_OPEN) {
this.state = STATE_CLOSE;
this.db.close(callback);
} else if (this.state === STATE_OPENNING) {
var that = this;
this.emitter.once('open', function (err, db) {
that.state = STATE_CLOSE;
db ? db.close(callback) : callback && callback(err);
});
}
return this;
};
/**
* Create or retrieval skin collection
*
* @param {String} name, the collection name.
* @param {Object} [options] collection options.
* @return {SkinCollection}
* @api public
*/
SkinDb.prototype.collection = function (name, options) {
var collection = this._collections[name];
if (!collection) {
this._collections[name] = collection = new SkinCollection(this, name, options);
}
return collection;
};
/**
* gridfs
*
* @return {SkinGridStore}
* @api public
*/
SkinDb.prototype.gridfs = function () {
return this.skinGridStore || (this.skinGridStore = new SkinGridStore(this));
};
/**
* bind additional method to SkinCollection
*
* 1. collectionName
* 2. collectionName, extends1, extends2,... extendsn
* 3. collectionName, SkinCollection
*
* @param {String} collectionName
* @param {Object|SkinCollection} [options]
* @return {SkinCollection}
* @api public
*/
SkinDb.prototype.bind = function (collectionName, options) {
var args = __slice.call(arguments);
var name = args[0];
if (typeof name !== 'string' || !name.trim()) {
throw new Error('Must provide collection name to bind.');
}
if (args.length === 1) {
return this.bind(name, this.collection(name));
}
if (args.length === 2 && args[1].constructor === SkinCollection) {
this._collections[name] = args[1];
Object.defineProperty(this, name, {
value: args[1],
writable: false,
enumerable: true
});
// support bind for system.js
var names = name.split('.');
if (names.length > 1){
var prev = this, next;
for (var i = 0; i < names.length - 1; i++) {
next = prev[names[i]];
if (!next) {
next = {};
Object.defineProperty(prev, names[i], {
value: next,
writable: false,
enumerable : true
});
}
prev = next;
}
Object.defineProperty(prev, names[names.length - 1], {
value: args[1],
writable: false,
enumerable : true
});
}
return args[1];
}
var isOptions = false;
var argsIndex = 1;
if (options && typeof options === 'object') {
isOptions = true;
argsIndex = 2;
for (var k in options) {
if (typeof options[k] === 'function') {
isOptions = false;
argsIndex = 1;
break;
}
}
}
var collection = this.collection(name, isOptions ? options : null);
for (var len = args.length; argsIndex < len; argsIndex++) {
var extend = args[argsIndex];
if (typeof extend !== 'object') {
throw new Error('the args[' + argsIndex + '] should be object, but is `' + extend + '`');
}
utils.extend(collection, extend);
}
return this.bind(name, collection);
};
var IGNORE_NAMES = [
'bind', 'open', 'close', 'collection', 'admin', 'state'
];
// bind method of mongodb.Db to SkinDb
for (var key in Db.prototype) {
if (!key || key[0] === '_' || IGNORE_NAMES.indexOf(key) >= 0) {
continue;
}
var method = Db.prototype[key];
utils.bindSkin('SkinDb', SkinDb, 'db', key, method);
}

View File

@@ -0,0 +1,67 @@
/*!
* mongoskin - gridfs.js
*
* Copyright(c) 2011 - 2012 kissjs.org
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
var GridStore = require('mongodb').GridStore;
var utils = require('./utils');
/**
* @param filename: filename or ObjectId
*/
var SkinGridStore = exports.SkinGridStore = function (skinDb) {
utils.SkinObject.call(this);
this.skinDb = skinDb;
};
utils.inherits(SkinGridStore, utils.SkinObject);
/**
* @param id
* @param filename
* @param mode
* @param options
* @param callback
* callback(err, gridStoreObject)
*/
SkinGridStore.prototype.open = function (id, filename, mode, options, callback) {
var args = Array.prototype.slice.call(arguments);
callback = args.pop();
this.skinDb.open(function (err, db) {
var gs = new GridStore(db, args[0], args[1], args[2], args[3]);
var props = {
length: gs.length,
contentType: gs.contentType,
uploadDate: gs.uploadDate,
metadata: gs.metadata,
chunkSize: gs.chunkSize
};
gs.open(function (error, reply) {
callback(error, reply, props);
});
});
};
/**
* @param filename: filename or ObjectId
*/
SkinGridStore.prototype.unlink = SkinGridStore.prototype.remove = function (filename, callback) {
this.skinDb.open(function (err, db) {
GridStore.unlink(db, filename, callback);
});
};
SkinGridStore.prototype.exist = function (filename, rootCollection, callback) {
this.skinDb.open(function (err, db) {
GridStore.exist(db, filename, rootCollection, callback);
});
};

View File

@@ -0,0 +1,200 @@
/*!
* mongoskin - index.js
*
* Copyright(c) 2011 - 2012 kissjs.org
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
var url = require('url');
var Router = require('./router').Router;
var mongo = require('mongodb');
var SkinServer = require('./server').SkinServer;
var SkinDb =require('./db').SkinDb;
var Db = mongo.Db;
var Server = mongo.Server;
var ReplSetServers = mongo.ReplSetServers;
var BSONNative = mongo.BSONNative;
var constant = require('./constant');
var DEFAULT_PORT = constant.DEFAULT_PORT;
function toBool(value) {
return value !== undefined && value !== 'false' && value !== 'no' && value !== 'off';
}
/**
* parse the database url to config
*
* [*://]username:password@host[:port]/database?options
*
* @param {String} serverUrl
* @return {Object} config
* - {String} host
* - {Number} port, default is `DEFAULT_PORT`.
* - {String} [database], no database by default.
* - {Object} options
* - {Bool} auto_reconnect, default is `false`.
* - {Number} poolSize, default is `1`.
* - {String} [username], no username by default.
* - {String} [password], no password by default.
* @api private
*/
var parseUrl = function (serverUrl) {
serverUrl = /\w+:\/\//.test(serverUrl) ? serverUrl : 'db://' + serverUrl;
var uri = url.parse(serverUrl, true);
var config = {};
var serverOptions = uri.query;
config.host = uri.hostname;
config.port = parseInt(uri.port, 10) || DEFAULT_PORT;
if (uri.pathname) {
config.database = uri.pathname.replace(/\//g, '');
}
config.options = {};
config.options.auto_reconnect = toBool(serverOptions.auto_reconnect);
config.options.poolSize = parseInt(serverOptions.poolSize || 1, 10);
if (uri && uri.auth) {
var auth = uri.auth;
var separator = auth.indexOf(':');
config.username = auth.substr(0, separator);
config.password = auth.substr(separator + 1);
}
return config;
};
/**
* constructor Server from url
*
* @param {String} serverUrl
* @return {Server}
* @api private
*/
var parseServer = function (serverUrl) {
var config = parseUrl(serverUrl);
return new Server(config.host, config.port, config.options);
};
/*
* exports mongo classes ObjectID Long Code DbRef ... to mongoskin
*/
for (var key in mongo) {
exports[key] = mongo[key];
}
/**
* constructor SkinDb from serverURL[s]
*
* ReplicaSet: mongoskin.db(serverURLs, dbOptions, replicasetOptions)
*
* ```js
* mongoskin.db([
* '192.168.0.1:27017/',
* '192.168.0.2/?auto_reconnect',
* '192.168.0.3'
* ], {database: 'mydb'}, {connectArbiter: false, socketOptions: {timeout: 2000}});
* ```
*
* Single Server: mongoskin.db(dbURL, options)
*
* ```js
* mongoskin.db('192.168.0.1:27017/mydb');
* // or
* mongoskin.db('192.168.0.1:27017', {database: 'mydb'});
* // set the connection timeout to `2000ms`
* mongoskin.db('192.168.0.1:27017', {database: 'mydb', socketOptions: {timeout: 2000}});
* ```
*
* @param {String|Array} serverUrl or server urls.
* @param {Object} [dbOptions]
* - {Object} socketOptions: @see http://mongodb.github.com/node-mongodb-native/markdown-docs/database.html#socket-options
* - the other, @see http://mongodb.github.com/node-mongodb-native/markdown-docs/database.html#db-options
* @param {Object} [replicasetOptions], options for replicaset.
* The detail of this options, please
* @see https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/connection/repl_set.js#L27.
* @return {SkinDb}
* @api public
*/
exports.db = function (serverUrl, dbOptions, replicasetOptions) {
dbOptions = dbOptions || {};
var server, database, config;
if (Array.isArray(serverUrl)) {
if (!dbOptions.database) {
throw new Error('Please provide a database in `dbOptions` to connect.');
}
database = dbOptions.database;
var len = serverUrl.length;
var servers = [];
for (var i = 0; i < len; i++) {
config = parseUrl(serverUrl[i]);
if (config.database || config.username) {
console.log('MONGOSKIN:WARN: database or username found in RepliSet server URL, ' + serverUrl[i]);
}
servers.push(new Server(config.host, config.port, config.options));
}
server = new ReplSetServers(servers, replicasetOptions);
} else {
config = parseUrl(serverUrl);
database = dbOptions.database || config.database;
if (!database) {
throw new Error('Please provide a database to connect to.');
}
var socketOptions = dbOptions.socketOptions;
if (socketOptions) {
delete dbOptions.socketOptions;
config.options.socketOptions = socketOptions;
}
server = new Server(config.host, config.port, config.options);
if (dbOptions.username === undefined) {
dbOptions.username = config.username;
dbOptions.password = config.password;
}
}
var skinServer = new SkinServer(server);
return skinServer.db(database, dbOptions);
};
/**
* select different db by collection name
*
* @param select `function(name)` returns SkinDb
*
* ```js
* var router = mongoskin.router(function (name) {
* swhich (name) {
* case 'user':
* case 'group':
* return authDb;
* default:
* return appDb;
* }
* });
* router.collection('user')
* ```
*
* @param {Function(name)} select
* @return {Router}
* @api public
*/
exports.router = function (select) {
return new Router(select);
};
/*
* export Skin classes from ./db ./collection ./cursor ./admin
*/
['server', 'db', 'collection', 'cursor', 'admin'].forEach(function (path) {
var module = require('./' + path);
for (var name in module) {
exports[name] = module[name];
}
});

View File

@@ -0,0 +1,49 @@
/*!
* mongoskin - router.js
*
* Copyright(c) 2011 - 2012 kissjs.org
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
/**
* Router
*
* @param {Function(name)} select
* @constructor
* @api public
*/
var Router = exports.Router = function (select) {
this._select = select;
this._collections = {};
};
/**
* Bind custom methods
*
* @param {String} name, collection name.
* @param {Object} [options]
* @return {Router} this
* @api public
*/
Router.prototype.bind = function (name, options) {
var args = Array.prototype.slice.call(arguments);
var database = this._select(name);
var collection = database.bind.apply(database, args);
this._collections[name] = collection;
Object.defineProperty(this, name, {
value: collection,
writable: false,
enumerable: true
});
return this;
};
Router.prototype.collection = function (name) {
return this._collections[name] || (this._collections[name] = this._select(name).collection(name));
};

View File

@@ -0,0 +1,54 @@
/*!
* mongoskin - server.js
*
* Copyright(c) 2011 - 2012 kissjs.org
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
var mongodb = require('mongodb');
var Db = mongodb.Db;
var Server = mongodb.Server;
var SkinDb = require('./db').SkinDb;
/**
* Construct SkinServer with native Server
*
* @param {Server} server
* @constructor
* @api public
*/
var SkinServer = exports.SkinServer = function (server) {
this.server = server;
this._cache_ = {};
};
/**
* Create SkinDb from a SkinServer
*
* @param {String} name database name
* @param {Object} [options]
* @return {SkinDb}
* @api public
*/
SkinServer.prototype.db = function (name, options) {
options = options || {};
var username = options.username || '';
var key = username + '@' + name;
var skinDb = this._cache_[key];
if (!skinDb || skinDb.fail) {
var password = options.password;
if (!options.native_parser) {
options.native_parser = !! mongodb.BSONNative;
}
var db = new Db(name, this.server, options);
skinDb = new SkinDb(db, username, password);
this._cache_[key] = skinDb;
}
return skinDb;
};

View File

@@ -0,0 +1,82 @@
/*!
* mongoskin - utils.js
*
* Copyright(c) 2011 - 2012 kissjs.org
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
var __slice = Array.prototype.slice;
var EventEmitter = require('events').EventEmitter;
var constant = require('./constant');
var STATE_OPEN = constant.STATE_OPEN;
var STATE_OPENNING = constant.STATE_OPENNING;
var STATE_CLOSE = constant.STATE_CLOSE;
exports.inherits = require('util').inherits;
exports.error = function (err, args, name) {
var cb = args.pop();
if (cb && typeof cb === 'function') {
cb(err);
} else {
console.error("Error occured with no callback to handle it while calling " + name, err);
}
};
/**
* SkinObject
*
* @constructor
* @api public
*/
exports.SkinObject = function () {
this.emitter = new EventEmitter();
this.state = STATE_CLOSE;
};
/**
* Skin method binding.
*
* @param {String} objName
* @param {Function} obj
* @param {String} nativeObjName
* @param {String} methodName
* @param {Function} method
* @return {Function}
*/
exports.bindSkin = function (objName, obj, nativeObjName, methodName, method) {
if (typeof method !== 'function') {
return;
}
return obj.prototype[methodName] = function () {
var args = __slice.call(arguments);
if (this.state === STATE_OPEN) {
method.apply(this[nativeObjName], args);
return this;
}
this.open(function (err, nativeObj) {
if (err) {
exports.error(err, args, objName + '.' + methodName);
} else {
return method.apply(nativeObj, args);
}
});
return this;
};
};
exports.extend = function (destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
exports.noop = function () {};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.6
- 0.8
- 0.9 # development version of 0.8, may be unstable

View File

@@ -0,0 +1,23 @@
## Contributing to the driver
### Bugfixes
- Before starting to write code, look for existing [tickets](https://github.com/mongodb/node-mongodb-native/issues) or [create one](https://github.com/mongodb/node-mongodb-native/issues/new) for your specific issue. That way you avoid working on something that might not be of interest or that has been addressed already in a different branch.
- Fork the [repo](https://github.com/mongodb/node-mongodb-native) _or_ for small documentation changes, navigate to the source on github and click the [Edit](https://github.com/blog/844-forking-with-the-edit-button) button.
- Follow the general coding style of the rest of the project:
- 2 space tabs
- no trailing whitespace
- comma last
- inline documentation for new methods, class members, etc
- 0 space between conditionals/functions, and their parenthesis and curly braces
- `if(..) {`
- `for(..) {`
- `while(..) {`
- `function(err) {`
- Write tests and make sure they pass (execute `make test` from the cmd line to run the test suite).
### Documentation
To contribute to the [API documentation](http://mongodb.github.com/node-mongodb-native/) just make your changes to the inline documentation of the appropriate [source code](https://github.com/mongodb/node-mongodb-native/tree/master/docs) in the master branch and submit a [pull request](https://help.github.com/articles/using-pull-requests/). You might also use the github [Edit](https://github.com/blog/844-forking-with-the-edit-button) button.
If you'd like to preview your documentation changes, first commit your changes to your local master branch, then execute `make generate_docs`. Make sure you have the python documentation framework sphinx installed `easy_install sphinx`. The docs are generated under `docs/build'. If all looks good, submit a [pull request](https://help.github.com/articles/using-pull-requests/) to the master branch with your changes.

View File

@@ -0,0 +1,64 @@
NODE = node
NPM = npm
NODEUNIT = node_modules/nodeunit/bin/nodeunit
DOX = node_modules/dox/bin/dox
name = all
total: build_native
test-coverage:
rm -rf lib-cov/
jscoverage lib/ lib-cov/
@TEST_COVERAGE=true nodeunit test/ test/gridstore test/connection
build_native:
test: build_native
@echo "\n == Run All tests minus replicaset tests=="
$(NODE) dev/tools/test_all.js --noreplicaset --boot
test_pure: build_native
@echo "\n == Run All tests minus replicaset tests=="
$(NODE) dev/tools/test_all.js --noreplicaset --boot --nonative
test_junit: build_native
@echo "\n == Run All tests minus replicaset tests=="
$(NODE) dev/tools/test_all.js --junit --noreplicaset --nokill
jenkins: build_native
@echo "\n == Run All tests minus replicaset tests=="
$(NODE) dev/tools/test_all.js --junit --noreplicaset --nokill
test_nodeunit_pure:
@echo "\n == Execute Test Suite using Pure JS BSON Parser == "
@$(NODEUNIT) test/ test/gridstore test/bson
test_nodeunit_replicaset_pure:
@echo "\n == Execute Test Suite using Pure JS BSON Parser == "
@$(NODEUNIT) test/replicaset
test_nodeunit_native:
@echo "\n == Execute Test Suite using Native BSON Parser == "
@TEST_NATIVE=TRUE $(NODEUNIT) test/ test/gridstore test/bson
test_nodeunit_replicaset_native:
@echo "\n == Execute Test Suite using Native BSON Parser == "
@TEST_NATIVE=TRUE $(NODEUNIT) test/replicaset
test_all: build_native
@echo "\n == Run All tests =="
$(NODE) dev/tools/test_all.js --boot
test_all_junit: build_native
@echo "\n == Run All tests =="
$(NODE) dev/tools/test_all.js --junit --boot
clean:
rm ./external-libs/bson/bson.node
rm -r ./external-libs/bson/build
generate_docs:
$(NODE) dev/tools/build-docs.js
make --directory=./docs/sphinx-docs --file=Makefile html
.PHONY: total

View File

@@ -0,0 +1,438 @@
Up to date documentation
========================
[Documentation](http://mongodb.github.com/node-mongodb-native/)
Install
=======
To install the most recent release from npm, run:
npm install mongodb
That may give you a warning telling you that bugs['web'] should be bugs['url'], it would be safe to ignore it (this has been fixed in the development version)
To install the latest from the repository, run::
npm install path/to/node-mongodb-native
Community
=========
Check out the google group [node-mongodb-native](http://groups.google.com/group/node-mongodb-native) for questions/answers from users of the driver.
Introduction
============
This is a node.js driver for MongoDB. It's a port (or close to a port) of the library for ruby at http://github.com/mongodb/mongo-ruby-driver/.
A simple example of inserting a document.
```javascript
var client = new Db('test', new Server("127.0.0.1", 27017, {})),
test = function (err, collection) {
collection.insert({a:2}, function(err, docs) {
collection.count(function(err, count) {
test.assertEquals(1, count);
});
// Locate all the entries using find
collection.find().toArray(function(err, results) {
test.assertEquals(1, results.length);
test.assertTrue(results[0].a === 2);
// Let's close the db
client.close();
});
});
};
client.open(function(err, p_client) {
client.collection('test_insert', test);
});
```
Data types
==========
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:
```javascript
// Get the objectID type
var ObjectID = require('mongodb').ObjectID;
var idString = '4e4e1638c85e808431000003';
collection.findOne({_id: new ObjectID(idString)}, console.log) // ok
collection.findOne({_id: idString}, console.log) // wrong! callback gets undefined
```
Here are the constructors the non-Javascript BSON primitive types:
```javascript
// 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
```
The C/C++ bson parser/serializer
--------------------------------
If you are running a version of this library has the C/C++ parser compiled, to enable the driver to use the C/C++ bson parser pass it the option native_parser:true like below
```javascript
// using native_parser:
var client = new Db('integration_tests_20',
new Server("127.0.0.1", 27017),
{native_parser:true});
```
The C++ parser uses the js objects both for serialization and deserialization.
GitHub information
==================
The source code is available at http://github.com/mongodb/node-mongodb-native.
You can either clone the repository or download a tarball of the latest release.
Once you have the source you can test the driver by running
$ make test
in the main directory. You will need to have a mongo instance running on localhost for the integration tests to pass.
Examples
========
For examples look in the examples/ directory. You can execute the examples using node.
$ cd examples
$ node queries.js
GridStore
=========
The GridStore class allows for storage of binary files in mongoDB using the mongoDB defined files and chunks collection definition.
For more information have a look at [Gridstore](https://github.com/mongodb/node-mongodb-native/blob/master/docs/gridfs.md)
Replicasets
===========
For more information about how to connect to a replicaset have a look at [Replicasets](https://github.com/mongodb/node-mongodb-native/blob/master/docs/replicaset.md)
Primary Key Factories
---------------------
Defining your own primary key factory allows you to generate your own series of id's
(this could f.ex be to use something like ISBN numbers). The generated the id needs to be a 12 byte long "string".
Simple example below
```javascript
// Custom factory (need to provide a 12 byte array);
CustomPKFactory = function() {}
CustomPKFactory.prototype = new Object();
CustomPKFactory.createPk = function() {
return new ObjectID("aaaaaaaaaaaa");
}
var p_client = new Db('integration_tests_20', new Server("127.0.0.1", 27017, {}), {'pk':CustomPKFactory});
p_client.open(function(err, p_client) {
p_client.dropDatabase(function(err, done) {
p_client.createCollection('test_custom_key', function(err, collection) {
collection.insert({'a':1}, function(err, docs) {
collection.find({'_id':new ObjectID("aaaaaaaaaaaa")}, function(err, cursor) {
cursor.toArray(function(err, items) {
test.assertEquals(1, items.length);
// Let's close the db
p_client.close();
});
});
});
});
});
});
```
Strict mode
-----------
Each database has an optional strict mode. If it is set then asking for a collection
that does not exist will return an Error object in the callback. Similarly if you
attempt to create a collection that already exists. Strict is provided for convenience.
```javascript
var error_client = new Db('integration_tests_', new Server("127.0.0.1", 27017, {auto_reconnect: false}), {strict:true});
test.assertEquals(true, error_client.strict);
error_client.open(function(err, error_client) {
error_client.collection('does-not-exist', function(err, collection) {
test.assertTrue(err instanceof Error);
test.assertEquals("Collection does-not-exist does not exist. Currently in strict mode.", err.message);
});
error_client.createCollection('test_strict_access_collection', function(err, collection) {
error_client.collection('test_strict_access_collection', function(err, collection) {
test.assertTrue(collection instanceof Collection);
// Let's close the db
error_client.close();
});
});
});
```
Documentation
=============
If this document doesn't answer your questions, see the source of
[Collection](https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/collection.js)
or [Cursor](https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/cursor.js),
or the documentation at MongoDB for query and update formats.
Find
----
The find method is actually a factory method to create
Cursor objects. A Cursor lazily uses the connection the first time
you call `nextObject`, `each`, or `toArray`.
The basic operation on a cursor is the `nextObject` method
that fetches the next matching document from the database. The convenience
methods `each` and `toArray` call `nextObject` until the cursor is exhausted.
Signatures:
```javascript
var cursor = collection.find(query, [fields], options);
cursor.sort(fields).limit(n).skip(m).
cursor.nextObject(function(err, doc) {});
cursor.each(function(err, doc) {});
cursor.toArray(function(err, docs) {});
cursor.rewind() // reset the cursor to its initial state.
```
Useful chainable methods of cursor. These can optionally be options of `find` instead of method calls:
* `.limit(n).skip(m)` to control paging.
* `.sort(fields)` Order by the given fields. There are several equivalent syntaxes:
* `.sort({field1: -1, field2: 1})` descending by field1, then ascending by field2.
* `.sort([['field1', 'desc'], ['field2', 'asc']])` same as above
* `.sort([['field1', 'desc'], 'field2'])` same as above
* `.sort('field1')` ascending by field1
Other options of `find`:
* `fields` the fields to fetch (to avoid transferring the entire document)
* `tailable` if true, makes the cursor [tailable](http://www.mongodb.org/display/DOCS/Tailable+Cursors).
* `batchSize` The number of the subset of results to request the database
to return for every request. This should initially be greater than 1 otherwise
the database will automatically close the cursor. The batch size can be set to 1
with `batchSize(n, function(err){})` after performing the initial query to the database.
* `hint` See [Optimization: hint](http://www.mongodb.org/display/DOCS/Optimization#Optimization-Hint).
* `explain` turns this into an explain query. You can also call
`explain()` on any cursor to fetch the explanation.
* `snapshot` prevents documents that are updated while the query is active
from being returned multiple times. See more
[details about query snapshots](http://www.mongodb.org/display/DOCS/How+to+do+Snapshotted+Queries+in+the+Mongo+Database).
* `timeout` if false, asks MongoDb not to time out this cursor after an
inactivity period.
For information on how to create queries, see the
[MongoDB section on querying](http://www.mongodb.org/display/DOCS/Querying).
```javascript
var mongodb = require('mongodb');
var server = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('test', server, {}).open(function (error, client) {
if (error) throw error;
var collection = new mongodb.Collection(client, 'test_collection');
collection.find({}, {limit:10}).toArray(function(err, docs) {
console.dir(docs);
});
});
```
Insert
------
Signature:
```javascript
collection.insert(docs, options, [callback]);
```
where `docs` can be a single document or an array of documents.
Useful options:
* `safe:true` Should always set if you have a callback.
See also: [MongoDB docs for insert](http://www.mongodb.org/display/DOCS/Inserting).
```javascript
var mongodb = require('mongodb');
var server = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('test', server, {}).open(function (error, client) {
if (error) throw error;
var collection = new mongodb.Collection(client, 'test_collection');
collection.insert({hello: 'world'}, {safe:true},
function(err, objects) {
if (err) console.warn(err.message);
if (err && err.message.indexOf('E11000 ') !== -1) {
// this _id was already inserted in the database
}
});
});
```
Note that there's no reason to pass a callback to the insert or update commands
unless you use the `safe:true` option. If you don't specify `safe:true`, then
your callback will be called immediately.
Update; update and insert (upsert)
----------------------------------
The update operation will update the first document that matches your query
(or all documents that match if you use `multi:true`).
If `safe:true`, `upsert` is not set, and no documents match, your callback will return 0 documents updated.
See the [MongoDB docs](http://www.mongodb.org/display/DOCS/Updating) for
the modifier (`$inc`, `$set`, `$push`, etc.) formats.
Signature:
```javascript
collection.update(criteria, objNew, options, [callback]);
```
Useful options:
* `safe:true` Should always set if you have a callback.
* `multi:true` If set, all matching documents are updated, not just the first.
* `upsert:true` Atomically inserts the document if no documents matched.
Example for `update`:
```javascript
var mongodb = require('mongodb');
var server = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('test', server, {}).open(function (error, client) {
if (error) throw error;
var collection = new mongodb.Collection(client, 'test_collection');
collection.update({hi: 'here'}, {$set: {hi: 'there'}}, {safe:true},
function(err) {
if (err) console.warn(err.message);
else console.log('successfully updated');
});
});
```
Find and modify
---------------
`findAndModify` is like `update`, but it also gives the updated document to
your callback. But there are a few key differences between findAndModify and
update:
1. The signatures differ.
2. You can only findAndModify a single item, not multiple items.
Signature:
```javascript
collection.findAndModify(query, sort, update, options, callback)
```
The sort parameter is used to specify which object to operate on, if more than
one document matches. It takes the same format as the cursor sort (see
Connection.find above).
See the
[MongoDB docs for findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command)
for more details.
Useful options:
* `remove:true` set to a true to remove the object before returning
* `new:true` set to true if you want to return the modified object rather than the original. Ignored for remove.
* `upsert:true` Atomically inserts the document if no documents matched.
Example for `findAndModify`:
```javascript
var mongodb = require('mongodb');
var server = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('test', server, {}).open(function (error, client) {
if (error) throw error;
var collection = new mongodb.Collection(client, 'test_collection');
collection.findAndModify({hello: 'world'}, [['_id','asc']], {$set: {hi: 'there'}}, {},
function(err, object) {
if (err) console.warn(err.message);
else console.dir(object); // undefined if no matching object exists.
});
});
```
Save
----
The `save` method is a shorthand for upsert if the document contains an
`_id`, or an insert if there is no `_id`.
Sponsors
========
Just as Felix Geisendörfer I'm also working on the driver for my own startup and this driver is a big project that also benefits other companies who are using MongoDB.
If your company could benefit from a even better-engineered node.js mongodb driver I would appreciate any type of sponsorship you may be able to provide. All the sponsors will get a lifetime display in this readme, priority support and help on problems and votes on the roadmap decisions for the driver. If you are interested contact me on [christkv AT g m a i l.com](mailto:christkv@gmail.com) for details.
And I'm very thankful for code contributions. If you are interested in working on features please contact me so we can discuss API design and testing.
Release Notes
=============
See HISTORY
Credits
=======
1. [10gen](http://github.com/mongodb/mongo-ruby-driver/)
2. [Google Closure Library](http://code.google.com/closure/library/)
3. [Jonas Raoni Soares Silva](http://jsfromhell.com/classes/binary-parser)
Contributors
============
Aaron Heckmann, Christoph Pojer, Pau Ramon Revilla, Nathan White, Emmerman, Seth LaForge, Boris Filipov, Stefan Schärmeli, Tedde Lundgren, renctan, Sergey Ukustov, Ciaran Jessup, kuno, srimonti, Erik Abele, Pratik Daga, Slobodan Utvic, Kristina Chodorow, Yonathan Randolph, Brian Noguchi, Sam Epstein, James Harrison Fisher, Vladimir Dronnikov, Ben Hockey, Henrik Johansson, Simon Weare, Alex Gorbatchev, Shimon Doodkin, Kyle Mueller, Eran Hammer-Lahav, Marcin Ciszak, François de Metz, Vinay Pulim, nstielau, Adam Wiggins, entrinzikyl, Jeremy Selier, Ian Millington, Public Keating, andrewjstone, Christopher Stott, Corey Jewett, brettkiefer, Rob Holland, Senmiao Liu, heroic, gitfy
License
=======
Copyright 2009 - 2012 Christian Amor Kvalheim.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -0,0 +1 @@
module.exports = require('./lib/mongodb');

View File

@@ -0,0 +1,40 @@
var spawn = require('child_process').spawn,
exec = require('child_process').exec;
process.stdout.write("================================================================================\n");
process.stdout.write("= =\n");
process.stdout.write("= To install with C++ bson parser do <npm install mongodb --mongodb:native> =\n");
process.stdout.write("= =\n");
process.stdout.write("================================================================================\n");
// Check if we want to build the native code
var build_native = process.env['npm_package_config_native'] != null ? process.env['npm_package_config_native'] : 'false';
build_native = build_native == 'true' ? true : false;
// If we are building the native bson extension ensure we use gmake if available
if(build_native) {
// Check if we need to use gmake
exec('which gmake', function(err, stdout, stderr) {
// Set up spawn command
var make = null;
// No gmake build using make
if(err != null) {
make = spawn('make', ['total']);
} else {
make = spawn('gmake', ['total']);
}
// Execute spawn
make.stdout.on('data', function(data) {
process.stdout.write(data);
})
make.stderr.on('data', function(data) {
process.stdout.write(data);
})
make.on('exit', function(code) {
process.stdout.write('child process exited with code ' + code + "\n");
})
});
}

View File

@@ -0,0 +1,340 @@
/*!
* Module dependencies.
*/
var Collection = require('./collection').Collection,
Cursor = require('./cursor').Cursor,
DbCommand = require('./commands/db_command').DbCommand;
/**
* Allows the user to access the admin functionality of MongoDB
*
* @class Represents the Admin methods of MongoDB.
* @param {Object} db Current db instance we wish to perform Admin operations on.
* @return {Function} Constructor for Admin type.
*/
function Admin(db) {
if(!(this instanceof Admin)) return new Admin(db);
this.db = db;
};
/**
* Retrieve the server information for the current
* instance of the db client
*
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.buildInfo = function(callback) {
this.serverInfo(callback);
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api private
*/
Admin.prototype.serverInfo = function(callback) {
this.db.executeDbAdminCommand({buildinfo:1}, function(err, doc) {
if(err != null) return callback(err, null);
return callback(null, doc.documents[0]);
});
}
/**
* Retrieve this db's server status.
*
* @param {Function} callback returns the server status.
* @return {null}
* @api public
*/
Admin.prototype.serverStatus = function(callback) {
var self = this;
this.db.executeDbAdminCommand({serverStatus: 1}, function(err, doc) {
if(err == null && doc.documents[0].ok === 1) {
callback(null, doc.documents[0]);
} else {
if(err) return callback(err, false);
return callback(self.db.wrap(doc.documents[0]), false);
}
});
};
/**
* Retrieve the current profiling Level for MongoDB
*
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.profilingLevel = function(callback) {
var self = this;
this.db.executeDbAdminCommand({profile:-1}, function(err, doc) {
doc = doc.documents[0];
if(err == null && doc.ok === 1) {
var was = doc.was;
if(was == 0) return callback(null, "off");
if(was == 1) return callback(null, "slow_only");
if(was == 2) return callback(null, "all");
return callback(new Error("Error: illegal profiling level value " + was), null);
} else {
err != null ? callback(err, null) : callback(new Error("Error with profile command"), null);
}
});
};
/**
* Ping the MongoDB server and retrieve results
*
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.ping = function(options, callback) {
// Unpack calls
var args = Array.prototype.slice.call(arguments, 0);
callback = args.pop();
this.db.executeDbAdminCommand({ping: 1}, callback);
}
/**
* Authenticate against MongoDB
*
* @param {String} username The user name for the authentication.
* @param {String} password The password for the authentication.
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.authenticate = function(username, password, callback) {
this.db.authenticate(username, password, {authdb: 'admin'}, function(err, doc) {
return callback(err, doc);
})
}
/**
* Logout current authenticated user
*
* @param {Object} [options] Optional parameters to the command.
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.logout = function(callback) {
this.db.logout({authdb: 'admin'}, function(err, doc) {
return callback(err, doc);
})
}
/**
* Add a user to the MongoDB server, if the user exists it will
* overwrite the current password
*
* Options
* - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
*
* @param {String} username The user name for the authentication.
* @param {String} password The password for the authentication.
* @param {Object} [options] additional options during update.
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.addUser = function(username, password, options, callback) {
var args = Array.prototype.slice.call(arguments, 2);
callback = args.pop();
options = args.length ? args.shift() : {};
options.dbName = 'admin';
// Add user
this.db.addUser(username, password, options, function(err, doc) {
return callback(err, doc);
})
}
/**
* Remove a user from the MongoDB server
*
* Options
* - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
*
* @param {String} username The user name for the authentication.
* @param {Object} [options] additional options during update.
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.removeUser = function(username, options, callback) {
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
callback = args.pop();
options = args.length ? args.shift() : {};
options.dbName = 'admin';
this.db.removeUser(username, options, function(err, doc) {
return callback(err, doc);
})
}
/**
* Set the current profiling level of MongoDB
*
* @param {String} level The new profiling level (off, slow_only, all)
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.setProfilingLevel = function(level, callback) {
var self = this;
var command = {};
var profile = 0;
if(level == "off") {
profile = 0;
} else if(level == "slow_only") {
profile = 1;
} else if(level == "all") {
profile = 2;
} else {
return callback(new Error("Error: illegal profiling level value " + level));
}
// Set up the profile number
command['profile'] = profile;
this.db.executeDbAdminCommand(command, function(err, doc) {
doc = doc.documents[0];
if(err == null && doc.ok === 1)
return callback(null, level);
return err != null ? callback(err, null) : callback(new Error("Error with profile command"), null);
});
};
/**
* Retrive the current profiling information for MongoDB
*
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.profilingInfo = function(callback) {
try {
new Cursor(this.db, new Collection(this.db, DbCommand.SYSTEM_PROFILE_COLLECTION), {}, null, null, null
, null, null, null, null, null, null, null, null, null, null
, null, null, null, null, null, null, null, null, 'admin').toArray(function(err, items) {
return callback(err, items);
});
} catch (err) {
return callback(err, null);
}
};
/**
* Execute a db command against the Admin database
*
* @param {Object} command A command object `{ping:1}`.
* @param {Object} [options] Optional parameters to the command.
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.command = function(command, options, callback) {
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
callback = args.pop();
options = args.length ? args.shift() : {};
// Execute a command
this.db.executeDbAdminCommand(command, options, function(err, doc) {
// Ensure change before event loop executes
return callback != null ? callback(err, doc) : null;
});
}
/**
* Validate an existing collection
*
* @param {String} collectionName The name of the collection to validate.
* @param {Object} [options] Optional parameters to the command.
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.validateCollection = function(collectionName, options, callback) {
var args = Array.prototype.slice.call(arguments, 1);
callback = args.pop();
options = args.length ? args.shift() : {};
var self = this;
var command = {validate: collectionName};
var keys = Object.keys(options);
// Decorate command with extra options
for(var i = 0; i < keys.length; i++) {
if(options.hasOwnProperty(keys[i])) {
command[keys[i]] = options[keys[i]];
}
}
this.db.executeDbCommand(command, function(err, doc) {
if(err != null) return callback(err, null);
doc = doc.documents[0];
if(doc.ok === 0)
return callback(new Error("Error with validate command"), null);
if(doc.result != null && doc.result.constructor != String)
return callback(new Error("Error with validation data"), null);
if(doc.result != null && doc.result.match(/exception|corrupt/) != null)
return callback(new Error("Error: invalid collection " + collectionName), null);
if(doc.valid != null && !doc.valid)
return callback(new Error("Error: invalid collection " + collectionName), null);
return callback(null, doc);
});
};
/**
* List the available databases
*
* @param {Function} callback Callback function of format `function(err, result) {}`.
* @return {null} Returns no result
* @api public
*/
Admin.prototype.listDatabases = function(callback) {
// Execute the listAllDatabases command
this.db.executeDbAdminCommand({listDatabases:1}, {}, function(err, doc) {
if(err != null) return callback(err, null);
return callback(null, doc.documents[0]);
});
}
/**
* Get ReplicaSet status
*
* @param {Function} callback returns the replica set status (if available).
* @return {null}
* @api public
*/
Admin.prototype.replSetGetStatus = function(callback) {
var self = this;
this.db.executeDbAdminCommand({replSetGetStatus:1}, function(err, doc) {
if(err == null && doc.documents[0].ok === 1)
return callback(null, doc.documents[0]);
if(err) return callback(err, false);
return callback(self.db.wrap(doc.documents[0]), false);
});
};
/**
* @ignore
*/
exports.Admin = Admin;

View File

@@ -0,0 +1,29 @@
/**
Base object used for common functionality
**/
var BaseCommand = exports.BaseCommand = function BaseCommand() {
};
var id = 1;
BaseCommand.prototype.getRequestId = function getRequestId() {
if (!this.requestId) this.requestId = id++;
return this.requestId;
};
BaseCommand.prototype.setMongosReadPreference = function setMongosReadPreference(readPreference, tags) {}
BaseCommand.prototype.updateRequestId = function() {
this.requestId = id++;
return this.requestId;
};
// OpCodes
BaseCommand.OP_REPLY = 1;
BaseCommand.OP_MSG = 1000;
BaseCommand.OP_UPDATE = 2001;
BaseCommand.OP_INSERT = 2002;
BaseCommand.OP_GET_BY_OID = 2003;
BaseCommand.OP_QUERY = 2004;
BaseCommand.OP_GET_MORE = 2005;
BaseCommand.OP_DELETE = 2006;
BaseCommand.OP_KILL_CURSORS = 2007;

View File

@@ -0,0 +1,214 @@
var QueryCommand = require('./query_command').QueryCommand,
InsertCommand = require('./insert_command').InsertCommand,
inherits = require('util').inherits,
crypto = require('crypto');
/**
Db Command
**/
var DbCommand = exports.DbCommand = function(dbInstance, collectionName, queryOptions, numberToSkip, numberToReturn, query, returnFieldSelector, options) {
QueryCommand.call(this);
this.collectionName = collectionName;
this.queryOptions = queryOptions;
this.numberToSkip = numberToSkip;
this.numberToReturn = numberToReturn;
this.query = query;
this.returnFieldSelector = returnFieldSelector;
this.db = dbInstance;
// Make sure we don't get a null exception
options = options == null ? {} : options;
// Let us defined on a command basis if we want functions to be serialized or not
if(options['serializeFunctions'] != null && options['serializeFunctions']) {
this.serializeFunctions = true;
}
};
inherits(DbCommand, QueryCommand);
// Constants
DbCommand.SYSTEM_NAMESPACE_COLLECTION = "system.namespaces";
DbCommand.SYSTEM_INDEX_COLLECTION = "system.indexes";
DbCommand.SYSTEM_PROFILE_COLLECTION = "system.profile";
DbCommand.SYSTEM_USER_COLLECTION = "system.users";
DbCommand.SYSTEM_COMMAND_COLLECTION = "$cmd";
DbCommand.SYSTEM_JS_COLLECTION = "system.js";
// New commands
DbCommand.NcreateIsMasterCommand = function(db, databaseName) {
return new DbCommand(db, databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'ismaster':1}, null);
};
// Provide constructors for different db commands
DbCommand.createIsMasterCommand = function(db) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'ismaster':1}, null);
};
DbCommand.createCollectionInfoCommand = function(db, selector) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_NAMESPACE_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, 0, selector, null);
};
DbCommand.createGetNonceCommand = function(db, options) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'getnonce':1}, null);
};
DbCommand.createAuthenticationCommand = function(db, username, password, nonce, authdb) {
// Use node md5 generator
var md5 = crypto.createHash('md5');
// Generate keys used for authentication
md5.update(username + ":mongo:" + password);
var hash_password = md5.digest('hex');
// Final key
md5 = crypto.createHash('md5');
md5.update(nonce + username + hash_password);
var key = md5.digest('hex');
// Creat selector
var selector = {'authenticate':1, 'user':username, 'nonce':nonce, 'key':key};
// Create db command
return new DbCommand(db, authdb + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NONE, 0, -1, selector, null);
};
DbCommand.createLogoutCommand = function(db) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'logout':1}, null);
};
DbCommand.createCreateCollectionCommand = function(db, collectionName, options) {
var selector = {'create':collectionName};
// Modify the options to ensure correct behaviour
for(var name in options) {
if(options[name] != null && options[name].constructor != Function) selector[name] = options[name];
}
// Execute the command
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, selector, null);
};
DbCommand.createDropCollectionCommand = function(db, collectionName) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'drop':collectionName}, null);
};
DbCommand.createRenameCollectionCommand = function(db, fromCollectionName, toCollectionName) {
var renameCollection = db.databaseName + "." + fromCollectionName;
var toCollection = db.databaseName + "." + toCollectionName;
return new DbCommand(db, "admin." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'renameCollection':renameCollection, 'to':toCollection}, null);
};
DbCommand.createGetLastErrorCommand = function(options, db) {
if (typeof db === 'undefined') {
db = options;
options = {};
}
// Final command
var command = {'getlasterror':1};
// If we have an options Object let's merge in the fields (fsync/wtimeout/w)
if('object' === typeof options) {
for(var name in options) {
command[name] = options[name]
}
}
// Execute command
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command, null);
};
DbCommand.createGetLastStatusCommand = DbCommand.createGetLastErrorCommand;
DbCommand.createGetPreviousErrorsCommand = function(db) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'getpreverror':1}, null);
};
DbCommand.createResetErrorHistoryCommand = function(db) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'reseterror':1}, null);
};
DbCommand.createCreateIndexCommand = function(db, collectionName, fieldOrSpec, options) {
var fieldHash = {};
var indexes = [];
var keys;
// Get all the fields accordingly
if (fieldOrSpec.constructor === String) { // 'type'
indexes.push(fieldOrSpec + '_' + 1);
fieldHash[fieldOrSpec] = 1;
} else if (fieldOrSpec.constructor === Array) { // [{location:'2d'}, ...]
fieldOrSpec.forEach(function(f) {
if (f.constructor === String) { // [{location:'2d'}, 'type']
indexes.push(f + '_' + 1);
fieldHash[f] = 1;
} else if (f.constructor === Array) { // [['location', '2d'],['type', 1]]
indexes.push(f[0] + '_' + (f[1] || 1));
fieldHash[f[0]] = f[1] || 1;
} else if (f.constructor === Object) { // [{location:'2d'}, {type:1}]
keys = Object.keys(f);
keys.forEach(function(k) {
indexes.push(k + '_' + f[k]);
fieldHash[k] = f[k];
});
} else {
// undefined
}
});
} else if (fieldOrSpec.constructor === Object) { // {location:'2d', type:1}
keys = Object.keys(fieldOrSpec);
keys.forEach(function(key) {
indexes.push(key + '_' + fieldOrSpec[key]);
fieldHash[key] = fieldOrSpec[key];
});
}
// Generate the index name
var indexName = typeof options.name == 'string' ? options.name : indexes.join("_");
// Build the selector
var selector = {'ns':(db.databaseName + "." + collectionName), 'key':fieldHash, 'name':indexName};
// Ensure we have a correct finalUnique
var finalUnique = options == null || 'object' === typeof options ? false : options;
// Set up options
options = options == null || typeof options == 'boolean' ? {} : options;
// Add all the options
var keys = Object.keys(options);
// Add all the fields to the selector
for(var i = 0; i < keys.length; i++) {
selector[keys[i]] = options[keys[i]];
}
// If we don't have the unique property set on the selector
if(selector['unique'] == null) selector['unique'] = finalUnique;
// Create the insert command for the index and return the document
return new InsertCommand(db, db.databaseName + "." + DbCommand.SYSTEM_INDEX_COLLECTION, false).add(selector);
};
DbCommand.logoutCommand = function(db, command_hash, options) {
var dbName = options != null && options['authdb'] != null ? options['authdb'] : db.databaseName;
// Create logout command
return new DbCommand(db, dbName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command_hash, null);
}
DbCommand.createDropIndexCommand = function(db, collectionName, indexName) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'deleteIndexes':collectionName, 'index':indexName}, null);
};
DbCommand.createReIndexCommand = function(db, collectionName) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'reIndex':collectionName}, null);
};
DbCommand.createDropDatabaseCommand = function(db) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'dropDatabase':1}, null);
};
DbCommand.createDbCommand = function(db, command_hash, options) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command_hash, null, options);
};
DbCommand.createAdminDbCommand = function(db, command_hash) {
return new DbCommand(db, "admin." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command_hash, null);
};
DbCommand.createAdminDbCommandSlaveOk = function(db, command_hash) {
return new DbCommand(db, "admin." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT | QueryCommand.OPTS_SLAVE, 0, -1, command_hash, null);
};
DbCommand.createDbSlaveOkCommand = function(db, command_hash, options) {
return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT | QueryCommand.OPTS_SLAVE, 0, -1, command_hash, null, options);
};

View File

@@ -0,0 +1,114 @@
var BaseCommand = require('./base_command').BaseCommand,
inherits = require('util').inherits;
/**
Insert Document Command
**/
var DeleteCommand = exports.DeleteCommand = function(db, collectionName, selector, flags) {
BaseCommand.call(this);
// Validate correctness off the selector
var object = selector;
if(Buffer.isBuffer(object)) {
var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
if(object_size != object.length) {
var error = new Error("delete raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
error.name = 'MongoError';
throw error;
}
}
this.flags = flags;
this.collectionName = collectionName;
this.selector = selector;
this.db = db;
};
inherits(DeleteCommand, BaseCommand);
DeleteCommand.OP_DELETE = 2006;
/*
struct {
MsgHeader header; // standard message header
int32 ZERO; // 0 - reserved for future use
cstring fullCollectionName; // "dbname.collectionname"
int32 ZERO; // 0 - reserved for future use
mongo.BSON selector; // query object. See below for details.
}
*/
DeleteCommand.prototype.toBinary = function() {
// Calculate total length of the document
var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + this.db.bson.calculateObjectSize(this.selector, false, true) + (4 * 4);
// Let's build the single pass buffer command
var _index = 0;
var _command = new Buffer(totalLengthOfCommand);
// Write the header information to the buffer
_command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
_command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
_command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
_command[_index] = totalLengthOfCommand & 0xff;
// Adjust index
_index = _index + 4;
// Write the request ID
_command[_index + 3] = (this.requestId >> 24) & 0xff;
_command[_index + 2] = (this.requestId >> 16) & 0xff;
_command[_index + 1] = (this.requestId >> 8) & 0xff;
_command[_index] = this.requestId & 0xff;
// Adjust index
_index = _index + 4;
// Write zero
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
// Write the op_code for the command
_command[_index + 3] = (DeleteCommand.OP_DELETE >> 24) & 0xff;
_command[_index + 2] = (DeleteCommand.OP_DELETE >> 16) & 0xff;
_command[_index + 1] = (DeleteCommand.OP_DELETE >> 8) & 0xff;
_command[_index] = DeleteCommand.OP_DELETE & 0xff;
// Adjust index
_index = _index + 4;
// Write zero
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
// Write the collection name to the command
_index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
_command[_index - 1] = 0;
// Write the flags
_command[_index + 3] = (this.flags >> 24) & 0xff;
_command[_index + 2] = (this.flags >> 16) & 0xff;
_command[_index + 1] = (this.flags >> 8) & 0xff;
_command[_index] = this.flags & 0xff;
// Adjust index
_index = _index + 4;
// Document binary length
var documentLength = 0
// Serialize the selector
// If we are passing a raw buffer, do minimal validation
if(Buffer.isBuffer(this.selector)) {
documentLength = this.selector.length;
// Copy the data into the current buffer
this.selector.copy(_command, _index);
} else {
documentLength = this.db.bson.serializeWithBufferAndIndex(this.selector, this.checkKeys, _command, _index) - _index + 1;
}
// Write the length to the document
_command[_index + 3] = (documentLength >> 24) & 0xff;
_command[_index + 2] = (documentLength >> 16) & 0xff;
_command[_index + 1] = (documentLength >> 8) & 0xff;
_command[_index] = documentLength & 0xff;
// Update index in buffer
_index = _index + documentLength;
// Add terminating 0 for the object
_command[_index - 1] = 0;
return _command;
};

View File

@@ -0,0 +1,83 @@
var BaseCommand = require('./base_command').BaseCommand,
inherits = require('util').inherits,
binaryutils = require('../utils');
/**
Get More Document Command
**/
var GetMoreCommand = exports.GetMoreCommand = function(db, collectionName, numberToReturn, cursorId) {
BaseCommand.call(this);
this.collectionName = collectionName;
this.numberToReturn = numberToReturn;
this.cursorId = cursorId;
this.db = db;
};
inherits(GetMoreCommand, BaseCommand);
GetMoreCommand.OP_GET_MORE = 2005;
GetMoreCommand.prototype.toBinary = function() {
// Calculate total length of the document
var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + 8 + (4 * 4);
// Let's build the single pass buffer command
var _index = 0;
var _command = new Buffer(totalLengthOfCommand);
// Write the header information to the buffer
_command[_index++] = totalLengthOfCommand & 0xff;
_command[_index++] = (totalLengthOfCommand >> 8) & 0xff;
_command[_index++] = (totalLengthOfCommand >> 16) & 0xff;
_command[_index++] = (totalLengthOfCommand >> 24) & 0xff;
// Write the request ID
_command[_index++] = this.requestId & 0xff;
_command[_index++] = (this.requestId >> 8) & 0xff;
_command[_index++] = (this.requestId >> 16) & 0xff;
_command[_index++] = (this.requestId >> 24) & 0xff;
// Write zero
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
// Write the op_code for the command
_command[_index++] = GetMoreCommand.OP_GET_MORE & 0xff;
_command[_index++] = (GetMoreCommand.OP_GET_MORE >> 8) & 0xff;
_command[_index++] = (GetMoreCommand.OP_GET_MORE >> 16) & 0xff;
_command[_index++] = (GetMoreCommand.OP_GET_MORE >> 24) & 0xff;
// Write zero
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
// Write the collection name to the command
_index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
_command[_index - 1] = 0;
// Number of documents to return
_command[_index++] = this.numberToReturn & 0xff;
_command[_index++] = (this.numberToReturn >> 8) & 0xff;
_command[_index++] = (this.numberToReturn >> 16) & 0xff;
_command[_index++] = (this.numberToReturn >> 24) & 0xff;
// Encode the cursor id
var low_bits = this.cursorId.getLowBits();
// Encode low bits
_command[_index++] = low_bits & 0xff;
_command[_index++] = (low_bits >> 8) & 0xff;
_command[_index++] = (low_bits >> 16) & 0xff;
_command[_index++] = (low_bits >> 24) & 0xff;
var high_bits = this.cursorId.getHighBits();
// Encode high bits
_command[_index++] = high_bits & 0xff;
_command[_index++] = (high_bits >> 8) & 0xff;
_command[_index++] = (high_bits >> 16) & 0xff;
_command[_index++] = (high_bits >> 24) & 0xff;
// Return command
return _command;
};

View File

@@ -0,0 +1,147 @@
var BaseCommand = require('./base_command').BaseCommand,
inherits = require('util').inherits;
/**
Insert Document Command
**/
var InsertCommand = exports.InsertCommand = function(db, collectionName, checkKeys, options) {
BaseCommand.call(this);
this.collectionName = collectionName;
this.documents = [];
this.checkKeys = checkKeys == null ? true : checkKeys;
this.db = db;
this.flags = 0;
this.serializeFunctions = false;
// Ensure valid options hash
options = options == null ? {} : options;
// Check if we have keepGoing set -> set flag if it's the case
if(options['keepGoing'] != null && options['keepGoing']) {
// This will finish inserting all non-index violating documents even if it returns an error
this.flags = 1;
}
// Check if we have keepGoing set -> set flag if it's the case
if(options['continueOnError'] != null && options['continueOnError']) {
// This will finish inserting all non-index violating documents even if it returns an error
this.flags = 1;
}
// Let us defined on a command basis if we want functions to be serialized or not
if(options['serializeFunctions'] != null && options['serializeFunctions']) {
this.serializeFunctions = true;
}
};
inherits(InsertCommand, BaseCommand);
// OpCodes
InsertCommand.OP_INSERT = 2002;
InsertCommand.prototype.add = function(document) {
if(Buffer.isBuffer(document)) {
var object_size = document[0] | document[1] << 8 | document[2] << 16 | document[3] << 24;
if(object_size != document.length) {
var error = new Error("insert raw message size does not match message header size [" + document.length + "] != [" + object_size + "]");
error.name = 'MongoError';
throw error;
}
}
this.documents.push(document);
return this;
};
/*
struct {
MsgHeader header; // standard message header
int32 ZERO; // 0 - reserved for future use
cstring fullCollectionName; // "dbname.collectionname"
BSON[] documents; // one or more documents to insert into the collection
}
*/
InsertCommand.prototype.toBinary = function() {
// Calculate total length of the document
var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + (4 * 4);
// var docLength = 0
for(var i = 0; i < this.documents.length; i++) {
if(Buffer.isBuffer(this.documents[i])) {
totalLengthOfCommand += this.documents[i].length;
} else {
// Calculate size of document
totalLengthOfCommand += this.db.bson.calculateObjectSize(this.documents[i], this.serializeFunctions, true);
}
}
// Let's build the single pass buffer command
var _index = 0;
var _command = new Buffer(totalLengthOfCommand);
// Write the header information to the buffer
_command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
_command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
_command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
_command[_index] = totalLengthOfCommand & 0xff;
// Adjust index
_index = _index + 4;
// Write the request ID
_command[_index + 3] = (this.requestId >> 24) & 0xff;
_command[_index + 2] = (this.requestId >> 16) & 0xff;
_command[_index + 1] = (this.requestId >> 8) & 0xff;
_command[_index] = this.requestId & 0xff;
// Adjust index
_index = _index + 4;
// Write zero
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
// Write the op_code for the command
_command[_index + 3] = (InsertCommand.OP_INSERT >> 24) & 0xff;
_command[_index + 2] = (InsertCommand.OP_INSERT >> 16) & 0xff;
_command[_index + 1] = (InsertCommand.OP_INSERT >> 8) & 0xff;
_command[_index] = InsertCommand.OP_INSERT & 0xff;
// Adjust index
_index = _index + 4;
// Write flags if any
_command[_index + 3] = (this.flags >> 24) & 0xff;
_command[_index + 2] = (this.flags >> 16) & 0xff;
_command[_index + 1] = (this.flags >> 8) & 0xff;
_command[_index] = this.flags & 0xff;
// Adjust index
_index = _index + 4;
// Write the collection name to the command
_index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
_command[_index - 1] = 0;
// Write all the bson documents to the buffer at the index offset
for(var i = 0; i < this.documents.length; i++) {
// Document binary length
var documentLength = 0
var object = this.documents[i];
// Serialize the selector
// If we are passing a raw buffer, do minimal validation
if(Buffer.isBuffer(object)) {
documentLength = object.length;
// Copy the data into the current buffer
object.copy(_command, _index);
} else {
// Serialize the document straight to the buffer
documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1;
}
// Write the length to the document
_command[_index + 3] = (documentLength >> 24) & 0xff;
_command[_index + 2] = (documentLength >> 16) & 0xff;
_command[_index + 1] = (documentLength >> 8) & 0xff;
_command[_index] = documentLength & 0xff;
// Update index in buffer
_index = _index + documentLength;
// Add terminating 0 for the object
_command[_index - 1] = 0;
}
return _command;
};

View File

@@ -0,0 +1,98 @@
var BaseCommand = require('./base_command').BaseCommand,
inherits = require('util').inherits,
binaryutils = require('../utils');
/**
Insert Document Command
**/
var KillCursorCommand = exports.KillCursorCommand = function(db, cursorIds) {
BaseCommand.call(this);
this.cursorIds = cursorIds;
this.db = db;
};
inherits(KillCursorCommand, BaseCommand);
KillCursorCommand.OP_KILL_CURSORS = 2007;
/*
struct {
MsgHeader header; // standard message header
int32 ZERO; // 0 - reserved for future use
int32 numberOfCursorIDs; // number of cursorIDs in message
int64[] cursorIDs; // array of cursorIDs to close
}
*/
KillCursorCommand.prototype.toBinary = function() {
// Calculate total length of the document
var totalLengthOfCommand = 4 + 4 + (4 * 4) + (this.cursorIds.length * 8);
// Let's build the single pass buffer command
var _index = 0;
var _command = new Buffer(totalLengthOfCommand);
// Write the header information to the buffer
_command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
_command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
_command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
_command[_index] = totalLengthOfCommand & 0xff;
// Adjust index
_index = _index + 4;
// Write the request ID
_command[_index + 3] = (this.requestId >> 24) & 0xff;
_command[_index + 2] = (this.requestId >> 16) & 0xff;
_command[_index + 1] = (this.requestId >> 8) & 0xff;
_command[_index] = this.requestId & 0xff;
// Adjust index
_index = _index + 4;
// Write zero
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
// Write the op_code for the command
_command[_index + 3] = (KillCursorCommand.OP_KILL_CURSORS >> 24) & 0xff;
_command[_index + 2] = (KillCursorCommand.OP_KILL_CURSORS >> 16) & 0xff;
_command[_index + 1] = (KillCursorCommand.OP_KILL_CURSORS >> 8) & 0xff;
_command[_index] = KillCursorCommand.OP_KILL_CURSORS & 0xff;
// Adjust index
_index = _index + 4;
// Write zero
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
// Number of cursors to kill
var numberOfCursors = this.cursorIds.length;
_command[_index + 3] = (numberOfCursors >> 24) & 0xff;
_command[_index + 2] = (numberOfCursors >> 16) & 0xff;
_command[_index + 1] = (numberOfCursors >> 8) & 0xff;
_command[_index] = numberOfCursors & 0xff;
// Adjust index
_index = _index + 4;
// Encode all the cursors
for(var i = 0; i < this.cursorIds.length; i++) {
// Encode the cursor id
var low_bits = this.cursorIds[i].getLowBits();
// Encode low bits
_command[_index + 3] = (low_bits >> 24) & 0xff;
_command[_index + 2] = (low_bits >> 16) & 0xff;
_command[_index + 1] = (low_bits >> 8) & 0xff;
_command[_index] = low_bits & 0xff;
// Adjust index
_index = _index + 4;
var high_bits = this.cursorIds[i].getHighBits();
// Encode high bits
_command[_index + 3] = (high_bits >> 24) & 0xff;
_command[_index + 2] = (high_bits >> 16) & 0xff;
_command[_index + 1] = (high_bits >> 8) & 0xff;
_command[_index] = high_bits & 0xff;
// Adjust index
_index = _index + 4;
}
return _command;
};

View File

@@ -0,0 +1,261 @@
var BaseCommand = require('./base_command').BaseCommand,
inherits = require('util').inherits;
/**
Insert Document Command
**/
var QueryCommand = exports.QueryCommand = function(db, collectionName, queryOptions, numberToSkip, numberToReturn, query, returnFieldSelector, options) {
BaseCommand.call(this);
// Validate correctness off the selector
var object = query,
object_size;
if(Buffer.isBuffer(object)) {
object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
if(object_size != object.length) {
var error = new Error("query selector raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
error.name = 'MongoError';
throw error;
}
}
object = returnFieldSelector;
if(Buffer.isBuffer(object)) {
object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
if(object_size != object.length) {
var error = new Error("query fields raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
error.name = 'MongoError';
throw error;
}
}
// Make sure we don't get a null exception
options = options == null ? {} : options;
// Set up options
this.collectionName = collectionName;
this.queryOptions = queryOptions;
this.numberToSkip = numberToSkip;
this.numberToReturn = numberToReturn;
// Ensure we have no null query
query = query == null ? {} : query;
// Wrap query in the $query parameter so we can add read preferences for mongos
this.query = query;
this.returnFieldSelector = returnFieldSelector;
this.db = db;
// Let us defined on a command basis if we want functions to be serialized or not
if(options['serializeFunctions'] != null && options['serializeFunctions']) {
this.serializeFunctions = true;
}
};
inherits(QueryCommand, BaseCommand);
QueryCommand.OP_QUERY = 2004;
/*
* Adds the read prefrence to the current command
*/
QueryCommand.prototype.setMongosReadPreference = function(readPreference, tags) {
// If we have readPreference set to true set to secondary prefered
if(readPreference == true) {
readPreference = 'secondaryPreferred';
} else if(readPreference == 'false') {
readPreference = 'primary';
}
// Force the slave ok flag to be set if we are not using primary read preference
if(readPreference != false && readPreference != 'primary') {
this.queryOptions |= QueryCommand.OPTS_SLAVE;
}
// Backward compatibility, ensure $query only set on read preference so 1.8.X works
if((readPreference != null || tags != null) && this.query['$query'] == null) {
this.query = {'$query': this.query};
}
// If we have no readPreference set and no tags, check if the slaveOk bit is set
if(readPreference == null && tags == null) {
// If we have a slaveOk bit set the read preference for MongoS
if(this.queryOptions & QueryCommand.OPTS_SLAVE) {
this.query['$readPreference'] = {mode: 'secondary'}
} else {
this.query['$readPreference'] = {mode: 'primary'}
}
}
// Build read preference object
if(typeof readPreference == 'object' && readPreference['_type'] == 'ReadPreference') {
this.query['$readPreference'] = readPreference.toObject();
} else if(readPreference != null) {
// Add the read preference
this.query['$readPreference'] = {mode: readPreference};
// If we have tags let's add them
if(tags != null) {
this.query['$readPreference']['tags'] = tags;
}
}
}
/*
struct {
MsgHeader header; // standard message header
int32 opts; // query options. See below for details.
cstring fullCollectionName; // "dbname.collectionname"
int32 numberToSkip; // number of documents to skip when returning results
int32 numberToReturn; // number of documents to return in the first OP_REPLY
BSON query ; // query object. See below for details.
[ BSON returnFieldSelector; ] // OPTIONAL : selector indicating the fields to return. See below for details.
}
*/
QueryCommand.prototype.toBinary = function() {
// Total length of the command
var totalLengthOfCommand = 0;
// Calculate total length of the document
if(Buffer.isBuffer(this.query)) {
totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + 4 + this.query.length + (4 * 4);
} else {
totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + 4 + this.db.bson.calculateObjectSize(this.query, this.serializeFunctions, true) + (4 * 4);
}
// Calculate extra fields size
if(this.returnFieldSelector != null && !(Buffer.isBuffer(this.returnFieldSelector))) {
if(Object.keys(this.returnFieldSelector).length > 0) {
totalLengthOfCommand += this.db.bson.calculateObjectSize(this.returnFieldSelector, this.serializeFunctions, true);
}
} else if(Buffer.isBuffer(this.returnFieldSelector)) {
totalLengthOfCommand += this.returnFieldSelector.length;
}
// Let's build the single pass buffer command
var _index = 0;
var _command = new Buffer(totalLengthOfCommand);
// Write the header information to the buffer
_command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
_command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
_command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
_command[_index] = totalLengthOfCommand & 0xff;
// Adjust index
_index = _index + 4;
// Write the request ID
_command[_index + 3] = (this.requestId >> 24) & 0xff;
_command[_index + 2] = (this.requestId >> 16) & 0xff;
_command[_index + 1] = (this.requestId >> 8) & 0xff;
_command[_index] = this.requestId & 0xff;
// Adjust index
_index = _index + 4;
// Write zero
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
// Write the op_code for the command
_command[_index + 3] = (QueryCommand.OP_QUERY >> 24) & 0xff;
_command[_index + 2] = (QueryCommand.OP_QUERY >> 16) & 0xff;
_command[_index + 1] = (QueryCommand.OP_QUERY >> 8) & 0xff;
_command[_index] = QueryCommand.OP_QUERY & 0xff;
// Adjust index
_index = _index + 4;
// Write the query options
_command[_index + 3] = (this.queryOptions >> 24) & 0xff;
_command[_index + 2] = (this.queryOptions >> 16) & 0xff;
_command[_index + 1] = (this.queryOptions >> 8) & 0xff;
_command[_index] = this.queryOptions & 0xff;
// Adjust index
_index = _index + 4;
// Write the collection name to the command
_index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
_command[_index - 1] = 0;
// Write the number of documents to skip
_command[_index + 3] = (this.numberToSkip >> 24) & 0xff;
_command[_index + 2] = (this.numberToSkip >> 16) & 0xff;
_command[_index + 1] = (this.numberToSkip >> 8) & 0xff;
_command[_index] = this.numberToSkip & 0xff;
// Adjust index
_index = _index + 4;
// Write the number of documents to return
_command[_index + 3] = (this.numberToReturn >> 24) & 0xff;
_command[_index + 2] = (this.numberToReturn >> 16) & 0xff;
_command[_index + 1] = (this.numberToReturn >> 8) & 0xff;
_command[_index] = this.numberToReturn & 0xff;
// Adjust index
_index = _index + 4;
// Document binary length
var documentLength = 0
var object = this.query;
// Serialize the selector
if(Buffer.isBuffer(object)) {
documentLength = object.length;
// Copy the data into the current buffer
object.copy(_command, _index);
} else {
// Serialize the document straight to the buffer
documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1;
}
// Write the length to the document
_command[_index + 3] = (documentLength >> 24) & 0xff;
_command[_index + 2] = (documentLength >> 16) & 0xff;
_command[_index + 1] = (documentLength >> 8) & 0xff;
_command[_index] = documentLength & 0xff;
// Update index in buffer
_index = _index + documentLength;
// Add terminating 0 for the object
_command[_index - 1] = 0;
// Push field selector if available
if(this.returnFieldSelector != null && !(Buffer.isBuffer(this.returnFieldSelector))) {
if(Object.keys(this.returnFieldSelector).length > 0) {
var documentLength = this.db.bson.serializeWithBufferAndIndex(this.returnFieldSelector, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1;
// Write the length to the document
_command[_index + 3] = (documentLength >> 24) & 0xff;
_command[_index + 2] = (documentLength >> 16) & 0xff;
_command[_index + 1] = (documentLength >> 8) & 0xff;
_command[_index] = documentLength & 0xff;
// Update index in buffer
_index = _index + documentLength;
// Add terminating 0 for the object
_command[_index - 1] = 0;
}
} if(this.returnFieldSelector != null && Buffer.isBuffer(this.returnFieldSelector)) {
// Document binary length
var documentLength = 0
var object = this.returnFieldSelector;
// Serialize the selector
documentLength = object.length;
// Copy the data into the current buffer
object.copy(_command, _index);
// Write the length to the document
_command[_index + 3] = (documentLength >> 24) & 0xff;
_command[_index + 2] = (documentLength >> 16) & 0xff;
_command[_index + 1] = (documentLength >> 8) & 0xff;
_command[_index] = documentLength & 0xff;
// Update index in buffer
_index = _index + documentLength;
// Add terminating 0 for the object
_command[_index - 1] = 0;
}
// Return finished command
return _command;
};
// Constants
QueryCommand.OPTS_NONE = 0;
QueryCommand.OPTS_TAILABLE_CURSOR = 2;
QueryCommand.OPTS_SLAVE = 4;
QueryCommand.OPTS_OPLOG_REPLY = 8;
QueryCommand.OPTS_NO_CURSOR_TIMEOUT = 16;
QueryCommand.OPTS_AWAIT_DATA = 32;
QueryCommand.OPTS_EXHAUST = 64;
QueryCommand.OPTS_PARTIAL = 128;

View File

@@ -0,0 +1,174 @@
var BaseCommand = require('./base_command').BaseCommand,
inherits = require('util').inherits;
/**
Update Document Command
**/
var UpdateCommand = exports.UpdateCommand = function(db, collectionName, spec, document, options) {
BaseCommand.call(this);
var object = spec;
if(Buffer.isBuffer(object)) {
var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
if(object_size != object.length) {
var error = new Error("update spec raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
error.name = 'MongoError';
throw error;
}
}
var object = document;
if(Buffer.isBuffer(object)) {
var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
if(object_size != object.length) {
var error = new Error("update document raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
error.name = 'MongoError';
throw error;
}
}
this.collectionName = collectionName;
this.spec = spec;
this.document = document;
this.db = db;
this.serializeFunctions = false;
// Generate correct flags
var db_upsert = 0;
var db_multi_update = 0;
db_upsert = options != null && options['upsert'] != null ? (options['upsert'] == true ? 1 : 0) : db_upsert;
db_multi_update = options != null && options['multi'] != null ? (options['multi'] == true ? 1 : 0) : db_multi_update;
// Flags
this.flags = parseInt(db_multi_update.toString() + db_upsert.toString(), 2);
// Let us defined on a command basis if we want functions to be serialized or not
if(options['serializeFunctions'] != null && options['serializeFunctions']) {
this.serializeFunctions = true;
}
};
inherits(UpdateCommand, BaseCommand);
UpdateCommand.OP_UPDATE = 2001;
/*
struct {
MsgHeader header; // standard message header
int32 ZERO; // 0 - reserved for future use
cstring fullCollectionName; // "dbname.collectionname"
int32 flags; // bit vector. see below
BSON spec; // the query to select the document
BSON document; // the document data to update with or insert
}
*/
UpdateCommand.prototype.toBinary = function() {
// Calculate total length of the document
var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + this.db.bson.calculateObjectSize(this.spec, false, true) +
this.db.bson.calculateObjectSize(this.document, this.serializeFunctions, true) + (4 * 4);
// Let's build the single pass buffer command
var _index = 0;
var _command = new Buffer(totalLengthOfCommand);
// Write the header information to the buffer
_command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
_command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
_command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
_command[_index] = totalLengthOfCommand & 0xff;
// Adjust index
_index = _index + 4;
// Write the request ID
_command[_index + 3] = (this.requestId >> 24) & 0xff;
_command[_index + 2] = (this.requestId >> 16) & 0xff;
_command[_index + 1] = (this.requestId >> 8) & 0xff;
_command[_index] = this.requestId & 0xff;
// Adjust index
_index = _index + 4;
// Write zero
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
// Write the op_code for the command
_command[_index + 3] = (UpdateCommand.OP_UPDATE >> 24) & 0xff;
_command[_index + 2] = (UpdateCommand.OP_UPDATE >> 16) & 0xff;
_command[_index + 1] = (UpdateCommand.OP_UPDATE >> 8) & 0xff;
_command[_index] = UpdateCommand.OP_UPDATE & 0xff;
// Adjust index
_index = _index + 4;
// Write zero
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
_command[_index++] = 0;
// Write the collection name to the command
_index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
_command[_index - 1] = 0;
// Write the update flags
_command[_index + 3] = (this.flags >> 24) & 0xff;
_command[_index + 2] = (this.flags >> 16) & 0xff;
_command[_index + 1] = (this.flags >> 8) & 0xff;
_command[_index] = this.flags & 0xff;
// Adjust index
_index = _index + 4;
// Document binary length
var documentLength = 0
var object = this.spec;
// Serialize the selector
// If we are passing a raw buffer, do minimal validation
if(Buffer.isBuffer(object)) {
var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
if(object_size != object.length) throw new Error("raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
documentLength = object.length;
// Copy the data into the current buffer
object.copy(_command, _index);
} else {
documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, false) - _index + 1;
}
// Write the length to the document
_command[_index + 3] = (documentLength >> 24) & 0xff;
_command[_index + 2] = (documentLength >> 16) & 0xff;
_command[_index + 1] = (documentLength >> 8) & 0xff;
_command[_index] = documentLength & 0xff;
// Update index in buffer
_index = _index + documentLength;
// Add terminating 0 for the object
_command[_index - 1] = 0;
// Document binary length
var documentLength = 0
var object = this.document;
// Serialize the document
// If we are passing a raw buffer, do minimal validation
if(Buffer.isBuffer(object)) {
var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
if(object_size != object.length) throw new Error("raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
documentLength = object.length;
// Copy the data into the current buffer
object.copy(_command, _index);
} else {
documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1;
}
// Write the length to the document
_command[_index + 3] = (documentLength >> 24) & 0xff;
_command[_index + 2] = (documentLength >> 16) & 0xff;
_command[_index + 1] = (documentLength >> 8) & 0xff;
_command[_index] = documentLength & 0xff;
// Update index in buffer
_index = _index + documentLength;
// Add terminating 0 for the object
_command[_index - 1] = 0;
return _command;
};
// Constants
UpdateCommand.DB_UPSERT = 0;
UpdateCommand.DB_MULTI_UPDATE = 1;

View File

@@ -0,0 +1,440 @@
var utils = require('./connection_utils'),
inherits = require('util').inherits,
net = require('net'),
EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits,
binaryutils = require('../utils'),
tls = require('tls');
var Connection = exports.Connection = function(id, socketOptions) {
// Set up event emitter
EventEmitter.call(this);
// Store all socket options
this.socketOptions = socketOptions ? socketOptions : {host:'localhost', port:27017, domainSocket:false};
// Set keep alive default if not overriden
if(this.socketOptions.keepAlive == null && (process.platform !== "sunos" || process.platform !== "win32")) this.socketOptions.keepAlive = 100;
// Id for the connection
this.id = id;
// State of the connection
this.connected = false;
// Set if this is a domain socket
this.domainSocket = this.socketOptions.domainSocket;
//
// Connection parsing state
//
this.maxBsonSize = socketOptions.maxBsonSize ? socketOptions.maxBsonSize : Connection.DEFAULT_MAX_BSON_SIZE;
// Contains the current message bytes
this.buffer = null;
// Contains the current message size
this.sizeOfMessage = 0;
// Contains the readIndex for the messaage
this.bytesRead = 0;
// Contains spill over bytes from additional messages
this.stubBuffer = 0;
// Just keeps list of events we allow
this.eventHandlers = {error:[], parseError:[], poolReady:[], message:[], close:[], timeout:[], end:[]};
// Just keeps list of events we allow
resetHandlers(this, false);
}
// Set max bson size
Connection.DEFAULT_MAX_BSON_SIZE = 1024 * 1024 * 4;
// Inherit event emitter so we can emit stuff wohoo
inherits(Connection, EventEmitter);
Connection.prototype.start = function() {
// If we have a normal connection
if(this.socketOptions.ssl) {
// Create a new stream
this.connection = new net.Socket();
// Set timeout allowing backward compatibility to timeout if no connectTimeoutMS is set
this.connection.setTimeout(this.socketOptions.connectTimeoutMS != null ? this.socketOptions.connectTimeoutMS : this.socketOptions.timeout);
// Work around for 0.4.X
if(process.version.indexOf("v0.4") == -1) this.connection.setNoDelay(this.socketOptions.noDelay);
// Set keep alive if defined
if(process.version.indexOf("v0.4") == -1) {
if(this.socketOptions.keepAlive > 0) {
this.connection.setKeepAlive(true, this.socketOptions.keepAlive);
} else {
this.connection.setKeepAlive(false);
}
}
// Set up pair for tls with server, accept self-signed certificates as well
var pair = this.pair = tls.createSecurePair(false);
// Set up encrypted streams
this.pair.encrypted.pipe(this.connection);
this.connection.pipe(this.pair.encrypted);
// Setup clearText stream
this.writeSteam = this.pair.cleartext;
// Add all handlers to the socket to manage it
this.pair.on("secure", connectHandler(this));
this.pair.cleartext.on("data", createDataHandler(this));
// Add handlers
this.connection.on("error", errorHandler(this));
// this.connection.on("connect", connectHandler(this));
this.connection.on("end", endHandler(this));
this.connection.on("timeout", timeoutHandler(this));
this.connection.on("drain", drainHandler(this));
this.writeSteam.on("close", closeHandler(this));
// Start socket
this.connection.connect(this.socketOptions.port, this.socketOptions.host);
if(this.logger != null && this.logger.doDebug){
this.logger.debug("opened connection", this.socketOptions);
}
} else {
// Create new connection instance
if(!this.domainSocket) {
this.connection = net.createConnection(this.socketOptions.port, this.socketOptions.host);
} else {
this.connection = net.createConnection(this.socketOptions.host);
}
if(this.logger != null && this.logger.doDebug){
this.logger.debug("opened connection", this.socketOptions);
}
// Set options on the socket
this.connection.setTimeout(this.socketOptions.connectTimeoutMS != null ? this.socketOptions.connectTimeoutMS : this.socketOptions.timeout);
// Work around for 0.4.X
if(process.version.indexOf("v0.4") == -1) this.connection.setNoDelay(this.socketOptions.noDelay);
// Set keep alive if defined
if(process.version.indexOf("v0.4") == -1) {
if(this.socketOptions.keepAlive > 0) {
this.connection.setKeepAlive(true, this.socketOptions.keepAlive);
} else {
this.connection.setKeepAlive(false);
}
}
// Set up write stream
this.writeSteam = this.connection;
// Add handlers
this.connection.on("error", errorHandler(this));
// Add all handlers to the socket to manage it
this.connection.on("connect", connectHandler(this));
// this.connection.on("end", endHandler(this));
this.connection.on("data", createDataHandler(this));
this.connection.on("timeout", timeoutHandler(this));
this.connection.on("drain", drainHandler(this));
this.connection.on("close", closeHandler(this));
}
}
// Check if the sockets are live
Connection.prototype.isConnected = function() {
return this.connected && !this.connection.destroyed && this.connection.writable && this.connection.readable;
}
// Write the data out to the socket
Connection.prototype.write = function(command, callback) {
try {
// If we have a list off commands to be executed on the same socket
if(Array.isArray(command)) {
for(var i = 0; i < command.length; i++) {
var binaryCommand = command[i].toBinary()
if(!this.socketOptions['disableDriverBSONSizeCheck'] && binaryCommand.length > this.maxBsonSize)
return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
if(this.logger != null && this.logger.doDebug)
this.logger.debug("writing command to mongodb", binaryCommand);
var r = this.writeSteam.write(binaryCommand);
}
} else {
var binaryCommand = command.toBinary()
if(!this.socketOptions['disableDriverBSONSizeCheck'] && binaryCommand.length > this.maxBsonSize)
return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
if(this.logger != null && this.logger.doDebug)
this.logger.debug("writing command to mongodb", binaryCommand);
var r = this.writeSteam.write(binaryCommand);
}
} catch (err) {
if(typeof callback === 'function') callback(err);
}
}
// Force the closure of the connection
Connection.prototype.close = function() {
// clear out all the listeners
resetHandlers(this, true);
// Add a dummy error listener to catch any weird last moment errors (and ignore them)
this.connection.on("error", function() {})
// destroy connection
this.connection.destroy();
if(this.logger != null && this.logger.doDebug){
this.logger.debug("closed connection", this.connection);
}
}
// Reset all handlers
var resetHandlers = function(self, clearListeners) {
self.eventHandlers = {error:[], connect:[], close:[], end:[], timeout:[], parseError:[], message:[]};
// If we want to clear all the listeners
if(clearListeners && self.connection != null) {
var keys = Object.keys(self.eventHandlers);
// Remove all listeners
for(var i = 0; i < keys.length; i++) {
self.connection.removeAllListeners(keys[i]);
}
}
}
//
// Handlers
//
// Connect handler
var connectHandler = function(self) {
return function() {
// Set connected
self.connected = true;
// Now that we are connected set the socket timeout
self.connection.setTimeout(self.socketOptions.socketTimeoutMS != null ? self.socketOptions.socketTimeoutMS : self.socketOptions.timeout);
// Emit the connect event with no error
self.emit("connect", null, self);
}
}
var createDataHandler = exports.Connection.createDataHandler = function(self) {
// We need to handle the parsing of the data
// and emit the messages when there is a complete one
return function(data) {
// Parse until we are done with the data
while(data.length > 0) {
// If we still have bytes to read on the current message
if(self.bytesRead > 0 && self.sizeOfMessage > 0) {
// Calculate the amount of remaining bytes
var remainingBytesToRead = self.sizeOfMessage - self.bytesRead;
// Check if the current chunk contains the rest of the message
if(remainingBytesToRead > data.length) {
// Copy the new data into the exiting buffer (should have been allocated when we know the message size)
data.copy(self.buffer, self.bytesRead);
// Adjust the number of bytes read so it point to the correct index in the buffer
self.bytesRead = self.bytesRead + data.length;
// Reset state of buffer
data = new Buffer(0);
} else {
// Copy the missing part of the data into our current buffer
data.copy(self.buffer, self.bytesRead, 0, remainingBytesToRead);
// Slice the overflow into a new buffer that we will then re-parse
data = data.slice(remainingBytesToRead);
// Emit current complete message
try {
var emitBuffer = self.buffer;
// Reset state of buffer
self.buffer = null;
self.sizeOfMessage = 0;
self.bytesRead = 0;
self.stubBuffer = null;
// Emit the buffer
self.emit("message", emitBuffer, self);
} catch(err) {
var errorObject = {err:"socketHandler", trace:err, bin:buffer, parseState:{
sizeOfMessage:self.sizeOfMessage,
bytesRead:self.bytesRead,
stubBuffer:self.stubBuffer}};
if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
// We got a parse Error fire it off then keep going
self.emit("parseError", errorObject, self);
}
}
} else {
// Stub buffer is kept in case we don't get enough bytes to determine the
// size of the message (< 4 bytes)
if(self.stubBuffer != null && self.stubBuffer.length > 0) {
// If we have enough bytes to determine the message size let's do it
if(self.stubBuffer.length + data.length > 4) {
// Prepad the data
var newData = new Buffer(self.stubBuffer.length + data.length);
self.stubBuffer.copy(newData, 0);
data.copy(newData, self.stubBuffer.length);
// Reassign for parsing
data = newData;
// Reset state of buffer
self.buffer = null;
self.sizeOfMessage = 0;
self.bytesRead = 0;
self.stubBuffer = null;
} else {
// Add the the bytes to the stub buffer
var newStubBuffer = new Buffer(self.stubBuffer.length + data.length);
// Copy existing stub buffer
self.stubBuffer.copy(newStubBuffer, 0);
// Copy missing part of the data
data.copy(newStubBuffer, self.stubBuffer.length);
// Exit parsing loop
data = new Buffer(0);
}
} else {
if(data.length > 4) {
// Retrieve the message size
var sizeOfMessage = binaryutils.decodeUInt32(data, 0);
// If we have a negative sizeOfMessage emit error and return
if(sizeOfMessage < 0 || sizeOfMessage > self.maxBsonSize) {
var errorObject = {err:"socketHandler", trace:'', bin:self.buffer, parseState:{
sizeOfMessage: sizeOfMessage,
bytesRead: self.bytesRead,
stubBuffer: self.stubBuffer}};
if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
// We got a parse Error fire it off then keep going
self.emit("parseError", errorObject, self);
return;
}
// Ensure that the size of message is larger than 0 and less than the max allowed
if(sizeOfMessage > 4 && sizeOfMessage < self.maxBsonSize && sizeOfMessage > data.length) {
self.buffer = new Buffer(sizeOfMessage);
// Copy all the data into the buffer
data.copy(self.buffer, 0);
// Update bytes read
self.bytesRead = data.length;
// Update sizeOfMessage
self.sizeOfMessage = sizeOfMessage;
// Ensure stub buffer is null
self.stubBuffer = null;
// Exit parsing loop
data = new Buffer(0);
} else if(sizeOfMessage > 4 && sizeOfMessage < self.maxBsonSize && sizeOfMessage == data.length) {
try {
var emitBuffer = data;
// Reset state of buffer
self.buffer = null;
self.sizeOfMessage = 0;
self.bytesRead = 0;
self.stubBuffer = null;
// Exit parsing loop
data = new Buffer(0);
// Emit the message
self.emit("message", emitBuffer, self);
} catch (err) {
var errorObject = {err:"socketHandler", trace:err, bin:self.buffer, parseState:{
sizeOfMessage:self.sizeOfMessage,
bytesRead:self.bytesRead,
stubBuffer:self.stubBuffer}};
if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
// We got a parse Error fire it off then keep going
self.emit("parseError", errorObject, self);
}
} else if(sizeOfMessage <= 4 || sizeOfMessage > self.maxBsonSize) {
var errorObject = {err:"socketHandler", trace:null, bin:data, parseState:{
sizeOfMessage:sizeOfMessage,
bytesRead:0,
buffer:null,
stubBuffer:null}};
if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
// We got a parse Error fire it off then keep going
self.emit("parseError", errorObject, self);
// Clear out the state of the parser
self.buffer = null;
self.sizeOfMessage = 0;
self.bytesRead = 0;
self.stubBuffer = null;
// Exit parsing loop
data = new Buffer(0);
} else {
try {
var emitBuffer = data.slice(0, sizeOfMessage);
// Reset state of buffer
self.buffer = null;
self.sizeOfMessage = 0;
self.bytesRead = 0;
self.stubBuffer = null;
// Copy rest of message
data = data.slice(sizeOfMessage);
// Emit the message
self.emit("message", emitBuffer, self);
} catch (err) {
var errorObject = {err:"socketHandler", trace:err, bin:self.buffer, parseState:{
sizeOfMessage:sizeOfMessage,
bytesRead:self.bytesRead,
stubBuffer:self.stubBuffer}};
if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
// We got a parse Error fire it off then keep going
self.emit("parseError", errorObject, self);
}
}
} else {
// Create a buffer that contains the space for the non-complete message
self.stubBuffer = new Buffer(data.length)
// Copy the data to the stub buffer
data.copy(self.stubBuffer, 0);
// Exit parsing loop
data = new Buffer(0);
}
}
}
}
}
}
var endHandler = function(self) {
return function() {
// Set connected to false
self.connected = false;
// Emit end event
self.emit("end", {err: 'connection received Fin packet from [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self);
}
}
var timeoutHandler = function(self) {
return function() {
self.emit("timeout", {err: 'connection to [' + self.socketOptions.host + ':' + self.socketOptions.port + '] timed out'}, self);
}
}
var drainHandler = function(self) {
return function() {
}
}
var errorHandler = function(self) {
return function(err) {
// Set connected to false
self.connected = false;
// Emit error
self.emit("error", {err: 'failed to connect to [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self);
}
}
var closeHandler = function(self) {
return function(hadError) {
// If we have an error during the connection phase
if(hadError && !self.connected) {
// Set disconnected
self.connected = false;
// Emit error
self.emit("error", {err: 'failed to connect to [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self);
} else {
// Set disconnected
self.connected = false;
// Emit close
self.emit("close", {err: 'connection closed to [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self);
}
}
}
// Some basic defaults
Connection.DEFAULT_PORT = 27017;

View File

@@ -0,0 +1,243 @@
var utils = require('./connection_utils'),
inherits = require('util').inherits,
net = require('net'),
EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits,
MongoReply = require("../responses/mongo_reply").MongoReply,
Connection = require("./connection").Connection;
var ConnectionPool = exports.ConnectionPool = function(host, port, poolSize, bson, socketOptions) {
if(typeof host !== 'string' || typeof port !== 'number') throw "host and port must be specified [" + host + ":" + port + "]";
// Set up event emitter
EventEmitter.call(this);
// Keep all options for the socket in a specific collection allowing the user to specify the
// Wished upon socket connection parameters
this.socketOptions = typeof socketOptions === 'object' ? socketOptions : {};
this.socketOptions.host = host;
this.socketOptions.port = port;
this.socketOptions.domainSocket = false;
this.bson = bson;
// PoolSize is always + 1 for special reserved "measurment" socket (like ping, stats etc)
this.poolSize = poolSize;
this.minPoolSize = Math.floor(this.poolSize / 2) + 1;
// Check if the host is a socket
if(host.match(/^\//)) this.socketOptions.domainSocket = true;
// Set default settings for the socket options
utils.setIntegerParameter(this.socketOptions, 'timeout', 0);
// Delay before writing out the data to the server
utils.setBooleanParameter(this.socketOptions, 'noDelay', true);
// Delay before writing out the data to the server
utils.setIntegerParameter(this.socketOptions, 'keepAlive', 0);
// Set the encoding of the data read, default is binary == null
utils.setStringParameter(this.socketOptions, 'encoding', null);
// Allows you to set a throttling bufferSize if you need to stop overflows
utils.setIntegerParameter(this.socketOptions, 'bufferSize', 0);
// Internal structures
this.openConnections = [];
// Assign connection id's
this.connectionId = 0;
// Current index for selection of pool connection
this.currentConnectionIndex = 0;
// The pool state
this._poolState = 'disconnected';
// timeout control
this._timeout = false;
}
inherits(ConnectionPool, EventEmitter);
ConnectionPool.prototype.setMaxBsonSize = function(maxBsonSize) {
if(maxBsonSize == null){
maxBsonSize = Connection.DEFAULT_MAX_BSON_SIZE;
}
for(var i = 0; i < this.openConnections.length; i++) {
this.openConnections[i].maxBsonSize = maxBsonSize;
}
}
// Start a function
var _connect = function(_self) {
return new function() {
// Create a new connection instance
var connection = new Connection(_self.connectionId++, _self.socketOptions);
// Set logger on pool
connection.logger = _self.logger;
// Connect handler
connection.on("connect", function(err, connection) {
// Add connection to list of open connections
_self.openConnections.push(connection);
// If the number of open connections is equal to the poolSize signal ready pool
if(_self.openConnections.length === _self.poolSize && _self._poolState !== 'disconnected') {
// Set connected
_self._poolState = 'connected';
// Emit pool ready
_self.emit("poolReady");
} else if(_self.openConnections.length < _self.poolSize) {
// We need to open another connection, make sure it's in the next
// tick so we don't get a cascade of errors
process.nextTick(function() {
_connect(_self);
});
}
});
var numberOfErrors = 0
// Error handler
connection.on("error", function(err, connection) {
numberOfErrors++;
// If we are already disconnected ignore the event
if(_self._poolState != 'disconnected' && _self.listeners("error").length > 0) {
_self.emit("error", err);
}
// Set disconnected
_self._poolState = 'disconnected';
// Stop
_self.stop();
});
// Close handler
connection.on("close", function() {
// If we are already disconnected ignore the event
if(_self._poolState !== 'disconnected' && _self.listeners("close").length > 0) {
_self.emit("close");
}
// Set disconnected
_self._poolState = 'disconnected';
// Stop
_self.stop();
});
// Timeout handler
connection.on("timeout", function(err, connection) {
// If we are already disconnected ignore the event
if(_self._poolState !== 'disconnected' && _self.listeners("timeout").length > 0) {
_self.emit("timeout", err);
}
// Set disconnected
_self._poolState = 'disconnected';
// Stop
_self.stop();
});
// Parse error, needs a complete shutdown of the pool
connection.on("parseError", function() {
// If we are already disconnected ignore the event
if(_self._poolState !== 'disconnected' && _self.listeners("parseError").length > 0) {
_self.emit("parseError", new Error("parseError occured"));
}
_self.stop();
});
connection.on("message", function(message) {
_self.emit("message", message);
});
// Start connection in the next tick
connection.start();
}();
}
// Start method, will throw error if no listeners are available
// Pass in an instance of the listener that contains the api for
// finding callbacks for a given message etc.
ConnectionPool.prototype.start = function() {
var markerDate = new Date().getTime();
var self = this;
if(this.listeners("poolReady").length == 0) {
throw "pool must have at least one listener ready that responds to the [poolReady] event";
}
// Set pool state to connecting
this._poolState = 'connecting';
this._timeout = false;
_connect(self);
}
// Restart a connection pool (on a close the pool might be in a wrong state)
ConnectionPool.prototype.restart = function() {
// Close all connections
this.stop(false);
// Now restart the pool
this.start();
}
// Stop the connections in the pool
ConnectionPool.prototype.stop = function(removeListeners) {
removeListeners = removeListeners == null ? true : removeListeners;
// Set disconnected
this._poolState = 'disconnected';
// Clear all listeners if specified
if(removeListeners) {
this.removeAllEventListeners();
}
// Close all connections
for(var i = 0; i < this.openConnections.length; i++) {
this.openConnections[i].close();
}
// Clean up
this.openConnections = [];
}
// Check the status of the connection
ConnectionPool.prototype.isConnected = function() {
return this._poolState === 'connected';
}
// Checkout a connection from the pool for usage, or grab a specific pool instance
ConnectionPool.prototype.checkoutConnection = function(id) {
var index = (this.currentConnectionIndex++ % (this.openConnections.length));
var connection = this.openConnections[index];
return connection;
}
ConnectionPool.prototype.getAllConnections = function() {
return this.openConnections;
}
// Remove all non-needed event listeners
ConnectionPool.prototype.removeAllEventListeners = function() {
this.removeAllListeners("close");
this.removeAllListeners("error");
this.removeAllListeners("timeout");
this.removeAllListeners("connect");
this.removeAllListeners("end");
this.removeAllListeners("parseError");
this.removeAllListeners("message");
this.removeAllListeners("poolReady");
}

View File

@@ -0,0 +1,23 @@
exports.setIntegerParameter = function(object, field, defaultValue) {
if(object[field] == null) {
object[field] = defaultValue;
} else if(typeof object[field] !== "number" && object[field] !== parseInt(object[field], 10)) {
throw "object field [" + field + "] must be a numeric integer value, attempted to set to [" + object[field] + "] type of [" + typeof object[field] + "]";
}
}
exports.setBooleanParameter = function(object, field, defaultValue) {
if(object[field] == null) {
object[field] = defaultValue;
} else if(typeof object[field] !== "boolean") {
throw "object field [" + field + "] must be a boolean value, attempted to set to [" + object[field] + "] type of [" + typeof object[field] + "]";
}
}
exports.setStringParameter = function(object, field, defaultValue) {
if(object[field] == null) {
object[field] = defaultValue;
} else if(typeof object[field] !== "string") {
throw "object field [" + field + "] must be a string value, attempted to set to [" + object[field] + "] type of [" + typeof object[field] + "]";
}
}

View File

@@ -0,0 +1,316 @@
var ReadPreference = require('./read_preference').ReadPreference;
/**
* Mongos constructor provides a connection to a mongos proxy including failover to additional servers
*
* Options
* - **socketOptions** {Object, default:null}, an object containing socket options to use (noDelay:(boolean), keepAlive:(number), connectTimeoutMS:(number), socketTimeoutMS:(number))
* - **ha** {Boolean, default:true}, turn on high availability, attempts to reconnect to down proxies
* - **haInterval** {Number, default:2000}, time between each replicaset status check.
*
* @class Represents a Mongos connection with failover to backup proxies
* @param {Array} list of mongos server objects
* @param {Object} [options] additional options for the mongos connection
*/
var Mongos = function Mongos(servers, options) {
// Set up basic
if(!(this instanceof Mongos))
return new Mongos(servers, options);
// Throw error on wrong setup
if(servers == null || !Array.isArray(servers) || servers.length == 0)
throw new Error("At least one mongos proxy must be in the array");
// Ensure we have at least an empty options object
this.options = options == null ? {} : options;
// Set default connection pool options
this.socketOptions = this.options.socketOptions != null ? this.options.socketOptions : {};
// Enabled ha
this.haEnabled = this.options['ha'] == null ? true : this.options['ha'];
// How often are we checking for new servers in the replicaset
this.mongosStatusCheckInterval = this.options['haInterval'] == null ? 2000 : this.options['haInterval'];
// Save all the server connections
this.servers = servers;
// Servers we need to attempt reconnect with
this.downServers = [];
// Just contains the current lowest ping time and server
this.lowestPingTimeServer = null;
this.lowestPingTime = 0;
// Add options to servers
for(var i = 0; i < this.servers.length; i++) {
var server = this.servers[i];
// Default empty socket options object
var socketOptions = {host: server.host, port: server.port};
// If a socket option object exists clone it
if(this.socketOptions != null) {
var keys = Object.keys(this.socketOptions);
for(var k = 0; k < keys.length;k++) socketOptions[keys[i]] = this.socketOptions[keys[i]];
}
// Set socket options
server.socketOptions = socketOptions;
}
}
/**
* @ignore
*/
Mongos.prototype.isMongos = function() {
return true;
}
/**
* @ignore
*/
Mongos.prototype.connect = function(db, options, callback) {
if('function' === typeof options) callback = options, options = {};
if(options == null) options = {};
if(!('function' === typeof callback)) callback = null;
var self = this;
// Keep reference to parent
this.db = db;
// Set server state to connecting
this._serverState = 'connecting';
// Number of total servers that need to initialized (known servers)
this._numberOfServersLeftToInitialize = this.servers.length;
// Default to the first proxy server as the first one to use
this._currentMongos = this.servers[0];
// Connect handler
var connectHandler = function(_server) {
return function(err, result) {
self._numberOfServersLeftToInitialize = self._numberOfServersLeftToInitialize - 1;
if(self._numberOfServersLeftToInitialize == 0) {
// Start ha function if it exists
if(self.haEnabled) {
// Setup the ha process
self._replicasetTimeoutId = setTimeout(self.mongosCheckFunction, self.mongosStatusCheckInterval);
}
// Set the mongos to connected
self._serverState = "connected";
// Emit the open event
self.db.emit("open", null, self.db);
// Callback
callback(null, self.db);
}
}
};
// Error handler
var errorOrCloseHandler = function(_server) {
return function(err, result) {
// Create current mongos comparision
var currentUrl = self._currentMongos.host + ":" + self._currentMongos.port;
var serverUrl = this.host + ":" + this.port;
// We need to check if the server that closed is the actual current proxy we are using, otherwise
// just ignore
if(currentUrl == serverUrl) {
// Remove the server from the list
if(self.servers.indexOf(_server) != -1) {
self.servers.splice(self.servers.indexOf(_server), 1);
}
// Pick the next one on the list if there is one
for(var i = 0; i < self.servers.length; i++) {
// Grab the server out of the array (making sure there is no servers in the list if none available)
var server = self.servers[i];
// Generate url for comparision
var serverUrl = server.host + ":" + server.port;
// It's not the current one and connected set it as the current db
if(currentUrl != serverUrl && server.isConnected()) {
self._currentMongos = server;
break;
}
}
}
// Ensure we don't store the _server twice
if(self.downServers.indexOf(_server) == -1) {
// Add the server instances
self.downServers.push(_server);
}
}
}
// Mongo function
this.mongosCheckFunction = function() {
// If we have down servers let's attempt a reconnect
if(self.downServers.length > 0) {
var numberOfServersLeft = self.downServers.length;
// Attempt to reconnect
for(var i = 0; i < self.downServers.length; i++) {
var downServer = self.downServers.pop();
// Attemp to reconnect
downServer.connect(self.db, {returnIsMasterResults: true}, function(_server) {
// Return a function to check for the values
return function(err, result) {
// Adjust the number of servers left
numberOfServersLeft = numberOfServersLeft - 1;
if(err != null) {
self.downServers.push(_server);
} else {
// Add server event handlers
_server.on("close", errorOrCloseHandler(_server));
_server.on("error", errorOrCloseHandler(_server));
// Add to list of servers
self.servers.push(_server);
}
if(numberOfServersLeft <= 0) {
// Perfom another ha
self._replicasetTimeoutId = setTimeout(self.mongosCheckFunction, self.mongosStatusCheckInterval);
}
}
}(downServer));
}
} else if(self.servers.length > 0) {
var numberOfServersLeft = self.servers.length;
var _s = new Date().getTime()
// Else let's perform a ping command
for(var i = 0; i < self.servers.length; i++) {
var executePing = function(_server) {
// Get a read connection
var _connection = _server.checkoutReader();
// Execute ping command
self.db.command({ping:1}, {connection:_connection}, function(err, result) {
var pingTime = new Date().getTime() - _s;
// If no server set set the first one, otherwise check
// the lowest ping time and assign the server if it's got a lower ping time
if(self.lowestPingTimeServer == null) {
self.lowestPingTimeServer = _server;
self.lowestPingTime = pingTime;
self._currentMongos = _server;
} else if(self.lowestPingTime > pingTime
&& (_server.host != self.lowestPingTimeServer.host || _server.port != self.lowestPingTimeServer.port)) {
self.lowestPingTimeServer = _server;
self.lowestPingTime = pingTime;
self._currentMongos = _server;
}
// Number of servers left
numberOfServersLeft = numberOfServersLeft - 1;
// All active mongos's pinged
if(numberOfServersLeft == 0) {
// Perfom another ha
self._replicasetTimeoutId = setTimeout(self.mongosCheckFunction, self.mongosStatusCheckInterval);
}
})
}
// Execute the function
executePing(self.servers[i]);
}
} else {
self._replicasetTimeoutId = setTimeout(self.mongosCheckFunction, self.mongosStatusCheckInterval);
}
}
// Connect all the server instances
for(var i = 0; i < this.servers.length; i++) {
// Get the connection
var server = this.servers[i];
server.mongosInstance = this;
// Add server event handlers
server.on("close", errorOrCloseHandler(server));
server.on("error", errorOrCloseHandler(server));
// Connect the instance
server.connect(self.db, {returnIsMasterResults: true}, connectHandler(server));
}
}
/**
* @ignore
* Just return the currently picked active connection
*/
Mongos.prototype.allServerInstances = function() {
return this.servers;
}
/**
* @ignore
*/
Mongos.prototype.allRawConnections = function() {
// Neeed to build a complete list of all raw connections, start with master server
var allConnections = [];
// Add all connections
for(var i = 0; i < this.servers.length; i++) {
allConnections = allConnections.concat(this.servers[i].allRawConnections)
}
// Return all the conections
return allConnections;
}
/**
* @ignore
*/
Mongos.prototype.isConnected = function() {
return this._serverState == "connected";
}
/**
* @ignore
*/
Mongos.prototype.checkoutWriter = function() {
// No current mongo, just pick first server
if(this._currentMongos == null && this.servers.length > 0) {
return this.servers[0].checkoutWriter();
}
return this._currentMongos.checkoutWriter();
}
/**
* @ignore
*/
Mongos.prototype.checkoutReader = function(read) {
// If we have a read preference object unpack it
if(typeof read == 'object' && read['_type'] == 'ReadPreference') {
// Validate if the object is using a valid mode
if(!read.isValid()) throw new Error("Illegal readPreference mode specified, " + read.mode);
} else if(!ReadPreference.isValid(read)) {
throw new Error("Illegal readPreference mode specified, " + read);
}
// No current mongo, just pick first server
if(this._currentMongos == null && this.servers.length > 0) {
return this.servers[0].checkoutReader();
}
return this._currentMongos.checkoutReader();
}
/**
* @ignore
*/
Mongos.prototype.close = function(callback) {
var self = this;
// Set server status as disconnected
this._serverState = 'disconnected';
// Number of connections to close
var numberOfConnectionsToClose = self.servers.length;
// If we have a ha process running kill it
if(self._replicasetTimeoutId != null) clearTimeout(self._replicasetTimeoutId);
// Close all proxy connections
for(var i = 0; i < self.servers.length; i++) {
self.servers[i].close(function(err, result) {
numberOfConnectionsToClose = numberOfConnectionsToClose - 1;
// Callback if we have one defined
if(numberOfConnectionsToClose == 0 && typeof callback == 'function') {
callback(null);
}
});
}
}
/**
* @ignore
* Return the used state
*/
Mongos.prototype._isUsed = function() {
return this._used;
}
exports.Mongos = Mongos;

View File

@@ -0,0 +1,66 @@
/**
* A class representation of the Read Preference.
*
* Read Preferences
* - **ReadPreference.PRIMARY**, Read from primary only. All operations produce an error (throw an exception where applicable) if primary is unavailable. Cannot be combined with tags (This is the default.).
* - **ReadPreference.PRIMARY_PREFERRED**, Read from primary if available, otherwise a secondary.
* - **ReadPreference.SECONDARY**, Read from secondary if available, otherwise error.
* - **ReadPreference.SECONDARY_PREFERRED**, Read from a secondary if available, otherwise read from the primary.
* - **ReadPreference.NEAREST**, All modes read from among the nearest candidates, but unlike other modes, NEAREST will include both the primary and all secondaries in the random selection.
*
* @class Represents a Read Preference.
* @param {String} the read preference type
* @param {Object} tags
* @return {ReadPreference}
*/
var ReadPreference = function(mode, tags) {
if(!(this instanceof ReadPreference))
return new ReadPreference(mode, tags);
this._type = 'ReadPreference';
this.mode = mode;
this.tags = tags;
}
/**
* @ignore
*/
ReadPreference.isValid = function(_mode) {
return (_mode == ReadPreference.PRIMARY || _mode == ReadPreference.PRIMARY_PREFERRED
|| _mode == ReadPreference.SECONDARY || _mode == ReadPreference.SECONDARY_PREFERRED
|| _mode == ReadPreference.NEAREST);
}
/**
* @ignore
*/
ReadPreference.prototype.isValid = function(mode) {
var _mode = typeof mode == 'string' ? mode : this.mode;
return ReadPreference.isValid(_mode);
}
/**
* @ignore
*/
ReadPreference.prototype.toObject = function() {
var object = {mode:this.mode};
if(this.tags != null) {
object['tags'] = this.tags;
}
return object;
}
/**
* @ignore
*/
ReadPreference.PRIMARY = 'primary';
ReadPreference.PRIMARY_PREFERRED = 'primaryPreferred';
ReadPreference.SECONDARY = 'secondary';
ReadPreference.SECONDARY_PREFERRED = 'secondaryPreferred';
ReadPreference.NEAREST = 'nearest'
/**
* @ignore
*/
exports.ReadPreference = ReadPreference;

View File

@@ -0,0 +1,848 @@
var Connection = require('./connection').Connection,
ReadPreference = require('./read_preference').ReadPreference,
DbCommand = require('../commands/db_command').DbCommand,
MongoReply = require('../responses/mongo_reply').MongoReply,
ConnectionPool = require('./connection_pool').ConnectionPool,
EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits;
/**
* Class representing a single MongoDB Server connection
*
* Options
* - **readPreference** {String, default:null}, set's the read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST)
* - **ssl** {Boolean, default:false}, use ssl connection (needs to have a mongod server with ssl support)
* - **slaveOk** {Boolean, default:false}, legacy option allowing reads from secondary, use **readPrefrence** instead.
* - **poolSize** {Number, default:1}, number of connections in the connection pool, set to 1 as default for legacy reasons.
* - **socketOptions** {Object, default:null}, an object containing socket options to use (noDelay:(boolean), keepAlive:(number), connectTimeoutMS:(number), socketTimeoutMS:(number))
* - **logger** {Object, default:null}, an object representing a logger that you want to use, needs to support functions debug, log, error **({error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}})**.
* - **auto_reconnect** {Boolean, default:false}, reconnect on error.
* - **disableDriverBSONSizeCheck** {Boolean, default:false}, force the server to error if the BSON message is to big
*
* @class Represents a Server connection.
* @param {String} host the server host
* @param {Number} port the server port
* @param {Object} [options] optional options for insert command
*/
function Server(host, port, options) {
// Set up event emitter
EventEmitter.call(this);
// Set up Server instance
if(!(this instanceof Server)) return new Server(host, port, options);
// Ensure correct values
if(port != null && typeof port == 'object') {
options = port;
port = Connection.DEFAULT_PORT;
}
var self = this;
this.host = host;
this.port = port;
this.options = options == null ? {} : options;
this.internalConnection;
this.internalMaster = false;
this.connected = false;
this.poolSize = this.options.poolSize == null ? 5 : this.options.poolSize;
this.disableDriverBSONSizeCheck = this.options.disableDriverBSONSizeCheck != null ? this.options.disableDriverBSONSizeCheck : false;
this.ssl = this.options.ssl == null ? false : this.options.ssl;
this.slaveOk = this.options["slave_ok"];
this._used = false;
// Get the readPreference
var readPreference = this.options['readPreference'];
// Read preference setting
if(readPreference != null) {
if(readPreference != ReadPreference.PRIMARY && readPreference != ReadPreference.SECONDARY && readPreference != ReadPreference.NEAREST
&& readPreference != ReadPreference.SECONDARY_PREFERRED && readPreference != ReadPreference.PRIMARY_PREFERRED) {
throw new Error("Illegal readPreference mode specified, " + readPreference);
}
// Set read Preference
this._readPreference = readPreference;
} else {
this._readPreference = null;
}
// Contains the isMaster information returned from the server
this.isMasterDoc;
// Set default connection pool options
this.socketOptions = this.options.socketOptions != null ? this.options.socketOptions : {};
if(this.disableDriverBSONSizeCheck) this.socketOptions.disableDriverBSONSizeCheck = this.disableDriverBSONSizeCheck;
// Set ssl up if it's defined
if(this.ssl) {
this.socketOptions.ssl = true;
}
// Set up logger if any set
this.logger = this.options.logger != null
&& (typeof this.options.logger.debug == 'function')
&& (typeof this.options.logger.error == 'function')
&& (typeof this.options.logger.log == 'function')
? this.options.logger : {error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}};
// Just keeps list of events we allow
this.eventHandlers = {error:[], parseError:[], poolReady:[], message:[], close:[], timeout:[]};
// Internal state of server connection
this._serverState = 'disconnected';
// this._timeout = false;
// Contains state information about server connection
this._state = {'runtimeStats': {'queryStats':new RunningStats()}};
// Do we record server stats or not
this.recordQueryStats = false;
};
/**
* @ignore
*/
// Inherit simple event emitter
inherits(Server, EventEmitter);
//
// Deprecated, USE ReadPreferences class
//
Server.READ_PRIMARY = ReadPreference.PRIMARY;
Server.READ_SECONDARY = ReadPreference.SECONDARY_PREFERRED;
Server.READ_SECONDARY_ONLY = ReadPreference.SECONDARY;
/**
* Always ourselves
* @ignore
*/
Server.prototype.setReadPreference = function() {}
/**
* @ignore
*/
Server.prototype.isMongos = function() {
return this.isMasterDoc != null && this.isMasterDoc['msg'] == "isdbgrid" ? true : false;
}
/**
* @ignore
*/
Server.prototype._isUsed = function() {
return this._used;
}
/**
* @ignore
*/
Server.prototype.close = function(callback) {
// Remove all local listeners
this.removeAllListeners();
if(this.connectionPool != null) {
// Remove all the listeners on the pool so it does not fire messages all over the place
this.connectionPool.removeAllEventListeners();
// Close the connection if it's open
this.connectionPool.stop(true);
}
// Set server status as disconnected
this._serverState = 'disconnected';
// Peform callback if present
if(typeof callback === 'function') callback();
};
/**
* @ignore
*/
Server.prototype.isConnected = function() {
return this._serverState == 'connected';
}
/**
* @ignore
*/
Server.prototype.allServerInstances = function() {
return [this];
}
/**
* @ignore
*/
Server.prototype.isSetMember = function() {
return this['replicasetInstance'] != null || this['mongosInstance'] != null;
}
/**
* @ignore
*/
Server.prototype.connect = function(dbInstance, options, callback) {
if('function' === typeof options) callback = options, options = {};
if(options == null) options = {};
if(!('function' === typeof callback)) callback = null;
// Currently needed to work around problems with multiple connections in a pool with ssl
// TODO fix if possible
if(this.ssl == true) {
// Set up socket options for ssl
this.socketOptions.ssl = true;
}
// Let's connect
var server = this;
// Let's us override the main receiver of events
var eventReceiver = options.eventReceiver != null ? options.eventReceiver : this;
// Creating dbInstance
this.dbInstance = dbInstance;
// Save reference to dbInstance
this.dbInstances = [dbInstance];
// Force connection pool if there is one
if(server.connectionPool) server.connectionPool.stop();
// Set server state to connecting
this._serverState = 'connecting';
// Ensure dbInstance can do a slave query if it's set
dbInstance.slaveOk = this.slaveOk ? this.slaveOk : dbInstance.slaveOk;
// Create connection Pool instance with the current BSON serializer
var connectionPool = new ConnectionPool(this.host, this.port, this.poolSize, dbInstance.bson, this.socketOptions);
// Set logger on pool
connectionPool.logger = this.logger;
// Set up a new pool using default settings
server.connectionPool = connectionPool;
// Set basic parameters passed in
var returnIsMasterResults = options.returnIsMasterResults == null ? false : options.returnIsMasterResults;
// Create a default connect handler, overriden when using replicasets
var connectCallback = function(err, reply) {
// ensure no callbacks get called twice
var internalCallback = callback;
callback = null;
// If something close down the connection and removed the callback before
// proxy killed connection etc, ignore the erorr as close event was isssued
if(err != null && internalCallback == null) return;
// Internal callback
if(err != null) return internalCallback(err, null);
server.master = reply.documents[0].ismaster == 1 ? true : false;
server.connectionPool.setMaxBsonSize(reply.documents[0].maxBsonObjectSize);
// Set server as connected
server.connected = true;
// Save document returned so we can query it
server.isMasterDoc = reply.documents[0];
// Emit open event
_emitAcrossAllDbInstances(server, eventReceiver, "open", null, returnIsMasterResults ? reply : dbInstance, null);
// If we have it set to returnIsMasterResults
if(returnIsMasterResults) {
internalCallback(null, reply, server);
} else {
internalCallback(null, dbInstance, server);
}
};
// Let's us override the main connect callback
var connectHandler = options.connectHandler == null ? connectCallback : options.connectHandler;
// Set up on connect method
connectionPool.on("poolReady", function() {
// Create db command and Add the callback to the list of callbacks by the request id (mapping outgoing messages to correct callbacks)
var db_command = DbCommand.NcreateIsMasterCommand(dbInstance, dbInstance.databaseName);
// Check out a reader from the pool
var connection = connectionPool.checkoutConnection();
// Set server state to connEcted
server._serverState = 'connected';
// Register handler for messages
dbInstance._registerHandler(db_command, false, connection, connectHandler);
// Write the command out
connection.write(db_command);
})
// Set up item connection
connectionPool.on("message", function(message) {
// Attempt to parse the message
try {
// Create a new mongo reply
var mongoReply = new MongoReply()
// Parse the header
mongoReply.parseHeader(message, connectionPool.bson)
// If message size is not the same as the buffer size
// something went terribly wrong somewhere
if(mongoReply.messageLength != message.length) {
// Emit the error
if(eventReceiver.listeners("error") && eventReceiver.listeners("error").length > 0) eventReceiver.emit("error", new Error("bson length is different from message length"), server);
// Remove all listeners
server.removeAllListeners();
} else {
var startDate = new Date().getTime();
// Callback instance
var callbackInfo = null;
var dbInstanceObject = null;
// Locate a callback instance and remove any additional ones
for(var i = 0; i < server.dbInstances.length; i++) {
var dbInstanceObjectTemp = server.dbInstances[i];
var hasHandler = dbInstanceObjectTemp._hasHandler(mongoReply.responseTo.toString());
// Assign the first one we find and remove any duplicate ones
if(hasHandler && callbackInfo == null) {
callbackInfo = dbInstanceObjectTemp._findHandler(mongoReply.responseTo.toString());
dbInstanceObject = dbInstanceObjectTemp;
} else if(hasHandler) {
dbInstanceObjectTemp._removeHandler(mongoReply.responseTo.toString());
}
}
// The command executed another request, log the handler again under that request id
if(mongoReply.requestId > 0 && mongoReply.cursorId.toString() != "0" && callbackInfo.info && callbackInfo.info.exhaust) {
dbInstance._reRegisterHandler(mongoReply.requestId, callbackInfo);
}
// Only execute callback if we have a caller
// chained is for findAndModify as it does not respect write concerns
if(callbackInfo && callbackInfo.callback && callbackInfo.info && Array.isArray(callbackInfo.info.chained)) {
// Check if callback has already been fired (missing chain command)
var chained = callbackInfo.info.chained;
var numberOfFoundCallbacks = 0;
for(var i = 0; i < chained.length; i++) {
if(dbInstanceObject._hasHandler(chained[i])) numberOfFoundCallbacks++;
}
// If we have already fired then clean up rest of chain and move on
if(numberOfFoundCallbacks != chained.length) {
for(var i = 0; i < chained.length; i++) {
dbInstanceObject._removeHandler(chained[i]);
}
// Just return from function
return;
}
// Parse the body
mongoReply.parseBody(message, connectionPool.bson, callbackInfo.info.raw, function(err) {
if(err != null) {
// If pool connection is already closed
if(server._serverState === 'disconnected') return;
// Set server state to disconnected
server._serverState = 'disconnected';
// Remove all listeners and close the connection pool
server.removeAllListeners();
connectionPool.stop(true);
// If we have a callback return the error
if(typeof callback === 'function') {
// ensure no callbacks get called twice
var internalCallback = callback;
callback = null;
// Perform callback
internalCallback(new Error("connection closed due to parseError"), null, server);
} else if(server.isSetMember()) {
if(server.listeners("parseError") && server.listeners("parseError").length > 0) server.emit("parseError", new Error("connection closed due to parseError"), server);
} else {
if(eventReceiver.listeners("parseError") && eventReceiver.listeners("parseError").length > 0) eventReceiver.emit("parseError", new Error("connection closed due to parseError"), server);
}
// If we are a single server connection fire errors correctly
if(!server.isSetMember()) {
// Fire all callback errors
_fireCallbackErrors(server, new Error("connection closed due to parseError"));
// Emit error
_emitAcrossAllDbInstances(server, eventReceiver, "parseError", server, null, true);
}
// Short cut
return;
}
// Fetch the callback
var callbackInfo = dbInstanceObject._findHandler(mongoReply.responseTo.toString());
// If we have an error let's execute the callback and clean up all other
// chained commands
var firstResult = mongoReply && mongoReply.documents;
// Check for an error, if we have one let's trigger the callback and clean up
// The chained callbacks
if(firstResult[0].err != null || firstResult[0].errmsg != null) {
// Trigger the callback for the error
dbInstanceObject._callHandler(mongoReply.responseTo, mongoReply, null);
} else {
var chainedIds = callbackInfo.info.chained;
if(chainedIds.length > 0 && chainedIds[chainedIds.length - 1] == mongoReply.responseTo) {
// Cleanup all other chained calls
chainedIds.pop();
// Remove listeners
for(var i = 0; i < chainedIds.length; i++) dbInstanceObject._removeHandler(chainedIds[i]);
// Call the handler
dbInstanceObject._callHandler(mongoReply.responseTo, callbackInfo.info.results.shift(), null);
} else{
// Add the results to all the results
for(var i = 0; i < chainedIds.length; i++) {
var handler = dbInstanceObject._findHandler(chainedIds[i]);
// Check if we have an object, if it's the case take the current object commands and
// and add this one
if(handler.info != null) {
handler.info.results = Array.isArray(callbackInfo.info.results) ? callbackInfo.info.results : [];
handler.info.results.push(mongoReply);
}
}
}
}
});
} else if(callbackInfo && callbackInfo.callback && callbackInfo.info) {
// Parse the body
mongoReply.parseBody(message, connectionPool.bson, callbackInfo.info.raw, function(err) {
if(err != null) {
// If pool connection is already closed
if(server._serverState === 'disconnected') return;
// Set server state to disconnected
server._serverState = 'disconnected';
// Remove all listeners and close the connection pool
server.removeAllListeners();
connectionPool.stop(true);
// If we have a callback return the error
if(typeof callback === 'function') {
// ensure no callbacks get called twice
var internalCallback = callback;
callback = null;
// Perform callback
internalCallback(new Error("connection closed due to parseError"), null, server);
} else if(server.isSetMember()) {
if(server.listeners("parseError") && server.listeners("parseError").length > 0) server.emit("parseError", new Error("connection closed due to parseError"), server);
} else {
if(eventReceiver.listeners("parseError") && eventReceiver.listeners("parseError").length > 0) eventReceiver.emit("parseError", new Error("connection closed due to parseError"), server);
}
// If we are a single server connection fire errors correctly
if(!server.isSetMember()) {
// Fire all callback errors
_fireCallbackErrors(server, new Error("connection closed due to parseError"));
// Emit error
_emitAcrossAllDbInstances(server, eventReceiver, "parseError", server, null, true);
}
// Short cut
return;
}
// Let's record the stats info if it's enabled
if(server.recordQueryStats == true && server._state['runtimeStats'] != null
&& server._state.runtimeStats['queryStats'] instanceof RunningStats) {
// Add data point to the running statistics object
server._state.runtimeStats.queryStats.push(new Date().getTime() - callbackInfo.info.start);
}
dbInstanceObject._callHandler(mongoReply.responseTo, mongoReply, null);
});
}
}
} catch (err) {
// Throw error in next tick
process.nextTick(function() {
throw err;
})
}
});
// Handle timeout
connectionPool.on("timeout", function(err) {
// If pool connection is already closed
if(server._serverState === 'disconnected') return;
// Set server state to disconnected
server._serverState = 'disconnected';
// If we have a callback return the error
if(typeof callback === 'function') {
// ensure no callbacks get called twice
var internalCallback = callback;
callback = null;
// Perform callback
internalCallback(err, null, server);
} else if(server.isSetMember()) {
if(server.listeners("timeout") && server.listeners("timeout").length > 0) server.emit("timeout", err, server);
} else {
if(eventReceiver.listeners("timeout") && eventReceiver.listeners("timeout").length > 0) eventReceiver.emit("timeout", err, server);
}
// If we are a single server connection fire errors correctly
if(!server.isSetMember()) {
// Fire all callback errors
_fireCallbackErrors(server, err);
// Emit error
_emitAcrossAllDbInstances(server, eventReceiver, "timeout", err, server, true);
}
});
// Handle errors
connectionPool.on("error", function(message) {
// If pool connection is already closed
if(server._serverState === 'disconnected') return;
// Set server state to disconnected
server._serverState = 'disconnected';
// If we have a callback return the error
if(typeof callback === 'function') {
// ensure no callbacks get called twice
var internalCallback = callback;
callback = null;
// Perform callback
internalCallback(new Error(message && message.err ? message.err : message), null, server);
} else if(server.isSetMember()) {
if(server.listeners("error") && server.listeners("error").length > 0) server.emit("error", new Error(message && message.err ? message.err : message), server);
} else {
if(eventReceiver.listeners("error") && eventReceiver.listeners("error").length > 0) eventReceiver.emit("error", new Error(message && message.err ? message.err : message), server);
}
// If we are a single server connection fire errors correctly
if(!server.isSetMember()) {
// Fire all callback errors
_fireCallbackErrors(server, new Error(message && message.err ? message.err : message));
// Emit error
_emitAcrossAllDbInstances(server, eventReceiver, "error", new Error(message && message.err ? message.err : message), server, true);
}
});
// Handle close events
connectionPool.on("close", function() {
// If pool connection is already closed
if(server._serverState === 'disconnected') return;
// Set server state to disconnected
server._serverState = 'disconnected';
// If we have a callback return the error
if(typeof callback == 'function') {
// ensure no callbacks get called twice
var internalCallback = callback;
callback = null;
// Perform callback
internalCallback(new Error("connection closed"), null, server);
} else if(server.isSetMember()) {
if(server.listeners("close") && server.listeners("close").length > 0) server.emit("close", new Error("connection closed"), server);
} else {
if(eventReceiver.listeners("close") && eventReceiver.listeners("close").length > 0) eventReceiver.emit("close", new Error("connection closed"), server);
}
// If we are a single server connection fire errors correctly
if(!server.isSetMember()) {
// Fire all callback errors
_fireCallbackErrors(server, new Error("connection closed"));
// Emit error
_emitAcrossAllDbInstances(server, eventReceiver, "close", server, null, true);
}
});
// If we have a parser error we are in an unknown state, close everything and emit
// error
connectionPool.on("parseError", function(message) {
// If pool connection is already closed
if(server._serverState === 'disconnected') return;
// Set server state to disconnected
server._serverState = 'disconnected';
// If we have a callback return the error
if(typeof callback === 'function') {
// ensure no callbacks get called twice
var internalCallback = callback;
callback = null;
// Perform callback
internalCallback(new Error("connection closed due to parseError"), null, server);
} else if(server.isSetMember()) {
if(server.listeners("parseError") && server.listeners("parseError").length > 0) server.emit("parseError", new Error("connection closed due to parseError"), server);
} else {
if(eventReceiver.listeners("parseError") && eventReceiver.listeners("parseError").length > 0) eventReceiver.emit("parseError", new Error("connection closed due to parseError"), server);
}
// If we are a single server connection fire errors correctly
if(!server.isSetMember()) {
// Fire all callback errors
_fireCallbackErrors(server, new Error("connection closed due to parseError"));
// Emit error
_emitAcrossAllDbInstances(server, eventReceiver, "parseError", server, null, true);
}
});
// Boot up connection poole, pass in a locator of callbacks
connectionPool.start();
}
/**
* Fire all the errors
* @ignore
*/
var _fireCallbackErrors = function(server, err) {
// Locate all the possible callbacks that need to return
for(var i = 0; i < server.dbInstances.length; i++) {
// Fetch the db Instance
var dbInstance = server.dbInstances[i];
// Check all callbacks
var keys = Object.keys(dbInstance._callBackStore._notReplied);
// For each key check if it's a callback that needs to be returned
for(var j = 0; j < keys.length; j++) {
var info = dbInstance._callBackStore._notReplied[keys[j]];
// Check if we have a chained command (findAndModify)
if(info && info['chained'] && Array.isArray(info['chained']) && info['chained'].length > 0) {
var chained = info['chained'];
// Only callback once and the last one is the right one
var finalCallback = chained.pop();
if(info.connection.socketOptions.host === server.host && info.connection.socketOptions.port === server.port) {
dbInstance._callBackStore.emit(finalCallback, err, null);
}
// Put back the final callback to ensure we don't call all commands in the chain
chained.push(finalCallback);
// Remove all chained callbacks
for(var i = 0; i < chained.length; i++) {
delete dbInstance._callBackStore._notReplied[chained[i]];
}
} else {
if(info && info.connection.socketOptions.host === server.host && info.connection.socketOptions.port === server.port) {
dbInstance._callBackStore.emit(keys[j], err, null);
}
}
}
}
}
/**
* @ignore
*/
var _emitAcrossAllDbInstances = function(server, filterDb, event, message, object, resetConnection) {
// Emit close event across all db instances sharing the sockets
var allServerInstances = server.allServerInstances();
// Fetch the first server instance
var serverInstance = allServerInstances[0];
// For all db instances signal all db instances
if(Array.isArray(serverInstance.dbInstances) && serverInstance.dbInstances.length >= 1) {
for(var i = 0; i < serverInstance.dbInstances.length; i++) {
var dbInstance = serverInstance.dbInstances[i];
// Set the parent
if(resetConnection && typeof dbInstance.openCalled != 'undefined')
dbInstance.openCalled = false;
// Check if it's our current db instance and skip if it is
if(filterDb == null || filterDb.databaseName !== dbInstance.databaseName || filterDb.tag !== dbInstance.tag) {
// Only emit if there is a listener
if(dbInstance.listeners(event).length > 0)
dbInstance.emit(event, message, object);
}
}
}
}
/**
* @ignore
*/
Server.prototype.allRawConnections = function() {
return this.connectionPool.getAllConnections();
}
/**
* Check if a writer can be provided
* @ignore
*/
var canCheckoutWriter = function(self, read) {
// We cannot write to an arbiter or secondary server
if(self.isMasterDoc['arbiterOnly'] == true) {
return new Error("Cannot write to an arbiter");
} if(self.isMasterDoc['secondary'] == true) {
return new Error("Cannot write to a secondary");
} else if(read == true && self._readPreference == ReadPreference.SECONDARY && self.isMasterDoc['ismaster'] == true) {
return new Error("Cannot read from primary when secondary only specified");
}
// Return no error
return null;
}
/**
* @ignore
*/
Server.prototype.checkoutWriter = function(read) {
if(read == true) return this.connectionPool.checkoutConnection();
// Check if are allowed to do a checkout (if we try to use an arbiter f.ex)
var result = canCheckoutWriter(this, read);
// If the result is null check out a writer
if(result == null && this.connectionPool != null) {
return this.connectionPool.checkoutConnection();
} else if(result == null) {
return null;
} else {
return result;
}
}
/**
* Check if a reader can be provided
* @ignore
*/
var canCheckoutReader = function(self) {
// We cannot write to an arbiter or secondary server
if(self.isMasterDoc && self.isMasterDoc['arbiterOnly'] == true) {
return new Error("Cannot write to an arbiter");
} else if(self._readPreference != null) {
// If the read preference is Primary and the instance is not a master return an error
if((self._readPreference == ReadPreference.PRIMARY) && self.isMasterDoc['ismaster'] != true) {
return new Error("Read preference is Server.PRIMARY and server is not master");
} else if(self._readPreference == ReadPreference.SECONDARY && self.isMasterDoc['ismaster'] == true) {
return new Error("Cannot read from primary when secondary only specified");
}
}
// Return no error
return null;
}
/**
* @ignore
*/
Server.prototype.checkoutReader = function() {
// Check if are allowed to do a checkout (if we try to use an arbiter f.ex)
var result = canCheckoutReader(this);
// If the result is null check out a writer
if(result == null && this.connectionPool != null) {
return this.connectionPool.checkoutConnection();
} else if(result == null) {
return null;
} else {
return result;
}
}
/**
* @ignore
*/
Server.prototype.enableRecordQueryStats = function(enable) {
this.recordQueryStats = enable;
}
/**
* Internal statistics object used for calculating average and standard devitation on
* running queries
* @ignore
*/
var RunningStats = function() {
var self = this;
this.m_n = 0;
this.m_oldM = 0.0;
this.m_oldS = 0.0;
this.m_newM = 0.0;
this.m_newS = 0.0;
// Define getters
Object.defineProperty(this, "numDataValues", { enumerable: true
, get: function () { return this.m_n; }
});
Object.defineProperty(this, "mean", { enumerable: true
, get: function () { return (this.m_n > 0) ? this.m_newM : 0.0; }
});
Object.defineProperty(this, "variance", { enumerable: true
, get: function () { return ((this.m_n > 1) ? this.m_newS/(this.m_n - 1) : 0.0); }
});
Object.defineProperty(this, "standardDeviation", { enumerable: true
, get: function () { return Math.sqrt(this.variance); }
});
Object.defineProperty(this, "sScore", { enumerable: true
, get: function () {
var bottom = this.mean + this.standardDeviation;
if(bottom == 0) return 0;
return ((2 * this.mean * this.standardDeviation)/(bottom));
}
});
}
/**
* @ignore
*/
RunningStats.prototype.push = function(x) {
// Update the number of samples
this.m_n = this.m_n + 1;
// See Knuth TAOCP vol 2, 3rd edition, page 232
if(this.m_n == 1) {
this.m_oldM = this.m_newM = x;
this.m_oldS = 0.0;
} else {
this.m_newM = this.m_oldM + (x - this.m_oldM) / this.m_n;
this.m_newS = this.m_oldS + (x - this.m_oldM) * (x - this.m_newM);
// set up for next iteration
this.m_oldM = this.m_newM;
this.m_oldS = this.m_newS;
}
}
/**
* @ignore
*/
Object.defineProperty(Server.prototype, "autoReconnect", { enumerable: true
, get: function () {
return this.options['auto_reconnect'] == null ? false : this.options['auto_reconnect'];
}
});
/**
* @ignore
*/
Object.defineProperty(Server.prototype, "connection", { enumerable: true
, get: function () {
return this.internalConnection;
}
, set: function(connection) {
this.internalConnection = connection;
}
});
/**
* @ignore
*/
Object.defineProperty(Server.prototype, "master", { enumerable: true
, get: function () {
return this.internalMaster;
}
, set: function(value) {
this.internalMaster = value;
}
});
/**
* @ignore
*/
Object.defineProperty(Server.prototype, "primary", { enumerable: true
, get: function () {
return this;
}
});
/**
* Getter for query Stats
* @ignore
*/
Object.defineProperty(Server.prototype, "queryStats", { enumerable: true
, get: function () {
return this._state.runtimeStats.queryStats;
}
});
/**
* @ignore
*/
Object.defineProperty(Server.prototype, "runtimeStats", { enumerable: true
, get: function () {
return this._state.runtimeStats;
}
});
/**
* Get Read Preference method
* @ignore
*/
Object.defineProperty(Server.prototype, "readPreference", { enumerable: true
, get: function () {
if(this._readPreference == null && this.readSecondary) {
return Server.READ_SECONDARY;
} else if(this._readPreference == null && !this.readSecondary) {
return Server.READ_PRIMARY;
} else {
return this._readPreference;
}
}
});
/**
* @ignore
*/
exports.Server = Server;

View File

@@ -0,0 +1,188 @@
var Server = require("../server").Server;
// The ping strategy uses pings each server and records the
// elapsed time for the server so it can pick a server based on lowest
// return time for the db command {ping:true}
var PingStrategy = exports.PingStrategy = function(replicaset, secondaryAcceptableLatencyMS) {
this.replicaset = replicaset;
this.secondaryAcceptableLatencyMS = secondaryAcceptableLatencyMS;
this.state = 'disconnected';
this.pingInterval = 5000;
// Class instance
this.Db = require("../../db").Db;
}
// Starts any needed code
PingStrategy.prototype.start = function(callback) {
// already running?
if ('connected' == this.state) return;
this.state = 'connected';
// Start ping server
this._pingServer(callback);
}
// Stops and kills any processes running
PingStrategy.prototype.stop = function(callback) {
// Stop the ping process
this.state = 'disconnected';
// optional callback
callback && callback(null, null);
}
PingStrategy.prototype.checkoutSecondary = function(tags, secondaryCandidates) {
// Servers are picked based on the lowest ping time and then servers that lower than that + secondaryAcceptableLatencyMS
// Create a list of candidat servers, containing the primary if available
var candidateServers = [];
// If we have not provided a list of candidate servers use the default setup
if(!Array.isArray(secondaryCandidates)) {
candidateServers = this.replicaset._state.master != null ? [this.replicaset._state.master] : [];
// Add all the secondaries
var keys = Object.keys(this.replicaset._state.secondaries);
for(var i = 0; i < keys.length; i++) {
candidateServers.push(this.replicaset._state.secondaries[keys[i]])
}
} else {
candidateServers = secondaryCandidates;
}
// Final list of eligable server
var finalCandidates = [];
// If we have tags filter by tags
if(tags != null && typeof tags == 'object') {
// If we have an array or single tag selection
var tagObjects = Array.isArray(tags) ? tags : [tags];
// Iterate over all tags until we find a candidate server
for(var _i = 0; _i < tagObjects.length; _i++) {
// Grab a tag object
var tagObject = tagObjects[_i];
// Matching keys
var matchingKeys = Object.keys(tagObject);
// Remove any that are not tagged correctly
for(var i = 0; i < candidateServers.length; i++) {
var server = candidateServers[i];
// If we have tags match
if(server.tags != null) {
var matching = true;
// Ensure we have all the values
for(var j = 0; j < matchingKeys.length; j++) {
if(server.tags[matchingKeys[j]] != tagObject[matchingKeys[j]]) {
matching = false;
break;
}
}
// If we have a match add it to the list of matching servers
if(matching) {
finalCandidates.push(server);
}
}
}
}
} else {
// Final array candidates
var finalCandidates = candidateServers;
}
// Sort by ping time
finalCandidates.sort(function(a, b) {
return a.runtimeStats['pingMs'] > b.runtimeStats['pingMs'];
});
if(0 === finalCandidates.length)
return new Error("No replica set members available for query");
// handle undefined pingMs
var lowestPing = finalCandidates[0].runtimeStats['pingMs'] | 0;
// determine acceptable latency
var acceptable = lowestPing + this.secondaryAcceptableLatencyMS;
// remove any server responding slower than acceptable
var len = finalCandidates.length;
while(len--) {
if(finalCandidates[len].runtimeStats['pingMs'] > acceptable) {
finalCandidates.splice(len, 1);
}
}
// If no candidates available return an error
if(finalCandidates.length == 0)
return new Error("No replica set members available for query");
// Pick a random acceptable server
return finalCandidates[Math.round(Math.random(1000000) * (finalCandidates.length - 1))].checkoutReader();
}
PingStrategy.prototype._pingServer = function(callback) {
var self = this;
// Ping server function
var pingFunction = function() {
if(self.state == 'disconnected') return;
var addresses = self.replicaset._state.addresses;
// Grab all servers
var serverKeys = Object.keys(addresses);
// Number of server entries
var numberOfEntries = serverKeys.length;
// We got keys
for(var i = 0; i < serverKeys.length; i++) {
// We got a server instance
var server = addresses[serverKeys[i]];
// Create a new server object, avoid using internal connections as they might
// be in an illegal state
new function(serverInstance) {
var options = { poolSize: 1, timeout: 500, auto_reconnect: false };
var server = new Server(serverInstance.host, serverInstance.port, options);
var db = new self.Db(self.replicaset.db.databaseName, server);
db.on("error", done);
// Open the db instance
db.open(function(err, _db) {
if(err) return done(_db);
// Startup time of the command
var startTime = Date.now();
// Execute ping on this connection
db.executeDbCommand({ping:1}, {failFast:true}, function() {
if(null != serverInstance.runtimeStats && serverInstance.isConnected()) {
serverInstance.runtimeStats['pingMs'] = Date.now() - startTime;
}
done(_db);
})
})
function done (_db) {
// Close connection
_db.close(true);
// Adjust the number of checks
numberOfEntries--;
// If we are done with all results coming back trigger ping again
if(0 === numberOfEntries && 'connected' == self.state) {
setTimeout(pingFunction, self.pingInterval);
}
}
}(server);
}
}
// Start pingFunction
setTimeout(pingFunction, 1000);
callback && callback(null);
}

View File

@@ -0,0 +1,78 @@
// The Statistics strategy uses the measure of each end-start time for each
// query executed against the db to calculate the mean, variance and standard deviation
// and pick the server which the lowest mean and deviation
var StatisticsStrategy = exports.StatisticsStrategy = function(replicaset) {
this.replicaset = replicaset;
}
// Starts any needed code
StatisticsStrategy.prototype.start = function(callback) {
callback(null, null);
}
StatisticsStrategy.prototype.stop = function(callback) {
callback && callback(null, null);
}
StatisticsStrategy.prototype.checkoutSecondary = function(tags, secondaryCandidates) {
// Servers are picked based on the lowest ping time and then servers that lower than that + secondaryAcceptableLatencyMS
// Create a list of candidat servers, containing the primary if available
var candidateServers = [];
// If we have not provided a list of candidate servers use the default setup
if(!Array.isArray(secondaryCandidates)) {
candidateServers = this.replicaset._state.master != null ? [this.replicaset._state.master] : [];
// Add all the secondaries
var keys = Object.keys(this.replicaset._state.secondaries);
for(var i = 0; i < keys.length; i++) {
candidateServers.push(this.replicaset._state.secondaries[keys[i]])
}
} else {
candidateServers = secondaryCandidates;
}
// Final list of eligable server
var finalCandidates = [];
// If we have tags filter by tags
if(tags != null && typeof tags == 'object') {
// If we have an array or single tag selection
var tagObjects = Array.isArray(tags) ? tags : [tags];
// Iterate over all tags until we find a candidate server
for(var _i = 0; _i < tagObjects.length; _i++) {
// Grab a tag object
var tagObject = tagObjects[_i];
// Matching keys
var matchingKeys = Object.keys(tagObject);
// Remove any that are not tagged correctly
for(var i = 0; i < candidateServers.length; i++) {
var server = candidateServers[i];
// If we have tags match
if(server.tags != null) {
var matching = true;
// Ensure we have all the values
for(var j = 0; j < matchingKeys.length; j++) {
if(server.tags[matchingKeys[j]] != tagObject[matchingKeys[j]]) {
matching = false;
break;
}
}
// If we have a match add it to the list of matching servers
if(matching) {
finalCandidates.push(server);
}
}
}
}
} else {
// Final array candidates
var finalCandidates = candidateServers;
}
// If no candidates available return an error
if(finalCandidates.length == 0) return new Error("No replica set members available for query");
// Pick a random server
return finalCandidates[Math.round(Math.random(1000000) * (finalCandidates.length - 1))].checkoutReader();
}

View File

@@ -0,0 +1,818 @@
var QueryCommand = require('./commands/query_command').QueryCommand,
GetMoreCommand = require('./commands/get_more_command').GetMoreCommand,
KillCursorCommand = require('./commands/kill_cursor_command').KillCursorCommand,
Long = require('bson').Long,
ReadPreference = require('./connection/read_preference').ReadPreference,
CursorStream = require('./cursorstream'),
utils = require('./utils');
/**
* Constructor for a cursor object that handles all the operations on query result
* using find. This cursor object is unidirectional and cannot traverse backwards. Clients should not be creating a cursor directly,
* but use find to acquire a cursor.
*
* @class Represents a Cursor.
* @param {Db} db the database object to work with.
* @param {Collection} collection the collection to query.
* @param {Object} selector the query selector.
* @param {Object} fields an object containing what fields to include or exclude from objects returned.
* @param {Number} skip number of documents to skip.
* @param {Number} limit the number of results to return. -1 has a special meaning and is used by Db.eval. A value of 1 will also be treated as if it were -1.
* @param {String|Array|Object} sort the required sorting for the query.
* @param {Object} hint force the query to use a specific index.
* @param {Boolean} explain return the explaination of the query.
* @param {Boolean} snapshot Snapshot mode assures no duplicates are returned.
* @param {Boolean} timeout allow the query to timeout.
* @param {Boolean} tailable allow the cursor to be tailable.
* @param {Boolean} awaitdata allow the cursor to wait for data, only applicable for tailable cursor.
* @param {Number} batchSize the number of the subset of results to request the database to return for every request. This should initially be greater than 1 otherwise the database will automatically close the cursor. The batch size can be set to 1 with cursorInstance.batchSize after performing the initial query to the database.
* @param {Boolean} raw return all query documents as raw buffers (default false).
* @param {Boolean} read specify override of read from source (primary/secondary).
* @param {Boolean} returnKey only return the index key.
* @param {Number} maxScan limit the number of items to scan.
* @param {Number} min set index bounds.
* @param {Number} max set index bounds.
* @param {Boolean} showDiskLoc show disk location of results.
* @param {String} comment you can put a $comment field on a query to make looking in the profiler logs simpler.
* @param {Number} numberOfRetries if using awaidata specifies the number of times to retry on timeout.
* @param {String} dbName override the default dbName.
* @param {Number} tailableRetryInterval specify the miliseconds between getMores on tailable cursor.
* @param {Boolean} exhaust have the server send all the documents at once as getMore packets.
* @param {Boolean} partial have the sharded system return a partial result from mongos.
*/
function Cursor(db, collection, selector, fields, skip, limit
, sort, hint, explain, snapshot, timeout, tailable, batchSize, slaveOk, raw, read
, returnKey, maxScan, min, max, showDiskLoc, comment, awaitdata, numberOfRetries, dbName, tailableRetryInterval, exhaust, partial) {
this.db = db;
this.collection = collection;
this.selector = selector;
this.fields = fields;
this.skipValue = skip == null ? 0 : skip;
this.limitValue = limit == null ? 0 : limit;
this.sortValue = sort;
this.hint = hint;
this.explainValue = explain;
this.snapshot = snapshot;
this.timeout = timeout == null ? true : timeout;
this.tailable = tailable;
this.awaitdata = awaitdata;
this.numberOfRetries = numberOfRetries == null ? 5 : numberOfRetries;
this.currentNumberOfRetries = this.numberOfRetries;
this.batchSizeValue = batchSize == null ? 0 : batchSize;
this.slaveOk = slaveOk == null ? collection.slaveOk : slaveOk;
this.raw = raw == null ? false : raw;
this.read = read == null ? ReadPreference.PRIMARY : read;
this.returnKey = returnKey;
this.maxScan = maxScan;
this.min = min;
this.max = max;
this.showDiskLoc = showDiskLoc;
this.comment = comment;
this.tailableRetryInterval = tailableRetryInterval || 100;
this.exhaust = exhaust || false;
this.partial = partial || false;
this.totalNumberOfRecords = 0;
this.items = [];
this.cursorId = Long.fromInt(0);
// This name
this.dbName = dbName;
// State variables for the cursor
this.state = Cursor.INIT;
// Keep track of the current query run
this.queryRun = false;
this.getMoreTimer = false;
// If we are using a specific db execute against it
if(this.dbName != null) {
this.collectionName = this.dbName + "." + this.collection.collectionName;
} else {
this.collectionName = (this.db.databaseName ? this.db.databaseName + "." : '') + this.collection.collectionName;
}
};
/**
* Resets this cursor to its initial state. All settings like the query string,
* tailable, batchSizeValue, skipValue and limits are preserved.
*
* @return {Cursor} returns itself with rewind applied.
* @api public
*/
Cursor.prototype.rewind = function() {
var self = this;
if (self.state != Cursor.INIT) {
if (self.state != Cursor.CLOSED) {
self.close(function() {});
}
self.numberOfReturned = 0;
self.totalNumberOfRecords = 0;
self.items = [];
self.cursorId = Long.fromInt(0);
self.state = Cursor.INIT;
self.queryRun = false;
}
return self;
};
/**
* Returns an array of documents. The caller is responsible for making sure that there
* is enough memory to store the results. Note that the array only contain partial
* results when this cursor had been previouly accessed. In that case,
* cursor.rewind() can be used to reset the cursor.
*
* @param {Function} callback This will be called after executing this method successfully. The first paramter will contain the Error object if an error occured, or null otherwise. The second paramter will contain an array of BSON deserialized objects as a result of the query.
* @return {null}
* @api public
*/
Cursor.prototype.toArray = function(callback) {
var self = this;
if(!callback) {
throw new Error('callback is mandatory');
}
if(this.tailable) {
callback(new Error("Tailable cursor cannot be converted to array"), null);
} else if(this.state != Cursor.CLOSED) {
var items = [];
this.each(function(err, item) {
if(err != null) return callback(err, null);
if(item != null && Array.isArray(items)) {
items.push(item);
} else {
var resultItems = items;
items = null;
self.items = [];
// Returns items
callback(err, resultItems);
}
});
} else {
callback(new Error("Cursor is closed"), null);
}
};
/**
* Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
* not all of the elements will be iterated if this cursor had been previouly accessed.
* In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike
* **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements
* at any given time if batch size is specified. Otherwise, the caller is responsible
* for making sure that the entire result can fit the memory.
*
* @param {Function} callback this will be called for while iterating every document of the query result. The first paramter will contain the Error object if an error occured, or null otherwise. While the second paramter will contain the document.
* @return {null}
* @api public
*/
Cursor.prototype.each = function(callback) {
var self = this;
if (!callback) {
throw new Error('callback is mandatory');
}
if(this.state != Cursor.CLOSED) {
//FIX: stack overflow (on deep callback) (cred: https://github.com/limp/node-mongodb-native/commit/27da7e4b2af02035847f262b29837a94bbbf6ce2)
process.nextTick(function(){
var s = new Date()
// Fetch the next object until there is no more objects
self.nextObject(function(err, item) {
if(err != null) return callback(err, null);
if(item != null) {
callback(null, item);
self.each(callback);
} else {
// Close the cursor if done
self.state = Cursor.CLOSED;
callback(err, null);
}
});
});
} else {
callback(new Error("Cursor is closed"), null);
}
};
/**
* Determines how many result the query for this cursor will return
*
* @param {Function} callback this will be after executing this method. The first paramter will contain the Error object if an error occured, or null otherwise. While the second paramter will contain the number of results or null if an error occured.
* @return {null}
* @api public
*/
Cursor.prototype.count = function(callback) {
this.collection.count(this.selector, callback);
};
/**
* Sets the sort parameter of this cursor to the given value.
*
* This method has the following method signatures:
* (keyOrList, callback)
* (keyOrList, direction, callback)
*
* @param {String|Array|Object} keyOrList This can be a string or an array. If passed as a string, the string will be the field to sort. If passed an array, each element will represent a field to be sorted and should be an array that contains the format [string, direction].
* @param {String|Number} direction this determines how the results are sorted. "asc", "ascending" or 1 for asceding order while "desc", "desceding or -1 for descending order. Note that the strings are case insensitive.
* @param {Function} callback this will be called after executing this method. The first parameter will contain an error object when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
* @return {Cursor} an instance of this object.
* @api public
*/
Cursor.prototype.sort = function(keyOrList, direction, callback) {
callback = callback || function(){};
if(typeof direction === "function") { callback = direction; direction = null; }
if(this.tailable) {
callback(new Error("Tailable cursor doesn't support sorting"), null);
} else if(this.queryRun == true || this.state == Cursor.CLOSED) {
callback(new Error("Cursor is closed"), null);
} else {
var order = keyOrList;
if(direction != null) {
order = [[keyOrList, direction]];
}
this.sortValue = order;
callback(null, this);
}
return this;
};
/**
* Sets the limit parameter of this cursor to the given value.
*
* @param {Number} limit the new limit.
* @param {Function} [callback] this optional callback will be called after executing this method. The first parameter will contain an error object when the limit given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
* @return {Cursor} an instance of this object.
* @api public
*/
Cursor.prototype.limit = function(limit, callback) {
if(this.tailable) {
if(callback) {
callback(new Error("Tailable cursor doesn't support limit"), null);
} else {
throw new Error("Tailable cursor doesn't support limit");
}
} else if(this.queryRun == true || this.state == Cursor.CLOSED) {
if(callback) {
callback(new Error("Cursor is closed"), null);
} else {
throw new Error("Cursor is closed");
}
} else {
if(limit != null && limit.constructor != Number) {
if(callback) {
callback(new Error("limit requires an integer"), null);
} else {
throw new Error("limit requires an integer");
}
} else {
this.limitValue = limit;
if(callback) return callback(null, this);
}
}
return this;
};
/**
* Sets the read preference for the cursor
*
* @param {String} the read preference for the cursor, one of Server.READ_PRIMARY, Server.READ_SECONDARY, Server.READ_SECONDARY_ONLY
* @param {Function} [callback] this optional callback will be called after executing this method. The first parameter will contain an error object when the read preference given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
* @return {Cursor} an instance of this object.
* @api public
*/
Cursor.prototype.setReadPreference = function(readPreference, tags, callback) {
if(typeof tags == 'function') callback = tags;
callback = callback || function() {};
if(this.queryRun == true || this.state == Cursor.CLOSED) {
callback(new Error("Cannot change read preference on executed query or closed cursor"));
} else if(readPreference == null && readPreference != 'primary'
&& readPreference != 'secondaryOnly' && readPreference != 'secondary') {
callback(new Error("only readPreference of primary, secondary or secondaryOnly supported"));
} else {
this.read = readPreference;
}
return this;
}
/**
* Sets the skip parameter of this cursor to the given value.
*
* @param {Number} skip the new skip value.
* @param {Function} [callback] this optional callback will be called after executing this method. The first parameter will contain an error object when the skip value given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
* @return {Cursor} an instance of this object.
* @api public
*/
Cursor.prototype.skip = function(skip, callback) {
callback = callback || function(){};
if(this.tailable) {
callback(new Error("Tailable cursor doesn't support skip"), null);
} else if(this.queryRun == true || this.state == Cursor.CLOSED) {
callback(new Error("Cursor is closed"), null);
} else {
if(skip != null && skip.constructor != Number) {
callback(new Error("skip requires an integer"), null);
} else {
this.skipValue = skip;
callback(null, this);
}
}
return this;
};
/**
* Sets the batch size parameter of this cursor to the given value.
*
* @param {Number} batchSize the new batch size.
* @param {Function} [callback] this optional callback will be called after executing this method. The first parameter will contain an error object when the batchSize given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
* @return {Cursor} an instance of this object.
* @api public
*/
Cursor.prototype.batchSize = function(batchSize, callback) {
if(this.state == Cursor.CLOSED) {
if(callback != null) {
return callback(new Error("Cursor is closed"), null);
} else {
throw new Error("Cursor is closed");
}
} else if(batchSize != null && batchSize.constructor != Number) {
if(callback != null) {
return callback(new Error("batchSize requires an integer"), null);
} else {
throw new Error("batchSize requires an integer");
}
} else {
this.batchSizeValue = batchSize;
if(callback != null) return callback(null, this);
}
return this;
};
/**
* The limit used for the getMore command
*
* @return {Number} The number of records to request per batch.
* @ignore
* @api private
*/
var limitRequest = function(self) {
var requestedLimit = self.limitValue;
var absLimitValue = Math.abs(self.limitValue);
var absBatchValue = Math.abs(self.batchSizeValue);
if(absLimitValue > 0) {
if (absBatchValue > 0) {
requestedLimit = Math.min(absLimitValue, absBatchValue);
}
} else {
requestedLimit = self.batchSizeValue;
}
return requestedLimit;
};
/**
* Generates a QueryCommand object using the parameters of this cursor.
*
* @return {QueryCommand} The command object
* @ignore
* @api private
*/
var generateQueryCommand = function(self) {
// Unpack the options
var queryOptions = QueryCommand.OPTS_NONE;
if(!self.timeout) {
queryOptions |= QueryCommand.OPTS_NO_CURSOR_TIMEOUT;
}
if(self.tailable != null) {
queryOptions |= QueryCommand.OPTS_TAILABLE_CURSOR;
self.skipValue = self.limitValue = 0;
// if awaitdata is set
if(self.awaitdata != null) {
queryOptions |= QueryCommand.OPTS_AWAIT_DATA;
}
}
if(self.exhaust) {
queryOptions |= QueryCommand.OPTS_EXHAUST;
}
if(self.slaveOk) {
queryOptions |= QueryCommand.OPTS_SLAVE;
}
if(self.partial) {
queryOptions |= QueryCommand.OPTS_PARTIAL;
}
// limitValue of -1 is a special case used by Db#eval
var numberToReturn = self.limitValue == -1 ? -1 : limitRequest(self);
// Check if we need a special selector
if(self.sortValue != null || self.explainValue != null || self.hint != null || self.snapshot != null
|| self.returnKey != null || self.maxScan != null || self.min != null || self.max != null
|| self.showDiskLoc != null || self.comment != null) {
// Build special selector
var specialSelector = {'$query':self.selector};
if(self.sortValue != null) specialSelector['orderby'] = utils.formattedOrderClause(self.sortValue);
if(self.hint != null && self.hint.constructor == Object) specialSelector['$hint'] = self.hint;
if(self.snapshot != null) specialSelector['$snapshot'] = true;
if(self.returnKey != null) specialSelector['$returnKey'] = self.returnKey;
if(self.maxScan != null) specialSelector['$maxScan'] = self.maxScan;
if(self.min != null) specialSelector['$min'] = self.min;
if(self.max != null) specialSelector['$max'] = self.max;
if(self.showDiskLoc != null) specialSelector['$showDiskLoc'] = self.showDiskLoc;
if(self.comment != null) specialSelector['$comment'] = self.comment;
// If we have explain set only return a single document with automatic cursor close
if(self.explainValue != null) {
numberToReturn = (-1)*Math.abs(numberToReturn);
specialSelector['$explain'] = true;
}
// Return the query
return new QueryCommand(self.db, self.collectionName, queryOptions, self.skipValue, numberToReturn, specialSelector, self.fields);
} else {
return new QueryCommand(self.db, self.collectionName, queryOptions, self.skipValue, numberToReturn, self.selector, self.fields);
}
};
/**
* @return {Object} Returns an object containing the sort value of this cursor with
* the proper formatting that can be used internally in this cursor.
* @ignore
* @api private
*/
Cursor.prototype.formattedOrderClause = function() {
return utils.formattedOrderClause(this.sortValue);
};
/**
* Converts the value of the sort direction into its equivalent numerical value.
*
* @param sortDirection {String|number} Range of acceptable values:
* 'ascending', 'descending', 'asc', 'desc', 1, -1
*
* @return {number} The equivalent numerical value
* @throws Error if the given sortDirection is invalid
* @ignore
* @api private
*/
Cursor.prototype.formatSortValue = function(sortDirection) {
return utils.formatSortValue(sortDirection);
};
/**
* Gets the next document from the cursor.
*
* @param {Function} callback this will be called after executing this method. The first parameter will contain an error object on error while the second parameter will contain a document from the returned result or null if there are no more results.
* @api public
*/
Cursor.prototype.nextObject = function(callback) {
var self = this;
if(self.state == Cursor.INIT) {
var cmd;
try {
cmd = generateQueryCommand(self);
} catch (err) {
return callback(err, null);
}
var commandHandler = function(err, result) {
if(err != null && result == null) return callback(err, null);
if(!err && result.documents[0] && result.documents[0]['$err']) {
return self.close(function() {callback(result.documents[0]['$err'], null);});
}
self.queryRun = true;
self.state = Cursor.OPEN; // Adjust the state of the cursor
self.cursorId = result.cursorId;
self.totalNumberOfRecords = result.numberReturned;
// Add the new documents to the list of items, using forloop to avoid
// new array allocations and copying
for(var i = 0; i < result.documents.length; i++) {
self.items.push(result.documents[i]);
}
// Ignore callbacks until the cursor is dead for exhausted
if(self.exhaust && result.cursorId.toString() == "0") {
self.nextObject(callback);
} else if(self.exhaust == false || self.exhaust == null) {
self.nextObject(callback);
}
};
// If we have no connection set on this cursor check one out
if(self.connection == null) {
try {
self.connection = this.read == null ? self.db.serverConfig.checkoutWriter() : self.db.serverConfig.checkoutReader(this.read);
} catch(err) {
return callback(err, null);
}
}
// Execute the command
self.db._executeQueryCommand(cmd, {exhaust: self.exhaust, raw:self.raw, read:this.read, connection:self.connection}, commandHandler);
// Set the command handler to null
commandHandler = null;
} else if(self.items.length) {
callback(null, self.items.shift());
} else if(self.cursorId.greaterThan(Long.fromInt(0))) {
getMore(self, callback);
} else {
// Force cursor to stay open
return self.close(function() {callback(null, null);});
}
}
/**
* Gets more results from the database if any.
*
* @param {Function} callback this will be called after executing this method. The first parameter will contain an error object on error while the second parameter will contain a document from the returned result or null if there are no more results.
* @ignore
* @api private
*/
var getMore = function(self, callback) {
var limit = 0;
if (!self.tailable && self.limitValue > 0) {
limit = self.limitValue - self.totalNumberOfRecords;
if (limit < 1) {
self.close(function() {callback(null, null);});
return;
}
}
try {
var getMoreCommand = new GetMoreCommand(
self.db
, self.collectionName
, limitRequest(self)
, self.cursorId
);
// Set up options
var options = {read: self.read, raw: self.raw, connection:self.connection };
// Execute the command
self.db._executeQueryCommand(getMoreCommand, options, function(err, result) {
try {
if(err != null) {
return callback(err, null);
}
var isDead = 1 === result.responseFlag && result.cursorId.isZero();
self.cursorId = result.cursorId;
self.totalNumberOfRecords += result.numberReturned;
// Determine if there's more documents to fetch
if(result.numberReturned > 0) {
if (self.limitValue > 0) {
var excessResult = self.totalNumberOfRecords - self.limitValue;
if (excessResult > 0) {
result.documents.splice(-1 * excessResult, excessResult);
}
}
// Reset the tries for awaitdata if we are using it
self.currentNumberOfRetries = self.numberOfRetries;
// Get the documents
for(var i = 0; i < result.documents.length; i++) {
self.items.push(result.documents[i]);
}
// result = null;
callback(null, self.items.shift());
} else if(self.tailable && !isDead && self.awaitdata) {
// Excute the tailable cursor once more, will timeout after ~4 sec if awaitdata used
self.currentNumberOfRetries = self.currentNumberOfRetries - 1;
if(self.currentNumberOfRetries == 0) {
self.close(function() {
callback(new Error("tailable cursor timed out"), null);
});
} else {
process.nextTick(function() { getMore(self, callback); });
}
} else if(self.tailable && !isDead) {
self.getMoreTimer = setTimeout(function() { getMore(self, callback); }, self.tailableRetryInterval);
} else {
self.close(function() {callback(null, null); });
}
result = null;
} catch(err) {
callback(err, null);
}
});
getMoreCommand = null;
} catch(err) {
var handleClose = function() {
callback(err, null);
};
self.close(handleClose);
handleClose = null;
}
}
/**
* Gets a detailed information about how the query is performed on this cursor and how
* long it took the database to process it.
*
* @param {Function} callback this will be called after executing this method. The first parameter will always be null while the second parameter will be an object containing the details.
* @api public
*/
Cursor.prototype.explain = function(callback) {
var limit = (-1)*Math.abs(this.limitValue);
// Create a new cursor and fetch the plan
var cursor = new Cursor(this.db, this.collection, this.selector, this.fields, this.skipValue, limit
, this.sortValue, this.hint, true, this.snapshot, this.timeout, this.tailable, this.batchSizeValue
, this.slaveOk, this.raw, this.read, this.returnKey, this.maxScan, this.min, this.max, this.showDiskLoc
, this.comment, this.awaitdata, this.numberOfRetries, this.dbName);
// Fetch the explaination document
cursor.nextObject(function(err, item) {
if(err != null) return callback(err, null);
// close the cursor
cursor.close(function(err, result) {
if(err != null) return callback(err, null);
callback(null, item);
});
});
};
/**
* @ignore
*/
Cursor.prototype.streamRecords = function(options) {
console.log("[WARNING] streamRecords method is deprecated, please use stream method which is much faster");
var args = Array.prototype.slice.call(arguments, 0);
options = args.length ? args.shift() : {};
var
self = this,
stream = new process.EventEmitter(),
recordLimitValue = this.limitValue || 0,
emittedRecordCount = 0,
queryCommand = generateQueryCommand(self);
// see http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol
queryCommand.numberToReturn = options.fetchSize ? options.fetchSize : 500;
// Execute the query
execute(queryCommand);
function execute(command) {
self.db._executeQueryCommand(command, {exhaust: self.exhaust, read:self.read, raw:self.raw, connection:self.connection}, function(err,result) {
if(err) {
stream.emit('error', err);
self.close(function(){});
return;
}
if (!self.queryRun && result) {
self.queryRun = true;
self.cursorId = result.cursorId;
self.state = Cursor.OPEN;
self.getMoreCommand = new GetMoreCommand(self.db, self.collectionName, queryCommand.numberToReturn, result.cursorId);
}
var resflagsMap = {
CursorNotFound:1<<0,
QueryFailure:1<<1,
ShardConfigStale:1<<2,
AwaitCapable:1<<3
};
if(result.documents && result.documents.length && !(result.responseFlag & resflagsMap.QueryFailure)) {
try {
result.documents.forEach(function(doc){
if(recordLimitValue && emittedRecordCount>=recordLimitValue) {
throw("done");
}
emittedRecordCount++;
stream.emit('data', doc);
});
} catch(err) {
if (err != "done") { throw err; }
else {
self.close(function(){
stream.emit('end', recordLimitValue);
});
self.close(function(){});
return;
}
}
// rinse & repeat
execute(self.getMoreCommand);
} else {
self.close(function(){
stream.emit('end', recordLimitValue);
});
}
});
}
return stream;
};
/**
* Returns a Node ReadStream interface for this cursor.
*
* @return {CursorStream} returns a stream object.
* @api public
*/
Cursor.prototype.stream = function stream () {
return new CursorStream(this);
}
/**
* Close the cursor.
*
* @param {Function} callback this will be called after executing this method. The first parameter will always contain null while the second parameter will contain a reference to this cursor.
* @return {null}
* @api public
*/
Cursor.prototype.close = function(callback) {
var self = this
this.getMoreTimer && clearTimeout(this.getMoreTimer);
// Close the cursor if not needed
if(this.cursorId instanceof Long && this.cursorId.greaterThan(Long.fromInt(0))) {
try {
var command = new KillCursorCommand(this.db, [this.cursorId]);
this.db._executeQueryCommand(command, {read:self.read, raw:self.raw, connection:self.connection}, null);
} catch(err) {}
}
// Null out the connection
self.connection = null;
// Reset cursor id
this.cursorId = Long.fromInt(0);
// Set to closed status
this.state = Cursor.CLOSED;
if(callback) {
callback(null, self);
self.items = [];
}
return this;
};
/**
* Check if the cursor is closed or open.
*
* @return {Boolean} returns the state of the cursor.
* @api public
*/
Cursor.prototype.isClosed = function() {
return this.state == Cursor.CLOSED ? true : false;
};
/**
* Init state
*
* @classconstant INIT
**/
Cursor.INIT = 0;
/**
* Cursor open
*
* @classconstant OPEN
**/
Cursor.OPEN = 1;
/**
* Cursor closed
*
* @classconstant CLOSED
**/
Cursor.CLOSED = 2;
/**
* @ignore
* @api private
*/
exports.Cursor = Cursor;

View File

@@ -0,0 +1,147 @@
/**
* Module dependecies.
*/
var Stream = require('stream').Stream;
/**
* CursorStream
*
* Returns a stream interface for the **cursor**.
*
* Events
* - **data** {function(item) {}} the data event triggers when a document is ready.
* - **error** {function(err) {}} the error event triggers if an error happens.
* - **close** {function() {}} the end event triggers when there is no more documents available.
*
* @class Represents a CursorStream.
* @param {Cursor} cursor a cursor object that the stream wraps.
* @return {Stream}
*/
function CursorStream(cursor) {
if(!(this instanceof CursorStream)) return new CursorStream(cursor);
Stream.call(this);
this.readable = true;
this.paused = false;
this._cursor = cursor;
this._destroyed = null;
// give time to hook up events
var self = this;
process.nextTick(function () {
self._init();
});
}
/**
* Inherit from Stream
* @ignore
* @api private
*/
CursorStream.prototype.__proto__ = Stream.prototype;
/**
* Flag stating whether or not this stream is readable.
*/
CursorStream.prototype.readable;
/**
* Flag stating whether or not this stream is paused.
*/
CursorStream.prototype.paused;
/**
* Initialize the cursor.
* @ignore
* @api private
*/
CursorStream.prototype._init = function () {
if (this._destroyed) return;
this._next();
}
/**
* Pull the next document from the cursor.
* @ignore
* @api private
*/
CursorStream.prototype._next = function () {
if (this.paused || this._destroyed) return;
var self = this;
// nextTick is necessary to avoid stack overflows when
// dealing with large result sets.
process.nextTick(function () {
self._cursor.nextObject(function (err, doc) {
self._onNextObject(err, doc);
});
});
}
/**
* Handle each document as its returned from the cursor.
* @ignore
* @api private
*/
CursorStream.prototype._onNextObject = function (err, doc) {
if (err) return this.destroy(err);
// when doc is null we hit the end of the cursor
if (!doc) {
this.emit('end')
return this.destroy();
}
this.emit('data', doc);
this._next();
}
/**
* Pauses the stream.
*
* @api public
*/
CursorStream.prototype.pause = function () {
this.paused = true;
}
/**
* Resumes the stream.
*
* @api public
*/
CursorStream.prototype.resume = function () {
var self = this;
process.nextTick(function() {
self.paused = false;
self._next();
})
}
/**
* Destroys the stream, closing the underlying
* cursor. No more events will be emitted.
*
* @api public
*/
CursorStream.prototype.destroy = function (err) {
if (this._destroyed) return;
this._destroyed = true;
this.readable = false;
this._cursor.close();
if (err) {
this.emit('error', err);
}
this.emit('close');
}
// TODO - maybe implement the raw option to pass binary?
//CursorStream.prototype.setEncoding = function () {
//}
module.exports = exports = CursorStream;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,213 @@
var Binary = require('bson').Binary,
ObjectID = require('bson').ObjectID;
/**
* Class for representing a single chunk in GridFS.
*
* @class
*
* @param file {GridStore} The {@link GridStore} object holding this chunk.
* @param mongoObject {object} The mongo object representation of this chunk.
*
* @throws Error when the type of data field for {@link mongoObject} is not
* supported. Currently supported types for data field are instances of
* {@link String}, {@link Array}, {@link Binary} and {@link Binary}
* from the bson module
*
* @see Chunk#buildMongoObject
*/
var Chunk = exports.Chunk = function(file, mongoObject) {
if(!(this instanceof Chunk)) return new Chunk(file, mongoObject);
this.file = file;
var self = this;
var mongoObjectFinal = mongoObject == null ? {} : mongoObject;
this.objectId = mongoObjectFinal._id == null ? new ObjectID() : mongoObjectFinal._id;
this.chunkNumber = mongoObjectFinal.n == null ? 0 : mongoObjectFinal.n;
this.data = new Binary();
if(mongoObjectFinal.data == null) {
} else if(typeof mongoObjectFinal.data == "string") {
var buffer = new Buffer(mongoObjectFinal.data.length);
buffer.write(mongoObjectFinal.data, 'binary', 0);
this.data = new Binary(buffer);
} else if(Array.isArray(mongoObjectFinal.data)) {
var buffer = new Buffer(mongoObjectFinal.data.length);
buffer.write(mongoObjectFinal.data.join(''), 'binary', 0);
this.data = new Binary(buffer);
} else if(mongoObjectFinal.data instanceof Binary || Object.prototype.toString.call(mongoObjectFinal.data) == "[object Binary]") {
this.data = mongoObjectFinal.data;
} else if(Buffer.isBuffer(mongoObjectFinal.data)) {
} else {
throw Error("Illegal chunk format");
}
// Update position
this.internalPosition = 0;
};
/**
* Writes a data to this object and advance the read/write head.
*
* @param data {string} the data to write
* @param callback {function(*, GridStore)} This will be called after executing
* this method. The first parameter will contain null and the second one
* will contain a reference to this object.
*/
Chunk.prototype.write = function(data, callback) {
this.data.write(data, this.internalPosition);
this.internalPosition = this.data.length();
if(callback != null) return callback(null, this);
return this;
};
/**
* Reads data and advances the read/write head.
*
* @param length {number} The length of data to read.
*
* @return {string} The data read if the given length will not exceed the end of
* the chunk. Returns an empty String otherwise.
*/
Chunk.prototype.read = function(length) {
// Default to full read if no index defined
length = length == null || length == 0 ? this.length() : length;
if(this.length() - this.internalPosition + 1 >= length) {
var data = this.data.read(this.internalPosition, length);
this.internalPosition = this.internalPosition + length;
return data;
} else {
return '';
}
};
Chunk.prototype.readSlice = function(length) {
if ((this.length() - this.internalPosition) >= length) {
var data = null;
if (this.data.buffer != null) { //Pure BSON
data = this.data.buffer.slice(this.internalPosition, this.internalPosition + length);
} else { //Native BSON
data = new Buffer(length);
length = this.data.readInto(data, this.internalPosition);
}
this.internalPosition = this.internalPosition + length;
return data;
} else {
return null;
}
};
/**
* Checks if the read/write head is at the end.
*
* @return {boolean} Whether the read/write head has reached the end of this
* chunk.
*/
Chunk.prototype.eof = function() {
return this.internalPosition == this.length() ? true : false;
};
/**
* Reads one character from the data of this chunk and advances the read/write
* head.
*
* @return {string} a single character data read if the the read/write head is
* not at the end of the chunk. Returns an empty String otherwise.
*/
Chunk.prototype.getc = function() {
return this.read(1);
};
/**
* Clears the contents of the data in this chunk and resets the read/write head
* to the initial position.
*/
Chunk.prototype.rewind = function() {
this.internalPosition = 0;
this.data = new Binary();
};
/**
* Saves this chunk to the database. Also overwrites existing entries having the
* same id as this chunk.
*
* @param callback {function(*, GridStore)} This will be called after executing
* this method. The first parameter will contain null and the second one
* will contain a reference to this object.
*/
Chunk.prototype.save = function(callback) {
var self = this;
self.file.chunkCollection(function(err, collection) {
if(err) return callback(err);
collection.remove({'_id':self.objectId}, {safe:true}, function(err, result) {
if(err) return callback(err);
if(self.data.length() > 0) {
self.buildMongoObject(function(mongoObject) {
collection.insert(mongoObject, {safe:true}, function(err, collection) {
callback(err, self);
});
});
} else {
callback(null, self);
}
});
});
};
/**
* Creates a mongoDB object representation of this chunk.
*
* @param callback {function(Object)} This will be called after executing this
* method. The object will be passed to the first parameter and will have
* the structure:
*
* <pre><code>
* {
* '_id' : , // {number} id for this chunk
* 'files_id' : , // {number} foreign key to the file collection
* 'n' : , // {number} chunk number
* 'data' : , // {bson#Binary} the chunk data itself
* }
* </code></pre>
*
* @see <a href="http://www.mongodb.org/display/DOCS/GridFS+Specification#GridFSSpecification-{{chunks}}">MongoDB GridFS Chunk Object Structure</a>
*/
Chunk.prototype.buildMongoObject = function(callback) {
var mongoObject = {'_id': this.objectId,
'files_id': this.file.fileId,
'n': this.chunkNumber,
'data': this.data};
callback(mongoObject);
};
/**
* @return {number} the length of the data
*/
Chunk.prototype.length = function() {
return this.data.length();
};
/**
* The position of the read/write head
* @name position
* @lends Chunk#
* @field
*/
Object.defineProperty(Chunk.prototype, "position", { enumerable: true
, get: function () {
return this.internalPosition;
}
, set: function(value) {
this.internalPosition = value;
}
});
/**
* The default chunk size
* @constant
*/
Chunk.DEFAULT_CHUNK_SIZE = 1024 * 256;

View File

@@ -0,0 +1,98 @@
var GridStore = require('./gridstore').GridStore,
ObjectID = require('bson').ObjectID;
/**
* A class representation of a simple Grid interface.
*
* @class Represents the Grid.
* @param {Db} db A database instance to interact with.
* @param {String} [fsName] optional different root collection for GridFS.
* @return {Grid}
*/
function Grid(db, fsName) {
if(!(this instanceof Grid)) return new Grid(db, fsName);
this.db = db;
this.fsName = fsName == null ? GridStore.DEFAULT_ROOT_COLLECTION : fsName;
}
/**
* Puts binary data to the grid
*
* @param {Buffer} data buffer with Binary Data.
* @param {Object} [options] the options for the files.
* @callback {Function} this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object.
* @return {null}
* @api public
*/
Grid.prototype.put = function(data, options, callback) {
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
callback = args.pop();
options = args.length ? args.shift() : {};
// If root is not defined add our default one
options['root'] = options['root'] == null ? this.fsName : options['root'];
// Return if we don't have a buffer object as data
if(!(Buffer.isBuffer(data))) return callback(new Error("Data object must be a buffer object"), null);
// Get filename if we are using it
var filename = options['filename'];
// Create gridstore
var gridStore = new GridStore(this.db, filename, "w", options);
gridStore.open(function(err, gridStore) {
if(err) return callback(err, null);
gridStore.write(data, function(err, result) {
if(err) return callback(err, null);
gridStore.close(function(err, result) {
if(err) return callback(err, null);
callback(null, result);
})
})
})
}
/**
* Get binary data to the grid
*
* @param {ObjectID} id ObjectID for file.
* @callback {Function} this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object.
* @return {null}
* @api public
*/
Grid.prototype.get = function(id, callback) {
// Validate that we have a valid ObjectId
if(!(id instanceof ObjectID)) return callback(new Error("Not a valid ObjectID", null));
// Create gridstore
var gridStore = new GridStore(this.db, id, "r", {root:this.fsName});
gridStore.open(function(err, gridStore) {
if(err) return callback(err, null);
// Return the data
gridStore.read(function(err, data) {
return callback(err, data)
});
})
}
/**
* Delete file from grid
*
* @param {ObjectID} id ObjectID for file.
* @callback {Function} this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object.
* @return {null}
* @api public
*/
Grid.prototype.delete = function(id, callback) {
// Validate that we have a valid ObjectId
if(!(id instanceof ObjectID)) return callback(new Error("Not a valid ObjectID", null));
// Create gridstore
GridStore.unlink(this.db, id, {root:this.fsName}, function(err, result) {
if(err) return callback(err, false);
return callback(null, true);
});
}
exports.Grid = Grid;

View File

@@ -0,0 +1,174 @@
var Stream = require('stream').Stream,
util = require('util');
/**
* ReadStream
*
* Returns a stream interface for the **file**.
*
* Events
* - **data** {function(item) {}} the data event triggers when a document is ready.
* - **end** {function() {}} the end event triggers when there is no more documents available.
* - **close** {function() {}} the close event triggers when the stream is closed.
* - **error** {function(err) {}} the error event triggers if an error happens.
*
* @class Represents a GridFS File Stream.
* @param {Boolean} autoclose automatically close file when the stream reaches the end.
* @param {GridStore} cursor a cursor object that the stream wraps.
* @return {ReadStream}
*/
function ReadStream(autoclose, gstore) {
if (!(this instanceof ReadStream)) return new ReadStream(autoclose, gstore);
Stream.call(this);
this.autoclose = !!autoclose;
this.gstore = gstore;
this.finalLength = gstore.length - gstore.position;
this.completedLength = 0;
this.currentChunkNumber = 0;
this.paused = false;
this.readable = true;
this.pendingChunk = null;
this.executing = false;
// Calculate the number of chunks
this.numberOfChunks = Math.ceil(gstore.length/gstore.chunkSize);
var self = this;
process.nextTick(function() {
self._execute();
});
};
/**
* Inherit from Stream
* @ignore
* @api private
*/
ReadStream.prototype.__proto__ = Stream.prototype;
/**
* Flag stating whether or not this stream is readable.
*/
ReadStream.prototype.readable;
/**
* Flag stating whether or not this stream is paused.
*/
ReadStream.prototype.paused;
/**
* @ignore
* @api private
*/
ReadStream.prototype._execute = function() {
if(this.paused === true || this.readable === false) {
return;
}
var gstore = this.gstore;
var self = this;
// Set that we are executing
this.executing = true;
var last = false;
var toRead = 0;
if(gstore.currentChunk.chunkNumber >= (this.numberOfChunks - 1)) {
self.executing = false;
last = true;
}
var data = gstore.currentChunk.readSlice(gstore.currentChunk.length());
if(data != null && gstore.currentChunk.chunkNumber == self.currentChunkNumber) {
self.currentChunkNumber = self.currentChunkNumber + 1;
self.completedLength += data.length;
self.pendingChunk = null;
self.emit("data", data);
}
if(last === true) {
self.readable = false;
self.emit("end");
if(self.autoclose === true) {
if(gstore.mode[0] == "w") {
gstore.close(function(err, doc) {
if (err) {
self.emit("error", err);
return;
}
self.readable = false;
self.emit("close", doc);
});
} else {
self.readable = false;
self.emit("close");
}
}
} else {
gstore._nthChunk(gstore.currentChunk.chunkNumber + 1, function(err, chunk) {
if(err) {
self.readable = false;
self.emit("error", err);
self.executing = false;
return;
}
self.pendingChunk = chunk;
if(self.paused === true) {
self.executing = false;
return;
}
gstore.currentChunk = self.pendingChunk;
self._execute();
});
}
};
/**
* Pauses this stream, then no farther events will be fired.
*
* @ignore
* @api public
*/
ReadStream.prototype.pause = function() {
if(!this.executing) {
this.paused = true;
}
};
/**
* Destroys the stream, then no farther events will be fired.
*
* @ignore
* @api public
*/
ReadStream.prototype.destroy = function() {
this.readable = false;
// Emit close event
this.emit("close");
};
/**
* Resumes this stream.
*
* @ignore
* @api public
*/
ReadStream.prototype.resume = function() {
if(this.paused === false || !this.readable) {
return;
}
this.paused = false;
var self = this;
process.nextTick(function() {
self._execute();
});
};
exports.ReadStream = ReadStream;

View File

@@ -0,0 +1,157 @@
try {
exports.BSONPure = require('bson').BSONPure;
exports.BSONNative = require('bson').BSONNative;
} catch(err) {
// do nothing
}
[ 'commands/base_command'
, 'commands/db_command'
, 'commands/delete_command'
, 'commands/get_more_command'
, 'commands/insert_command'
, 'commands/kill_cursor_command'
, 'commands/query_command'
, 'commands/update_command'
, 'responses/mongo_reply'
, 'admin'
, 'collection'
, 'connection/read_preference'
, 'connection/connection'
, 'connection/server'
, 'connection/mongos'
, 'connection/repl_set'
, 'cursor'
, 'db'
, 'gridfs/grid'
, 'gridfs/chunk'
, 'gridfs/gridstore'].forEach(function (path) {
var module = require('./' + path);
for (var i in module) {
exports[i] = module[i];
}
// backwards compat
exports.ReplSetServers = exports.ReplSet;
// Add BSON Classes
exports.Binary = require('bson').Binary;
exports.Code = require('bson').Code;
exports.DBRef = require('bson').DBRef;
exports.Double = require('bson').Double;
exports.Long = require('bson').Long;
exports.MinKey = require('bson').MinKey;
exports.MaxKey = require('bson').MaxKey;
exports.ObjectID = require('bson').ObjectID;
exports.Symbol = require('bson').Symbol;
exports.Timestamp = require('bson').Timestamp;
// Add BSON Parser
exports.BSON = require('bson').BSONPure.BSON;
});
// Exports all the classes for the PURE JS BSON Parser
exports.pure = function() {
var classes = {};
// Map all the classes
[ 'commands/base_command'
, 'commands/db_command'
, 'commands/delete_command'
, 'commands/get_more_command'
, 'commands/insert_command'
, 'commands/kill_cursor_command'
, 'commands/query_command'
, 'commands/update_command'
, 'responses/mongo_reply'
, 'admin'
, 'collection'
, 'connection/read_preference'
, 'connection/connection'
, 'connection/server'
, 'connection/mongos'
, 'connection/repl_set'
, 'cursor'
, 'db'
, 'gridfs/grid'
, 'gridfs/chunk'
, 'gridfs/gridstore'].forEach(function (path) {
var module = require('./' + path);
for (var i in module) {
classes[i] = module[i];
}
});
// backwards compat
classes.ReplSetServers = exports.ReplSet;
// Add BSON Classes
classes.Binary = require('bson').Binary;
classes.Code = require('bson').Code;
classes.DBRef = require('bson').DBRef;
classes.Double = require('bson').Double;
classes.Long = require('bson').Long;
classes.MinKey = require('bson').MinKey;
classes.MaxKey = require('bson').MaxKey;
classes.ObjectID = require('bson').ObjectID;
classes.Symbol = require('bson').Symbol;
classes.Timestamp = require('bson').Timestamp;
// Add BSON Parser
classes.BSON = require('bson').BSONPure.BSON;
// Return classes list
return classes;
}
// Exports all the classes for the PURE JS BSON Parser
exports.native = function() {
var classes = {};
// Map all the classes
[ 'commands/base_command'
, 'commands/db_command'
, 'commands/delete_command'
, 'commands/get_more_command'
, 'commands/insert_command'
, 'commands/kill_cursor_command'
, 'commands/query_command'
, 'commands/update_command'
, 'responses/mongo_reply'
, 'admin'
, 'collection'
, 'connection/read_preference'
, 'connection/connection'
, 'connection/server'
, 'connection/mongos'
, 'connection/repl_set'
, 'cursor'
, 'db'
, 'gridfs/grid'
, 'gridfs/chunk'
, 'gridfs/gridstore'].forEach(function (path) {
var module = require('./' + path);
for (var i in module) {
classes[i] = module[i];
}
});
// Add BSON Classes
classes.Binary = require('bson').Binary;
classes.Code = require('bson').Code;
classes.DBRef = require('bson').DBRef;
classes.Double = require('bson').Double;
classes.Long = require('bson').Long;
classes.MinKey = require('bson').MinKey;
classes.MaxKey = require('bson').MaxKey;
classes.ObjectID = require('bson').ObjectID;
classes.Symbol = require('bson').Symbol;
classes.Timestamp = require('bson').Timestamp;
// backwards compat
classes.ReplSetServers = exports.ReplSet;
// Add BSON Parser
classes.BSON = require('bson').BSONNative.BSON;
// Return classes list
return classes;
}

View File

@@ -0,0 +1,140 @@
var Long = require('bson').Long;
/**
Reply message from mongo db
**/
var MongoReply = exports.MongoReply = function() {
this.documents = [];
this.index = 0;
};
MongoReply.prototype.parseHeader = function(binary_reply, bson) {
// Unpack the standard header first
this.messageLength = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
this.index = this.index + 4;
// Fetch the request id for this reply
this.requestId = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
this.index = this.index + 4;
// Fetch the id of the request that triggered the response
this.responseTo = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
// Skip op-code field
this.index = this.index + 4 + 4;
// Unpack the reply message
this.responseFlag = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
this.index = this.index + 4;
// Unpack the cursor id (a 64 bit long integer)
var low_bits = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
this.index = this.index + 4;
var high_bits = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
this.index = this.index + 4;
this.cursorId = new Long(low_bits, high_bits);
// Unpack the starting from
this.startingFrom = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
this.index = this.index + 4;
// Unpack the number of objects returned
this.numberReturned = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
this.index = this.index + 4;
}
MongoReply.prototype.parseBody = function(binary_reply, bson, raw, callback) {
raw = raw == null ? false : raw;
// Just set a doc limit for deserializing
var docLimitSize = 1024*20;
// If our message length is very long, let's switch to process.nextTick for messages
if(this.messageLength > docLimitSize) {
var batchSize = this.numberReturned;
this.documents = new Array(this.numberReturned);
// Just walk down until we get a positive number >= 1
for(var i = 50; i > 0; i--) {
if((this.numberReturned/i) >= 1) {
batchSize = i;
break;
}
}
// Actual main creator of the processFunction setting internal state to control the flow
var parseFunction = function(_self, _binary_reply, _batchSize, _numberReturned) {
var object_index = 0;
// Internal loop process that will use nextTick to ensure we yield some time
var processFunction = function() {
// Adjust batchSize if we have less results left than batchsize
if((_numberReturned - object_index) < _batchSize) {
_batchSize = _numberReturned - object_index;
}
// If raw just process the entries
if(raw) {
// Iterate over the batch
for(var i = 0; i < _batchSize; i++) {
// Are we done ?
if(object_index <= _numberReturned) {
// Read the size of the bson object
var bsonObjectSize = _binary_reply[_self.index] | _binary_reply[_self.index + 1] << 8 | _binary_reply[_self.index + 2] << 16 | _binary_reply[_self.index + 3] << 24;
// If we are storing the raw responses to pipe straight through
_self.documents[object_index] = binary_reply.slice(_self.index, _self.index + bsonObjectSize);
// Adjust binary index to point to next block of binary bson data
_self.index = _self.index + bsonObjectSize;
// Update number of docs parsed
object_index = object_index + 1;
}
}
} else {
try {
// Parse documents
_self.index = bson.deserializeStream(binary_reply, _self.index, _batchSize, _self.documents, object_index);
// Adjust index
object_index = object_index + _batchSize;
} catch (err) {
return callback(err);
}
}
// If we hav more documents process NextTick
if(object_index < _numberReturned) {
process.nextTick(processFunction);
} else {
callback(null);
}
}
// Return the process function
return processFunction;
}(this, binary_reply, batchSize, this.numberReturned)();
} else {
try {
// Let's unpack all the bson documents, deserialize them and store them
for(var object_index = 0; object_index < this.numberReturned; object_index++) {
// Read the size of the bson object
var bsonObjectSize = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
// If we are storing the raw responses to pipe straight through
if(raw) {
// Deserialize the object and add to the documents array
this.documents.push(binary_reply.slice(this.index, this.index + bsonObjectSize));
} else {
// Deserialize the object and add to the documents array
this.documents.push(bson.deserialize(binary_reply.slice(this.index, this.index + bsonObjectSize)));
}
// Adjust binary index to point to next block of binary bson data
this.index = this.index + bsonObjectSize;
}
} catch(err) {
return callback(err);
}
// No error return
callback(null);
}
}
MongoReply.prototype.is_error = function(){
if(this.documents.length == 1) {
return this.documents[0].ok == 1 ? false : true;
}
return false;
};
MongoReply.prototype.error_message = function() {
return this.documents.length == 1 && this.documents[0].ok == 1 ? '' : this.documents[0].errmsg;
};

View File

@@ -0,0 +1,74 @@
/**
* Sort functions, Normalize and prepare sort parameters
*/
var formatSortValue = exports.formatSortValue = function(sortDirection) {
var value = ("" + sortDirection).toLowerCase();
switch (value) {
case 'ascending':
case 'asc':
case '1':
return 1;
case 'descending':
case 'desc':
case '-1':
return -1;
default:
throw new Error("Illegal sort clause, must be of the form "
+ "[['field1', '(ascending|descending)'], "
+ "['field2', '(ascending|descending)']]");
}
};
var formattedOrderClause = exports.formattedOrderClause = function(sortValue) {
var orderBy = {};
if (Array.isArray(sortValue)) {
for(var i = 0; i < sortValue.length; i++) {
if(sortValue[i].constructor == String) {
orderBy[sortValue[i]] = 1;
} else {
orderBy[sortValue[i][0]] = formatSortValue(sortValue[i][1]);
}
}
} else if(Object.prototype.toString.call(sortValue) === '[object Object]') {
orderBy = sortValue;
} else if (sortValue.constructor == String) {
orderBy[sortValue] = 1;
} else {
throw new Error("Illegal sort clause, must be of the form " +
"[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]");
}
return orderBy;
};
exports.encodeInt = function(value) {
var buffer = new Buffer(4);
buffer[3] = (value >> 24) & 0xff;
buffer[2] = (value >> 16) & 0xff;
buffer[1] = (value >> 8) & 0xff;
buffer[0] = value & 0xff;
return buffer;
}
exports.encodeIntInPlace = function(value, buffer, index) {
buffer[index + 3] = (value >> 24) & 0xff;
buffer[index + 2] = (value >> 16) & 0xff;
buffer[index + 1] = (value >> 8) & 0xff;
buffer[index] = value & 0xff;
}
exports.encodeCString = function(string) {
var buf = new Buffer(string, 'utf8');
return [buf, new Buffer([0])];
}
exports.decodeUInt32 = function(array, index) {
return array[index] | array[index + 1] << 8 | array[index + 2] << 16 | array[index + 3] << 24;
}
// Decode the int
exports.decodeUInt8 = function(array, index) {
return array[index];
}

View File

@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.6
- 0.8
- 0.9 # development version of 0.8, may be unstable

View File

@@ -0,0 +1,16 @@
NODE = node
NPM = npm
NODEUNIT = node_modules/nodeunit/bin/nodeunit
all: clean node_gyp
test: clean node_gyp
npm test
node_gyp: clean
node-gyp configure build
clean:
node-gyp clean
.PHONY: all

View File

@@ -0,0 +1 @@
A JS/C++ Bson parser for node, used in the MongoDB Native driver

View File

@@ -0,0 +1,130 @@
// var BSON = require('../../lib/mongodb').BSONNative.BSON,
// ObjectID = require('../../lib/mongodb').BSONNative.ObjectID,
// Code = require('../../lib/mongodb').BSONNative.Code,
// Long = require('../../lib/mongodb').BSONNative.Long,
// Binary = require('../../lib/mongodb').BSONNative.Binary,
// debug = require('util').debug,
// inspect = require('util').inspect,
//
// Long = require('../../lib/mongodb').Long,
// ObjectID = require('../../lib/mongodb').ObjectID,
// Binary = require('../../lib/mongodb').Binary,
// Code = require('../../lib/mongodb').Code,
// DBRef = require('../../lib/mongodb').DBRef,
// Symbol = require('../../lib/mongodb').Symbol,
// Double = require('../../lib/mongodb').Double,
// MaxKey = require('../../lib/mongodb').MaxKey,
// MinKey = require('../../lib/mongodb').MinKey,
// Timestamp = require('../../lib/mongodb').Timestamp;
// var BSON = require('../../lib/mongodb').BSONPure.BSON,
// ObjectID = require('../../lib/mongodb').BSONPure.ObjectID,
// Code = require('../../lib/mongodb').BSONPure.Code,
// Long = require('../../lib/mongodb').BSONPure.Long,
// Binary = require('../../lib/mongodb').BSONPure.Binary;
var BSON = require('../lib/bson').BSONNative.BSON,
Long = require('../lib/bson').Long,
ObjectID = require('../lib/bson').ObjectID,
Binary = require('../lib/bson').Binary,
Code = require('../lib/bson').Code,
DBRef = require('../lib/bson').DBRef,
Symbol = require('../lib/bson').Symbol,
Double = require('../lib/bson').Double,
MaxKey = require('../lib/bson').MaxKey,
MinKey = require('../lib/bson').MinKey,
Timestamp = require('../lib/bson').Timestamp;
// console.dir(require('../lib/bson'))
var COUNT = 1000;
var COUNT = 100;
var object = {
string: "Strings are great",
decimal: 3.14159265,
bool: true,
integer: 5,
date: new Date(),
double: new Double(1.4),
id: new ObjectID(),
min: new MinKey(),
max: new MaxKey(),
symbol: new Symbol('hello'),
long: Long.fromNumber(100),
bin: new Binary(new Buffer(100)),
subObject: {
moreText: "Bacon ipsum dolor sit amet cow pork belly rump ribeye pastrami andouille. Tail hamburger pork belly, drumstick flank salami t-bone sirloin pork chop ribeye ham chuck pork loin shankle. Ham fatback pork swine, sirloin shankle short loin andouille shank sausage meatloaf drumstick. Pig chicken cow bresaola, pork loin jerky meatball tenderloin brisket strip steak jowl spare ribs. Biltong sirloin pork belly boudin, bacon pastrami rump chicken. Jowl rump fatback, biltong bacon t-bone turkey. Turkey pork loin boudin, tenderloin jerky beef ribs pastrami spare ribs biltong pork chop beef.",
longKeylongKeylongKeylongKeylongKeylongKey: "Pork belly boudin shoulder ribeye pork chop brisket biltong short ribs. Salami beef pork belly, t-bone sirloin meatloaf tail jowl spare ribs. Sirloin biltong bresaola cow turkey. Biltong fatback meatball, bresaola tail shankle turkey pancetta ham ribeye flank bacon jerky pork chop. Boudin sirloin shoulder, salami swine flank jerky t-bone pork chop pork beef tongue. Bresaola ribeye jerky andouille. Ribeye ground round sausage biltong beef ribs chuck, shank hamburger chicken short ribs spare ribs tenderloin meatloaf pork loin."
},
subArray: [1,2,3,4,5,6,7,8,9,10],
anotherString: "another string",
code: new Code("function() {}", {i:1})
}
// Number of objects
var numberOfObjects = 10000;
var bson = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]);
console.log("---------------------- 1")
var s = new Date()
// Object serialized
for(var i = 0; i < numberOfObjects; i++) {
objectBSON = bson.serialize(object, null, true)
}
console.log("====================== " + (new Date().getTime() - s.getTime()) + " :: " + ((new Date().getTime() - s.getTime()))/numberOfObjects)
console.log("---------------------- 2")
var s = new Date()
// Object serialized
for(var i = 0; i < numberOfObjects; i++) {
bson.deserialize(objectBSON);
}
console.log("====================== " + (new Date().getTime() - s.getTime()) + " :: " + ((new Date().getTime() - s.getTime()))/numberOfObjects)
// // Buffer With copies of the objectBSON
// var data = new Buffer(objectBSON.length * numberOfObjects);
// var index = 0;
//
// // Copy the buffer 1000 times to create a strea m of objects
// for(var i = 0; i < numberOfObjects; i++) {
// // Copy data
// objectBSON.copy(data, index);
// // Adjust index
// index = index + objectBSON.length;
// }
//
// // console.log("-----------------------------------------------------------------------------------")
// // console.dir(objectBSON)
//
// var x, start, end, j
// var objectBSON, objectJSON
//
// // Allocate the return array (avoid concatinating everything)
// var results = new Array(numberOfObjects);
//
// console.log(COUNT + "x (objectBSON = BSON.serialize(object))")
// start = new Date
//
// // var objects = BSON.deserializeStream(data, 0, numberOfObjects);
// // console.log("----------------------------------------------------------------------------------- 0")
// // var objects = BSON.deserialize(data);
// // console.log("----------------------------------------------------------------------------------- 1")
// // console.dir(objects)
//
// for (j=COUNT; --j>=0; ) {
// var nextIndex = BSON.deserializeStream(data, 0, numberOfObjects, results, 0);
// }
//
// end = new Date
// var opsprsecond = COUNT / ((end - start)/1000);
// console.log("bson size (bytes): ", objectBSON.length);
// console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec");
// console.log("MB/s = " + ((opsprsecond*objectBSON.length)/1024));
//
// // console.dir(nextIndex)
// // console.dir(results)

View File

@@ -0,0 +1,17 @@
{
'targets': [
{
'target_name': 'bson',
'sources': [ 'ext/bson.cc' ],
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'conditions': [
['OS=="mac"', {
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
}
}]
]
}
]
}

View File

@@ -0,0 +1,355 @@
# We borrow heavily from the kernel build setup, though we are simpler since
# we don't have Kconfig tweaking settings on us.
# The implicit make rules have it looking for RCS files, among other things.
# We instead explicitly write all the rules we care about.
# It's even quicker (saves ~200ms) to pass -r on the command line.
MAKEFLAGS=-r
# The source directory tree.
srcdir := ..
abs_srcdir := $(abspath $(srcdir))
# The name of the builddir.
builddir_name ?= .
# The V=1 flag on command line makes us verbosely print command lines.
ifdef V
quiet=
else
quiet=quiet_
endif
# Specify BUILDTYPE=Release on the command line for a release build.
BUILDTYPE ?= Release
# Directory all our build output goes into.
# Note that this must be two directories beneath src/ for unit tests to pass,
# as they reach into the src/ directory for data with relative paths.
builddir ?= $(builddir_name)/$(BUILDTYPE)
abs_builddir := $(abspath $(builddir))
depsdir := $(builddir)/.deps
# Object output directory.
obj := $(builddir)/obj
abs_obj := $(abspath $(obj))
# We build up a list of every single one of the targets so we can slurp in the
# generated dependency rule Makefiles in one pass.
all_deps :=
# C++ apps need to be linked with g++.
#
# Note: flock is used to seralize linking. Linking is a memory-intensive
# process so running parallel links can often lead to thrashing. To disable
# the serialization, override LINK via an envrionment variable as follows:
#
# export LINK=g++
#
# This will allow make to invoke N linker processes as specified in -jN.
LINK ?= ./gyp-mac-tool flock $(builddir)/linker.lock $(CXX)
CC.target ?= $(CC)
CFLAGS.target ?= $(CFLAGS)
CXX.target ?= $(CXX)
CXXFLAGS.target ?= $(CXXFLAGS)
LINK.target ?= $(LINK)
LDFLAGS.target ?= $(LDFLAGS)
AR.target ?= $(AR)
# TODO(evan): move all cross-compilation logic to gyp-time so we don't need
# to replicate this environment fallback in make as well.
CC.host ?= gcc
CFLAGS.host ?=
CXX.host ?= g++
CXXFLAGS.host ?=
LINK.host ?= g++
LDFLAGS.host ?=
AR.host ?= ar
# Define a dir function that can handle spaces.
# http://www.gnu.org/software/make/manual/make.html#Syntax-of-Functions
# "leading spaces cannot appear in the text of the first argument as written.
# These characters can be put into the argument value by variable substitution."
empty :=
space := $(empty) $(empty)
# http://stackoverflow.com/questions/1189781/using-make-dir-or-notdir-on-a-path-with-spaces
replace_spaces = $(subst $(space),?,$1)
unreplace_spaces = $(subst ?,$(space),$1)
dirx = $(call unreplace_spaces,$(dir $(call replace_spaces,$1)))
# Flags to make gcc output dependency info. Note that you need to be
# careful here to use the flags that ccache and distcc can understand.
# We write to a dep file on the side first and then rename at the end
# so we can't end up with a broken dep file.
depfile = $(depsdir)/$(call replace_spaces,$@).d
DEPFLAGS = -MMD -MF $(depfile).raw
# We have to fixup the deps output in a few ways.
# (1) the file output should mention the proper .o file.
# ccache or distcc lose the path to the target, so we convert a rule of
# the form:
# foobar.o: DEP1 DEP2
# into
# path/to/foobar.o: DEP1 DEP2
# (2) we want missing files not to cause us to fail to build.
# We want to rewrite
# foobar.o: DEP1 DEP2 \
# DEP3
# to
# DEP1:
# DEP2:
# DEP3:
# so if the files are missing, they're just considered phony rules.
# We have to do some pretty insane escaping to get those backslashes
# and dollar signs past make, the shell, and sed at the same time.
# Doesn't work with spaces, but that's fine: .d files have spaces in
# their names replaced with other characters.
define fixup_dep
# The depfile may not exist if the input file didn't have any #includes.
touch $(depfile).raw
# Fixup path as in (1).
sed -e "s|^$(notdir $@)|$@|" $(depfile).raw >> $(depfile)
# Add extra rules as in (2).
# We remove slashes and replace spaces with new lines;
# remove blank lines;
# delete the first line and append a colon to the remaining lines.
sed -e 's|\\||' -e 'y| |\n|' $(depfile).raw |\
grep -v '^$$' |\
sed -e 1d -e 's|$$|:|' \
>> $(depfile)
rm $(depfile).raw
endef
# Command definitions:
# - cmd_foo is the actual command to run;
# - quiet_cmd_foo is the brief-output summary of the command.
quiet_cmd_cc = CC($(TOOLSET)) $@
cmd_cc = $(CC.$(TOOLSET)) $(GYP_CFLAGS) $(DEPFLAGS) $(CFLAGS.$(TOOLSET)) -c -o $@ $<
quiet_cmd_cxx = CXX($(TOOLSET)) $@
cmd_cxx = $(CXX.$(TOOLSET)) $(GYP_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
quiet_cmd_objc = CXX($(TOOLSET)) $@
cmd_objc = $(CC.$(TOOLSET)) $(GYP_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $<
quiet_cmd_objcxx = CXX($(TOOLSET)) $@
cmd_objcxx = $(CXX.$(TOOLSET)) $(GYP_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $<
# Commands for precompiled header files.
quiet_cmd_pch_c = CXX($(TOOLSET)) $@
cmd_pch_c = $(CC.$(TOOLSET)) $(GYP_PCH_CFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
quiet_cmd_pch_cc = CXX($(TOOLSET)) $@
cmd_pch_cc = $(CC.$(TOOLSET)) $(GYP_PCH_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
quiet_cmd_pch_m = CXX($(TOOLSET)) $@
cmd_pch_m = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $<
quiet_cmd_pch_mm = CXX($(TOOLSET)) $@
cmd_pch_mm = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $<
# gyp-mac-tool is written next to the root Makefile by gyp.
# Use $(4) for the command, since $(2) and $(3) are used as flag by do_cmd
# already.
quiet_cmd_mac_tool = MACTOOL $(4) $<
cmd_mac_tool = ./gyp-mac-tool $(4) $< "$@"
quiet_cmd_mac_package_framework = PACKAGE FRAMEWORK $@
cmd_mac_package_framework = ./gyp-mac-tool package-framework "$@" $(4)
quiet_cmd_infoplist = INFOPLIST $@
cmd_infoplist = $(CC.$(TOOLSET)) -E -P -Wno-trigraphs -x c $(INFOPLIST_DEFINES) "$<" -o "$@"
quiet_cmd_touch = TOUCH $@
cmd_touch = touch $@
quiet_cmd_copy = COPY $@
# send stderr to /dev/null to ignore messages when linking directories.
cmd_copy = rm -rf "$@" && cp -af "$<" "$@"
quiet_cmd_alink = LIBTOOL-STATIC $@
cmd_alink = rm -f $@ && ./gyp-mac-tool filter-libtool libtool $(GYP_LIBTOOLFLAGS) -static -o $@ $(filter %.o,$^)
quiet_cmd_link = LINK($(TOOLSET)) $@
cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS)
# TODO(thakis): Find out and document the difference between shared_library and
# loadable_module on mac.
quiet_cmd_solink = SOLINK($(TOOLSET)) $@
cmd_solink = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS)
# TODO(thakis): The solink_module rule is likely wrong. Xcode seems to pass
# -bundle -single_module here (for osmesa.so).
quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@
cmd_solink_module = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS)
# Define an escape_quotes function to escape single quotes.
# This allows us to handle quotes properly as long as we always use
# use single quotes and escape_quotes.
escape_quotes = $(subst ','\'',$(1))
# This comment is here just to include a ' to unconfuse syntax highlighting.
# Define an escape_vars function to escape '$' variable syntax.
# This allows us to read/write command lines with shell variables (e.g.
# $LD_LIBRARY_PATH), without triggering make substitution.
escape_vars = $(subst $$,$$$$,$(1))
# Helper that expands to a shell command to echo a string exactly as it is in
# make. This uses printf instead of echo because printf's behaviour with respect
# to escape sequences is more portable than echo's across different shells
# (e.g., dash, bash).
exact_echo = printf '%s\n' '$(call escape_quotes,$(1))'
# Helper to compare the command we're about to run against the command
# we logged the last time we ran the command. Produces an empty
# string (false) when the commands match.
# Tricky point: Make has no string-equality test function.
# The kernel uses the following, but it seems like it would have false
# positives, where one string reordered its arguments.
# arg_check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
# $(filter-out $(cmd_$@), $(cmd_$(1))))
# We instead substitute each for the empty string into the other, and
# say they're equal if both substitutions produce the empty string.
# .d files contain ? instead of spaces, take that into account.
command_changed = $(or $(subst $(cmd_$(1)),,$(cmd_$(call replace_spaces,$@))),\
$(subst $(cmd_$(call replace_spaces,$@)),,$(cmd_$(1))))
# Helper that is non-empty when a prerequisite changes.
# Normally make does this implicitly, but we force rules to always run
# so we can check their command lines.
# $? -- new prerequisites
# $| -- order-only dependencies
prereq_changed = $(filter-out FORCE_DO_CMD,$(filter-out $|,$?))
# Helper that executes all postbuilds, and deletes the output file when done
# if any of the postbuilds failed.
define do_postbuilds
@E=0;\
for p in $(POSTBUILDS); do\
eval $$p;\
F=$$?;\
if [ $$F -ne 0 ]; then\
E=$$F;\
fi;\
done;\
if [ $$E -ne 0 ]; then\
rm -rf "$@";\
exit $$E;\
fi
endef
# do_cmd: run a command via the above cmd_foo names, if necessary.
# Should always run for a given target to handle command-line changes.
# Second argument, if non-zero, makes it do asm/C/C++ dependency munging.
# Third argument, if non-zero, makes it do POSTBUILDS processing.
# Note: We intentionally do NOT call dirx for depfile, since it contains ? for
# spaces already and dirx strips the ? characters.
define do_cmd
$(if $(or $(command_changed),$(prereq_changed)),
@$(call exact_echo, $($(quiet)cmd_$(1)))
@mkdir -p "$(call dirx,$@)" "$(dir $(depfile))"
$(if $(findstring flock,$(word 2,$(cmd_$1))),
@$(cmd_$(1))
@echo " $(quiet_cmd_$(1)): Finished",
@$(cmd_$(1))
)
@$(call exact_echo,$(call escape_vars,cmd_$(call replace_spaces,$@) := $(cmd_$(1)))) > $(depfile)
@$(if $(2),$(fixup_dep))
$(if $(and $(3), $(POSTBUILDS)),
$(call do_postbuilds)
)
)
endef
# Declare the "all" target first so it is the default,
# even though we don't have the deps yet.
.PHONY: all
all:
# make looks for ways to re-generate included makefiles, but in our case, we
# don't have a direct way. Explicitly telling make that it has nothing to do
# for them makes it go faster.
%.d: ;
# Use FORCE_DO_CMD to force a target to run. Should be coupled with
# do_cmd.
.PHONY: FORCE_DO_CMD
FORCE_DO_CMD:
TOOLSET := target
# Suffix rules, putting all outputs into $(obj).
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.c FORCE_DO_CMD
@$(call do_cmd,cc,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cc FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cpp FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cxx FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.m FORCE_DO_CMD
@$(call do_cmd,objc,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.mm FORCE_DO_CMD
@$(call do_cmd,objcxx,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.S FORCE_DO_CMD
@$(call do_cmd,cc,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.s FORCE_DO_CMD
@$(call do_cmd,cc,1)
# Try building from generated source, too.
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.c FORCE_DO_CMD
@$(call do_cmd,cc,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cc FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cpp FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cxx FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.m FORCE_DO_CMD
@$(call do_cmd,objc,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.mm FORCE_DO_CMD
@$(call do_cmd,objcxx,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.S FORCE_DO_CMD
@$(call do_cmd,cc,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.s FORCE_DO_CMD
@$(call do_cmd,cc,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.c FORCE_DO_CMD
@$(call do_cmd,cc,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.cc FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.cpp FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.cxx FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.m FORCE_DO_CMD
@$(call do_cmd,objc,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.mm FORCE_DO_CMD
@$(call do_cmd,objcxx,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.S FORCE_DO_CMD
@$(call do_cmd,cc,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.s FORCE_DO_CMD
@$(call do_cmd,cc,1)
ifeq ($(strip $(foreach prefix,$(NO_LOAD),\
$(findstring $(join ^,$(prefix)),\
$(join ^,bson.target.mk)))),)
include bson.target.mk
endif
quiet_cmd_regen_makefile = ACTION Regenerating $@
cmd_regen_makefile = /usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp -fmake --ignore-environment "--toplevel-dir=." -I/Users/azat/Documents/Development/mongoui/node_modules/monk/node_modules/mongoskin/node_modules/mongodb/node_modules/bson/build/config.gypi -I/usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi -I/Users/azat/.node-gyp/0.10.12/common.gypi "--depth=." "-Goutput_dir=." "--generator-output=build" "-Dlibrary=shared_library" "-Dvisibility=default" "-Dnode_root_dir=/Users/azat/.node-gyp/0.10.12" "-Dmodule_root_dir=/Users/azat/Documents/Development/mongoui/node_modules/monk/node_modules/mongoskin/node_modules/mongodb/node_modules/bson" binding.gyp
Makefile: $(srcdir)/../../../../../../../../../../../../../usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi $(srcdir)/build/config.gypi $(srcdir)/binding.gyp $(srcdir)/../../../../../../../../../../../.node-gyp/0.10.12/common.gypi
$(call do_cmd,regen_makefile)
# "all" is a concatenation of the "all" targets from all the included
# sub-makefiles. This is just here to clarify.
all:
# Add in dependency-tracking rules. $(all_deps) is the list of every single
# target in our tree. Only consider the ones with .d (dependency) info:
d_files := $(wildcard $(foreach f,$(all_deps),$(depsdir)/$(f).d))
ifneq ($(d_files),)
include $(d_files)
endif

View File

@@ -0,0 +1 @@
cmd_Release/bson.node := ./gyp-mac-tool flock ./Release/linker.lock c++ -shared -Wl,-search_paths_first -mmacosx-version-min=10.5 -arch x86_64 -L./Release -install_name @rpath/bson.node -o Release/bson.node Release/obj.target/bson/ext/bson.o -undefined dynamic_lookup

View File

@@ -0,0 +1,24 @@
cmd_Release/obj.target/bson/ext/bson.o := c++ '-D_DARWIN_USE_64_BIT_INODE=1' '-D_LARGEFILE_SOURCE' '-D_FILE_OFFSET_BITS=64' '-DBUILDING_NODE_EXTENSION' -I/Users/azat/.node-gyp/0.10.12/src -I/Users/azat/.node-gyp/0.10.12/deps/uv/include -I/Users/azat/.node-gyp/0.10.12/deps/v8/include -Os -gdwarf-2 -mmacosx-version-min=10.5 -arch x86_64 -Wall -Wendif-labels -W -Wno-unused-parameter -fno-rtti -fno-threadsafe-statics -fno-strict-aliasing -MMD -MF ./Release/.deps/Release/obj.target/bson/ext/bson.o.d.raw -c -o Release/obj.target/bson/ext/bson.o ../ext/bson.cc
Release/obj.target/bson/ext/bson.o: ../ext/bson.cc \
/Users/azat/.node-gyp/0.10.12/deps/v8/include/v8.h \
/Users/azat/.node-gyp/0.10.12/deps/v8/include/v8stdint.h \
/Users/azat/.node-gyp/0.10.12/src/node.h \
/Users/azat/.node-gyp/0.10.12/deps/uv/include/uv.h \
/Users/azat/.node-gyp/0.10.12/deps/uv/include/uv-private/uv-unix.h \
/Users/azat/.node-gyp/0.10.12/deps/uv/include/uv-private/ngx-queue.h \
/Users/azat/.node-gyp/0.10.12/deps/uv/include/uv-private/uv-darwin.h \
/Users/azat/.node-gyp/0.10.12/src/node_object_wrap.h \
/Users/azat/.node-gyp/0.10.12/src/node_version.h \
/Users/azat/.node-gyp/0.10.12/src/node_buffer.h ../ext/bson.h
../ext/bson.cc:
/Users/azat/.node-gyp/0.10.12/deps/v8/include/v8.h:
/Users/azat/.node-gyp/0.10.12/deps/v8/include/v8stdint.h:
/Users/azat/.node-gyp/0.10.12/src/node.h:
/Users/azat/.node-gyp/0.10.12/deps/uv/include/uv.h:
/Users/azat/.node-gyp/0.10.12/deps/uv/include/uv-private/uv-unix.h:
/Users/azat/.node-gyp/0.10.12/deps/uv/include/uv-private/ngx-queue.h:
/Users/azat/.node-gyp/0.10.12/deps/uv/include/uv-private/uv-darwin.h:
/Users/azat/.node-gyp/0.10.12/src/node_object_wrap.h:
/Users/azat/.node-gyp/0.10.12/src/node_version.h:
/Users/azat/.node-gyp/0.10.12/src/node_buffer.h:
../ext/bson.h:

View File

@@ -0,0 +1,6 @@
# This file is generated by gyp; do not edit.
export builddir_name ?= build/./.
.PHONY: all
all:
$(MAKE) bson

View File

@@ -0,0 +1,154 @@
# This file is generated by gyp; do not edit.
TOOLSET := target
TARGET := bson
DEFS_Debug := \
'-D_DARWIN_USE_64_BIT_INODE=1' \
'-D_LARGEFILE_SOURCE' \
'-D_FILE_OFFSET_BITS=64' \
'-DBUILDING_NODE_EXTENSION' \
'-DDEBUG' \
'-D_DEBUG'
# Flags passed to all source files.
CFLAGS_Debug := \
-O0 \
-gdwarf-2 \
-mmacosx-version-min=10.5 \
-arch x86_64 \
-Wall \
-Wendif-labels \
-W \
-Wno-unused-parameter
# Flags passed to only C files.
CFLAGS_C_Debug := \
-fno-strict-aliasing
# Flags passed to only C++ files.
CFLAGS_CC_Debug := \
-fno-rtti \
-fno-threadsafe-statics \
-fno-strict-aliasing
# Flags passed to only ObjC files.
CFLAGS_OBJC_Debug :=
# Flags passed to only ObjC++ files.
CFLAGS_OBJCC_Debug :=
INCS_Debug := \
-I/Users/azat/.node-gyp/0.10.12/src \
-I/Users/azat/.node-gyp/0.10.12/deps/uv/include \
-I/Users/azat/.node-gyp/0.10.12/deps/v8/include
DEFS_Release := \
'-D_DARWIN_USE_64_BIT_INODE=1' \
'-D_LARGEFILE_SOURCE' \
'-D_FILE_OFFSET_BITS=64' \
'-DBUILDING_NODE_EXTENSION'
# Flags passed to all source files.
CFLAGS_Release := \
-Os \
-gdwarf-2 \
-mmacosx-version-min=10.5 \
-arch x86_64 \
-Wall \
-Wendif-labels \
-W \
-Wno-unused-parameter
# Flags passed to only C files.
CFLAGS_C_Release := \
-fno-strict-aliasing
# Flags passed to only C++ files.
CFLAGS_CC_Release := \
-fno-rtti \
-fno-threadsafe-statics \
-fno-strict-aliasing
# Flags passed to only ObjC files.
CFLAGS_OBJC_Release :=
# Flags passed to only ObjC++ files.
CFLAGS_OBJCC_Release :=
INCS_Release := \
-I/Users/azat/.node-gyp/0.10.12/src \
-I/Users/azat/.node-gyp/0.10.12/deps/uv/include \
-I/Users/azat/.node-gyp/0.10.12/deps/v8/include
OBJS := \
$(obj).target/$(TARGET)/ext/bson.o
# Add to the list of files we specially track dependencies for.
all_deps += $(OBJS)
# CFLAGS et al overrides must be target-local.
# See "Target-specific Variable Values" in the GNU Make manual.
$(OBJS): TOOLSET := $(TOOLSET)
$(OBJS): GYP_CFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_C_$(BUILDTYPE))
$(OBJS): GYP_CXXFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_CC_$(BUILDTYPE))
$(OBJS): GYP_OBJCFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_C_$(BUILDTYPE)) $(CFLAGS_OBJC_$(BUILDTYPE))
$(OBJS): GYP_OBJCXXFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_CC_$(BUILDTYPE)) $(CFLAGS_OBJCC_$(BUILDTYPE))
# Suffix rules, putting all outputs into $(obj).
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(srcdir)/%.cc FORCE_DO_CMD
@$(call do_cmd,cxx,1)
# Try building from generated source, too.
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(obj).$(TOOLSET)/%.cc FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(obj)/%.cc FORCE_DO_CMD
@$(call do_cmd,cxx,1)
# End of this set of suffix rules
### Rules for final target.
LDFLAGS_Debug := \
-Wl,-search_paths_first \
-mmacosx-version-min=10.5 \
-arch x86_64 \
-L$(builddir) \
-install_name @rpath/bson.node
LIBTOOLFLAGS_Debug := \
-Wl,-search_paths_first
LDFLAGS_Release := \
-Wl,-search_paths_first \
-mmacosx-version-min=10.5 \
-arch x86_64 \
-L$(builddir) \
-install_name @rpath/bson.node
LIBTOOLFLAGS_Release := \
-Wl,-search_paths_first
LIBS := \
-undefined dynamic_lookup
$(builddir)/bson.node: GYP_LDFLAGS := $(LDFLAGS_$(BUILDTYPE))
$(builddir)/bson.node: LIBS := $(LIBS)
$(builddir)/bson.node: GYP_LIBTOOLFLAGS := $(LIBTOOLFLAGS_$(BUILDTYPE))
$(builddir)/bson.node: TOOLSET := $(TOOLSET)
$(builddir)/bson.node: $(OBJS) FORCE_DO_CMD
$(call do_cmd,solink_module)
all_deps += $(builddir)/bson.node
# Add target alias
.PHONY: bson
bson: $(builddir)/bson.node
# Short alias for building this executable.
.PHONY: bson.node
bson.node: $(builddir)/bson.node
# Add executable to "all" target.
.PHONY: all
all: $(builddir)/bson.node

Some files were not shown because too many files have changed in this diff Show More