Commit 9bb5cdfc authored by Hardik Juneja's avatar Hardik Juneja

Improve performance and make build query use new functions

parent 2ca6c591
...@@ -5,6 +5,59 @@ ...@@ -5,6 +5,59 @@
Query) { Query) {
"use strict"; "use strict";
function inverseMappingDict(_mapping_dict, key) {
var new_mapping_dict = {},
new_key,
new_value = {},
new_kv = {};
if (_mapping_dict[key].value !== undefined) {
new_key = Object.keys(_mapping_dict[key].value)[0];
new_value = inverseMappingDict(_mapping_dict[key].value,
new_key);
}
if (_mapping_dict[key].equal !== undefined) {
if (typeof _mapping_dict[key].equal === "string") {
new_mapping_dict[_mapping_dict[key].equal] = {'equal' : key};
if (new_value !== undefined) {
new_mapping_dict[_mapping_dict[key].equal].value = new_value;
}
return new_mapping_dict;
}
//xx: is it possible for equal to have children?
}
if (_mapping_dict[key].regex !== undefined) {
new_mapping_dict = {
regexp: _mapping_dict[key].regex,
match_value: key
};
return new_mapping_dict;
}
if (key === "switch") {
//xx: assuming switch is just used in case of value
new_mapping_dict['switch'] = {};
new_mapping_dict['switch'].regexps = [];
for (new_key in _mapping_dict['switch']) {
if (_mapping_dict['switch'].hasOwnProperty(new_key)) {
new_kv = inverseMappingDict(_mapping_dict['switch'], new_key);
new_key = Object.keys(new_kv)[0];
if (new_key === "regexp") {
new_mapping_dict['switch'].regexps.push(new_kv);
} else {
new_mapping_dict['switch'][new_key] = new_kv[new_key];
}
}
}
return new_mapping_dict;
}
new_mapping_dict[key] = {};
new_mapping_dict[key].value = new_value;
return new_mapping_dict;
}
function MappingStorage(spec) { function MappingStorage(spec) {
this._mapping_dict = spec.mapping_dict || {}; this._mapping_dict = spec.mapping_dict || {};
this._sub_storage = jIO.createJIO(spec.sub_storage); this._sub_storage = jIO.createJIO(spec.sub_storage);
...@@ -19,7 +72,8 @@ ...@@ -19,7 +72,8 @@
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");
var property, query_list = []; var property, query_list = [], new_key,
new_kv = {}, inverse_mapping_dict = {};
// handle default_value. // handle default_value.
for (property in this._mapping_dict) { for (property in this._mapping_dict) {
...@@ -33,8 +87,12 @@ ...@@ -33,8 +87,12 @@
type: "simple" type: "simple"
})); }));
} }
new_kv = inverseMappingDict(this._mapping_dict, property);
new_key = Object.keys(new_kv)[0];
inverse_mapping_dict[new_key] = new_kv[new_key];
} }
} }
this._inverse_mapping_dict = inverse_mapping_dict;
if (this._query.query !== undefined) { if (this._query.query !== undefined) {
query_list.push(QueryFactory.create(this._query.query)); query_list.push(QueryFactory.create(this._query.query));
...@@ -123,10 +181,11 @@ ...@@ -123,10 +181,11 @@
}); });
} }
function mapToSubProperty(storage, _mapping_dict, key, value, for_value) { function mapProperty(_mapping_dict, key, value, for_value) {
var new_prop, var new_prop,
i,
regexp, regexp,
new_mapping_dict, new_mapping_dict = {},
mapped_kv = {}; mapped_kv = {};
if (_mapping_dict !== undefined) { if (_mapping_dict !== undefined) {
...@@ -142,12 +201,10 @@ ...@@ -142,12 +201,10 @@
key = value; key = value;
} else { } else {
new_mapping_dict = _mapping_dict.value[new_prop]; new_mapping_dict = _mapping_dict.value[new_prop];
new_mapping_dict._sub_value = new_prop;
} }
} }
value = mapToSubProperty(storage, new_mapping_dict, key, value = mapProperty(new_mapping_dict, key, value, true).value;
value, true)[key];
} }
if (_mapping_dict.equal !== undefined) { if (_mapping_dict.equal !== undefined) {
...@@ -158,160 +215,90 @@ ...@@ -158,160 +215,90 @@
new_prop = Object.keys(_mapping_dict.equal)[0]; new_prop = Object.keys(_mapping_dict.equal)[0];
new_mapping_dict = _mapping_dict.equal[new_prop]; new_mapping_dict = _mapping_dict.equal[new_prop];
} }
return mapToSubProperty(storage, new_mapping_dict, return mapProperty(new_mapping_dict, key, value, for_value);
key, value, for_value);
} }
if (_mapping_dict.regex !== undefined) { if (_mapping_dict.regex !== undefined || _mapping_dict.regexps) {
if (_mapping_dict.regexps && _mapping_dict.regexps.length) {
for (i = 0; i < _mapping_dict.regexps.length; i = i + 1) {
try { try {
regexp = new RegExp(_mapping_dict.regex); regexp = new RegExp(_mapping_dict.regexps[i].regexp);
} catch (e) { } catch (e) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Regex for key " + key + ":" + e, "Regex for key " + key + ":" + e,
400 400
); );
} }
if (regexp.test(value) === true) { if (regexp.test(value) === true) {
mapped_kv[key] = _mapping_dict._sub_value; mapped_kv.key = key;
mapped_kv.value = _mapping_dict.regexps[i].match_value;
return mapped_kv; return mapped_kv;
} }
mapped_kv[key] = value; }
}
if (_mapping_dict.default_value === undefined) {
throw new jIO.util.jIOError(
"default_value should be defined for regex: " + _mapping_dict,
400
);
}
mapped_kv.key = key;
mapped_kv.value = _mapping_dict.default_value;
return mapped_kv; return mapped_kv;
} }
if (_mapping_dict["switch"] !== undefined) { if (_mapping_dict["switch"] !== undefined) {
if (_mapping_dict["switch"].hasOwnProperty(key)) { if (_mapping_dict["switch"].hasOwnProperty(key)) {
return mapToSubProperty(storage, _mapping_dict['switch'][key], return mapProperty(_mapping_dict['switch'][key], key, value, true);
key, value, true);
} }
mapped_kv[key] = value; if (_mapping_dict["switch"].regexps) {
return mapped_kv; new_mapping_dict.regexps = _mapping_dict["switch"].regexps;
return mapProperty(new_mapping_dict, key, value, true);
} }
mapped_kv.key = key;
if (_mapping_dict.default_value !== undefined) { mapped_kv.value = value;
return {key: _mapping_dict.default_value}; return mapped_kv;
} }
if (typeof _mapping_dict === "string") { if (typeof _mapping_dict === "string") {
if (for_value === false) { if (for_value === false) {
mapped_kv[_mapping_dict] = value; mapped_kv.key = _mapping_dict;
mapped_kv.value = value;
return mapped_kv; return mapped_kv;
} }
mapped_kv[key] = _mapping_dict; mapped_kv.key = key;
mapped_kv.value = _mapping_dict;
return mapped_kv; return mapped_kv;
} }
if (!storage._map_all_property) { if (_mapping_dict.default_value !== undefined) {
throw new jIO.util.jIOError( mapped_kv.key = key;
"Unsuported option(s): " + _mapping_dict, mapped_kv.value = _mapping_dict.default_value;
400
);
}
}
mapped_kv[key] = value;
return mapped_kv; return mapped_kv;
} }
function inverseMappingDict(_mapping_dict, key) {
var new_mapping_dict = {},
value_dict = {},
new_key,
new_kv;
if (_mapping_dict[key].value !== undefined) {
new_key = Object.keys(_mapping_dict[key].value)[0];
value_dict[key] = {};
value_dict[key].value = inverseMappingDict(_mapping_dict[key].value,
new_key);
}
if (_mapping_dict[key].equal !== undefined) {
if (typeof _mapping_dict[key].equal === "string") {
new_mapping_dict[_mapping_dict[key].equal] = {'equal' : key};
if (value_dict[key] !== undefined) {
new_mapping_dict[_mapping_dict[key].equal].value =
value_dict[key].value;
}
return new_mapping_dict;
}
//xx: is it possible for equal to have children?
} }
mapped_kv.key = key;
if (_mapping_dict[key].regex !== undefined) { mapped_kv.value = value;
if (_mapping_dict[key].default_value === undefined) { return mapped_kv;
throw new jIO.util.jIOError(
"default_value should be defined for regex: " + _mapping_dict,
400
);
}
new_mapping_dict[key] = {'equal' : _mapping_dict[key].default_value};
return new_mapping_dict;
}
if (key === "switch") {
//xx: assuming switch is just used in case of value
new_mapping_dict['switch'] = {};
for (new_key in _mapping_dict['switch']) {
if (_mapping_dict['switch'].hasOwnProperty(new_key)) {
new_kv = inverseMappingDict(_mapping_dict['switch'], new_key);
new_key = Object.keys(new_kv)[0];
new_mapping_dict['switch'][new_key] = new_kv[new_key];
}
}
return new_mapping_dict;
}
return value_dict;
}
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];
}
return storage._mapping_dict[property].equal;
}
if (storage._mapping_dict[property].default_value !== undefined) {
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, delete_id_from_doc) { function mapToMainDocument(storage, sub_doc, sub_id, delete_id_from_doc) {
var doc = {}, var doc = {},
property, property,
new_key, new_kv = {};
new_kv = {},
inverse_mapping_dict = {};
if (sub_id) { if (sub_id) {
sub_doc.id = sub_id; sub_doc.id = sub_id;
} }
for (property in storage._mapping_dict) {
if (storage._mapping_dict.hasOwnProperty(property)) {
new_kv = inverseMappingDict(storage._mapping_dict, property);
new_key = Object.keys(new_kv)[0];
inverse_mapping_dict[new_key] = new_kv[new_key];
}
}
if (storage._map_all_property) { if (storage._map_all_property) {
for (property in sub_doc) { for (property in sub_doc) {
if (sub_doc.hasOwnProperty(property)) { if (sub_doc.hasOwnProperty(property)) {
new_kv = mapToSubProperty(storage, inverse_mapping_dict[property], new_kv = mapProperty(storage._inverse_mapping_dict[property],
property, sub_doc[property], false); property, sub_doc[property], false);
new_key = Object.keys(new_kv)[0]; doc[new_kv.key] = new_kv.value;
doc[new_key] = new_kv[new_key];
} }
} }
} }
...@@ -324,19 +311,17 @@ ...@@ -324,19 +311,17 @@
function mapToSubstorageDocument(storage, doc, id, delete_id_from_doc) { function mapToSubstorageDocument(storage, doc, id, delete_id_from_doc) {
var sub_doc = {}, var sub_doc = {},
mapped_kv = {}, mapped_kv = {},
property, property;
mapped_key;
doc.id = id; doc.id = id;
for (property in doc) { for (property in doc) {
if (doc.hasOwnProperty(property)) { if (doc.hasOwnProperty(property)) {
mapped_kv = mapToSubProperty(storage, storage._mapping_dict[property], mapped_kv = mapProperty(storage._mapping_dict[property],
property, doc[property], false); property, doc[property], false);
if (!mapped_kv && storage._map_all_property) { if (!mapped_kv && storage._map_all_property) {
sub_doc[property] = doc[property]; sub_doc[property] = doc[property];
} else { } else {
mapped_key = Object.keys(mapped_kv)[0]; sub_doc[mapped_kv.key] = mapped_kv.value;
sub_doc[mapped_key] = mapped_kv[mapped_key];
} }
} }
} }
...@@ -522,7 +507,7 @@ ...@@ -522,7 +507,7 @@
sort_on = []; sort_on = [];
function mapQuery(one_query) { function mapQuery(one_query) {
var j, query_list = []; var j, mapped_kv, query_list = [];
if (one_query.type === "complex") { if (one_query.type === "complex") {
for (j = 0; j < one_query.query_list.length; j += 1) { for (j = 0; j < one_query.query_list.length; j += 1) {
query_list.push(mapQuery(one_query.query_list[j])); query_list.push(mapQuery(one_query.query_list[j]));
...@@ -530,13 +515,18 @@ ...@@ -530,13 +515,18 @@
one_query.query_list = query_list; one_query.query_list = query_list;
return one_query; return one_query;
} }
one_query.key = mapToMainProperty(context, one_query.key, {}, {});
mapped_kv = mapProperty(context._mapping_dict[one_query.key],
one_query.key, one_query.value, false);
one_query.key = mapped_kv.key;
one_query.value = mapped_kv.value;
return one_query; return one_query;
} }
if (option.sort_on !== undefined) { if (option.sort_on !== undefined) {
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 = mapProperty(this._mapping_dict[option.sort_on[i][0]],
option.sort_on[i][0], undefined, false).key;
if (property && sort_on.indexOf(property) < 0) { if (property && sort_on.indexOf(property) < 0) {
select_list.push([property, option.sort_on[i][1]]); select_list.push([property, option.sort_on[i][1]]);
} }
...@@ -544,7 +534,8 @@ ...@@ -544,7 +534,8 @@
} }
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 = mapToMainProperty(this, this._query.sort_on[i], {}, {}); property = mapProperty(this._mapping_dict[this._query.sort_on[i]],
this._query.sort_on[i], undefined, false).key;
if (sort_on.indexOf(property) < 0) { if (sort_on.indexOf(property) < 0) {
select_list.push([property, option.sort_on[i][1]]); select_list.push([property, option.sort_on[i][1]]);
} }
...@@ -552,7 +543,8 @@ ...@@ -552,7 +543,8 @@
} }
if (option.select_list !== undefined) { if (option.select_list !== undefined) {
for (i = 0; i < option.select_list.length; i += 1) { for (i = 0; i < option.select_list.length; i += 1) {
property = mapToMainProperty(this, option.select_list[i], {}, {}); property = mapProperty(this._mapping_dict[option.select_list[i]],
option.select_list[i], undefined, false).key;
if (property && select_list.indexOf(property) < 0) { if (property && select_list.indexOf(property) < 0) {
select_list.push(property); select_list.push(property);
} }
......
...@@ -428,7 +428,7 @@ ...@@ -428,7 +428,7 @@
}); });
}); });
test("get with property and its value mapped using regex", function () { test("get with property ad value mapped using regex and switch", function () {
stop(); stop();
expect(2); expect(2);
...@@ -439,10 +439,16 @@ ...@@ -439,10 +439,16 @@
}, },
mapping_dict: { mapping_dict: {
"title": { "title": {
"equal": "someTitle",
"value": { "value": {
"otherFoo": { "switch": {
"regex": "foo.*|bar.*", "foo": {
"regex": "other.*",
"default_value": "otherFoo" "default_value": "otherFoo"
},
"bar": {
"equal": "otherBar"
}
} }
} }
} }
...@@ -451,13 +457,13 @@ ...@@ -451,13 +457,13 @@
Storage2713.prototype.get = function (id) { Storage2713.prototype.get = function (id) {
equal(id, "42", "get 2713 called"); equal(id, "42", "get 2713 called");
return {"title": "otherFoo", "smth": "bar"}; return {"someTitle": "otherFoo", "smth": "bar"};
}; };
jio.get("42") jio.get("42")
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
"title": "otherFoo", "title": "foo",
"smth": "bar" "smth": "bar"
}); });
}).push(undefined, function (error) { }).push(undefined, function (error) {
...@@ -832,9 +838,55 @@ ...@@ -832,9 +838,55 @@
"title": { "title": {
"equal": "someTitle", "equal": "someTitle",
"value": { "value": {
"otherFoo": { "foo": {
"regex": "foo.*|bar.*", "regex": "foo.*|bar.*",
"use": "otherFoo" "default_value": "otherFoo"
}
}
}
}
});
Storage2713.prototype.put = function (id, doc) {
deepEqual(doc,
{"someTitle": "otherFoo", "smth": "bar"}, "post 2713 called");
equal(id, "42", "put 2713 called");
return id;
};
jio.put("42", {"title": "foo", "smth": "bar"})
.push(function (result) {
equal(result, "42");
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("put with property, value mapped using regex and switch", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"title": {
"equal": "someTitle",
"value": {
"switch": {
"foo": {
"regex": "other.*",
"default_value": "otherFoo"
},
"bar": {
"equal": "otherBar"
}
} }
} }
} }
...@@ -848,7 +900,7 @@ ...@@ -848,7 +900,7 @@
return id; return id;
}; };
jio.put("42", {"title": "foo1", "smth": "bar"}) jio.put("42", {"title": "foo", "smth": "bar"})
.push(function (result) { .push(function (result) {
equal(result, "42"); equal(result, "42");
}) })
...@@ -1732,11 +1784,9 @@ ...@@ -1732,11 +1784,9 @@
deepEqual( deepEqual(
result, result,
[{ [{
"id": "foo",
"title": "bar", "title": "bar",
"bar": "foo" "bar": "foo"
}, { }, {
"id": "bar",
"title": "foo", "title": "foo",
"foo": "bar" "foo": "bar"
}], }],
......
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