Created
August 21, 2011 22:51
-
-
Save ybogdanov/1161295 to your computer and use it in GitHub Desktop.
examples of code which uses node-async vs node-sync with more complex logic
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
createEdge : function(e, callback) | |
{ | |
var self = this, tryNum = 0, | |
isolateRetryAttempts = 20, | |
isolateRetryInterval = 1000; | |
callback = callback || function(){}; | |
if (!(e instanceof Edge) || !e.isValid()) { | |
return callback(new Error("Invalid edge passed")); | |
} | |
// Call this function when need to create the edge | |
var createEdge = function(callback) | |
{ | |
// Create edge object in db | |
self.db.edge.insert(e.getData(), {safe:true}, function(err, docs){ | |
if (err) return callback(err); | |
// Update edge var | |
e.update(docs[0]); | |
e._wasNew = true; | |
async.parallel([ | |
// Update edge outgoing counts | |
function(callback) { | |
var inc = {}; | |
inc['edges.' + e.getOutKey()] = 1; // in/user/reading | |
self.db.vertex.update({_id : e.from}, {$inc : inc}, callback); | |
}, | |
// Update edge incoming counts | |
function(callback) { | |
var inc = {}; | |
inc['edges.' + e.getInKey()] = 1; // out/user/reading | |
self.db.vertex.update({_id : e.to}, {$inc : inc}, callback); | |
} | |
], function(err){ | |
callback(err, e); | |
}) | |
}); | |
} | |
// Repeat this function until it find the edge object or aquire lock succeed | |
var tryToFind = function() | |
{ | |
// Try to find edge | |
self.db.edge.findOne(e.getCond(), function(err, doc){ | |
// Edge found | |
if (doc) { | |
e.update(doc); | |
return callback(null, e); | |
} | |
self.isolate(e, function(callback){ | |
createEdge(callback) | |
}, function(err){ | |
if (err) { | |
if (err instanceof Mutex.Error && ++tryNum < isolateRetryAttempts) { | |
setTimeout(tryToFind, isolateRetryInterval); | |
} | |
else { | |
callback(err); | |
} | |
} | |
else { | |
callback.apply(null, arguments); | |
} | |
}, false) | |
}); | |
} | |
tryToFind(); | |
}, | |
createEdgeSync : function(e) | |
{ | |
var dbEdge = this.db.edge, | |
dbVertex = this.db.vertex, | |
tryNum = 0, | |
isolateRetryAttempts = 20, | |
isolateRetryInterval = 1000; | |
if (!(e instanceof Edge) || !e.isValid()) { | |
throw new Error("Invalid edge passed"); | |
} | |
// Call this function when need to create the edge | |
var createEdge = function() | |
{ | |
// Create edge object in db | |
var docs = dbEdge.insert.sync(dbEdge, e.getData(), {safe:true}); | |
// Update edge var | |
e.update(docs[0]); | |
e._wasNew = true; | |
// Update edge incoming counts | |
var incIn = {}, incOut = {}; | |
incIn['edges.' + e.getOutKey()] = 1; // in/user/reading | |
dbVertex.update.sync(dbVertex, {_id : e.from}, {$inc : incIn}); | |
// Update edge outgoing counts | |
incOut['edges.' + e.getInKey()] = 1; // out/user/reading | |
dbVertex.update.sync(dbVertex, {_id : e.to}, {$inc : incOut}); | |
}.async() | |
// Repeat this function until it find the edge object or aquire lock succeed | |
while (true) { | |
// Try to find edge | |
var doc = dbEdge.findOne.sync(dbEdge, e.getCond()); | |
// Edge found | |
if (doc) { | |
e.update(doc); | |
return e; | |
} | |
try { | |
this.isolate.sync(this, e, createEdge); | |
} | |
catch (err) { | |
if (err && err instanceof Mutex.Error) { | |
if (++tryNum < isolateRetryAttempts) { | |
Sync.sleep(isolateRetryInterval); | |
continue; | |
} | |
} | |
throw err; | |
} | |
} | |
}.async(), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment