Commit f51d8d5f authored by Tristan Cavelier's avatar Tristan Cavelier

replicaterevisionstorage.js updated for JIO v2

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