Commit f31d0c39 authored by Tristan Cavelier's avatar Tristan Cavelier

gidstorage.js upgraded for JIO v2

parent df53bc45
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
*/ */
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global define, jIO, setTimeout */ /*global define, jIO */
/** /**
* JIO GID Storage. Type = 'gid'. * JIO GID Storage. Type = 'gid'.
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
}(['jio'], function (jIO) { }(['jio'], function (jIO) {
"use strict"; "use strict";
var dcmi_types, metadata_actions, content_type_re; var dcmi_types, metadata_actions, content_type_re, tool;
dcmi_types = { dcmi_types = {
'Collection': 'Collection', 'Collection': 'Collection',
'Dataset': 'Dataset', 'Dataset': 'Dataset',
...@@ -163,8 +163,14 @@ ...@@ -163,8 +163,14 @@
} }
} }
}; };
content_type_re = content_type_re = new RegExp(
/^([a-z]+\/[a-zA-Z0-9\+\-\.]+)(?:\s*;\s*charset\s*=\s*([a-zA-Z0-9\-]+))?$/; '^([a-z]+\\/[a-zA-Z0-9\\+\\-\\.]+)' +
'((?:\\s*;\\s*[a-zA-Z\\+\\-\\.]+\\s*=' +
'\\s*[a-zA-Z0-9\\-\\+\\.,]+)*)$'
);
tool = {
"deepClone": jIO.util.deepClone
};
/** /**
* Creates a gid from metadata and constraints. * Creates a gid from metadata and constraints.
...@@ -213,10 +219,9 @@ ...@@ -213,10 +219,9 @@
* Convert a gid to a complex query. * Convert a gid to a complex query.
* *
* @param {Object,String} gid The gid * @param {Object,String} gid The gid
* @param {Object} constraints The constraints
* @return {Object} A complex serialized object * @return {Object} A complex serialized object
*/ */
function gidToComplexQuery(gid, contraints) { function gidToComplexQuery(gid) {
var k, i, result = [], meta, content; var k, i, result = [], meta, content;
if (typeof gid === 'string') { if (typeof gid === 'string') {
gid = JSON.parse(gid); gid = JSON.parse(gid);
...@@ -276,10 +281,10 @@ ...@@ -276,10 +281,10 @@
* to generate global ids can be define in the storage description. It allows * to generate global ids can be define in the storage description. It allows
* us use duplicating storage with different sub storage kind. * us use duplicating storage with different sub storage kind.
* *
* @class gidStorage * @class GidStorage
*/ */
function gidStorage(spec, my) { function GidStorage(spec) {
var that = my.basicStorage(spec, my), priv = {}; var that = this, priv = {};
priv.sub_storage = spec.sub_storage; priv.sub_storage = spec.sub_storage;
priv.constraints = spec.constraints || { priv.constraints = spec.constraints || {
...@@ -289,15 +294,6 @@ ...@@ -289,15 +294,6 @@
} }
}; };
// Overrides
that.specToStore = function () {
return {
"sub_storage": priv.sub_storage,
"constraints": priv.constraints
};
};
// JIO Commands // JIO Commands
/** /**
...@@ -313,54 +309,50 @@ ...@@ -313,54 +309,50 @@
* @param {Command} command The JIO command * @param {Command} command The JIO command
* @param {String} method The command method * @param {String} method The command method
*/ */
priv.putOrPost = function (command, method) { priv.putOrPost = function (command, metadata, method) {
setTimeout(function () { var gid, complex_query, doc = tool.deepClone(metadata);
var gid, complex_query, doc = command.cloneDoc(); gid = gidFormat(doc, priv.constraints);
gid = gidFormat(doc, priv.constraints); if (gid === undefined || (doc._id && gid !== doc._id)) {
if (gid === undefined || (doc._id && gid !== doc._id)) { return command.error(
return that.error({ "bad_request",
"status": 400, "metadata should respect constraints",
"statusText": "Bad Request", "Cannot " + method + " document"
"error": "bad_request", );
"message": "Cannot " + method + " document", }
"reason": "metadata should respect constraints" complex_query = gidToComplexQuery(gid);
}); command.storage(priv.sub_storage).allDocs({
} "query": complex_query,
complex_query = gidToComplexQuery(gid); "wildcard_character": null
that.addJob('allDocs', priv.sub_storage, {}, { }).then(function (response) {
"query": complex_query, var update_method = method;
"wildcard_character": null response = response.data;
}, function (response) { if (response.total_rows !== 0) {
var update_method = method; if (method === 'post') {
if (response.total_rows !== 0) { return command.error(
if (method === 'post') { "conflict",
return that.error({ "Document already exist",
"status": 409, "Cannot " + method + " document"
"statusText": "Conflict", );
"error": "conflict",
"message": "Cannot " + method + " document",
"reason": "Document already exist"
});
}
doc = command.cloneDoc();
doc._id = response.rows[0].id;
} else {
doc = command.cloneDoc();
delete doc._id;
update_method = 'post';
} }
that.addJob(update_method, priv.sub_storage, doc, { doc = tool.deepClone(metadata);
}, function (response) { doc._id = response.rows[0].id;
response.id = gid; } else {
that.success(response); doc = tool.deepClone(metadata);
}, function (err) { delete doc._id;
err.message = "Cannot " + method + " document"; update_method = 'post';
that.error(err); }
}); command.storage(priv.sub_storage)[update_method](
doc
).then(function (response) {
response.id = gid;
command.success(response);
}, function (err) { }, function (err) {
err.message = "Cannot " + method + " document"; err.message = "Cannot " + method + " document";
that.error(err); command.error(err);
}); });
}, function (err) {
err.message = "Cannot " + method + " document";
command.error(err);
}); });
}; };
...@@ -373,52 +365,49 @@ ...@@ -373,52 +365,49 @@
* *
* @method putGetOrRemoveAttachment * @method putGetOrRemoveAttachment
* @private * @private
* @param {Command} command The JIO command * @param {Object} command The JIO command
* @param {Object} doc The command parameters
* @param {String} method The command method * @param {String} method The command method
*/ */
priv.putGetOrRemoveAttachment = function (command, method) { priv.putGetOrRemoveAttachment = function (command, doc, method) {
setTimeout(function () { var gid_object, complex_query;
var gid_object, complex_query, doc = command.cloneDoc(); gid_object = gidParse(doc._id, priv.constraints);
gid_object = gidParse(doc._id, priv.constraints); if (gid_object === undefined) {
if (gid_object === undefined) { return command.error(
return that.error({ "bad_request",
"status": 400, "metadata should respect constraints",
"statusText": "Bad Request", "Cannot " + method + " attachment"
"error": "bad_request", );
"message": "Cannot " + method + " attachment", }
"reason": "metadata should respect constraints" complex_query = gidToComplexQuery(gid_object);
}); command.storage(priv.sub_storage).allDocs({
"query": complex_query,
"wildcard_character": null
}).then(function (response) {
response = response.data;
if (response.total_rows === 0) {
return command.error(
"not_found",
"Document already exist",
"Cannot " + method + " attachment"
);
} }
complex_query = gidToComplexQuery(gid_object); gid_object = doc._id;
that.addJob('allDocs', priv.sub_storage, {}, { doc._id = response.rows[0].id;
"query": complex_query, command.storage(priv.sub_storage)[method + "Attachment"](
"wildcard_character": null doc
}, function (response) { ).then(function (response) {
if (response.total_rows === 0) { if (method !== 'get') {
return that.error({ response.id = gid_object;
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot " + method + " attachment",
"reason": "Document already exist"
});
} }
gid_object = doc._id; command.success(response);
doc._id = response.rows[0].id;
that.addJob(method + "Attachment", priv.sub_storage, doc, {
}, function (response) {
if (method !== 'get') {
response.id = gid_object;
}
that.success(response);
}, function (err) {
err.message = "Cannot " + method + " attachment";
that.error(err);
});
}, function (err) { }, function (err) {
err.message = "Cannot " + method + " attachment"; err.message = "Cannot " + method + " attachment";
that.error(err); command.error(err);
}); });
}, function (err) {
err.message = "Cannot " + method + " attachment";
command.error(err);
}); });
}; };
...@@ -428,8 +417,8 @@ ...@@ -428,8 +417,8 @@
* @method post * @method post
* @param {Command} command The JIO command * @param {Command} command The JIO command
*/ */
that.post = function (command) { that.post = function (command, metadata) {
priv.putOrPost(command, 'post'); priv.putOrPost(command, metadata, 'post');
}; };
/** /**
...@@ -438,8 +427,8 @@ ...@@ -438,8 +427,8 @@
* @method put * @method put
* @param {Command} command The JIO command * @param {Command} command The JIO command
*/ */
that.put = function (command) { that.put = function (command, metadata) {
priv.putOrPost(command, 'put'); priv.putOrPost(command, metadata, 'put');
}; };
/** /**
...@@ -449,50 +438,45 @@ ...@@ -449,50 +438,45 @@
* @method putAttachment * @method putAttachment
* @param {Command} command The JIO command * @param {Command} command The JIO command
*/ */
that.putAttachment = function (command) { that.putAttachment = function (command, param) {
priv.putGetOrRemoveAttachment(command, 'put'); priv.putGetOrRemoveAttachment(command, param, 'put');
}; };
/** /**
* Gets a document thank to its gid, a sub allDocs and a complex query. * Gets a document thank to its gid, a sub allDocs and a complex query.
* *
* @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) {
setTimeout(function () { var gid_object, complex_query;
var gid_object, complex_query; gid_object = gidParse(param._id, priv.constraints);
gid_object = gidParse(command.getDocId(), priv.constraints); if (gid_object === undefined) {
if (gid_object === undefined) { return command.error(
return that.error({ "bad_request",
"status": 400, "metadata should respect constraints",
"statusText": "Bad Request", "Cannot get document"
"error": "bad_request", );
"message": "Cannot get document", }
"reason": "metadata should respect constraints" complex_query = gidToComplexQuery(gid_object);
}); command.storage(priv.sub_storage).allDocs({
"query": complex_query,
"wildcard_character": null,
"include_docs": true
}).then(function (response) {
response = response.data;
if (response.total_rows === 0) {
return command.error(
"not_found",
"missing",
"Cannot get document"
);
} }
complex_query = gidToComplexQuery(gid_object); response.rows[0].doc._id = param._id;
that.addJob('allDocs', priv.sub_storage, {}, { return command.success({"data": response.rows[0].doc});
"query": complex_query, }, function (err) {
"wildcard_character": null, err.message = "Cannot get document";
"include_docs": true return command.error(err);
}, function (response) {
if (response.total_rows === 0) {
return that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot get document",
"reason": "missing"
});
}
response.rows[0].doc._id = command.getDocId();
return that.success(response.rows[0].doc);
}, function (err) {
err.message = "Cannot get document";
return that.error(err);
});
}); });
}; };
...@@ -503,8 +487,8 @@ ...@@ -503,8 +487,8 @@
* @method getAttachment * @method getAttachment
* @param {Command} command The JIO command * @param {Command} command The JIO command
*/ */
that.getAttachment = function (command) { that.getAttachment = function (command, param) {
priv.putGetOrRemoveAttachment(command, 'get'); priv.putGetOrRemoveAttachment(command, param, 'get');
}; };
/** /**
...@@ -513,47 +497,43 @@ ...@@ -513,47 +497,43 @@
* @method remove * @method remove
* @param {Command} command The JIO command. * @param {Command} command The JIO command.
*/ */
that.remove = function (command) { that.remove = function (command, doc) {
setTimeout(function () { var gid_object, complex_query;
var gid_object, complex_query, doc = command.cloneDoc(); gid_object = gidParse(doc._id, priv.constraints);
gid_object = gidParse(doc._id, priv.constraints); if (gid_object === undefined) {
if (gid_object === undefined) { return command.error(
return that.error({ "bad_request",
"status": 400, "metadata should respect constraints",
"statusText": "Bad Request", "Cannot remove document"
"error": "bad_request", );
"message": "Cannot remove document", }
"reason": "metadata should respect constraints" complex_query = gidToComplexQuery(gid_object);
}); command.storage(priv.sub_storage).allDocs({
"query": complex_query,
"wildcard_character": null
}).then(function (response) {
response = response.data;
if (response.total_rows === 0) {
return command.error(
"not_found",
"missing",
"Cannot remove document"
);
} }
complex_query = gidToComplexQuery(gid_object); gid_object = doc._id;
that.addJob('allDocs', priv.sub_storage, {}, { doc = {"_id": response.rows[0].id};
"query": complex_query, command.storage(priv.sub_storage).remove(
"wildcard_character": null doc
}, function (response) { ).then(function (response) {
if (response.total_rows === 0) { response.id = gid_object;
return that.error({ command.success(response);
"status": 404,
"statusText": "Not found",
"error": "not_found",
"message": "Cannot remove document",
"reason": "missing"
});
}
gid_object = doc._id;
doc = {"_id": response.rows[0].id};
that.addJob('remove', priv.sub_storage, doc, {
}, function (response) {
response.id = gid_object;
that.success(response);
}, function (err) {
err.message = "Cannot remove document";
that.error(err);
});
}, function (err) { }, function (err) {
err.message = "Cannot remove document"; err.message = "Cannot remove document";
that.error(err); command.error(err);
}); });
}, function (err) {
err.message = "Cannot remove document";
command.error(err);
}); });
}; };
...@@ -564,8 +544,8 @@ ...@@ -564,8 +544,8 @@
* @method removeAttachment * @method removeAttachment
* @param {Command} command The JIO command * @param {Command} command The JIO command
*/ */
that.removeAttachment = function (command) { that.removeAttachment = function (command, param) {
priv.putGetOrRemoveAttachment(command, 'remove'); priv.putGetOrRemoveAttachment(command, param, 'remove');
}; };
/** /**
...@@ -574,42 +554,46 @@ ...@@ -574,42 +554,46 @@
* @method allDocs * @method allDocs
* @param {Command} command The JIO command * @param {Command} command The JIO command
*/ */
that.allDocs = function (command) { that.allDocs = function (command, param, options) {
setTimeout(function () { /*jslint unparam: true */
var options = command.cloneOption(), include_docs; var include_docs;
include_docs = options.include_docs; include_docs = options.include_docs;
options.include_docs = true; options.include_docs = true;
that.addJob('allDocs', priv.sub_storage, { command.storage(priv.sub_storage).allDocs(
}, options, function (response) { options
var result = [], doc_gids = {}, i, row, gid; ).then(function (response) {
while ((row = response.rows.shift()) !== undefined) { /*jslint ass: true */
if ((gid = gidFormat(row.doc, priv.constraints)) !== undefined) { var result = [], doc_gids = {}, row, gid;
if (!doc_gids[gid]) { response = response.data;
doc_gids[gid] = true; while ((row = response.rows.shift()) !== undefined) {
row.id = gid; gid = gidFormat(row.doc, priv.constraints);
delete row.key; if (gid !== undefined) {
result[result.length] = row; if (!doc_gids[gid]) {
if (include_docs === true) { doc_gids[gid] = true;
row.doc._id = gid; row.id = gid;
} else { delete row.key;
delete row.doc; result[result.length] = row;
} if (include_docs === true) {
row.doc._id = gid;
} else {
delete row.doc;
} }
} }
} }
doc_gids = undefined; // free memory }
row = undefined; doc_gids = undefined; // free memory
that.success({"total_rows": result.length, "rows": result}); row = undefined;
}, function (err) { command.success({"data": {
err.message = "Cannot get all documents"; "total_rows": result.length,
return that.error(err); "rows": result
}); }});
}, function (err) {
err.message = "Cannot get all documents";
return command.error(err);
}); });
}; };
return that;
} }
jIO.addStorageType('gid', gidStorage); jIO.addStorage('gid', GidStorage);
})); }));
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