Commit 6ddd86f1 authored by Romain Courteaud's avatar Romain Courteaud

wip indexeddb: use openCUrsor to fetch all blobs

parent 2d255b4e
...@@ -389,7 +389,8 @@ ...@@ -389,7 +389,8 @@
} }
var db_name = this._database_name, var db_name = this._database_name,
start, start,
end; end,
array_buffer_list = [];
start = options.start || 0; start = options.start || 0;
end = options.end; end = options.end;
...@@ -399,25 +400,15 @@ ...@@ -399,25 +400,15 @@
return waitForOpenIndexedDB(db_name, function (db) { return waitForOpenIndexedDB(db_name, function (db) {
return waitForTransaction(db, ["attachment", "blob"], "readonly", return waitForTransaction(db, ["attachment", "blob"], "readonly",
function (tx) { function (tx) {
var promise_list, var key_path = buildKeyPath([id, name]),
key_path = buildKeyPath([id, name]), attachment_store = tx.objectStore("attachment"),
blob_store; blob_store = tx.objectStore("blob");
/* /*
start_index, start_index,
end_index, end_index,
i; i;
*/ */
promise_list = [
// Get the attachment info (mime type)
waitForIDBRequest(tx.objectStore("attachment").get(
key_path
))
];
blob_store = tx.objectStore("blob");
function getBlob(cursor) { function getBlob(cursor) {
var index = parseInt( var index = parseInt(
cursor.primaryKey.slice(key_path.length + 1), cursor.primaryKey.slice(key_path.length + 1),
...@@ -425,27 +416,30 @@ ...@@ -425,27 +416,30 @@
), ),
i = index; i = index;
// Extend array size // Extend array size
while (i > promise_list.length - 1) { while (i > array_buffer_list.length) {
promise_list.push(null); array_buffer_list.push(null);
i -= 1; i -= 1;
} }
// Sort the blob by their index // Sort the blob by their index
promise_list.splice( array_buffer_list.splice(
index + 1, index,
0, 0,
waitForIDBRequest(blob_store.get(cursor.primaryKey)) cursor.value.blob
); );
} }
// Get all needed blobs return RSVP.all([
return waitForAllSynchronousCursor( // Get the attachment info (mime type)
blob_store.index("_id_attachment") waitForIDBRequest(attachment_store.get(
.openKeyCursor(IDBKeyRange.only([id, name])), key_path
getBlob )),
) // Get all needed blobs
.then(function () { waitForAllSynchronousCursor(
return RSVP.all(promise_list); blob_store.index("_id_attachment")
}); .openCursor(IDBKeyRange.only([id, name])),
getBlob
)
]);
}); });
}); });
...@@ -488,9 +482,7 @@ ...@@ -488,9 +482,7 @@
// No need to keep the IDB open // No need to keep the IDB open
var blob, var blob,
index, index,
attachment = result_list[0].target.result, attachment = result_list[0].target.result;
array_buffer_list = [],
i;
// Should raise if key is not good // Should raise if key is not good
if (!attachment) { if (!attachment) {
...@@ -502,9 +494,6 @@ ...@@ -502,9 +494,6 @@
); );
} }
for (i = 1; i < result_list.length; i += 1) {
array_buffer_list.push(result_list[i].target.result.blob);
}
if ((options.start === undefined) && (options.end === undefined)) { if ((options.start === undefined) && (options.end === undefined)) {
blob = new Blob(array_buffer_list, blob = new Blob(array_buffer_list,
{type: attachment.info.content_type}); {type: attachment.info.content_type});
......
...@@ -1223,6 +1223,8 @@ ...@@ -1223,6 +1223,8 @@
context.spy_index = sinon.spy(IDBObjectStore.prototype, "index"); context.spy_index = sinon.spy(IDBObjectStore.prototype, "index");
context.spy_create_index = sinon.spy(IDBObjectStore.prototype, context.spy_create_index = sinon.spy(IDBObjectStore.prototype,
"createIndex"); "createIndex");
context.spy_cursor = sinon.spy(IDBIndex.prototype, "openCursor");
context.spy_key_cursor = sinon.spy(IDBIndex.prototype, "openKeyCursor");
return context.jio.getAttachment("foo", attachment); return context.jio.getAttachment("foo", attachment);
}) })
...@@ -1252,17 +1254,24 @@ ...@@ -1252,17 +1254,24 @@
deepEqual(context.spy_store.secondCall.args[0], "blob", deepEqual(context.spy_store.secondCall.args[0], "blob",
"store first argument"); "store first argument");
equal(context.spy_get.callCount, 3, "get count " + equal(context.spy_get.callCount, 1, "get count " +
context.spy_get.callCount); context.spy_get.callCount);
deepEqual(context.spy_get.firstCall.args[0], "foo_attachment", deepEqual(context.spy_get.firstCall.args[0], "foo_attachment",
"get first argument"); "get first argument");
deepEqual(context.spy_get.secondCall.args[0], "foo_attachment_0",
"get first argument");
deepEqual(context.spy_get.thirdCall.args[0], "foo_attachment_1",
"get first argument");
ok(context.spy_index.called, "index count " + ok(context.spy_index.called, "index count " +
context.spy_index.callCount); context.spy_index.callCount);
equal(context.spy_cursor.callCount, 1, "cursor count " +
context.spy_cursor.callCount);
ok(!context.spy_key_cursor.called, "cursor key count " +
context.spy_key_cursor.callCount);
ok(context.spy_key_range.calledOnce, "key range count " +
context.spy_key_range.callCount);
deepEqual(context.spy_key_range.firstCall.args[0],
["foo", "attachment"],
"key range first argument");
}) })
.always(function () { .always(function () {
context.spy_open.restore(); context.spy_open.restore();
...@@ -1279,6 +1288,10 @@ ...@@ -1279,6 +1288,10 @@
delete context.spy_index; delete context.spy_index;
context.spy_create_index.restore(); context.spy_create_index.restore();
delete context.spy_create_index; delete context.spy_create_index;
context.spy_cursor.restore();
delete context.spy_cursor;
context.spy_key_cursor.restore();
delete context.spy_key_cursor;
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
......
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