Created
May 31, 2010 20:33
-
-
Save robotarmy/420252 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Ledger.prototype.collection = function(callback) { | |
var client = new DStore('experiment',new Server("127.0.0.1", 27017, {},{strict:true})); | |
client.open(function() { | |
client.createCollection('ledger', function(err, collection) { | |
client.collection('ledger', function(err, collection) { | |
callback.apply(this,[err,collection,client]); | |
}); | |
}); | |
}); | |
}; |
This file contains hidden or 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
Ledger.prototype.service = function() { | |
var self = this; | |
var http = require('http'); | |
var sys = require('sys'); | |
self.server = http.createServer(function(request,response) { | |
var req = request; | |
var resp = response; | |
self.find_all(function(err,items){ | |
sys.puts(err); | |
var callback = undefined; | |
var query = url.parse(request.url,true).query | |
if (query != undefined) | |
callback = query['callback']; | |
if (callback != undefined){ | |
var body = ";"+callback+"("+JSON.stringify(items)+");"; | |
resp.writeHead(200, { | |
'Content-Length': body.length, | |
'Content-Type': 'application/json;charset=utf-8' | |
}); | |
resp.write(body,'utf8') | |
resp.end(); | |
} | |
}); | |
}); | |
self.server.addListener('close',function(errno) { | |
sys.puts('close ' + errno); | |
}); | |
self.server.listen(7001); | |
}; |
This file contains hidden or 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
Ledger.prototype.find_all = function(callback) { | |
this.collection(function(err, collection) { | |
collection.find(function(err, cursor) { | |
cursor.toArray(function(err, docs) { | |
callback.apply(this,[err,docs]); | |
}); | |
}); | |
}); | |
}; | |
# Same problems with this write of it | |
# | |
Ledger.prototype.find_all = function(callback) { | |
this.collection(function(err, collection, client) { | |
raise_if(err); | |
collection.find(function(err, cursor) { | |
raise_if(err); | |
docs = [] | |
cursor.each(function(err, doc) { | |
raise_if(err); | |
docs[docs.length] = doc | |
}); | |
callback.apply(this,[err,docs]); | |
client.close(); | |
}); | |
}); | |
}; |
thank you so much Christian for your thought and efforts.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One thing that strikes me is that cursor.each will fail due to it being asynchronous each call will execute nextObject on the cursor fetching another item from the db. The way you are using it, it will most likely execute
callback.apply(this,[err,docs]);
client.close();
Before the .each method have finished.
Try something like the following below. A good starting point for the use is the integration_tests.js file that
contains a lot of usages.
Ledger.prototype.find_all = function(callback) {
this.collection(function(err, collection, client) {
raise_if(err);
});
};
Another possibility is to use the streaming api that the guys from the hummingbird project provided
Cursor.prototype.streamRecords = function(callback)
Have a look at benchmark/streaming_benchmark.js for an example of usage.
Hope it helps
Cheers
Christian