Commit 6db8d597 authored by Aurel's avatar Aurel

Revert "use working version of mapping"

Come back to the lastet version, we will update the storage config
This reverts commit 450b591f.
parent 9729722a
...@@ -5,6 +5,141 @@ ...@@ -5,6 +5,141 @@
Query) { Query) {
"use strict"; "use strict";
function getSubIdEqualSubProperty(storage, value, key) {
var query;
if (storage._no_sub_query_id) {
throw new jIO.util.jIOError('no sub query id active', 404);
}
query = new SimpleQuery({
key: key,
value: value,
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,
"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) {
throw new jIO.util.jIOError(
"Can not find id",
404
);
}
if (data.data.rows.length > 1) {
throw new TypeError("id must be unique field: " + key
+ ", result:" + data.data.rows.toString());
}
return data.data.rows[0].id;
});
}
/*jslint unparam: true*/
var mapping_function = {
"equalSubProperty": {
"mapToSubProperty": function (property, sub_doc, doc, args, id) {
sub_doc[args] = doc[property];
return args;
},
"mapToMainProperty": function (property, sub_doc, doc, args, sub_id) {
if (sub_doc.hasOwnProperty(args)) {
doc[property] = sub_doc[args];
}
return args;
},
"mapToSubId": function (storage, doc, id, args) {
if (doc !== undefined) {
if (storage._property_for_sub_id &&
doc.hasOwnProperty(storage._property_for_sub_id)) {
return doc[storage._property_for_sub_id];
}
if (doc.hasOwnProperty(args)) {
return doc[args];
}
}
return getSubIdEqualSubProperty(storage, id, storage._map_id[1]);
},
"mapToId": function (storage, sub_doc, sub_id, args) {
return sub_doc[args];
}
},
"equalValue": {
"mapToSubProperty": function (property, sub_doc, doc, args) {
sub_doc[property] = args;
return property;
},
"mapToMainProperty": function (property) {
return property;
}
},
"ignore": {
"mapToSubProperty": function () {
return false;
},
"mapToMainProperty": function (property) {
return property;
}
},
"equalSubId": {
"mapToSubProperty": function (property, sub_doc, doc) {
sub_doc[property] = doc[property];
return property;
},
"mapToMainProperty": function (property, sub_doc, doc, args, sub_id) {
if (sub_id === undefined && sub_doc.hasOwnProperty(property)) {
doc[property] = sub_doc[property];
} else {
doc[property] = sub_id;
}
return property;
},
"mapToSubId": function (storage, doc, id, args) {
return id;
},
"mapToId": function (storage, sub_doc, sub_id) {
return sub_id;
}
},
"keep": {
"mapToSubProperty": function (property, sub_doc, doc) {
sub_doc[property] = doc[property];
return property;
},
"mapToMainProperty": function (property, sub_doc, doc) {
doc[property] = sub_doc[property];
return property;
}
},
"switchPropertyValue": {
"mapToSubProperty": function (property, sub_doc, doc, args) {
sub_doc[args[0]] = args[1][doc[property]];
return args[0];
},
"mapToMainProperty": function (property, sub_doc, doc, args) {
var subvalue, value = sub_doc[args[0]];
for (subvalue in args[1]) {
if (args[1].hasOwnProperty(subvalue)) {
if (value === args[1][subvalue]) {
doc[property] = subvalue;
return property;
}
}
}
}
}
};
/*jslint unparam: false*/
function initializeQueryAndDefaultMapping(storage) { function initializeQueryAndDefaultMapping(storage) {
var property, query_list = []; var property, query_list = [];
for (property in storage._mapping_dict) { for (property in storage._mapping_dict) {
...@@ -47,14 +182,15 @@ ...@@ -47,14 +182,15 @@
} }
function MappingStorage(spec) { function MappingStorage(spec) {
this._mapping_dict = spec.mapping_dict || {}; this._mapping_dict = spec.property || {};
this._sub_storage = jIO.createJIO(spec.sub_storage); this._sub_storage = jIO.createJIO(spec.sub_storage);
this._map_all_property = spec.map_all_property !== undefined ? this._map_all_property = spec.map_all_property !== undefined ?
spec.map_all_property : true; spec.map_all_property : true;
this._attachment_mapping_dict = spec.attachment_mapping_dict || {}; this._no_sub_query_id = spec.no_sub_query_id;
this._attachment_mapping_dict = spec.attachment || {};
this._query = spec.query || {}; this._query = spec.query || {};
this._map_id = spec.map_id; this._map_id = spec.id || ["equalSubId"];
this._id_mapped = (spec.map_id !== undefined) ? spec.map_id[1] : false; this._id_mapped = (spec.id !== undefined) ? spec.id[1] : false;
if (this._query.query !== undefined) { if (this._query.query !== undefined) {
this._query.query = QueryFactory.create(this._query.query); this._query.query = QueryFactory.create(this._query.query);
...@@ -78,113 +214,42 @@ ...@@ -78,113 +214,42 @@
} }
function getSubStorageId(storage, id, doc) { function getSubStorageId(storage, id, doc) {
var query;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
if (storage._property_for_sub_id !== undefined && var map_info = storage._map_id || ["equalSubId"];
doc !== undefined && if (storage._property_for_sub_id && doc !== undefined &&
doc[storage._property_for_sub_id] !== undefined) { doc.hasOwnProperty(storage._property_for_sub_id)) {
return doc[storage._property_for_sub_id]; return doc[storage._property_for_sub_id];
} }
if (!storage._id_mapped) { return mapping_function[map_info[0]].mapToSubId(
return id; storage,
} doc,
if (storage._map_id[0] === "equalSubProperty") { id,
query = new SimpleQuery({ map_info[1]
key: storage._map_id[1],
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,
"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) {
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
+ ", result:" + data.data.rows.toString());
}
return data.data.rows[0].id;
});
}
throw new jIO.util.jIOError(
"Unsuported option: " + storage._mapping_dict.id,
400
); );
}); });
} }
function mapToSubProperty(storage, property, sub_doc, doc) { function mapToSubProperty(storage, property, sub_doc, doc, id) {
var mapping_function, parameter; var mapping_info = storage._mapping_dict[property] || ["keep"];
if (storage._mapping_dict[property] !== undefined) { return mapping_function[mapping_info[0]].mapToSubProperty(
mapping_function = storage._mapping_dict[property][0]; property,
parameter = storage._mapping_dict[property][1]; sub_doc,
if (mapping_function === "equalSubProperty") { doc,
sub_doc[parameter] = doc[property]; mapping_info[1],
return parameter; id
}
if (mapping_function === "equalValue") {
sub_doc[property] = parameter;
return property;
}
if (mapping_function === "ignore" || mapping_function === "equalSubId") {
return false;
}
}
if (!storage._map_all_property) {
return false;
}
if (storage._map_all_property) {
sub_doc[property] = doc[property];
return property;
}
throw new jIO.util.jIOError(
"Unsuported option(s): " + storage._mapping_dict[property],
400
); );
} }
function mapToMainProperty(storage, property, sub_doc, doc) { function mapToMainProperty(storage, property, sub_doc, doc, sub_id) {
var mapping_function, parameter; var mapping_info = storage._mapping_dict[property] || ["keep"];
if (storage._mapping_dict[property] !== undefined) { return mapping_function[mapping_info[0]].mapToMainProperty(
mapping_function = storage._mapping_dict[property][0]; property,
parameter = storage._mapping_dict[property][1]; sub_doc,
if (mapping_function === "equalSubProperty") { doc,
if (sub_doc.hasOwnProperty(parameter)) { mapping_info[1],
doc[property] = sub_doc[parameter]; sub_id
} );
return parameter;
}
if (mapping_function === "equalValue") {
return property;
}
if (mapping_function === "ignore") {
return property;
}
}
if (storage._map_all_property) {
if (sub_doc.hasOwnProperty(property)) {
doc[property] = sub_doc[property];
}
return property;
}
return false;
} }
function mapToMainDocument(storage, sub_doc, sub_id) { function mapToMainDocument(storage, sub_doc, sub_id) {
...@@ -193,7 +258,13 @@ ...@@ -193,7 +258,13 @@
property_list = [storage._id_mapped]; property_list = [storage._id_mapped];
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,
sub_id
));
} }
} }
if (storage._map_all_property) { if (storage._map_all_property) {
...@@ -205,9 +276,8 @@ ...@@ -205,9 +276,8 @@
} }
} }
} }
if (storage._property_for_sub_id !== undefined && if (storage._map_for_sub_storage_id !== undefined) {
sub_id !== undefined) { doc[storage._map_for_sub_storage_id] = sub_id;
doc[storage._property_for_sub_id] = sub_id;
} }
return doc; return doc;
} }
...@@ -217,7 +287,7 @@ ...@@ -217,7 +287,7 @@
for (property in doc) { for (property in doc) {
if (doc.hasOwnProperty(property)) { if (doc.hasOwnProperty(property)) {
mapToSubProperty(storage, property, sub_doc, doc); mapToSubProperty(storage, property, sub_doc, doc, id);
} }
} }
for (property in storage._default_mapping) { for (property in storage._default_mapping) {
...@@ -225,36 +295,36 @@ ...@@ -225,36 +295,36 @@
sub_doc[property] = storage._default_mapping[property]; sub_doc[property] = storage._default_mapping[property];
} }
} }
if (storage._id_mapped && id !== undefined) { if (storage._map_id[0] === "equalSubProperty" && id !== undefined) {
sub_doc[storage._id_mapped] = id; sub_doc[storage._map_id[1]] = id;
} }
return sub_doc; return sub_doc;
} }
function handleAttachment(context, argument_list, method) { function handleAttachment(storage, argument_list, method) {
return getSubStorageId(context, argument_list[0]) return getSubStorageId(storage, argument_list[0])
.push(function (sub_id) { .push(function (sub_id) {
argument_list[0] = sub_id; argument_list[0] = sub_id;
argument_list[1] = getAttachmentId( argument_list[1] = getAttachmentId(
context, storage,
sub_id, sub_id,
argument_list[1], argument_list[1],
method method
); );
return context._sub_storage[method + "Attachment"].apply( return storage._sub_storage[method + "Attachment"].apply(
context._sub_storage, storage._sub_storage,
argument_list argument_list
); );
}); });
} }
MappingStorage.prototype.get = function (id) { MappingStorage.prototype.get = function (id) {
var context = this; var storage = 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 storage._sub_storage.get(sub_id)
.push(function (sub_doc) { .push(function (sub_doc) {
return mapToMainDocument(context, sub_doc, sub_id); return mapToMainDocument(storage, sub_doc, sub_id);
}); });
}); });
}; };
...@@ -278,15 +348,15 @@ ...@@ -278,15 +348,15 @@
}; };
MappingStorage.prototype.put = function (id, doc) { MappingStorage.prototype.put = function (id, doc) {
var context = this, var storage = this,
sub_doc = mapToSubstorageDocument(this, doc, id); sub_doc = mapToSubstorageDocument(this, doc, id);
return getSubStorageId(this, id, doc) return getSubStorageId(this, id, doc)
.push(function (sub_id) { .push(function (sub_id) {
return context._sub_storage.put(sub_id, sub_doc); return storage._sub_storage.put(sub_id, sub_doc);
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
if (error instanceof jIO.util.jIOError && error.status_code === 404) { if (error instanceof jIO.util.jIOError && error.status_code === 404) {
return context._sub_storage.post(sub_doc); return storage._sub_storage.post(sub_doc);
} }
throw error; throw error;
}) })
...@@ -296,10 +366,10 @@ ...@@ -296,10 +366,10 @@
}; };
MappingStorage.prototype.remove = function (id) { MappingStorage.prototype.remove = function (id) {
var context = this; var storage = this;
return getSubStorageId(this, id) return getSubStorageId(this, id)
.push(function (sub_id) { .push(function (sub_id) {
return context._sub_storage.remove(sub_id); return storage._sub_storage.remove(sub_id);
}) })
.push(function () { .push(function () {
return id; return id;
...@@ -325,19 +395,19 @@ ...@@ -325,19 +395,19 @@
}; };
MappingStorage.prototype.allAttachments = function (id) { MappingStorage.prototype.allAttachments = function (id) {
var context = this, sub_id; var storage = this, sub_id;
return getSubStorageId(context, id) return getSubStorageId(storage, id)
.push(function (sub_id_result) { .push(function (sub_id_result) {
sub_id = sub_id_result; sub_id = sub_id_result;
return context._sub_storage.allAttachments(sub_id); return storage._sub_storage.allAttachments(sub_id);
}) })
.push(function (result) { .push(function (result) {
var attachment_id, var attachment_id,
attachments = {}, attachments = {},
mapping_dict = {}; mapping_dict = {};
for (attachment_id in context._attachment_mapping_dict) { for (attachment_id in storage._attachment_mapping_dict) {
if (context._attachment_mapping_dict.hasOwnProperty(attachment_id)) { if (storage._attachment_mapping_dict.hasOwnProperty(attachment_id)) {
mapping_dict[getAttachmentId(context, sub_id, attachment_id, "get")] mapping_dict[getAttachmentId(storage, sub_id, attachment_id, "get")]
= attachment_id; = attachment_id;
} }
} }
...@@ -363,10 +433,10 @@ ...@@ -363,10 +433,10 @@
}; };
MappingStorage.prototype.bulk = function (id_list) { MappingStorage.prototype.bulk = function (id_list) {
var context = this; var storage = this;
function mapId(parameter) { function mapId(parameter) {
return getSubStorageId(context, parameter.parameter_list[0]) return getSubStorageId(storage, parameter.parameter_list[0])
.push(function (id) { .push(function (id) {
return {"method": parameter.method, "parameter_list": [id]}; return {"method": parameter.method, "parameter_list": [id]};
}); });
...@@ -378,13 +448,13 @@ ...@@ -378,13 +448,13 @@
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 storage._sub_storage.bulk(id_list_mapped);
}) })
.push(function (result) { .push(function (result) {
var mapped_result = [], i; var mapped_result = [], i;
for (i = 0; i < result.length; i += 1) { for (i = 0; i < result.length; i += 1) {
mapped_result.push(mapToMainDocument( mapped_result.push(mapToMainDocument(
context, storage,
result[i] result[i]
)); ));
} }
...@@ -393,7 +463,7 @@ ...@@ -393,7 +463,7 @@
}; };
MappingStorage.prototype.buildQuery = function (option) { MappingStorage.prototype.buildQuery = function (option) {
var context = this, var storage = this,
i, i,
query, query,
property, property,
...@@ -412,7 +482,7 @@ ...@@ -412,7 +482,7 @@
one_query.query_list = query_list; one_query.query_list = query_list;
return one_query; return one_query;
} }
key = mapToMainProperty(context, one_query.key, {}, {}); key = mapToMainProperty(storage, one_query.key, {}, {});
if (key) { if (key) {
one_query.key = key; one_query.key = key;
return one_query; return one_query;
...@@ -483,21 +553,25 @@ ...@@ -483,21 +553,25 @@
} }
) )
.push(function (result) { .push(function (result) {
var doc; var sub_doc, map_info = storage._map_id || ["equalSubId"];
for (i = 0; i < result.data.total_rows; i += 1) { for (i = 0; i < result.data.total_rows; i += 1) {
doc = result.data.rows[i].value; sub_doc = result.data.rows[i].value;
result.data.rows[i].id =
mapping_function[map_info[0]].mapToId(
storage,
sub_doc,
result.data.rows[i].id,
map_info[1]
);
result.data.rows[i].value = result.data.rows[i].value =
mapToMainDocument( mapToMainDocument(
context, storage,
doc sub_doc
); );
if (context._id_mapped) {
result.data.rows[i].id = doc[context._id_mapped];
}
} }
return result.data.rows; return result.data.rows;
}); });
}; };
jIO.addStorage('mapping', MappingStorage); jIO.addStorage('mapping', MappingStorage);
}(jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory, Query)); }(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