Commit 0b1dc6ee authored by Romain Courteaud's avatar Romain Courteaud

MemoryStorage: speed up get and allDocs

Add support to the include_docs parameter in allDocs.
Drop JSON convertion and keep document as is in memory.
parent eb77c9c3
...@@ -39,13 +39,13 @@ ...@@ -39,13 +39,13 @@
attachments: {} attachments: {}
}; };
} }
this._database[id].doc = JSON.stringify(metadata); this._database[id].doc = metadata;
return id; return id;
}; };
MemoryStorage.prototype.get = function (id) { MemoryStorage.prototype.get = function (id) {
try { try {
return JSON.parse(this._database[id].doc); return this._database[id].doc;
} catch (error) { } catch (error) {
if (error instanceof TypeError) { if (error instanceof TypeError) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
...@@ -133,18 +133,26 @@ ...@@ -133,18 +133,26 @@
MemoryStorage.prototype.hasCapacity = function (name) { MemoryStorage.prototype.hasCapacity = function (name) {
return (name === "list"); return ((name === "list") || (name === "include"));
}; };
MemoryStorage.prototype.buildQuery = function () { MemoryStorage.prototype.buildQuery = function (options) {
var rows = [], var rows = [],
i; i;
for (i in this._database) { for (i in this._database) {
if (this._database.hasOwnProperty(i)) { if (this._database.hasOwnProperty(i)) {
rows.push({ if (options.include_docs === true) {
id: i, rows.push({
value: {} id: i,
}); value: {},
doc: this._database[i]
});
} else {
rows.push({
id: i,
value: {}
});
}
} }
} }
......
...@@ -58,9 +58,9 @@ ...@@ -58,9 +58,9 @@
equal(uuid, "put1"); equal(uuid, "put1");
deepEqual(test.jio.__storage._database.put1, { deepEqual(test.jio.__storage._database.put1, {
attachments: {}, attachments: {},
doc: JSON.stringify({ doc: {
"title": "myPut1" "title": "myPut1"
}) }
}); });
}) })
.fail(function (error) { .fail(function (error) {
...@@ -88,9 +88,9 @@ ...@@ -88,9 +88,9 @@
deepEqual(test.jio.__storage._database.put1, { deepEqual(test.jio.__storage._database.put1, {
"foo": "bar", "foo": "bar",
"attachments": {"foo": "bar"}, "attachments": {"foo": "bar"},
doc: JSON.stringify({ doc: {
"title": "myPut2" "title": "myPut2"
}) }
}); });
}) })
.fail(function (error) { .fail(function (error) {
...@@ -133,7 +133,7 @@ ...@@ -133,7 +133,7 @@
test("get document", function () { test("get document", function () {
var id = "post1"; var id = "post1";
this.jio.__storage._database[id] = { this.jio.__storage._database[id] = {
"doc": JSON.stringify({title: "myPost1"}) "doc": {title: "myPost1"}
}; };
stop(); stop();
expect(1); expect(1);
...@@ -156,7 +156,7 @@ ...@@ -156,7 +156,7 @@
var id = "putattmt1"; var id = "putattmt1";
this.jio.__storage._database[id] = { this.jio.__storage._database[id] = {
"doc": JSON.stringify({}), "doc": {},
"attachments": { "attachments": {
putattmt2: undefined putattmt2: undefined
} }
...@@ -554,4 +554,40 @@ ...@@ -554,4 +554,40 @@
}); });
}); });
test("list documents with include_docs", function () {
this.jio.__storage._database.foo2 = "bar2";
this.jio.__storage._database.foo1 = "bar1";
stop();
expect(1);
this.jio.allDocs({include_docs: true})
.then(function (result) {
deepEqual(result, {
"data": {
"rows": [
{
"id": "foo2",
"value": {},
"doc": "bar2"
},
{
"id": "foo1",
"value": {},
"doc": "bar1"
}
],
"total_rows": 2
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob)); }(jIO, QUnit, Blob));
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