Created
June 10, 2013 04:19
-
-
Save shiedman/5746515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
//cnode的2.4.1版mongoose的poolSize为1,同步这个设定 | |
mongoose.connect('mongodb://localhost/test',{server:{poolSize:1}}); | |
var ObjectId=mongoose.Schema.ObjectId; | |
var User = mongoose.model('User', mongoose.Schema({ | |
name: { type: String} | |
})); | |
var Topic = mongoose.model('Topic', mongoose.Schema({ | |
title: { type: String }, | |
content: { type: String }, | |
author_id: { type: ObjectId } | |
})); | |
var Reply = mongoose.model('Reply', mongoose.Schema({ | |
content: { type: String }, | |
topic_id: { type: ObjectId}, | |
author_id: { type: ObjectId } | |
})); | |
var Message = mongoose.model('Message', mongoose.Schema({ | |
master_id: {type: ObjectId }, | |
author_id: { type: ObjectId }, | |
topic_id: { type: ObjectId }, | |
reply_id: { type: ObjectId } | |
})); | |
function initDb(){ | |
var user=new User({name:'admin'}); | |
user.save(function(err){ | |
var topic=new Topic({title:'test mongoose',author_id:user.id}); | |
topic.save(function(err){ | |
var reply=new Reply({ | |
content:'this is a reply', | |
topic_id:topic.id, | |
author_id:user.id | |
}); | |
reply.save(function(err){ | |
function initMessage(n){ | |
for(var i=0;i;ltn;i++){ | |
var msg=new Message({ | |
master_id:user.id, | |
author_id:user.id, | |
topic_id:topic.id, | |
reply_id:reply.id | |
}); | |
msg.save(); | |
} | |
} | |
initMessage(20); | |
}); | |
}); | |
}); | |
} | |
//模拟https://github.com/cnodejs/nodeclub/blob/master/proxy/message.js#L31 | |
function getMessagesByUserId(userId,callback){ | |
callback=callback||function(){}; | |
Message.find({master_id:userId},function(err,messages){ | |
var n=messages.length*3; | |
var cb=function(key,err,value){ | |
this[key]=value; | |
if(--n===0)callback(messages); | |
}; | |
messages.forEach(function(msg){ | |
User.findById(msg.author_id,cb.bind(msg,'author')); | |
Topic.findById(msg.topic_id,cb.bind(msg,'topic')); | |
Reply.findById(msg.reply_id,cb.bind(msg,'reply')); | |
}); | |
}); | |
} | |
var db = mongoose.connection; | |
db.on('error', function(err){throw err}); | |
db.once('open', function() { | |
User.find(function(err,users){ | |
//init db if first run | |
if(users.length == 0)initDb(); | |
}); | |
}); | |
var clients=0; | |
require('http').createServer(function(req,res){ | |
clients++; | |
User.findOne({name:'admin'},function(err,user){ | |
if(err){res.end('error');return console.error(err);} | |
getMessagesByUserId(user.id,function(messages){ | |
messages.forEach(function(msg){ | |
res.write(JSON.stringify(msg,null,2)); | |
res.write(JSON.stringify(msg.author,null,2)); | |
res.write(JSON.stringify(msg.topic,null,2)); | |
res.write(JSON.stringify(msg.reply,null,2)); | |
}); | |
res.end(); | |
clients--; | |
}); | |
}); | |
}).listen(3000,function(){ | |
console.log('server listening on port',this.address().port); | |
}) | |
//print memory usages | |
setInterval(function(){ | |
if(clients==0 && typeof gc == 'function')gc(); | |
var m=process.memoryUsage(); | |
console.log('connections:',clients,',',m); | |
},3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment