Commit b0169a35 authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: add some prop

parent f083a2c5
/*jslint indent:2, maxlen: 80, nomen: true */ /*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory, /*global jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory,
Query*/ Query*/
(function (jIO, RSVP) { (function (jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory,
Query) {
"use strict"; "use strict";
function MappingStorage(spec) { function MappingStorage(spec) {
...@@ -18,6 +19,7 @@ ...@@ -18,6 +19,7 @@
this._default_mapping = {}; this._default_mapping = {};
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");
this._no_query_sub_id = spec.no_query_sub_id || false;
var property, query_list = []; var property, query_list = [];
// handle default_value. // handle default_value.
...@@ -52,20 +54,18 @@ ...@@ -52,20 +54,18 @@
function getAttachmentId(storage, sub_id, attachment_id, method) { function getAttachmentId(storage, sub_id, attachment_id, method) {
var mapping_dict = storage._attachment_mapping_dict; var mapping_dict = storage._attachment_mapping_dict;
return new RSVP.Queue()
.push(function () {
if (mapping_dict !== undefined if (mapping_dict !== undefined
&& mapping_dict[attachment_id] !== undefined && mapping_dict[attachment_id] !== undefined
&& mapping_dict[attachment_id][method] !== undefined
&& mapping_dict[attachment_id][method].uri_template !== undefined) { && mapping_dict[attachment_id][method].uri_template !== undefined) {
return UriTemplate.parse( return UriTemplate.parse(
mapping_dict[attachment_id][method].uri_template mapping_dict[attachment_id][method].uri_template
).expand({id: sub_id}); ).expand({id: sub_id});
} }
return attachment_id; return attachment_id;
});
} }
function getSubStorageId(storage, id) { function getSubStorageId(storage, id, sub_doc) {
var query; var query;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
...@@ -73,6 +73,17 @@ ...@@ -73,6 +73,17 @@
return id; return id;
} }
if (storage._mapping_dict.id.equal !== undefined) { if (storage._mapping_dict.id.equal !== undefined) {
if (JSON.stringify(storage._mapping_dict)
.indexOf('{"equal":"id"}') >= 0
&& sub_doc !== undefined) {
if (sub_doc.id !== undefined) {
return sub_doc.id;
}
}
if (storage._no_query_sub_id) {
throw new jIO.util.jIOError("can not find document cause no query sub id",
404);
}
query = new SimpleQuery({ query = new SimpleQuery({
key: storage._mapping_dict.id.equal, key: storage._mapping_dict.id.equal,
value: id, value: id,
...@@ -89,12 +100,55 @@ ...@@ -89,12 +100,55 @@
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,
"select_list": storage._query.select_list,
"limit": storage._query.limit "limit": storage._query.limit
})
.push(function (data) {
if (data.data.rows.length === 0 &&
storage._mapping_dict.id.default_property) {
query = new SimpleQuery({
key: storage._mapping_dict.id.default_property,
value: id,
type: "simple"
});
if (storage._query.query !== undefined) {
query = new ComplexQuery({
operator: "AND",
query_list: [query, storage._query.query],
type: "complex"
});
}
query = Query.objectToSearchText(query);
return storage._sub_storage.allDocs({
"query": query,
"select_list": [storage._mapping_dict.id.equal],
"sort_on": storage._query.sort_on,
"limit": storage._query.limit
})
.push(function (data) {
var i,
rows = [],
orig_rows = data.data.rows;
if (orig_rows.length > 1) {
for (i = 0; i < orig_rows.length; i += 1) {
if (!orig_rows[i].value[
storage._mapping_dict.id.equal
]) {
rows.push(orig_rows[i]);
}
}
data.data.rows = rows;
}
return data;
});
}
return data;
}) })
.push(function (data) { .push(function (data) {
if (data.data.rows.length === 0) { if (data.data.rows.length === 0) {
return undefined; throw new jIO.util.jIOError(
"Can not find id",
404
);
} }
if (data.data.rows.length > 1) { if (data.data.rows.length > 1) {
throw new TypeError("id must be unique field: " + id throw new TypeError("id must be unique field: " + id
...@@ -111,16 +165,21 @@ ...@@ -111,16 +165,21 @@
} }
function mapToSubProperty(storage, property, sub_doc, doc) { function mapToSubProperty(storage, property, sub_doc, doc) {
if (storage._mapping_dict[property] !== undefined) { var sub_property = storage._mapping_dict[property];
if (storage._mapping_dict[property].equal !== undefined) { if (sub_property !== undefined) {
sub_doc[storage._mapping_dict[property].equal] = doc[property]; if (sub_property.equal !== undefined) {
return storage._mapping_dict[property].equal; sub_doc[sub_property.equal] = doc[property];
} return sub_property.equal;
if (storage._mapping_dict[property].default_value !== undefined) { }
sub_doc[property] = storage._mapping_dict[property].default_value; if (sub_property.default_value !== undefined) {
sub_doc[property] = sub_property.default_value;
return property; return property;
} }
} }
if (!storage._map_all_property ||
storage._mapping_dict[property] === "ignore") {
return false;
}
if (storage._map_all_property) { if (storage._map_all_property) {
sub_doc[property] = doc[property]; sub_doc[property] = doc[property];
return property; return property;
...@@ -132,17 +191,28 @@ ...@@ -132,17 +191,28 @@
} }
function mapToMainProperty(storage, property, sub_doc, doc) { function mapToMainProperty(storage, property, sub_doc, doc) {
if (storage._mapping_dict[property] !== undefined) { var main_property = storage._mapping_dict[property];
if (storage._mapping_dict[property].equal !== undefined) { if (main_property !== undefined) {
if (sub_doc.hasOwnProperty(storage._mapping_dict[property].equal)) { if (main_property.equal !== undefined) {
doc[property] = sub_doc[storage._mapping_dict[property].equal]; if (sub_doc.hasOwnProperty(main_property.equal)) {
doc[property] = sub_doc[main_property.equal];
return main_property.equal;
} }
return storage._mapping_dict[property].equal; if (main_property.default_property &&
sub_doc.hasOwnProperty(main_property.default_property)) {
doc[property] = sub_doc[main_property.default_property];
return main_property.default_property;
} }
if (storage._mapping_dict[property].default_value !== undefined) { return main_property.equal;
}
if (main_property.default_value !== undefined) {
return property; return property;
} }
} }
if (!storage._map_all_property ||
storage._mapping_dict[property] === "ignore") {
return property;
}
if (storage._map_all_property) { if (storage._map_all_property) {
if (sub_doc.hasOwnProperty(property)) { if (sub_doc.hasOwnProperty(property)) {
doc[property] = sub_doc[property]; doc[property] = sub_doc[property];
...@@ -152,10 +222,13 @@ ...@@ -152,10 +222,13 @@
return false; return false;
} }
function mapToMainDocument(storage, sub_doc, delete_id_from_doc) { function mapToMainDocument(storage, sub_doc, sub_id, delete_id_from_doc) {
var doc = {}, var doc = {},
property, property,
property_list = []; property_list = [];
if (sub_id) {
sub_doc.id = sub_id;
}
for (property in storage._mapping_dict) { for (property in storage._mapping_dict) {
if (storage._mapping_dict.hasOwnProperty(property)) { if (storage._mapping_dict.hasOwnProperty(property)) {
property_list.push(mapToMainProperty(storage, property, sub_doc, doc)); property_list.push(mapToMainProperty(storage, property, sub_doc, doc));
...@@ -176,8 +249,9 @@ ...@@ -176,8 +249,9 @@
return doc; return doc;
} }
function mapToSubstorageDocument(storage, doc) { function mapToSubstorageDocument(storage, doc, id, delete_id_from_doc) {
var sub_doc = {}, property; var sub_doc = {}, property;
doc.id = id;
for (property in doc) { for (property in doc) {
if (doc.hasOwnProperty(property)) { if (doc.hasOwnProperty(property)) {
...@@ -189,28 +263,48 @@ ...@@ -189,28 +263,48 @@
sub_doc[property] = storage._default_mapping[property]; sub_doc[property] = storage._default_mapping[property];
} }
} }
if (delete_id_from_doc) {
delete sub_doc.id; delete sub_doc.id;
}
return sub_doc; return sub_doc;
} }
function handleAttachment(context, argument_list, method) {
return getSubStorageId(context, argument_list[0])
.push(function (sub_id) {
argument_list[0] = sub_id;
argument_list[1] = getAttachmentId(
context,
sub_id,
argument_list[1],
method
);
return context._sub_storage[method + "Attachment"].apply(
context._sub_storage,
argument_list
);
});
}
MappingStorage.prototype.get = function (id) { MappingStorage.prototype.get = function (id) {
var context = this; var context = this;
return getSubStorageId(this, id) return getSubStorageId(this, id)
.push(function (sub_id) { .push(function (sub_id) {
return context._sub_storage.get(sub_id); return context._sub_storage.get(sub_id)
})
.push(function (sub_doc) { .push(function (sub_doc) {
return mapToMainDocument(context, sub_doc, true); return mapToMainDocument(context, sub_doc, sub_id, true);
}) });
.push(undefined, function (error) {
throw new jIO.util.jIOError("Cannot find document " + id
+ ", cause: " + error.message, 404);
}); });
}; };
MappingStorage.prototype.post = function (doc) { MappingStorage.prototype.post = function (doc) {
if (!this._id_is_mapped) { if (!this._id_is_mapped) {
return this._sub_storage.post(mapToSubstorageDocument(this, doc)); return this._sub_storage.post(mapToSubstorageDocument(
this,
doc,
true,
true
));
} }
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"post is not supported with id mapped", "post is not supported with id mapped",
...@@ -220,19 +314,18 @@ ...@@ -220,19 +314,18 @@
MappingStorage.prototype.put = function (id, doc) { MappingStorage.prototype.put = function (id, doc) {
var context = this, var context = this,
sub_doc = mapToSubstorageDocument(this, doc); sub_doc = mapToSubstorageDocument(this, doc, id, false);
return getSubStorageId(this, id) return getSubStorageId(this, id, sub_doc)
.push(function (sub_id) { .push(function (sub_id) {
if (context._id_is_mapped) { delete sub_doc.id;
sub_doc[context._mapping_dict.id.equal] = id;
}
if (id === undefined) {
throw new Error();
}
return context._sub_storage.put(sub_id, sub_doc); return context._sub_storage.put(sub_id, sub_doc);
}) })
.push(undefined, function () { .push(undefined, function (error) {
if (error instanceof jIO.util.jIOError && error.status_code === 404) {
delete sub_doc.id;
return context._sub_storage.post(sub_doc); return context._sub_storage.post(sub_doc);
}
throw error;
}) })
.push(function () { .push(function () {
return id; return id;
...@@ -251,55 +344,55 @@ ...@@ -251,55 +344,55 @@
}; };
MappingStorage.prototype.putAttachment = function (id, attachment_id) { MappingStorage.prototype.putAttachment = function (id, attachment_id) {
var context = this, argument_list = arguments; return handleAttachment(this, arguments, "put", id)
return getSubStorageId(context, id)
.push(function (sub_id) {
argument_list[0] = sub_id;
return getAttachmentId(context, sub_id, attachment_id, "put");
})
.push(function (sub_attachment_id) {
argument_list[1] = sub_attachment_id;
return context._sub_storage.putAttachment.apply(context._sub_storage,
argument_list);
})
.push(function () { .push(function () {
return attachment_id; return attachment_id;
}); });
}; };
MappingStorage.prototype.getAttachment = function (id, attachment_id) { MappingStorage.prototype.getAttachment = function () {
var context = this, argument_list = arguments; return handleAttachment(this, arguments, "get");
return getSubStorageId(context, id)
.push(function (sub_id) {
argument_list[0] = sub_id;
return getAttachmentId(context, sub_id, attachment_id, "get");
})
.push(function (sub_attachment_id) {
argument_list[1] = sub_attachment_id;
return context._sub_storage.getAttachment.apply(context._sub_storage,
argument_list);
});
}; };
MappingStorage.prototype.removeAttachment = function (id, attachment_id) { MappingStorage.prototype.removeAttachment = function (id, attachment_id) {
var context = this, argument_list = arguments; return handleAttachment(this, arguments, "remove", id)
return getSubStorageId(context, id)
.push(function (sub_id) {
argument_list[0] = sub_id;
return getAttachmentId(context, sub_id, attachment_id, "remove");
})
.push(function (sub_attachment_id) {
argument_list[1] = sub_attachment_id;
return context._sub_storage.removeAttachment.apply(context._sub_storage,
argument_list);
})
.push(function () { .push(function () {
return attachment_id; return attachment_id;
}); });
}; };
MappingStorage.prototype.hasCapacity = function () { MappingStorage.prototype.allAttachments = function (id) {
return this._sub_storage.hasCapacity.apply(this._sub_storage, arguments); var context = this, sub_id;
return getSubStorageId(context, id)
.push(function (sub_id_result) {
sub_id = sub_id_result;
return context._sub_storage.allAttachments(sub_id);
})
.push(function (result) {
var attachment_id,
attachments = {},
mapping_dict = {};
for (attachment_id in context._attachment_mapping_dict) {
if (context._attachment_mapping_dict.hasOwnProperty(attachment_id)) {
mapping_dict[getAttachmentId(context, sub_id, attachment_id, "get")]
= attachment_id;
}
}
for (attachment_id in result) {
if (result.hasOwnProperty(attachment_id)) {
if (mapping_dict.hasOwnProperty(attachment_id)) {
attachments[mapping_dict[attachment_id]] = {};
} else {
attachments[attachment_id] = {};
}
}
}
return attachments;
});
};
MappingStorage.prototype.hasCapacity = function (name) {
return this._sub_storage.hasCapacity(name);
}; };
MappingStorage.prototype.repair = function () { MappingStorage.prototype.repair = function () {
...@@ -307,26 +400,32 @@ ...@@ -307,26 +400,32 @@
}; };
MappingStorage.prototype.bulk = function (id_list) { MappingStorage.prototype.bulk = function (id_list) {
var i, var context = this;
context = this,
mapped_result = [], function mapId(parameter) {
promise_list = id_list.map(function (parameter) {
return getSubStorageId(context, parameter.parameter_list[0]) return getSubStorageId(context, parameter.parameter_list[0])
.push(function (id) { .push(function (id) {
return {"method": parameter.method, "parameter_list": [id]}; return {"method": parameter.method, "parameter_list": [id]};
}); });
}); }
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
var promise_list = id_list.map(mapId);
return RSVP.all(promise_list); return RSVP.all(promise_list);
}) })
.push(function (id_list_mapped) { .push(function (id_list_mapped) {
return context._sub_storage.bulk(id_list_mapped); return context._sub_storage.bulk(id_list_mapped);
}) })
.push(function (result) { .push(function (result) {
var mapped_result = [], i;
for (i = 0; i < result.length; i += 1) { for (i = 0; i < result.length; i += 1) {
mapped_result.push(mapToMainDocument(context, result[i], false)); mapped_result.push(mapToMainDocument(
context,
result[i],
false,
true
));
} }
return mapped_result; return mapped_result;
}); });
...@@ -341,10 +440,10 @@ ...@@ -341,10 +440,10 @@
sort_on = []; sort_on = [];
function mapQuery(one_query) { function mapQuery(one_query) {
var i, query_list = []; var j, query_list = [];
if (one_query.type === "complex") { if (one_query.type === "complex") {
for (i = 0; i < one_query.query_list.length; i += 1) { for (j = 0; j < one_query.query_list.length; j += 1) {
query_list.push(mapQuery(one_query.query_list[i])); query_list.push(mapQuery(one_query.query_list[j]));
} }
one_query.query_list = query_list; one_query.query_list = query_list;
return one_query; return one_query;
...@@ -357,15 +456,15 @@ ...@@ -357,15 +456,15 @@
for (i = 0; i < option.sort_on.length; i += 1) { for (i = 0; i < option.sort_on.length; i += 1) {
property = mapToMainProperty(this, option.sort_on[i][0], {}, {}); property = mapToMainProperty(this, option.sort_on[i][0], {}, {});
if (property && sort_on.indexOf(property) < 0) { if (property && sort_on.indexOf(property) < 0) {
select_list.push([property, option.sort_on[i][1]]); sort_on.push([property, option.sort_on[i][1]]);
} }
} }
} }
if (this._query.sort_on !== undefined) { if (this._query.sort_on !== undefined) {
for (i = 0; i < this._query.sort_on.length; i += 1) { for (i = 0; i < this._query.sort_on.length; i += 1) {
property = this._query.sort_on[i]; property = mapToMainProperty(this, this._query.sort_on[i], {}, {});
if (sort_on.indexOf(property) < 0) { if (sort_on.indexOf(property) < 0) {
sort_on.push(property); sort_on.push([property, option.sort_on[i][1]]);
} }
} }
} }
...@@ -387,6 +486,9 @@ ...@@ -387,6 +486,9 @@
} }
if (this._id_is_mapped) { if (this._id_is_mapped) {
select_list.push(this._mapping_dict.id.equal); select_list.push(this._mapping_dict.id.equal);
if (this._mapping_dict.id.default_property) {
select_list.push(this._mapping_dict.id.default_property);
}
} }
if (option.query !== undefined) { if (option.query !== undefined) {
query = mapQuery(QueryFactory.create(option.query)); query = mapQuery(QueryFactory.create(option.query));
...@@ -417,16 +519,21 @@ ...@@ -417,16 +519,21 @@
.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 =
mapToMainDocument(context, result.data.rows[i].value, false); mapToMainDocument(
if (result.data.rows[i].id !== undefined && context._id_is_mapped) { context,
result.data.rows[i].value,
false,
false
);
if (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;
} }
return result.data.rows; return result.data.rows;
}); });
}; };
jIO.addStorage('mapping', MappingStorage); jIO.addStorage('mapping', MappingStorage);
}(jIO, RSVP)); }(jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory, Query));
\ No newline at end of file \ No newline at end of file
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