Permalink
Browse files

Ported to old mongoose

  • Loading branch information...
1 parent 7e5fe15 commit 0c779fb0ad5e178287fead990cb2b7d1392a26d1 @indutny committed Dec 3, 2010
Showing with 69 additions and 62 deletions.
  1. +64 −57 lib/tracker/db.mongo.js
  2. +3 −1 lib/tracker/error.js
  3. +1 −3 lib/tracker/storage.mongo.js
  4. +1 −1 run.js
@@ -1,69 +1,76 @@
/**
* Init db
*/
-var mongoose = require('../../support/mongoose'),
- document = mongoose.define;
+var mongoose = require('mongoose').Mongoose;
module.exports = function(options, callback) {
var db = mongoose.connect(options.mongo_url);
- document('Peer')
- .oid('_id')
- .string('peer_id')
- .string('info_hash')
- .string('short_addr')
- .string('ip')
- .number('port')
- .number('left')
- .number('updated_at')
- .addIndex({peer_id: 1, info_hash: 1}, {unique: true, dropDubs: true})
- .addIndex({info_hash: 1})
- .addIndex({updated_at: -1})
- .static('cleanup', function cleanup(since) {
- this.remove({updated_at: {$lt: since}}, function() {});
- });
- db.Peer = mongoose.Peer;
+ mongoose.model('Peer', {
+ properties: ['peer_id', 'info_hash', 'short_addr', 'ip', 'port', 'left',
+ 'updated_at'],
+ cast: {
+ 'port': Number,
+ 'left': Number,
+ 'updated_at': Number
+ },
+ indexes: [
+ [{peer_id: 1, info_hash: 1}, {unique: true}],
+ {info_hash: 1},
+ {updated_at: -1}
+ ],
+ static: {
+ cleanup: function cleanup(since) {
+ this.remove({updated_at: {$lt: since}}, function() {});
+ }
+ }
+ });
+ db.Peer = db.model('Peer');
- document('Cache')
- .oid('_id')
- .string('info_hash')
- .string('peers')
- .number('updated_at')
- .addIndex({info_hash: 1}, {unique: true, dropDubs: true})
- .addIndex({updated_at_at: -1})
- .static('cleanup', function cleanup(since) {
- this.remove({updated_at: {$lt: since}}, function() {});
- })
- .static('access', function access(info_hash, numwant, callback) {
- this.find({info_hash: info_hash}).one(function(err, cache) {
- if (err) return callback(err);
-
- if (!cache) {
- db.Peer
- .find({info_hash: info_hash})
- .sort([['updated_at', -1]])
- .limit(numwant)
- .all(function(err, docs) {
- if (err) return callback(err);
-
- var peers = docs.map(peer_id).join('');
+ mongoose.model('Cache', {
+ properties: ['info_hash', 'peers', 'updated_at'],
+ cast: {
+ updated_at: Number
+ },
+ indexes: [
+ [{info_hash: 1}, {unique: true}],
+ {updated_at: -1}
+ ],
+ static: {
+ cleanup: function cleanup(since) {
+ this.remove({updated_at: {$lt: since}}, function() {});
+ },
+ access: function access(info_hash, numwant, callback) {
+ this.find({info_hash: info_hash}).one(function(cache) {
+ if (!cache) {
+ db.Peer
+ .find({info_hash: info_hash})
+ .sort([['updated_at', -1]])
+ .limit(numwant)
+ .all(function(docs) {
+ if (!docs) return callback(Error('Cache.access error'));
+
+ var peers = docs.map(peer_id).join('');
- callback(null, peers);
-
- new (db.Cache)({
- info_hash: info_hash,
- peers: escape(peers),
- updated_at: +new Date
- }).save();
- });
- } else {
- callback(null, unescape(cache.peers));
- }
- });
- });
- db.Cache = mongoose.Cache;
-
- callback(null, db);
+ callback(null, peers);
+
+ new (db.Cache)({
+ info_hash: info_hash,
+ peers: escape(peers),
+ updated_at: +new Date
+ }).save();
+ });
+ } else {
+ callback(null, unescape(cache.peers));
+ }
+ });
+ }
+ }
+ });
+ db.Cache = db.model('Cache');
+ db.on('open', function() {
+ callback(null, db);
+ });
};
@@ -4,7 +4,9 @@
module.exports = function(req, res, next) {
res.writeError = function(error) {
res.writeHead(200);
- error = (error || '').toString();
+ error = typeof error == 'object' ?
+ JSON.stringify(error) :
+ (error || '').toString();
res.end('d14:failure reason' + error.length + ':' + error + 'e');
};
next();
@@ -42,9 +42,7 @@ Storage.prototype.put = function(event, peer, callback) {
db.Peer
.find({peer_id: peer.peer_id, info_hash: peer.info_hash})
- .one(function(err, instance) {
- if (err) return callback(err);
-
+ .one(function(instance) {
if (!instance) {
// Create
instance = new (db.Peer)(peer);
View
@@ -17,6 +17,6 @@ tracker({
},
"setup": true,
"engine": "mongo",
- "mongo_url": "mongodb://localhost/tracker"
+ "mongo_url": "mongodb://test:test@flame.mongohq.com:27068/tracker"
}
});

0 comments on commit 0c779fb

Please sign in to comment.