Commit b0169a35 authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: add some prop

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