Commit 6dbd0d97 authored by Tristan Cavelier's avatar Tristan Cavelier

Improve querystorage allDocs speed

parent 52472bef
...@@ -144,41 +144,63 @@ ...@@ -144,41 +144,63 @@
// remove them later if they were not required. // remove them later if they were not required.
include_docs = (options.include_docs || options.query) ? true : false; include_docs = (options.include_docs || options.query) ? true : false;
substorage.allDocs({include_docs: include_docs}). substorage.allDocs({
then(function (response) { "include_docs": include_docs
}).then(function (response) {
if (options.query) { var data_rows = response.data.rows, docs = {}, row, i, l;
var docs = response.data.rows.map(function (row) {
return row.doc;
}), rows_map = {};
// build a mapping to avoid slowness if (!include_docs) {
response.data.rows.forEach(function (row) { return response;
rows_map[row.id] = row; }
});
if (options.include_docs) {
for (i = 0, l = data_rows.length; i < l; i += 1) {
row = data_rows[i];
docs[row.id] = JSON.parse(JSON.stringify(row.doc));
row.doc._id = row.id;
data_rows[i] = row.doc;
}
} else {
for (i = 0, l = data_rows.length; i < l; i += 1) {
row = data_rows[i];
row.doc._id = row.id;
data_rows[i] = row.doc;
}
}
if (options.select_list) {
options.select_list.push("_id");
}
return jIO.QueryFactory.create(options.query, return jIO.QueryFactory.create(options.query || "", that._key_schema).
that._key_schema). exec(data_rows, options).
exec(docs, options).
then(function (filtered_docs) { then(function (filtered_docs) {
// reconstruct filtered rows, preserving the order from docs // reconstruct filtered rows, preserving the order from docs
var rows = filtered_docs.map(function (doc) { if (options.include_docs) {
var row = rows_map[doc._id]; for (i = 0, l = filtered_docs.length; i < l; i += 1) {
// remove docs if not needed in the original call filtered_docs[i] = {
if (!options.include_docs) { "id": filtered_docs[i]._id,
delete row.doc; "doc": docs[filtered_docs[i]._id],
"value": options.select_list ? filtered_docs[i] : {}
};
delete filtered_docs[i].value._id;
} }
return row; } else {
}); for (i = 0, l = filtered_docs.length; i < l; i += 1) {
response.data.rows = rows; filtered_docs[i] = {
response.data.total_rows = rows.length; "id": filtered_docs[i]._id,
"value": options.select_list ? filtered_docs[i] : {}
};
delete filtered_docs[i].value._id;
}
}
response.data.rows = filtered_docs;
response.data.total_rows = filtered_docs.length;
return response; return response;
}); });
}
return RSVP.resolve(response);
}). }).then(command.success, command.error, command.notify);
then(command.success, command.error, command.notify);
}; };
......
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