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 @@
attachments: {}
};
}
this._database[id].doc = JSON.stringify(metadata);
this._database[id].doc = metadata;
return id;
};
MemoryStorage.prototype.get = function (id) {
try {
return JSON.parse(this._database[id].doc);
return this._database[id].doc;
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError(
......@@ -133,18 +133,26 @@
MemoryStorage.prototype.hasCapacity = function (name) {
return (name === "list");
return ((name === "list") || (name === "include"));
};
MemoryStorage.prototype.buildQuery = function () {
MemoryStorage.prototype.buildQuery = function (options) {
var rows = [],
i;
for (i in this._database) {
if (this._database.hasOwnProperty(i)) {
if (options.include_docs === true) {
rows.push({
id: i,
value: {},
doc: this._database[i]
});
} else {
rows.push({
id: i,
value: {}
});
}
}
}
......
......@@ -58,9 +58,9 @@
equal(uuid, "put1");
deepEqual(test.jio.__storage._database.put1, {
attachments: {},
doc: JSON.stringify({
doc: {
"title": "myPut1"
})
}
});
})
.fail(function (error) {
......@@ -88,9 +88,9 @@
deepEqual(test.jio.__storage._database.put1, {
"foo": "bar",
"attachments": {"foo": "bar"},
doc: JSON.stringify({
doc: {
"title": "myPut2"
})
}
});
})
.fail(function (error) {
......@@ -133,7 +133,7 @@
test("get document", function () {
var id = "post1";
this.jio.__storage._database[id] = {
"doc": JSON.stringify({title: "myPost1"})
"doc": {title: "myPost1"}
};
stop();
expect(1);
......@@ -156,7 +156,7 @@
var id = "putattmt1";
this.jio.__storage._database[id] = {
"doc": JSON.stringify({}),
"doc": {},
"attachments": {
putattmt2: undefined
}
......@@ -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));
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