fully implementing bulk functional transform

This commit is contained in:
Max Ogden
2011-07-02 23:55:25 -07:00
parent 64ef71fdd4
commit ba9264b212
6 changed files with 47 additions and 81 deletions

View File

@@ -2,58 +2,29 @@
var costco = function() {
var toUpdate = [];
function handleEditorChange(e) {
mapDocs(e.target.value);
}
function computeChanges() {
$("#notification-message").text("Computing changes...");
var text = $("#map-function").val();
var docs;
try {
docs = JSON.parse(text);
}
catch(e) {
try {
docs = JSON.parse("[" + text + "]");
}
catch(e) {
// not JSON, must be an edit function
return mapDocs(text);
}
}
if(!docs.length) docs = [docs];
toUpdate = docs;
$("#notification-message").text("Computing changes...");
$("#status").html("<span class='warning'>About to add " + docs.length
+ " docs to " + getDb().name + "</span>");
$("#update-container").show();
mapDocs(app.cache, e.target.value, true);
}
function mapDocs(funcString) {
var errors = $('.expression-preview-parsing-status');
function mapDocs(docs, funcString, preview) {
if(preview) var errors = $('.expression-preview-parsing-status');
try {
eval("var editFunc = " + funcString);
errors.text('No syntax error.');
if(preview) errors.text('No syntax error.');
} catch(e) {
errors.text(e+"");
if(preview) errors.text(e+"");
return;
}
toUpdate = [];
var deleted = 0
var toUpdate = []
, deleted = 0
, edited = 0
, failed = 0
, count = 0
, preview = [];
;
if(preview) var preview = [];
_.each(app.cache, function(doc) {
_.each(docs, function(doc) {
try {
var updated = editFunc(_.clone(doc));
} catch(e) {
@@ -69,37 +40,21 @@ var costco = function() {
toUpdate.push(updated);
edited++;
}
preview.push({before: doc[app.currentColumn], after: updated[app.currentColumn]});
count++;
if(preview) preview.push({before: doc[app.currentColumn], after: updated[app.currentColumn]});
});
util.render('editPreview', 'editPreview', {rows: preview});
//
// // todo: make template for this
// $("#status").html("<span class='warning'>About to edit " + edited
// + " docs and delete " + deleted + " docs from "
// + getDb().name + "</span>");
// if(failed)
// $("#status").append(". Edit function threw on " + failed + " docs");
// $("#update-container").show();
if(preview) util.render('editPreview', 'editPreview', {rows: preview});
return toUpdate;
}
function updateDocs(callback) {
if(!toUpdate.length)
return callback();
getDb().bulkSave({docs: toUpdate}, {
success: callback,
error: function(req, status, err) {
$("#status").html("<span class='error'>error updating docs: "
+ err + "</span>");
}
});
function updateDocs(docs, callback) {
if(!docs.length)
return callback("Failed to update");
couch.request({url: app.baseURL + "api/_bulk_docs", type: "POST", data: JSON.stringify({docs: docs})}).then(callback);
}
return {
handleEditorChange: handleEditorChange,
computeChanges: computeChanges,
mapDocs: mapDocs,
updateDocs: updateDocs
};