Commit 2ca6c591 authored by Hardik Juneja's avatar Hardik Juneja

Mapping Strorage: Add support to switch, map values and apply regex

parent 94b0bcb1
...@@ -123,27 +123,150 @@ ...@@ -123,27 +123,150 @@
}); });
} }
function mapToSubProperty(storage, property, sub_doc, doc) { function mapToSubProperty(storage, _mapping_dict, key, value, for_value) {
if (storage._mapping_dict[property] !== undefined) { var new_prop,
if (storage._mapping_dict[property].equal !== undefined) { regexp,
sub_doc[storage._mapping_dict[property].equal] = doc[property]; new_mapping_dict,
return storage._mapping_dict[property].equal; mapped_kv = {};
if (_mapping_dict !== undefined) {
if (_mapping_dict.value !== undefined) {
if (typeof _mapping_dict.value === "string") {
new_prop = _mapping_dict.value;
new_mapping_dict = _mapping_dict.value;
} else {
//XX: Find other usecases of switch and remove this block
new_prop = Object.keys(_mapping_dict.value)[0];
if (new_prop === "switch") {
new_mapping_dict = _mapping_dict.value;
key = value;
} else {
new_mapping_dict = _mapping_dict.value[new_prop];
new_mapping_dict._sub_value = new_prop;
} }
if (storage._mapping_dict[property].default_value !== undefined) {
sub_doc[property] = storage._mapping_dict[property].default_value;
return property;
} }
value = mapToSubProperty(storage, new_mapping_dict, key,
value, true)[key];
}
if (_mapping_dict.equal !== undefined) {
if (typeof _mapping_dict.equal === "string") {
new_prop = _mapping_dict.equal;
new_mapping_dict = _mapping_dict.equal;
} else {
new_prop = Object.keys(_mapping_dict.equal)[0];
new_mapping_dict = _mapping_dict.equal[new_prop];
} }
if (storage._map_all_property) { return mapToSubProperty(storage, new_mapping_dict,
sub_doc[property] = doc[property]; key, value, for_value);
return property;
} }
if (_mapping_dict.regex !== undefined) {
try {
regexp = new RegExp(_mapping_dict.regex);
} catch (e) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Unsuported option(s): " + storage._mapping_dict[property], "Regex for key " + key + ":" + e,
400 400
); );
} }
if (regexp.test(value) === true) {
mapped_kv[key] = _mapping_dict._sub_value;
return mapped_kv;
}
mapped_kv[key] = 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);
}
mapped_kv[key] = 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;
return mapped_kv;
}
mapped_kv[key] = _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];
}
}
return new_mapping_dict;
}
return value_dict;
}
function mapToMainProperty(storage, property, sub_doc, doc) { function mapToMainProperty(storage, property, sub_doc, doc) {
if (storage._mapping_dict[property] !== undefined) { if (storage._mapping_dict[property] !== undefined) {
if (storage._mapping_dict[property].equal !== undefined) { if (storage._mapping_dict[property].equal !== undefined) {
...@@ -168,21 +291,27 @@ ...@@ -168,21 +291,27 @@
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,
property_list = []; new_key,
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) { 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)); 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)) {
if (property_list.indexOf(property) < 0) { new_kv = mapToSubProperty(storage, inverse_mapping_dict[property],
doc[property] = sub_doc[property]; property, sub_doc[property], false);
} new_key = Object.keys(new_kv)[0];
doc[new_key] = new_kv[new_key];
} }
} }
} }
...@@ -193,12 +322,22 @@ ...@@ -193,12 +322,22 @@
} }
function mapToSubstorageDocument(storage, doc, id, delete_id_from_doc) { function mapToSubstorageDocument(storage, doc, id, delete_id_from_doc) {
var sub_doc = {}, property; var sub_doc = {},
doc.id = id; mapped_kv = {},
property,
mapped_key;
doc.id = id;
for (property in doc) { for (property in doc) {
if (doc.hasOwnProperty(property)) { if (doc.hasOwnProperty(property)) {
mapToSubProperty(storage, property, sub_doc, doc); mapped_kv = mapToSubProperty(storage, 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];
}
} }
} }
for (property in storage._default_mapping) { for (property in storage._default_mapping) {
......
...@@ -246,6 +246,228 @@ ...@@ -246,6 +246,228 @@
}); });
}); });
test("get with property value mapped", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"title": {
"value": {
"foo": {
"equal": "otherFoo"
}
}
}
},
query: {"query": 'title: "otherFoo"'}
});
Storage2713.prototype.get = function (id) {
equal(id, "42", "get 2713 called");
return {"title": "otherFoo"};
};
jio.get("42")
.push(function (result) {
deepEqual(result, {
"title": "foo"
});
}).push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get with both property and its value mapped", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"title": {
"equal": "otherTitle",
"value": {
"foo": {
"equal": "otherFoo"
}
}
}
}
});
Storage2713.prototype.get = function (id) {
equal(id, "42", "get 2713 called");
return {"otherTitle": "otherFoo"};
};
jio.get("42")
.push(function (result) {
deepEqual(result, {
"title": "foo"
});
}).push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get with property, value and id mapped", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"title": {
"equal": "otherTitle",
"value": {
"foo": {
"equal": "otherFoo"
}
}
},
"id": {
"equal": "otherId"
}
},
query: {"query": 'otherTitle: "otherFoo"'}
});
Storage2713.prototype.hasCapacity = function () {
return true;
};
Storage2713.prototype.buildQuery = function (options) {
equal(
options.query,
'( otherId: "42" AND otherTitle: "otherFoo" )',
"allDoc 2713 called"
);
return [{id: "2713"}];
};
Storage2713.prototype.get = function (id) {
equal(id, "2713", "get 2713 called");
return {"otherTitle": "otherFoo"};
};
jio.get("42")
.push(function (result) {
deepEqual(result, {
"title": "foo"
});
}).push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get with id and value mapped using switch for value", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"title": {
"equal": "otherTitle",
"value": {
"switch": {
"foo": {
"equal": "otherFoo"
},
"bar": {
"equal": "otherBar"
}
}
}
},
"smth": {
"equal": "otherSmth"
}
}
});
Storage2713.prototype.get = function (id) {
equal(id, "42", "get 2713 called");
return {"otherTitle": "otherFoo", "otherSmth": "bar"};
};
jio.get("42")
.push(function (result) {
deepEqual(result, {
"title": "foo",
"smth": "bar"
});
}).push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get with property and its value mapped using regex", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"title": {
"value": {
"otherFoo": {
"regex": "foo.*|bar.*",
"default_value": "otherFoo"
}
}
}
}
});
Storage2713.prototype.get = function (id) {
equal(id, "42", "get 2713 called");
return {"title": "otherFoo", "smth": "bar"};
};
jio.get("42")
.push(function (result) {
deepEqual(result, {
"title": "otherFoo",
"smth": "bar"
});
}).push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get with map_all_property", function () { test("get with map_all_property", function () {
stop(); stop();
expect(3); expect(3);
...@@ -466,6 +688,177 @@ ...@@ -466,6 +688,177 @@
}); });
}); });
test("put with property value mapped", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"title": {
"value": {
"foo": {
"equal": "foo1"
}
}
}
}
});
Storage2713.prototype.put = function (id, doc) {
deepEqual(doc,
{"title": "foo1", "smth": "bar", "smth2": "bar2"}, "post 2713 called");
equal(id, "42", "put 2713 called");
return id;
};
jio.put("42", {"title": "foo", "smth": "bar", "smth2": "bar2"})
.push(function (result) {
equal(result, "42");
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("put with both property and its value mapped", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"title": {
"equal": "bar",
"value": {
"foo": {
"equal": "foo1"
}
}
}
}
});
Storage2713.prototype.put = function (id, doc) {
deepEqual(doc,
{"bar": "foo1", "smth": "bar", "smth2": "bar2"}, "post 2713 called");
equal(id, "42", "put 2713 called");
return id;
};
jio.put("42", {"title": "foo", "smth": "bar", "smth2": "bar2"})
.push(function (result) {
equal(result, "42");
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("put with prop and value mapped using switch for value", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"title": {
"equal": "bar",
"value": {
"switch": {
"foo": {
"equal": "foo1"
}
}
}
},
"smth": {
"equal": "somth",
"value": {
"switch": {
"bar": {
"equal": "bar1"
}
}
}
}
}
});
Storage2713.prototype.put = function (id, doc) {
deepEqual(doc,
{"bar": "foo1", "somth": "bar1", "smth2": "bar2"}, "post 2713 called");
equal(id, "42", "put 2713 called");
return id;
};
jio.put("42", {"title": "foo", "smth": "bar", "smth2": "bar2"})
.push(function (result) {
equal(result, "42");
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("put with property and its value mapped using regex", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {
"title": {
"equal": "someTitle",
"value": {
"otherFoo": {
"regex": "foo.*|bar.*",
"use": "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": "foo1", "smth": "bar"})
.push(function (result) {
equal(result, "42");
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// mappingStorage.remove // mappingStorage.remove
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
......
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