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

wip indexeddb: use openCUrsor to fetch all blobs

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