Commit 40bae9df authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: add repair, delete default_dict and add prop default_value

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