Commit 068b809a authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: add repair, delete default_dict and add prop default_value

parent d874bc0d
......@@ -5,17 +5,15 @@
function MappingStorage(spec) {
this._mapping_dict = spec.mapping_dict || {};
this._default_dict = spec.default_dict || {};
this._sub_storage = jIO.createJIO(spec.sub_storage);
this._map_all_property = spec.map_all_property || false;
this._mapping_dict_attachment = spec.mapping_dict_attachment || undefined;
this._mapping_dict_attachment = spec.mapping_dict_attachment || {};
this._query = spec.query || {};
this._id_is_mapped = (this._mapping_dict.id !== undefined
&& this._mapping_dict.id.equal !== "id");
}
function getAttachmentId(storage, sub_id, attachment_id, method) {
var mapping_dict = storage._mapping_dict_attachment;
return new RSVP.Queue()
......@@ -35,7 +33,6 @@
var query;
return new RSVP.Queue()
.push(function () {
var property;
if (!storage._id_is_mapped) {
return index;
}
......@@ -44,12 +41,6 @@
if (storage._query.query !== undefined) {
query += ' AND ' + storage._query.query;
}
for (property in storage._default_dict) {
if (storage._default_dict.hasOwnProperty(property)) {
query += ' AND ' + property + ': "'
+ storage._default_dict[property].equal + '"';
}
}
return storage._sub_storage.allDocs({
"query": query,
"sort_on": storage._query.sort_on,
......@@ -78,6 +69,10 @@
doc[storage._mapping_dict[property].equal] = mapped_doc[property];
return storage._mapping_dict[property].equal;
}
if (storage._mapping_dict[property].default_value !== undefined) {
doc[property] = storage._mapping_dict[property].default_value;
return property;
}
throw new jIO.util.jIOError(
"Unsuported option(s): " + storage._mapping_dict[property],
400
......@@ -92,15 +87,7 @@
}
return;
}
throw new jIO.util.jIOError(
"Unsuported option(s): " + storage._mapping_dict[property],
400
);
}
function unmapDefaultProperty(storage, doc, property) {
if (storage._default_dict[property].equal !== undefined) {
doc[property] = storage._default_dict[property].equal;
if (storage._mapping_dict[property].default_value !== undefined) {
return property;
}
throw new jIO.util.jIOError(
......@@ -134,49 +121,49 @@
}
function unmapDocument(storage, mapped_doc) {
var doc = {}, property;
for (property in storage._default_dict) {
if (storage._default_dict.hasOwnProperty(property)) {
unmapDefaultProperty(storage, doc, property);
var doc = {}, property, property_list = [];
for (property in storage._mapping_dict) {
if (storage._mapping_dict.hasOwnProperty(property)) {
property_list.push(unmapProperty(storage, property, doc, mapped_doc));
}
}
for (property in mapped_doc) {
if (mapped_doc.hasOwnProperty(property)) {
if (storage._mapping_dict[property] !== undefined) {
unmapProperty(storage, property, doc, mapped_doc);
} else {
if (storage._map_all_property
&& !storage._default_dict.hasOwnProperty(property)) {
if (storage._map_all_property) {
for (property in mapped_doc) {
if (mapped_doc.hasOwnProperty(property)) {
if (property_list.indexOf(property) < 0) {
doc[property] = mapped_doc[property];
}
}
}
}
delete doc.id;
return doc;
}
MappingStorage.prototype.get = function (index) {
var that = this;
return getSubStorageId(this, index)
.push(function (id) {
if (id !== undefined) {
return that._sub_storage.get(id);
}
throw new jIO.util.jIOError("Cannot find document " + id, 404);
})
.push(function (doc) {
return mapDocument(that, doc, true);
})
.push(undefined, function (error) {
throw error;
});
if (index !== undefined) {
return getSubStorageId(this, index)
.push(function (id) {
if (id !== undefined) {
return that._sub_storage.get(id);
}
throw new jIO.util.jIOError("Cannot find document " + index, 404);
})
.push(function (doc) {
return mapDocument(that, doc, true);
});
}
throw new jIO.util.jIOError("Cannot find document " + index, 404);
};
MappingStorage.prototype.post = function () {
MappingStorage.prototype.post = function (doc_mapped) {
if (!this._id_is_mapped) {
return this._sub_storage.post.apply(this._sub_storage, arguments);
return this._sub_storage.post.apply(
this._sub_storage,
unmapDocument(this, doc_mapped)
);
}
throw new jIO.util.jIOError("Can't use post with id mapped", 400);
};
MappingStorage.prototype.put = function (index, doc) {
......@@ -184,7 +171,7 @@
mapped_doc = unmapDocument(this, doc);
return getSubStorageId(this, index)
.push(function (id) {
if (that._mapping_dict.id && that._mapping_dict.id.equal !== "id") {
if (that._id_is_mapped) {
mapped_doc[that._mapping_dict.id.equal] = index;
}
if (id !== undefined) {
......@@ -222,7 +209,6 @@
});
};
MappingStorage.prototype.getAttachment = function (id, attachment_id) {
var context = this, argument_list = arguments;
return getSubStorageId(context, id)
......@@ -255,6 +241,10 @@
return this._sub_storage.hasCapacity.apply(this._sub_storage, arguments);
};
MappingStorage.prototype.repair = function () {
return this._sub_storage.repair.apply(this._sub_storage, arguments);
};
MappingStorage.prototype.bulk = function () {
var i, that = this, mapped_result = [];
return this._sub_storage.bulk.apply(arguments)
......
......@@ -35,7 +35,9 @@
ok(jio.__storage._sub_storage instanceof jio.constructor);
equal(jio.__storage._sub_storage.__type, "mappingstorage2713");
deepEqual(jio.__storage._mapping_dict, {});
deepEqual(jio.__storage._default_dict, {});
deepEqual(jio.__storage._mapping_dict_attachment, {});
deepEqual(jio.__storage._query, {});
equal(jio.__storage._map_all_property, false);
});
test("accept parameters", function () {
......@@ -45,12 +47,15 @@
type: "mappingstorage2713"
},
mapping_dict: { "bar": {"equal": "foo"}},
default_dict: { "foo": {"equal": "bar"}}
map_all_property: true,
query: {"query": "foo"},
mapping_dict_attachment: {"foo": {"get": "bar"}}
});
deepEqual(jio.__storage._mapping_dict, {"bar": {"equal": "foo"}});
deepEqual(jio.__storage._default_dict, {"foo": {"equal": "bar"}});
equal(jio.__storage._map_all_property, false);
deepEqual(jio.__storage._query, {"query": "foo"});
deepEqual(jio.__storage._mapping_dict_attachment, {"foo": {"get": "bar"}});
equal(jio.__storage._map_all_property, true);
});
/////////////////////////////////////////////////////////////////
......@@ -297,7 +302,7 @@
sub_storage: {
type: "mappingstorage2713"
},
default_dict: {"title": {"equal": "foobar"}}
mapping_dict: {"title": {"default_value": "foobar"}}
});
Storage2713.prototype.put = function (id, param) {
......@@ -503,7 +508,8 @@
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict_attachment: {"2713": {"uri_template": "www.2713.foo/{id}"}}
mapping_dict_attachment: {"2713": {"put":
{"uri_template": "www.2713.foo/{id}"}}}
}),
blob = new Blob([""]);
Storage2713.prototype.putAttachment = function (doc_id,
......@@ -566,7 +572,7 @@
type: "mappingstorage2713"
},
mapping_dict_attachment: {
"2713": {"uri_template": "www.2713/{id}/ok.com"}
"2713": {"get": {"uri_template": "www.2713/{id}/ok.com"}}
}
}),
blob = new Blob([""]);
......@@ -626,7 +632,8 @@
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict_attachment: {"2713": {"uri_template": "www.2713/{id}.bar"}}
mapping_dict_attachment: {"2713":
{"remove": {"uri_template": "www.2713/{id}.bar"}}}
});
Storage2713.prototype.removeAttachment = function (doc_id, attachment) {
equal(doc_id, "42", "putAttachment 2713 called");
......@@ -820,7 +827,7 @@
/////////////////////////////////////////////////////////////////
// mappingStorage.bulk
/////////////////////////////////////////////////////////////////
module("mappingstorage.bulk");
/* module("mappingstorage.bulk");
test("bulk with map_all_property", function () {
stop();
expect(2);
......@@ -875,6 +882,39 @@
.always(function () {
start();
});
});
});*/
/////////////////////////////////////////////////////////////////
// mappingStorage.repair
/////////////////////////////////////////////////////////////////
module("mappingStorage.repair");
test("repair called substorage repair", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: {"title": {"equal": "title"}}
});
Storage2713.prototype.repair = function (id_list) {
deepEqual(id_list, ["foo", "bar"], "repair 2713 called");
return "foobar";
};
jio.repair(["foo", "bar"])
.then(function (result) {
equal(result, "foobar", "Check repair");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob));
\ 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