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 that.error({ return command.error(
"status": 400, "bad_request",
"statusText": "Bad Request", "metadata should respect constraints",
"error": "bad_request", "Cannot " + method + " document"
"message": "Cannot " + method + " document", );
"reason": "metadata should respect constraints"
});
} }
complex_query = gidToComplexQuery(gid); complex_query = gidToComplexQuery(gid);
that.addJob('allDocs', priv.sub_storage, {}, { command.storage(priv.sub_storage).allDocs({
"query": complex_query, "query": complex_query,
"wildcard_character": null "wildcard_character": null
}, function (response) { }).then(function (response) {
var update_method = method; var update_method = method;
response = response.data;
if (response.total_rows !== 0) { if (response.total_rows !== 0) {
if (method === 'post') { if (method === 'post') {
return that.error({ return command.error(
"status": 409, "conflict",
"statusText": "Conflict", "Document already exist",
"error": "conflict", "Cannot " + method + " document"
"message": "Cannot " + method + " document", );
"reason": "Document already exist"
});
} }
doc = command.cloneDoc(); doc = tool.deepClone(metadata);
doc._id = response.rows[0].id; doc._id = response.rows[0].id;
} else { } else {
doc = command.cloneDoc(); doc = tool.deepClone(metadata);
delete doc._id; delete doc._id;
update_method = 'post'; update_method = 'post';
} }
that.addJob(update_method, priv.sub_storage, doc, { command.storage(priv.sub_storage)[update_method](
}, function (response) { doc
).then(function (response) {
response.id = gid; response.id = gid;
that.success(response); 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) { }, function (err) {
err.message = "Cannot " + method + " document"; err.message = "Cannot " + method + " document";
that.error(err); 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 that.error({ return command.error(
"status": 400, "bad_request",
"statusText": "Bad Request", "metadata should respect constraints",
"error": "bad_request", "Cannot " + method + " attachment"
"message": "Cannot " + method + " attachment", );
"reason": "metadata should respect constraints"
});
} }
complex_query = gidToComplexQuery(gid_object); complex_query = gidToComplexQuery(gid_object);
that.addJob('allDocs', priv.sub_storage, {}, { command.storage(priv.sub_storage).allDocs({
"query": complex_query, "query": complex_query,
"wildcard_character": null "wildcard_character": null
}, function (response) { }).then(function (response) {
response = response.data;
if (response.total_rows === 0) { if (response.total_rows === 0) {
return that.error({ return command.error(
"status": 404, "not_found",
"statusText": "Not Found", "Document already exist",
"error": "not_found", "Cannot " + method + " attachment"
"message": "Cannot " + method + " attachment", );
"reason": "Document already exist"
});
} }
gid_object = doc._id; gid_object = doc._id;
doc._id = response.rows[0].id; doc._id = response.rows[0].id;
that.addJob(method + "Attachment", priv.sub_storage, doc, { command.storage(priv.sub_storage)[method + "Attachment"](
}, function (response) { doc
).then(function (response) {
if (method !== 'get') { if (method !== 'get') {
response.id = gid_object; response.id = gid_object;
} }
that.success(response); command.success(response);
}, function (err) { }, function (err) {
err.message = "Cannot " + method + " attachment"; err.message = "Cannot " + method + " attachment";
that.error(err); command.error(err);
}); });
}, function (err) { }, function (err) {
err.message = "Cannot " + method + " attachment"; err.message = "Cannot " + method + " attachment";
that.error(err); 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(command.getDocId(), priv.constraints); gid_object = gidParse(param._id, priv.constraints);
if (gid_object === undefined) { if (gid_object === undefined) {
return that.error({ return command.error(
"status": 400, "bad_request",
"statusText": "Bad Request", "metadata should respect constraints",
"error": "bad_request", "Cannot get document"
"message": "Cannot get document", );
"reason": "metadata should respect constraints"
});
} }
complex_query = gidToComplexQuery(gid_object); complex_query = gidToComplexQuery(gid_object);
that.addJob('allDocs', priv.sub_storage, {}, { command.storage(priv.sub_storage).allDocs({
"query": complex_query, "query": complex_query,
"wildcard_character": null, "wildcard_character": null,
"include_docs": true "include_docs": true
}, function (response) { }).then(function (response) {
response = response.data;
if (response.total_rows === 0) { if (response.total_rows === 0) {
return that.error({ return command.error(
"status": 404, "not_found",
"statusText": "Not Found", "missing",
"error": "not_found", "Cannot get document"
"message": "Cannot get document", );
"reason": "missing" }
}); response.rows[0].doc._id = param._id;
} return command.success({"data": response.rows[0].doc});
response.rows[0].doc._id = command.getDocId();
return that.success(response.rows[0].doc);
}, function (err) { }, function (err) {
err.message = "Cannot get document"; err.message = "Cannot get document";
return that.error(err); return command.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 that.error({ return command.error(
"status": 400, "bad_request",
"statusText": "Bad Request", "metadata should respect constraints",
"error": "bad_request", "Cannot remove document"
"message": "Cannot remove document", );
"reason": "metadata should respect constraints"
});
} }
complex_query = gidToComplexQuery(gid_object); complex_query = gidToComplexQuery(gid_object);
that.addJob('allDocs', priv.sub_storage, {}, { command.storage(priv.sub_storage).allDocs({
"query": complex_query, "query": complex_query,
"wildcard_character": null "wildcard_character": null
}, function (response) { }).then(function (response) {
response = response.data;
if (response.total_rows === 0) { if (response.total_rows === 0) {
return that.error({ return command.error(
"status": 404, "not_found",
"statusText": "Not found", "missing",
"error": "not_found", "Cannot remove document"
"message": "Cannot remove document", );
"reason": "missing"
});
} }
gid_object = doc._id; gid_object = doc._id;
doc = {"_id": response.rows[0].id}; doc = {"_id": response.rows[0].id};
that.addJob('remove', priv.sub_storage, doc, { command.storage(priv.sub_storage).remove(
}, function (response) { doc
).then(function (response) {
response.id = gid_object; response.id = gid_object;
that.success(response); command.success(response);
}, function (err) { }, function (err) {
err.message = "Cannot remove document"; err.message = "Cannot remove document";
that.error(err); command.error(err);
}); });
}, function (err) { }, function (err) {
err.message = "Cannot remove document"; err.message = "Cannot remove document";
that.error(err); 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,16 +554,20 @@ ...@@ -574,16 +554,20 @@
* @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) {
/*jslint ass: true */
var result = [], doc_gids = {}, row, gid;
response = response.data;
while ((row = response.rows.shift()) !== undefined) { while ((row = response.rows.shift()) !== undefined) {
if ((gid = gidFormat(row.doc, priv.constraints)) !== undefined) { gid = gidFormat(row.doc, priv.constraints);
if (gid !== undefined) {
if (!doc_gids[gid]) { if (!doc_gids[gid]) {
doc_gids[gid] = true; doc_gids[gid] = true;
row.id = gid; row.id = gid;
...@@ -599,17 +583,17 @@ ...@@ -599,17 +583,17 @@
} }
doc_gids = undefined; // free memory doc_gids = undefined; // free memory
row = undefined; row = undefined;
that.success({"total_rows": result.length, "rows": result}); command.success({"data": {
"total_rows": result.length,
"rows": result
}});
}, function (err) { }, function (err) {
err.message = "Cannot get all documents"; err.message = "Cannot get all documents";
return that.error(err); 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