Created
March 21, 2012 21:08
-
-
Save jimklo/2152850 to your computer and use it in GitHub Desktop.
Replication via Slice
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 https = require('https'); | |
var http = require('http'); | |
var querystring = require('querystring'); | |
var _ = require('underscore'); | |
// https://node01.public.learningregistry.net/slice?identity=Brokers+of+Expertise | |
function GetSlice(identity, token, fn, db_name, doc_list) { | |
var q = {}; | |
if (identity) { | |
q["identity"] = identity; | |
} | |
if (token) { | |
q["resumption_token"] = token; | |
} | |
var options = { | |
host: "node01.public.learningregistry.net", | |
port: 443, | |
path: "/slice?"+querystring.stringify(q), | |
method: "GET", | |
headers: { "Content-Type": "application/json; charset=utf-8" } | |
}; | |
var req = https.request(options, function(res) { | |
console.log("statusCode: ", res.statusCode); | |
console.log("headers: ", res.headers); | |
var raw = []; | |
res.on('data', function(d) { | |
raw.push(d.toString()); | |
}); | |
res.on('end', function() { | |
var j = JSON.parse(raw.join("")); | |
if (j.documents) { | |
console.log("#docs:"+j.documents.length); | |
for (var index in j.documents) { | |
if (fn && j.documents[index].resource_data_description) { | |
if (!doc_list) { | |
fn(j.documents[index].resource_data_description, db_name); | |
} else { | |
doc_list.push(j.documents[index].resource_data_description); | |
} | |
} else { | |
console.log("doc:"+JSON.stringify(j.documents[index])); | |
} | |
} | |
if (doc_list.length > 0) { | |
fn(doc_list, db_name); | |
doc_list = []; | |
} | |
} | |
if (j.resumption_token && j.resumption_token !== "") { | |
console.log("rt: "+ j.resumption_token); | |
GetSlice(identity, j.resumption_token, fn, db_name, doc_list); | |
} | |
}); | |
}); | |
req.end(); | |
req.on('error', function(e) { | |
console.error(e); | |
}) | |
} | |
function DisappearInCouchCushion(docs, db_name) { | |
var options = { | |
host: "localhost", | |
port: "5984", | |
path: "/"+db_name+"/_bulk_docs", | |
method: "POST", | |
headers: {"Content-Type": "application/json; charset-utf-8"} | |
}; | |
var req = http.request(options, function (res) { | |
_.each(docs, function(d) {console.log( JSON.stringify({ _id: d._id, _rev: d._rev }))}); | |
console.log("statusCode: ", res.statusCode); | |
console.log("headers: ", res.headers); | |
res.on('data', function(d) { | |
console.log(d.toString()); | |
}); | |
}); | |
req.on('error', function(e) { | |
console.error(e.toString()); | |
}); | |
req.write(JSON.stringify({ new_edits:false, docs:docs }), 'utf8'); | |
req.end(); | |
} | |
function LoseInCouchCushion(doc, db_name) { | |
var options = { | |
host: "localhost", | |
port: "5984", | |
path: "/"+db_name+"/"+doc._id, | |
method: "PUT", | |
headers: {"Content-Type": "application/json; charset-utf-8"} | |
}; | |
var req = http.request(options, function (res) { | |
console.log("orig-id: "+doc._id+" orig-rev: "+doc._rev); | |
console.log("statusCode: ", res.statusCode); | |
console.log("headers: ", res.headers); | |
res.on('data', function(d) { | |
console.log(d.toString()); | |
}); | |
}); | |
req.on('error', function(e) { | |
console.error(e.toString()); | |
}) | |
var clean_doc = _.clone(doc); | |
delete clean_doc._id; | |
delete clean_doc._rev; | |
req.write(JSON.stringify(clean_doc), 'utf8'); | |
req.end(); | |
} | |
function TorchCouchDB(db_name, fn) { | |
var options = { | |
host: "localhost", | |
port: "5984", | |
path: "/"+db_name, | |
method: "DELETE", | |
headers: {"Content-Type": "application/json; charset-utf-8"} | |
}; | |
var req = http.request(options, function (res) { | |
console.log("statusCode: ", res.statusCode); | |
console.log("headers: ", res.headers); | |
res.on('data', function(d) { | |
console.log(d.toString()); | |
}); | |
res.on('end', function() { | |
fn(db_name); | |
}); | |
}); | |
req.end(); | |
} | |
function ConstructCouchDB(db_name, fn) { | |
var options = { | |
host: "localhost", | |
port: "5984", | |
path: "/"+db_name, | |
method: "PUT", | |
headers: {"Content-Type": "application/json; charset-utf-8"} | |
}; | |
var req = http.request(options, function (res) { | |
console.log("statusCode: ", res.statusCode); | |
console.log("headers: ", res.headers); | |
res.on('data', function(d) { | |
console.log(d.toString()); | |
}); | |
res.on('end', function() { | |
fn(db_name); | |
}); | |
}); | |
req.end(); | |
} | |
var db = "oercommons" | |
TorchCouchDB(db, function() { | |
ConstructCouchDB(db, function() { | |
// GetSlice("Brokers of Expertise", null, LoseInCouchCushion, db); | |
GetSlice("OER Commons", null, DisappearInCouchCushion, db, []); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment