Commit 09cc6aec authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: upgrade code quality

parent d4671ec4
/*jslint indent:2, maxlen: 80, nomen: true */ /*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO, RSVP, UriTemplate */ /*global jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory,
Query*/
(function (jIO, RSVP) { (function (jIO, RSVP) {
"use strict"; "use strict";
...@@ -10,6 +11,9 @@ ...@@ -10,6 +11,9 @@
this._mapping_dict_attachment = spec.mapping_dict_attachment || {}; this._mapping_dict_attachment = spec.mapping_dict_attachment || {};
this._query = spec.query || {}; this._query = spec.query || {};
if (this._query.query !== undefined) {
this._query.query = QueryFactory.create(this._query.query);
}
this._id_is_mapped = (this._mapping_dict.id !== undefined this._id_is_mapped = (this._mapping_dict.id !== undefined
&& this._mapping_dict.id.equal !== "id"); && this._mapping_dict.id.equal !== "id");
var property, query_list = []; var property, query_list = [];
...@@ -65,10 +69,19 @@ ...@@ -65,10 +69,19 @@
return id; return id;
} }
if (storage._mapping_dict.id.equal !== undefined) { if (storage._mapping_dict.id.equal !== undefined) {
query = storage._mapping_dict.id.equal + ': "' + id + '"'; query = new SimpleQuery({
key: storage._mapping_dict.id.equal,
value: id,
type: "simple"
});
if (storage._query.query !== undefined) { if (storage._query.query !== undefined) {
query += ' AND ' + storage._query.query; query = new ComplexQuery({
operator: "AND",
query_list: [query, storage._query.query],
type: "complex"
});
} }
query = Query.objectToSearchText(query);
return storage._sub_storage.allDocs({ return storage._sub_storage.allDocs({
"query": query, "query": query,
"sort_on": storage._query.sort_on, "sort_on": storage._query.sort_on,
...@@ -129,7 +142,7 @@ ...@@ -129,7 +142,7 @@
return false; return false;
} }
function mapDocument(storage, doc, delete_id) { function mapDocument(storage, doc, id_delete) {
var mapped_doc = {}, var mapped_doc = {},
property, property,
property_list = []; property_list = [];
...@@ -147,7 +160,7 @@ ...@@ -147,7 +160,7 @@
} }
} }
} }
if (delete_id) { if (id_delete) {
delete mapped_doc.id; delete mapped_doc.id;
} }
return mapped_doc; return mapped_doc;
...@@ -174,13 +187,13 @@ ...@@ -174,13 +187,13 @@
} }
MappingStorage.prototype.get = function (id) { MappingStorage.prototype.get = function (id) {
var that = this; var context = this;
return getSubStorageId(this, id) return getSubStorageId(this, id)
.push(function (id_mapped) { .push(function (sub_id) {
return that._sub_storage.get(id_mapped); return context._sub_storage.get(sub_id);
}) })
.push(function (doc) { .push(function (doc) {
return mapDocument(that, doc, true); return mapDocument(context, doc, true);
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
throw new jIO.util.jIOError("Cannot find document " + id throw new jIO.util.jIOError("Cannot find document " + id
...@@ -198,20 +211,20 @@ ...@@ -198,20 +211,20 @@
}; };
MappingStorage.prototype.put = function (id, doc) { MappingStorage.prototype.put = function (id, doc) {
var that = this, var context = this,
mapped_doc = unmapDocument(this, doc); mapped_doc = unmapDocument(this, doc);
return getSubStorageId(this, id) return getSubStorageId(this, id)
.push(function (id_mapped) { .push(function (sub_id) {
if (that._id_is_mapped) { if (context._id_is_mapped) {
mapped_doc[that._mapping_dict.id.equal] = id; mapped_doc[context._mapping_dict.id.equal] = id;
} }
if (id === undefined) { if (id === undefined) {
throw new Error(); throw new Error();
} }
return that._sub_storage.put(id_mapped, mapped_doc); return context._sub_storage.put(sub_id, mapped_doc);
}) })
.push(undefined, function () { .push(undefined, function () {
return that._sub_storage.post(mapped_doc); return context._sub_storage.post(mapped_doc);
}) })
.push(function () { .push(function () {
return id; return id;
...@@ -219,10 +232,10 @@ ...@@ -219,10 +232,10 @@
}; };
MappingStorage.prototype.remove = function (id) { MappingStorage.prototype.remove = function (id) {
var that = this; var context = this;
return getSubStorageId(this, id) return getSubStorageId(this, id)
.push(function (id_mapped) { .push(function (sub_id) {
return that._sub_storage.remove(id_mapped); return context._sub_storage.remove(sub_id);
}) })
.push(function () { .push(function () {
return id; return id;
...@@ -281,20 +294,11 @@ ...@@ -281,20 +294,11 @@
MappingStorage.prototype.bulk = function (id_list) { MappingStorage.prototype.bulk = function (id_list) {
var i, var i,
that = this, context = this,
mapped_result = [], mapped_result = [],
promise_list = id_list.map(function (parameter) { promise_list = id_list.map(function (parameter) {
return getSubStorageId(that, parameter.parameter_list[0]) return getSubStorageId(context, parameter.parameter_list[0])
.push(function (id) { .push(function (id) {
if (parameter.method === "put") {
return {
"method": parameter.method,
"parameter_list": [
id,
unmapDocument(parameter.parameter_list[1])
]
};
}
return {"method": parameter.method, "parameter_list": [id]}; return {"method": parameter.method, "parameter_list": [id]};
}); });
}); });
...@@ -304,18 +308,18 @@ ...@@ -304,18 +308,18 @@
return RSVP.all(promise_list); return RSVP.all(promise_list);
}) })
.push(function (id_list_mapped) { .push(function (id_list_mapped) {
return that._sub_storage.bulk(id_list_mapped); return context._sub_storage.bulk(id_list_mapped);
}) })
.push(function (result) { .push(function (result) {
for (i = 0; i < result.length; i += 1) { for (i = 0; i < result.length; i += 1) {
mapped_result.push(mapDocument(that, result[i], false)); mapped_result.push(mapDocument(context, result[i], false));
} }
return mapped_result; return mapped_result;
}); });
}; };
MappingStorage.prototype.buildQuery = function (option) { MappingStorage.prototype.buildQuery = function (option) {
var that = this, var context = this,
i, i,
query, query,
property, property,
...@@ -323,27 +327,16 @@ ...@@ -323,27 +327,16 @@
sort_on = []; sort_on = [];
function mapQuery(one_query) { function mapQuery(one_query) {
var i, result = "(", key; var i, query_list = [];
if (one_query.type === "complex") { if (one_query.type === "complex") {
for (i = 0; i < one_query.query_list.length; i += 1) { for (i = 0; i < one_query.query_list.length; i += 1) {
result += "(" + mapQuery(one_query.query_list[i]) + ")"; query_list.push(mapQuery(one_query.query_list[i]));
if (i < one_query.query_list.length - 1) {
result += " " + one_query.operator + " ";
}
}
result += ")";
return result;
}
if (that._mapping_dict.hasOwnProperty(one_query.key)) {
key = that._mapping_dict[one_query.key].equal;
} else {
if (that._map_all_property) {
key = one_query.key;
} }
one_query.query_list = query_list;
return one_query;
} }
return (key ? key + ":" : "") + one_query.key = mapProperty(context, one_query.key, {}, {});
(one_query.operator ? " " + one_query.operator : "") + return one_query;
' "' + one_query.value + '"';
} }
if (option.sort_on !== undefined) { if (option.sort_on !== undefined) {
...@@ -382,16 +375,22 @@ ...@@ -382,16 +375,22 @@
select_list.push(this._mapping_dict.id.equal); select_list.push(this._mapping_dict.id.equal);
} }
if (option.query !== undefined) { if (option.query !== undefined) {
query = mapQuery(jIO.QueryFactory.create(option.query)); query = mapQuery(QueryFactory.create(option.query));
} }
if (this._query.query !== undefined) { if (this._query.query !== undefined) {
if (query !== undefined) { if (query === undefined) {
query += ' AND '; query = this._query.query;
} else {
query = "";
} }
query += this._query.query; query = new ComplexQuery({
operator: "AND",
query_list: [query, this._query.query],
type: "complex"
});
}
if (query !== undefined) {
query = Query.objectToSearchText(query);
} }
return this._sub_storage.allDocs( return this._sub_storage.allDocs(
{ {
...@@ -404,8 +403,8 @@ ...@@ -404,8 +403,8 @@
.push(function (result) { .push(function (result) {
for (i = 0; i < result.data.total_rows; i += 1) { for (i = 0; i < result.data.total_rows; i += 1) {
result.data.rows[i].value = result.data.rows[i].value =
mapDocument(that, result.data.rows[i].value, false); mapDocument(context, result.data.rows[i].value, false);
if (result.data.rows[i].id !== undefined && that._id_is_mapped) { if (result.data.rows[i].id !== undefined && context._id_is_mapped) {
result.data.rows[i].id = result.data.rows[i].id =
result.data.rows[i].value.id; result.data.rows[i].value.id;
delete result.data.rows[i].value.id; delete result.data.rows[i].value.id;
......
...@@ -48,12 +48,14 @@ ...@@ -48,12 +48,14 @@
}, },
mapping_dict: { "bar": {"equal": "foo"}}, mapping_dict: { "bar": {"equal": "foo"}},
map_all_property: true, map_all_property: true,
query: {"query": "foo"}, query: {"query": 'foo: "bar"'},
mapping_dict_attachment: {"foo": {"get": "bar"}} mapping_dict_attachment: {"foo": {"get": "bar"}}
}); });
deepEqual(jio.__storage._mapping_dict, {"bar": {"equal": "foo"}}); deepEqual(jio.__storage._mapping_dict, {"bar": {"equal": "foo"}});
deepEqual(jio.__storage._query, {"query": "foo"}); equal(jio.__storage._query.query.key, "foo");
equal(jio.__storage._query.query.value, "bar");
equal(jio.__storage._query.query.type, "simple");
deepEqual(jio.__storage._mapping_dict_attachment, {"foo": {"get": "bar"}}); deepEqual(jio.__storage._mapping_dict_attachment, {"foo": {"get": "bar"}});
equal(jio.__storage._map_all_property, true); equal(jio.__storage._map_all_property, true);
}); });
...@@ -144,7 +146,7 @@ ...@@ -144,7 +146,7 @@
}; };
Storage2713.prototype.buildQuery = function (options) { Storage2713.prototype.buildQuery = function (options) {
equal(options.query, 'otherId: "42"', "allDoc 2713 called"); equal(options.query, 'otherId: "42"', "allDoc 2713 called");
return [{id: "2713"}]; return [{id: "2713"}];
}; };
...@@ -189,7 +191,7 @@ ...@@ -189,7 +191,7 @@
Storage2713.prototype.buildQuery = function (options) { Storage2713.prototype.buildQuery = function (options) {
equal( equal(
options.query, options.query,
'otherId: "42" AND otherTitle: "foo"', '( otherId: "42" AND otherTitle: "foo" )',
"allDoc 2713 called" "allDoc 2713 called"
); );
return [{id: "2713"}]; return [{id: "2713"}];
...@@ -235,7 +237,7 @@ ...@@ -235,7 +237,7 @@
Storage2713.prototype.buildQuery = function (options) { Storage2713.prototype.buildQuery = function (options) {
equal( equal(
options.query, options.query,
'otherId: "42"', 'otherId: "42"',
"allDoc 2713 called" "allDoc 2713 called"
); );
return [{id: "2713"}]; return [{id: "2713"}];
...@@ -348,7 +350,7 @@ ...@@ -348,7 +350,7 @@
}; };
Storage2713.prototype.buildQuery = function (option) { Storage2713.prototype.buildQuery = function (option) {
equal(option.query, 'otherId: "42"', "allDocs 2713 called"); equal(option.query, 'otherId: "42"', "allDocs 2713 called");
return []; return [];
}; };
...@@ -446,7 +448,7 @@ ...@@ -446,7 +448,7 @@
}; };
Storage2713.prototype.buildQuery = function (options) { Storage2713.prototype.buildQuery = function (options) {
equal(options.query, 'otherId: "42"', "allDoc 2713 called"); equal(options.query, 'otherId: "42"', "allDoc 2713 called");
return [{id: "2713"}]; return [{id: "2713"}];
}; };
...@@ -905,10 +907,10 @@ ...@@ -905,10 +907,10 @@
}; };
Storage2713.prototype.buildQuery = function (option) { Storage2713.prototype.buildQuery = function (option) {
if (option.query === 'otherId: "id1"') { if (option.query === 'otherId: "id1"') {
return [{id: "foo"}]; return [{id: "foo"}];
} }
if (option.query === 'otherId: "id2"') { if (option.query === 'otherId: "id2"') {
return [{id: "bar"}]; return [{id: "bar"}];
} }
throw new Error("invalid option:" + option.toString()); throw new Error("invalid option:" + option.toString());
......
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