Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created April 20, 2012 14:47
Show Gist options
  • Save Raynos/11fdc6545c175792fc86 to your computer and use it in GitHub Desktop.
Save Raynos/11fdc6545c175792fc86 to your computer and use it in GitHub Desktop.
console.log("constructing relations")
// for each post in g reader
after.forEach(this.data, createForwardAndBackwardLinks,
this, this.finish)
function createForwardAndBackwardLinks(item, callback) {
var collection = this.collection
console.log("looping over links in", item)
// for each link in post
after.forEach(item.uris, insertForwardLinks,
this, insertBackwardLinks)
function insertForwardLinks(uri, key, callback) {
// find all posts of that link
if (key % 300 === 0) {
console.log("updating a collection with the uri", uri,
item.uris.length)
}
// and insert the item.link into the forwardLinks
collection.update({
uri: uri
}, {
$addToSet: {
forwardLinks: item.link
}
}, {
multi: true,
safe: true
// inner loop done
}, callback)
}
function insertBackwardLinks() {
console.log("done updating ever uri in items.uri", item)
// find all documents which contain me in forwardLinks
collection.find({
forwardLinks: item.uri
}).toArray(updateItemWithBackwardLinks)
function updateItemWithBackwardLinks(err, array) {
console.log("found all forwardLinks, now updating")
// map the documents to their links
array = array.map(extractUri)
// add all those links into my backLinks
collect.update({
uri: item.uri
}, {
$addToSet: {
backLinks: array
}
}, {
multi: true,
safe: true
// outer loop done
}, callback)
}
}
// all data is done
}
function extractUri(item) {
return item.uri
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment