Commit b86d500e authored by Tristan Cavelier's avatar Tristan Cavelier

splitstorage.js updated for JIO v2

parent a84f4578
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
/*jslint indent:2, maxlen: 80, nomen: true */ /*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO: true, exports: true, define: true */ /*global jIO, define */
/** /**
* Provides a split storage for JIO. This storage splits data * Provides a split storage for JIO. This storage splits data
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
* "storage_list": [<storage description>, ...] * "storage_list": [<storage description>, ...]
* } * }
*/ */
// define([module_name], [dependencies], module); // define([dependencies], module);
(function (dependencies, module) { (function (dependencies, module) {
"use strict"; "use strict";
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
...@@ -113,11 +113,12 @@ ...@@ -113,11 +113,12 @@
if (this.response_list.length === 0) { if (this.response_list.length === 0) {
return []; return [];
} }
while ((row = this.response_list[0].rows.shift()) !== undefined) { /*jslint ass: true */
while ((row = this.response_list[0].data.rows.shift()) !== undefined) {
to_merge[0] = row; to_merge[0] = row;
for (i = 1; i < this.response_list.length; i += 1) { for (i = 1; i < this.response_list.length; i += 1) {
to_merge[i] = AllDocsResponseMerger.listPopFromRowId( to_merge[i] = AllDocsResponseMerger.listPopFromRowId(
this.response_list[i].rows, this.response_list[i].data.rows,
row.id row.id
); );
if (to_merge[i] === undefined) { if (to_merge[i] === undefined) {
...@@ -197,6 +198,7 @@ ...@@ -197,6 +198,7 @@
delete rows.dict[doc_id]; delete rows.dict[doc_id];
return row; return row;
} }
/*jslint ass: true*/
while ((row = rows.shift()) !== undefined) { while ((row = rows.shift()) !== undefined) {
if (row.id === doc_id) { if (row.id === doc_id) {
return row; return row;
...@@ -212,10 +214,10 @@ ...@@ -212,10 +214,10 @@
* A split storage instance is able to i/o on several sub storages with * A split storage instance is able to i/o on several sub storages with
* split documents. * split documents.
* *
* @class splitStorage * @class SplitStorage
*/ */
function splitStorage(spec, my) { function SplitStorage(spec) {
var that = my.basicStorage(spec, my), priv = {}; var that = this, priv = {};
/** /**
* The list of sub storages we want to use to store part of documents. * The list of sub storages we want to use to store part of documents.
...@@ -226,24 +228,6 @@ ...@@ -226,24 +228,6 @@
*/ */
priv.storage_list = spec.storage_list; priv.storage_list = spec.storage_list;
//////////////////////////////////////////////////////////////////////
// Overrides
/**
* Overrides the original {{#crossLink "storage/specToStore:method"}}
* specToStore method{{/crossLink}}.
*
* @method specToStore
* @return {Object} The specificities to store
*/
that.specToStore = function () {
return {"storage_list": priv.storage_list};
};
/**
* TODO validateState
*/
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Tools // Tools
...@@ -266,7 +250,7 @@ ...@@ -266,7 +250,7 @@
* @param {Object} option The command option * @param {Object} option The command option
* @param {Function} callback Called at the end * @param {Function} callback Called at the end
*/ */
priv.send = function (method, doc, option, callback) { priv.send = function (command, method, doc, option, callback) {
var i, answer_list = [], failed = false; var i, answer_list = [], failed = false;
function onEnd() { function onEnd() {
i += 1; i += 1;
...@@ -293,25 +277,19 @@ ...@@ -293,25 +277,19 @@
} }
if (!Array.isArray(doc)) { if (!Array.isArray(doc)) {
for (i = 0; i < priv.storage_list.length; i += 1) { for (i = 0; i < priv.storage_list.length; i += 1) {
that.addJob( if (method === 'allDocs') {
method, command.storage(priv.storage_list[i])[method](option).
priv.storage_list[i], then(onSuccess(i), onError(i));
doc, } else {
option, command.storage(priv.storage_list[i])[method](doc, option).
onSuccess(i), then(onSuccess(i), onError(i));
onError(i) }
);
} }
} else { } else {
for (i = 0; i < priv.storage_list.length; i += 1) { for (i = 0; i < priv.storage_list.length; i += 1) {
that.addJob( // we assume that alldocs is not called if the there is several docs
method, command.storage(priv.storage_list[i])[method](doc[i], option).
priv.storage_list[i], then(onSuccess(i), onError(i));
doc[i],
option,
onSuccess(i),
onError(i)
);
} }
} }
i = 0; i = 0;
...@@ -326,10 +304,10 @@ ...@@ -326,10 +304,10 @@
* @param {Object} option Command option properties * @param {Object} option Command option properties
* @param {String} method The command method ('post' or 'put') * @param {String} method The command method ('post' or 'put')
*/ */
priv.postOrPut = function (doc, option, method) { priv.postOrPut = function (command, doc, option, method) {
var i, data, doc_list = [], doc_underscores = {}; var i, data, doc_list = [], doc_underscores = {};
if (!doc._id) { if (!doc._id) {
doc._id = generateUuid(); doc._id = generateUuid(); // XXX should let gidstorage guess uid
} }
for (i in doc) { for (i in doc) {
if (doc.hasOwnProperty(i)) { if (doc.hasOwnProperty(i)) {
...@@ -347,13 +325,13 @@ ...@@ -347,13 +325,13 @@
(data.length / priv.storage_list.length) * (i + 1) (data.length / priv.storage_list.length) * (i + 1)
); );
} }
priv.send(method, doc_list, option, function (err, response) { priv.send(command, method, doc_list, option, function (err) {
if (err) { if (err) {
err.message = "Unable to " + method + " document"; err.message = "Unable to " + method + " document";
delete err.index; delete err.index;
return that.error(err); return that.error(err);
} }
that.success({"ok": true, "id": doc_underscores._id}); that.success({"id": doc_underscores._id});
}); });
}; };
...@@ -364,52 +342,50 @@ ...@@ -364,52 +342,50 @@
* Split document metadata then store them to the sub storages. * Split document metadata then store them to the sub storages.
* *
* @method post * @method post
* @param {Command} command The JIO command * @param {Object} command The JIO command
*/ */
that.post = function (command) { that.post = function (command, metadata, option) {
priv.postOrPut(command.cloneDoc(), command.cloneOption(), 'post'); priv.postOrPut(command, metadata, option, 'post');
}; };
/** /**
* Split document metadata then store them to the sub storages. * Split document metadata then store them to the sub storages.
* *
* @method put * @method put
* @param {Command} command The JIO command * @param {Object} command The JIO command
*/ */
that.put = function (command) { that.put = function (command, metadata, option) {
priv.postOrPut(command.cloneDoc(), command.cloneOption(), 'put'); priv.postOrPut(command, metadata, option, 'put');
}; };
/** /**
* Puts an attachment to the sub storages. * Puts an attachment to the sub storages.
* *
* @method putAttachment * @method putAttachment
* @param {Command} command The JIO command * @param {Object} command The JIO command
*/ */
that.putAttachment = function (command) { that.putAttachment = function (command, param, option) {
var i, attachment_list = [], data = command.getAttachmentData(); var i, attachment_list = [], data = param._blob;
for (i = 0; i < priv.storage_list.length; i += 1) { for (i = 0; i < priv.storage_list.length; i += 1) {
attachment_list[i] = command.cloneDoc(); attachment_list[i] = jIO.util.deepClone(param);
attachment_list[i]._data = data.slice( attachment_list[i]._data = data.slice(
(data.length / priv.storage_list.length) * i, (data.length / priv.storage_list.length) * i,
(data.length / priv.storage_list.length) * (i + 1) (data.length / priv.storage_list.length) * (i + 1),
"application/octet-stream"
); );
} }
priv.send( priv.send(
command,
'putAttachment', 'putAttachment',
attachment_list, attachment_list,
command.cloneOption(), option,
function (err, response) { function (err) {
if (err) { if (err) {
err.message = "Unable to put attachment"; err.message = "Unable to put attachment";
delete err.index; delete err.index;
return that.error(err); return command.error(err);
} }
that.success({ command.success();
"ok": true,
"id": command.getDocId(),
"attachment": command.getAttachmentId()
});
} }
); );
}; };
...@@ -418,21 +394,20 @@ ...@@ -418,21 +394,20 @@
* Gets splited document metadata then returns real document. * Gets splited document metadata then returns real document.
* *
* @method get * @method get
* @param {Command} command The JIO command * @param {Object} command The JIO command
*/ */
that.get = function (command) { that.get = function (command, param, option) {
var doc, option, data, attachments; var doc = param;
doc = command.cloneDoc(); priv.send(command, 'get', doc, option, function (err, response) {
option = command.cloneOption();
priv.send('get', doc, option, function (err, response) {
var i, k; var i, k;
if (err) { if (err) {
err.message = "Unable to get document"; err.message = "Unable to get document";
delete err.index; delete err.index;
return that.error(err); return command.error(err);
} }
doc = ''; doc = '';
for (i = 0; i < response.length; i += 1) { for (i = 0; i < response.length; i += 1) {
response[i] = response[i].data;
doc += response[i].data; doc += response[i].data;
} }
doc = JSON.parse(doc); doc = JSON.parse(doc);
...@@ -472,8 +447,7 @@ ...@@ -472,8 +447,7 @@
} }
} }
} }
doc._id = command.getDocId(); that.success({"data": doc});
that.success(doc);
}); });
}; };
...@@ -481,24 +455,24 @@ ...@@ -481,24 +455,24 @@
* Gets splited document attachment then returns real attachment data. * Gets splited document attachment then returns real attachment data.
* *
* @method getAttachment * @method getAttachment
* @param {Command} command The JIO command * @param {Object} command The JIO command
*/ */
that.getAttachment = function (command) { that.getAttachment = function (command, param, option) {
var doc, option; priv.send(command, 'getAttachment', param, option, function (
doc = command.cloneDoc(); err,
option = command.cloneOption(); response
priv.send('getAttachment', doc, option, function (err, response) { ) {
var i, k; var i, doc;
if (err) { if (err) {
err.message = "Unable to get attachment"; err.message = "Unable to get attachment";
delete err.index; delete err.index;
return that.error(err); return command.error(err);
} }
doc = ''; doc = '';
for (i = 0; i < response.length; i += 1) { for (i = 0; i < response.length; i += 1) {
doc += response[i]; doc += response[i].data;
} }
that.success(doc); that.success({"data": doc}); // XXX get mimetype
}); });
}; };
...@@ -506,20 +480,21 @@ ...@@ -506,20 +480,21 @@
* Removes a document from the sub storages. * Removes a document from the sub storages.
* *
* @method remove * @method remove
* @param {Command} command The JIO command * @param {Object} command The JIO command
*/ */
that.remove = function (command) { that.remove = function (command, param, option) {
priv.send( priv.send(
command,
'remove', 'remove',
command.cloneDoc(), param,
command.cloneOption(), option,
function (err, response_list) { function (err) {
if (err) { if (err) {
err.message = "Unable to remove document"; err.message = "Unable to remove document";
delete err.index; delete err.index;
return that.error(err); return command.error(err);
} }
that.success({"id": command.getDocId(), "ok": true}); command.success();
} }
); );
}; };
...@@ -528,25 +503,21 @@ ...@@ -528,25 +503,21 @@
* Removes an attachment from the sub storages. * Removes an attachment from the sub storages.
* *
* @method removeAttachment * @method removeAttachment
* @param {Command} command The JIO command * @param {Object} command The JIO command
*/ */
that.removeAttachment = function (command) { that.removeAttachment = function (command, param, option) {
var doc = command.cloneDoc();
priv.send( priv.send(
command,
'removeAttachment', 'removeAttachment',
doc, param,
command.cloneOption(), option,
function (err, response_list) { function (err) {
if (err) { if (err) {
err.message = "Unable to remove attachment"; err.message = "Unable to remove attachment";
delete err.index; delete err.index;
return that.error(err); return command.error(err);
} }
that.success({ command.success();
"id": doc._id,
"attachment": doc._attachment,
"ok": true
});
} }
); );
}; };
...@@ -558,31 +529,30 @@ ...@@ -558,31 +529,30 @@
* the first sub storage. Else, it will merge results and return. * the first sub storage. Else, it will merge results and return.
* *
* @method allDocs * @method allDocs
* @param {Command} command The JIO command * @param {Object} command The JIO command
*/ */
that.allDocs = function (command) { that.allDocs = function (command, param, option) {
var option = command.cloneOption();
option = {"include_docs": option.include_docs}; option = {"include_docs": option.include_docs};
priv.send( priv.send(
command,
'allDocs', 'allDocs',
command.cloneDoc(), param,
option, option,
function (err, response_list) { function (err, response_list) {
var all_docs_merger; var all_docs_merger;
if (err) { if (err) {
err.message = "Unable to retrieve document list"; err.message = "Unable to retrieve document list";
delete err.index; delete err.index;
return that.error(err); return command.error(err);
} }
all_docs_merger = new AllDocsResponseMerger(); all_docs_merger = new AllDocsResponseMerger();
all_docs_merger.addResponseList(response_list); all_docs_merger.addResponseList(response_list);
return that.success(all_docs_merger.merge(option)); return command.success({"data": all_docs_merger.merge(option)});
} }
); );
}; };
return that;
} // end of splitStorage } // end of splitStorage
jIO.addStorageType('split', splitStorage); jIO.addStorage('split', SplitStorage);
})); }));
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