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 @@
Query) {
"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) {
this._mapping_dict = spec.mapping_dict || {};
this._sub_storage = jIO.createJIO(spec.sub_storage);
......@@ -19,7 +72,8 @@
this._default_mapping = {};
this._id_is_mapped = (this._mapping_dict.id !== undefined
&& this._mapping_dict.id.equal !== "id");
var property, query_list = [];
var property, query_list = [], new_key,
new_kv = {}, inverse_mapping_dict = {};
// handle default_value.
for (property in this._mapping_dict) {
......@@ -33,8 +87,12 @@
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) {
query_list.push(QueryFactory.create(this._query.query));
......@@ -123,10 +181,11 @@
});
}
function mapToSubProperty(storage, _mapping_dict, key, value, for_value) {
function mapProperty(_mapping_dict, key, value, for_value) {
var new_prop,
i,
regexp,
new_mapping_dict,
new_mapping_dict = {},
mapped_kv = {};
if (_mapping_dict !== undefined) {
......@@ -142,12 +201,10 @@
key = value;
} else {
new_mapping_dict = _mapping_dict.value[new_prop];
new_mapping_dict._sub_value = new_prop;
}
}
value = mapToSubProperty(storage, new_mapping_dict, key,
value, true)[key];
value = mapProperty(new_mapping_dict, key, value, true).value;
}
if (_mapping_dict.equal !== undefined) {
......@@ -158,160 +215,90 @@
new_prop = Object.keys(_mapping_dict.equal)[0];
new_mapping_dict = _mapping_dict.equal[new_prop];
}
return mapToSubProperty(storage, new_mapping_dict,
key, value, for_value);
return mapProperty(new_mapping_dict, key, value, for_value);
}
if (_mapping_dict.regex !== undefined) {
try {
regexp = new RegExp(_mapping_dict.regex);
} catch (e) {
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 {
regexp = new RegExp(_mapping_dict.regexps[i].regexp);
} catch (e) {
throw new jIO.util.jIOError(
"Regex for key " + key + ":" + e,
400
);
}
if (regexp.test(value) === true) {
mapped_kv.key = key;
mapped_kv.value = _mapping_dict.regexps[i].match_value;
return mapped_kv;
}
}
}
if (_mapping_dict.default_value === undefined) {
throw new jIO.util.jIOError(
"Regex for key " + key + ":" + e,
"default_value should be defined for regex: " + _mapping_dict,
400
);
}
if (regexp.test(value) === true) {
mapped_kv[key] = _mapping_dict._sub_value;
return mapped_kv;
}
mapped_kv[key] = value;
mapped_kv.key = key;
mapped_kv.value = _mapping_dict.default_value;
return mapped_kv;
}
if (_mapping_dict["switch"] !== undefined) {
if (_mapping_dict["switch"].hasOwnProperty(key)) {
return mapToSubProperty(storage, _mapping_dict['switch'][key],
key, value, true);
return mapProperty(_mapping_dict['switch'][key], key, value, true);
}
mapped_kv[key] = value;
if (_mapping_dict["switch"].regexps) {
new_mapping_dict.regexps = _mapping_dict["switch"].regexps;
return mapProperty(new_mapping_dict, key, value, true);
}
mapped_kv.key = key;
mapped_kv.value = value;
return mapped_kv;
}
if (_mapping_dict.default_value !== undefined) {
return {key: _mapping_dict.default_value};
}
if (typeof _mapping_dict === "string") {
if (for_value === false) {
mapped_kv[_mapping_dict] = value;
mapped_kv.key = _mapping_dict;
mapped_kv.value = value;
return mapped_kv;
}
mapped_kv[key] = _mapping_dict;
mapped_kv.key = key;
mapped_kv.value = _mapping_dict;
return mapped_kv;
}
if (!storage._map_all_property) {
throw new jIO.util.jIOError(
"Unsuported option(s): " + _mapping_dict,
400
);
}
}
mapped_kv[key] = value;
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?
}
if (_mapping_dict[key].regex !== undefined) {
if (_mapping_dict[key].default_value === undefined) {
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];
}
if (_mapping_dict.default_value !== undefined) {
mapped_kv.key = key;
mapped_kv.value = _mapping_dict.default_value;
return mapped_kv;
}
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;
mapped_kv.key = key;
mapped_kv.value = value;
return mapped_kv;
}
function mapToMainDocument(storage, sub_doc, sub_id, delete_id_from_doc) {
var doc = {},
property,
new_key,
new_kv = {},
inverse_mapping_dict = {};
new_kv = {};
if (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) {
for (property in sub_doc) {
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);
new_key = Object.keys(new_kv)[0];
doc[new_key] = new_kv[new_key];
doc[new_kv.key] = new_kv.value;
}
}
}
......@@ -324,19 +311,17 @@
function mapToSubstorageDocument(storage, doc, id, delete_id_from_doc) {
var sub_doc = {},
mapped_kv = {},
property,
mapped_key;
property;
doc.id = id;
for (property in doc) {
if (doc.hasOwnProperty(property)) {
mapped_kv = mapToSubProperty(storage, storage._mapping_dict[property],
mapped_kv = mapProperty(storage._mapping_dict[property],
property, doc[property], false);
if (!mapped_kv && storage._map_all_property) {
sub_doc[property] = doc[property];
} else {
mapped_key = Object.keys(mapped_kv)[0];
sub_doc[mapped_key] = mapped_kv[mapped_key];
sub_doc[mapped_kv.key] = mapped_kv.value;
}
}
}
......@@ -522,7 +507,7 @@
sort_on = [];
function mapQuery(one_query) {
var j, query_list = [];
var j, mapped_kv, query_list = [];
if (one_query.type === "complex") {
for (j = 0; j < one_query.query_list.length; j += 1) {
query_list.push(mapQuery(one_query.query_list[j]));
......@@ -530,13 +515,18 @@
one_query.query_list = query_list;
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;
}
if (option.sort_on !== undefined) {
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) {
select_list.push([property, option.sort_on[i][1]]);
}
......@@ -544,7 +534,8 @@
}
if (this._query.sort_on !== undefined) {
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) {
select_list.push([property, option.sort_on[i][1]]);
}
......@@ -552,7 +543,8 @@
}
if (option.select_list !== undefined) {
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) {
select_list.push(property);
}
......
......@@ -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();
expect(2);
......@@ -439,10 +439,16 @@
},
mapping_dict: {
"title": {
"equal": "someTitle",
"value": {
"otherFoo": {
"regex": "foo.*|bar.*",
"default_value": "otherFoo"
"switch": {
"foo": {
"regex": "other.*",
"default_value": "otherFoo"
},
"bar": {
"equal": "otherBar"
}
}
}
}
......@@ -451,13 +457,13 @@
Storage2713.prototype.get = function (id) {
equal(id, "42", "get 2713 called");
return {"title": "otherFoo", "smth": "bar"};
return {"someTitle": "otherFoo", "smth": "bar"};
};
jio.get("42")
.push(function (result) {
deepEqual(result, {
"title": "otherFoo",
"title": "foo",
"smth": "bar"
});
}).push(undefined, function (error) {
......@@ -832,9 +838,55 @@
"title": {
"equal": "someTitle",
"value": {
"otherFoo": {
"foo": {
"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 @@
return id;
};
jio.put("42", {"title": "foo1", "smth": "bar"})
jio.put("42", {"title": "foo", "smth": "bar"})
.push(function (result) {
equal(result, "42");
})
......@@ -1732,11 +1784,9 @@
deepEqual(
result,
[{
"id": "foo",
"title": "bar",
"bar": "foo"
}, {
"id": "bar",
"title": "foo",
"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