Skip to content

Instantly share code, notes, and snippets.

@robotarmy

robotarmy/col.js

Created May 31, 2010
Embed
What would you like to do?
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]);
});
});
});
};
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);
};
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();
});
});
};
@christkv

This comment has been minimized.

Copy link

@christkv christkv commented Jun 1, 2010

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);

collection.find(function(err, cursor) {
  raise_if(err);
  docs = []
  cursor.each(function(err, doc) {
  raise_if(err);
    if(!doc) {
      callback.apply(this,[err,docs]);
      client.close();                               
    } else {
       docs[docs.length] = doc;
    } 
  });
});

});
};

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

@robotarmy

This comment has been minimized.

Copy link
Owner Author

@robotarmy robotarmy commented Jun 1, 2010

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