From f51d8d5f2df20200e170bfaed85a93196bbd1a7b Mon Sep 17 00:00:00 2001 From: Tristan Cavelier <tristan.cavelier@tiolive.com> Date: Fri, 22 Nov 2013 16:31:27 +0100 Subject: [PATCH] replicaterevisionstorage.js updated for JIO v2 --- src/jio.storage/replicaterevisionstorage.js | 190 ++++++++++---------- 1 file changed, 100 insertions(+), 90 deletions(-) diff --git a/src/jio.storage/replicaterevisionstorage.js b/src/jio.storage/replicaterevisionstorage.js index b20f701..0f3cac0 100644 --- a/src/jio.storage/replicaterevisionstorage.js +++ b/src/jio.storage/replicaterevisionstorage.js @@ -22,24 +22,19 @@ module(jIO); }(['jio'], function (jIO) { "use strict"; - jIO.addStorageType('replicaterevision', function (spec, my) { - var that, priv = {}; + jIO.addStorageType('replicaterevision', function (spec) { + var that = this, priv = {}; spec = spec || {}; - that = my.basicStorage(spec, my); priv.storage_list_key = "storage_list"; priv.storage_list = spec[priv.storage_list_key]; - priv.emptyFunction = function () {}; - - that.specToStore = function () { - var o = {}; - o[priv.storage_list_key] = priv.storage_list; - return o; + priv.emptyFunction = function () { + return; }; /** * Generate a new uuid - * @method generateUuid + * * @return {string} The new uuid */ priv.generateUuid = function () { @@ -61,7 +56,7 @@ /** * Create an array containing dictionnary keys - * @method dictKeys2Array + * * @param {object} dict The object to convert * @return {array} The array of keys */ @@ -77,7 +72,7 @@ /** * Checks a revision format - * @method checkRevisionFormat + * * @param {string} revision The revision string * @return {boolean} True if ok, else false */ @@ -87,7 +82,7 @@ /** * Clones an object in deep (without functions) - * @method clone + * * @param {any} object The object to clone * @return {any} The cloned object */ @@ -101,7 +96,7 @@ /** * Like addJob but also return the method and the index of the storage - * @method send + * * @param {string} method The request method * @param {number} index The storage index * @param {object} doc The document object @@ -112,7 +107,7 @@ * - {object} The error object * - {object} The response object */ - priv.send = function (method, index, doc, option, callback) { + priv.send = function (command, method, index, doc, option, callback) { var wrapped_callback_success, wrapped_callback_error; callback = callback || priv.emptyFunction; wrapped_callback_success = function (response) { @@ -121,20 +116,19 @@ wrapped_callback_error = function (err) { callback(method, index, err, undefined); }; - that.addJob( - method, - priv.storage_list[index], - doc, - option, - wrapped_callback_success, - wrapped_callback_error - ); + if (method === 'allDocs') { + command.storage(priv.storage_list[index]).allDocs(option). + then(wrapped_callback_success, wrapped_callback_error); + } else { + command.storage(priv.storage_list[index])[method](doc, option). + then(wrapped_callback_success, wrapped_callback_error); + } }; /** * Use "send" method to all sub storages. * Calling "callback" for each storage response. - * @method sendToAll + * * @param {string} method The request method * @param {object} doc The document object * @param {object} option The request option @@ -144,17 +138,17 @@ * - {object} The error object * - {object} The response object */ - priv.sendToAll = function (method, doc, option, callback) { + priv.sendToAll = function (command, method, doc, option, callback) { var i; for (i = 0; i < priv.storage_list.length; i += 1) { - priv.send(method, i, doc, option, callback); + priv.send(command, method, i, doc, option, callback); } }; /** * Use "send" method to all sub storages. * Calling "callback" only with the first response - * @method sendToAllFastestResponseOnly + * * @param {string} method The request method * @param {object} doc The document object * @param {object} option The request option @@ -163,18 +157,14 @@ * - {object} The error object * - {object} The response object */ - priv.sendToAllFastestResponseOnly = function ( - method, - doc, - option, - callback - ) { - var i, callbackWrapper, error_count, last_error; + priv.sendToAllFastestResponseOnly = function (command, method, + doc, option, callback) { + var i, callbackWrapper, error_count; error_count = 0; callbackWrapper = function (method, index, err, response) { + /*jslint unparam: true */ if (err) { error_count += 1; - last_error = err; if (error_count === priv.storage_list.length) { return callback(method, err, response); } @@ -182,14 +172,14 @@ callback(method, err, response); }; for (i = 0; i < priv.storage_list.length; i += 1) { - priv.send(method, i, doc, option, callbackWrapper); + priv.send(command, method, i, doc, option, callbackWrapper); } }; /** * Use "sendToAll" method, calling "callback" at the last response with * the response list - * @method sendToAllGetResponseList + * * @param {string} method The request method * @param {object} doc The document object * @param {object} option The request option @@ -198,10 +188,12 @@ * - {object} The error object * - {object} The response object */ - priv.sendToAllGetResponseList = function (method, doc, option, callback) { + priv.sendToAllGetResponseList = function (command, method, + doc, option, callback) { var wrapper, callback_count = 0, response_list = [], error_list = []; response_list.length = priv.storage_list.length; wrapper = function (method, index, err, response) { + /*jslint unparam: true */ error_list[index] = err; response_list[index] = response; callback_count += 1; @@ -209,7 +201,7 @@ callback(error_list, response_list); } }; - priv.sendToAll(method, doc, option, wrapper); + priv.sendToAll(command, method, doc, option, wrapper); }; /** @@ -217,16 +209,22 @@ * @method check * @param {object} command The JIO command */ - that.check = function (command) { - function callback(err, response) { + that.check = function (command, param, option) { + function callback(err) { if (err) { - return that.error(err); + return command.error(err); } - that.success(response); + command.success(); + } + if (!param._id) { + return callback({ + "status": 501 + }); } priv.check( - command.cloneDoc(), - command.cloneOption(), + command, + param, + option, callback ); }; @@ -236,26 +234,32 @@ * @method repair * @param {object} command The JIO command */ - that.repair = function (command) { - function callback(err, response) { + that.repair = function (command, param, option) { + function callback(err) { if (err) { - return that.error(err); + return command.error(err); } - that.success(response); + command.success(); + } + if (!param._id) { + return callback({ + "status": 501 + }); } priv.repair( - command.cloneDoc(), - command.cloneOption(), + command, + param, + option, true, callback ); }; - priv.check = function (doc, option, success, error) { - priv.repair(doc, option, false, success, error); + priv.check = function (command, doc, option, success, error) { + priv.repair(command, doc, option, false, success, error); }; - priv.repair = function (doc, option, repair, callback) { + priv.repair = function (command, doc, option, repair, callback) { var functions = {}; callback = callback || priv.emptyFunction; option = option || {}; @@ -265,6 +269,7 @@ var i; for (i = 0; i < priv.storage_list.length; i += 1) { priv.send( + command, repair ? "repair" : "check", i, doc, @@ -276,8 +281,9 @@ functions.repair_sub_storages_count = 0; functions.repairAllSubStoragesCallback = function (method, index, err, response) { + /*jslint unparam: true */ if (err) { - return that.error(err); + return command.error(err); } functions.repair_sub_storages_count += 1; if (functions.repair_sub_storages_count === priv.storage_list.length) { @@ -324,18 +330,22 @@ return param; }; functions.getAllDocuments = function (param) { - var i, doc = priv.clone(param.doc), option = priv.clone(param.option); + var i, metadata, cloned_option; + metadata = priv.clone(param.doc); + cloned_option = priv.clone(param.option); option.conflicts = true; option.revs = true; option.revs_info = true; for (i = 0; i < priv.storage_list.length; i += 1) { // if the document is not loaded - priv.send("get", i, doc, option, functions.dealResults(param)); + priv.send(command, "get", i, + metadata, cloned_option, functions.dealResults(param)); } functions.finished_count += 1; }; functions.dealResults = function (param) { return function (method, index, err, response) { + /*jslint unparam: true */ var response_object = {}; if (param.deal_result_state !== "ok") { // deal result is in a wrong state, exit @@ -346,15 +356,14 @@ // get document failed, exit param.deal_result_state = "error"; callback({ - "status": 40, - "statusText": "Check Failed", - "error": "check_failed", + "status": "conflict", "message": "An error occured on the sub storage", "reason": err.reason }, undefined); return; } } + response = response.data; // success to get the document // add the response in memory param.responses.count += 1; @@ -385,9 +394,7 @@ if (param.repair === false) { // do not repair callback({ - "status": 41, - "statusText": "Check Not Ok", - "error": "check_not_ok", + "status": "conflict", "message": "Some documents are different in the sub storages", "reason": "Storage contents differ" }, undefined); @@ -427,6 +434,7 @@ if ((parsed_response._attachments).hasOwnProperty(attachment)) { functions.get_attachment_count += 1; priv.send( + command, "getAttachment", param.responses.stats[response][0], { @@ -447,22 +455,18 @@ } }; functions.get_attachment_count = 0; - functions.getAttachmentsCallback = function ( - param, - attachment_id, - index_list - ) { + functions.getAttachmentsCallback = function (param, attachment_id) { return function (method, index, err, response) { + /*jslint unparam: true */ if (err) { callback({ - "status": 40, - "statusText": "Check Failed", - "error": "check_failed", + "status": "conflict", "message": "Unable to retreive attachments", "reason": err.reason }, undefined); return; } + response = response.data; functions.get_attachment_count -= 1; param.responses.attachments[attachment_id] = response; if (functions.get_attachment_count === 0) { @@ -517,6 +521,7 @@ for (i = 0; i < storage_list.length; i += 1) { functions.finished_count += attachment_to_put.length || 1; priv.send( + command, "put", storage_list[i], new_doc, @@ -546,6 +551,7 @@ }; functions.putAttachments = function (param, attachment_to_put) { return function (method, index, err, response) { + /*jslint unparam: true */ var i, attachment; if (err) { return callback({ @@ -566,6 +572,7 @@ "_data": param.responses.attachments[attachment_to_put[i]._id] }; priv.send( + command, "putAttachment", index, attachment, @@ -579,6 +586,7 @@ }; }; functions.putAttachmentCallback = function (param) { + /*jslint unparam: true */ return function (method, index, err, response) { if (err) { return callback(err, undefined); @@ -608,18 +616,20 @@ * @param {object} command The JIO command * @param {string} method The method to use */ - that.genericRequest = function (command, method) { - var doc = command.cloneDoc(); + that.genericRequest = function (command, method, param, option) { + var doc = param; doc._id = doc._id || priv.generateUuid(); priv.sendToAllFastestResponseOnly( + command, method, doc, - command.cloneOption(), + option, function (method, err, response) { + /*jslint unparam: true */ if (err) { - return that.error(err); + return command.error(err); } - that.success(response); + command.success(response); } ); }; @@ -629,8 +639,8 @@ * @method post * @param {object} command The JIO command */ - that.post = function (command) { - that.genericRequest(command, "put"); + that.post = function (command, metadata, option) { + that.genericRequest(command, "put", metadata, option); }; /** @@ -638,8 +648,8 @@ * @method put * @param {object} command The JIO command */ - that.put = function (command) { - that.genericRequest(command, "post"); + that.put = function (command, metadata, option) { + that.genericRequest(command, "post", metadata, option); }; /** @@ -647,8 +657,8 @@ * @method putAttachment * @param {object} command The JIO command */ - that.putAttachment = function (command) { - that.genericRequest(command, "putAttachment"); + that.putAttachment = function (command, param, option) { + that.genericRequest(command, "putAttachment", param, option); }; /** @@ -656,8 +666,8 @@ * @method get * @param {object} command The JIO command */ - that.get = function (command) { - that.genericRequest(command, "get"); + that.get = function (command, param, option) { + that.genericRequest(command, "get", param, option); }; /** @@ -665,8 +675,8 @@ * @method getAttachment * @param {object} command The JIO command */ - that.getAttachment = function (command) { - that.genericRequest(command, "getAttachment"); + that.getAttachment = function (command, param, option) { + that.genericRequest(command, "getAttachment", param, option); }; /** @@ -674,8 +684,8 @@ * @method remove * @param {object} command The JIO command */ - that.remove = function (command) { - that.genericRequest(command, "remove"); + that.remove = function (command, param, option) { + that.genericRequest(command, "remove", param, option); }; /** @@ -683,8 +693,8 @@ * @method remove * @param {object} command The JIO command */ - that.removeAttachment = function (command) { - that.genericRequest(command, "removeAttachment"); + that.removeAttachment = function (command, param, option) { + that.genericRequest(command, "removeAttachment", param, option); }; return that; -- 2.30.9