Commit e29efa38 authored by Romain Courteaud's avatar Romain Courteaud

wip indexeddb

parent 1f09d1ac
......@@ -346,25 +346,33 @@
return waitForTransaction(db, ["metadata", "attachment", "blob"],
"readwrite", function (tx) {
var promise_list = [];
var promise_list = [],
metadata_store = tx.objectStore("metadata"),
attachment_store = tx.objectStore("attachment"),
blob_store = tx.objectStore("blob");
function deleteEntry(cursor) {
function deleteAttachment(cursor) {
promise_list.push(
waitForIDBRequest(attachment_store.delete(cursor.primaryKey))
);
}
function deleteBlob(cursor) {
promise_list.push(
waitForIDBRequest(cursor.delete())
waitForIDBRequest(blob_store.delete(cursor.primaryKey))
);
}
return RSVP.all([
waitForIDBRequest(tx.objectStore("metadata").delete(id)),
// XXX Why not possible to delete with KeyCursor?
waitForIDBRequest(metadata_store.delete(id)),
waitForAllSynchronousCursor(
tx.objectStore("attachment").index("_id")
.openCursor(IDBKeyRange.only(id)),
deleteEntry
attachment_store.index("_id")
.openKeyCursor(IDBKeyRange.only(id)),
deleteAttachment
),
waitForAllSynchronousCursor(
tx.objectStore("blob").index("_id")
.openCursor(IDBKeyRange.only(id)),
deleteEntry
blob_store.index("_id")
.openKeyCursor(IDBKeyRange.only(id)),
deleteBlob
),
])
.then(function () {
......@@ -498,7 +506,6 @@
array_buffer_list.push(result_list[i].target.result.blob);
}
// total_length = attachment.info.length;
console.log('array_buffer_list', array_buffer_list);
if ((options.start === undefined) && (options.end === undefined)) {
return new Blob(array_buffer_list,
{type: attachment.info.content_type});
......@@ -511,30 +518,6 @@
};
function removeAttachment(tx, id, name) {
var promise_list = [];
function deleteEntry(cursor) {
promise_list.push(
waitForIDBRequest(cursor.delete())
);
}
return RSVP.all([
waitForIDBRequest(
tx.objectStore("attachment").delete(buildKeyPath([id, name]))
),
waitForAllSynchronousCursor(
tx.objectStore("blob").index("_id_attachment")
.openCursor(IDBKeyRange.only([id, name])),
deleteEntry
)
])
.then(function () {
return RSVP.all(promise_list);
});
}
IndexedDBStorage.prototype.putAttachment = function (id, name, blob) {
var db_name = this._database_name;
return new RSVP.Queue()
......@@ -557,9 +540,6 @@
return waitForOpenIndexedDB(db_name, function (db) {
return waitForTransaction(db, ["attachment", "blob"], "readwrite",
function (tx) {
// First remove the previous attachment
// return removeAttachment(tx, id, name)
// .then(function () {
var blob_store,
promise_list,
delete_promise_list = [],
......@@ -598,7 +578,7 @@
);
if (index > blob_part.length + 1) {
delete_promise_list.push(
waitForIDBRequest(cursor.delete())
waitForIDBRequest(blob_store.delete(cursor.primaryKey))
);
}
}
......@@ -606,8 +586,8 @@
// Finally, remove all remaining blobs
promise_list.push(
waitForAllSynchronousCursor(
tx.objectStore("blob").index("_id_attachment")
.openCursor(IDBKeyRange.only([id, name])),
blob_store.index("_id_attachment")
.openKeyCursor(IDBKeyRange.only([id, name])),
deleteEntry
)
);
......@@ -628,7 +608,30 @@
return waitForOpenIndexedDB(this._database_name, function (db) {
return waitForTransaction(db, ["attachment", "blob"], "readwrite",
function (tx) {
return removeAttachment(tx, id, name);
var promise_list = [],
attachment_store = tx.objectStore("attachment"),
blob_store = tx.objectStore("blob");
function deleteEntry(cursor) {
promise_list.push(
waitForIDBRequest(blob_store.delete(cursor.primaryKey))
);
}
return RSVP.all([
waitForIDBRequest(
attachment_store.delete(buildKeyPath([id, name]))
),
waitForAllSynchronousCursor(
blob_store.index("_id_attachment")
.openKeyCursor(IDBKeyRange.only([id, name])),
deleteEntry
)
])
.then(function () {
return RSVP.all(promise_list);
});
});
});
};
......
......@@ -942,7 +942,7 @@
test("spy indexedDB usage with one document", function () {
var context = this;
stop();
expect(21);
expect(22);
deleteIndexedDB(context.jio)
.then(function () {
......@@ -960,6 +960,7 @@
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");
context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete");
context.spy_key_range = sinon.spy(IDBKeyRange, "only");
......@@ -1005,9 +1006,11 @@
deepEqual(context.spy_index.secondCall.args[0], "_id",
"index first argument");
ok(context.spy_cursor.calledTwice, "cursor count " +
equal(context.spy_cursor.callCount, 0, "cursor count " +
context.spy_cursor.callCount);
equal(context.spy_cursor_delete.callCount, 0, "cursor count " +
ok(context.spy_key_cursor.calledTwice, "cursor key count " +
context.spy_key_cursor.callCount);
equal(context.spy_cursor_delete.callCount, 0, "cursor delete count " +
context.spy_cursor_delete.callCount);
ok(context.spy_key_range.calledTwice, "key range count " +
......@@ -1033,6 +1036,8 @@
delete context.spy_index;
context.spy_create_index.restore();
delete context.spy_create_index;
context.spy_key_cursor.restore();
delete context.spy_key_cursor;
context.spy_cursor.restore();
delete context.spy_cursor;
context.spy_cursor_delete.restore();
......@@ -1051,7 +1056,7 @@
test("spy indexedDB usage with 2 attachments", function () {
var context = this;
stop();
expect(21);
expect(26);
deleteIndexedDB(context.jio)
.then(function () {
......@@ -1075,6 +1080,7 @@
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");
context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete");
context.spy_key_range = sinon.spy(IDBKeyRange, "only");
......@@ -1107,10 +1113,18 @@
deepEqual(context.spy_store.thirdCall.args[0], "blob",
"store first argument");
equal(context.spy_delete.callCount, 1, "delete count " +
equal(context.spy_delete.callCount, 5, "delete count " +
context.spy_delete.callCount);
deepEqual(context.spy_delete.firstCall.args[0], "foo",
"delete first argument");
deepEqual(context.spy_delete.secondCall.args[0], "foo_attachment1",
"second delete first argument");
deepEqual(context.spy_delete.thirdCall.args[0], "foo_attachment1_0",
"third delete first argument");
deepEqual(context.spy_delete.getCall(3).args[0], "foo_attachment2",
"fourth delete first argument");
deepEqual(context.spy_delete.getCall(4).args[0], "foo_attachment2_0",
"fifth delete first argument");
ok(context.spy_index.calledTwice, "index count " +
context.spy_index.callCount);
......@@ -1119,10 +1133,12 @@
deepEqual(context.spy_index.secondCall.args[0], "_id",
"index first argument");
ok(context.spy_cursor.calledTwice, "cursor count " +
equal(context.spy_cursor.callCount, 0, "cursor count " +
context.spy_cursor.callCount);
ok(context.spy_key_cursor.calledTwice, "cursor key count " +
context.spy_key_cursor.callCount);
equal(context.spy_cursor_delete.callCount, 4, "cursor count " +
equal(context.spy_cursor_delete.callCount, 0, "cursor count " +
context.spy_cursor_delete.callCount);
ok(context.spy_key_range.calledTwice, "key range count " +
......@@ -1148,6 +1164,8 @@
delete context.spy_index;
context.spy_create_index.restore();
delete context.spy_create_index;
context.spy_key_cursor.restore();
delete context.spy_key_cursor;
context.spy_cursor.restore();
delete context.spy_cursor;
context.spy_cursor_delete.restore();
......@@ -1435,7 +1453,7 @@
var context = this,
attachment = "attachment";
stop();
expect(17);
expect(20);
deleteIndexedDB(context.jio)
.then(function () {
......@@ -1457,6 +1475,7 @@
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");
context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete");
context.spy_key_range = sinon.spy(IDBKeyRange, "only");
......@@ -1489,17 +1508,23 @@
deepEqual(context.spy_store.secondCall.args[0], "blob",
"store first argument");
equal(context.spy_delete.callCount, 1, "delete count " +
equal(context.spy_delete.callCount, 3, "delete count " +
context.spy_delete.callCount);
deepEqual(context.spy_delete.firstCall.args[0], "foo_attachment",
"delete first argument");
deepEqual(context.spy_delete.secondCall.args[0], "foo_attachment_0",
"second delete first argument");
deepEqual(context.spy_delete.thirdCall.args[0], "foo_attachment_1",
"third delete first argument");
ok(context.spy_index.calledOnce, "index count " +
context.spy_index.callCount);
ok(context.spy_cursor.calledOnce, "cursor count " +
equal(context.spy_cursor.callCount, 0, "cursor count " +
context.spy_cursor.callCount);
equal(context.spy_cursor_delete.callCount, 2, "cursor count " +
ok(context.spy_key_cursor.calledOnce, "cursor key count " +
context.spy_key_cursor.callCount);
equal(context.spy_cursor_delete.callCount, 0, "cursor count " +
context.spy_cursor_delete.callCount);
ok(context.spy_key_range.calledOnce, "key range count " +
......@@ -1525,6 +1550,8 @@
delete context.spy_create_index;
context.spy_cursor.restore();
delete context.spy_cursor;
context.spy_key_cursor.restore();
delete context.spy_key_cursor;
context.spy_cursor_delete.restore();
delete context.spy_cursor_delete;
context.spy_key_range.restore();
......@@ -1560,6 +1587,9 @@
.then(function () {
return context.jio.put("foo", {"title": "bar"});
})
.then(function () {
return context.jio.putAttachment("foo", attachment, big_string);
})
.then(function () {
context.spy_open = sinon.spy(indexedDB, "open");
context.spy_create_store = sinon.spy(IDBDatabase.prototype,
......@@ -1573,10 +1603,11 @@
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");
context.spy_cursor_delete = sinon.spy(IDBCursor.prototype, "delete");
context.spy_key_range = sinon.spy(IDBKeyRange, "only");
return context.jio.putAttachment("foo", attachment, big_string);
return context.jio.putAttachment("foo", attachment, 'small_string');
})
.fail(function (error) {
ok(false, error);
......@@ -1613,17 +1644,19 @@
deepEqual(context.spy_store.getCall(3).args[0], "blob",
"store first argument");
*/
equal(context.spy_delete.callCount, 0, "delete count " +
equal(context.spy_delete.callCount, 110, "delete count " +
context.spy_delete.callCount);
/*
deepEqual(context.spy_delete.firstCall.args[0], "foo_attachment",
"delete first argument");
*/
equal(context.spy_index.callCount, 0, "index count " +
equal(context.spy_index.callCount, 1, "index count " +
context.spy_index.callCount);
equal(context.spy_cursor.callCount, 0, "cursor count " +
context.spy_cursor.callCount);
equal(context.spy_key_cursor.callCount, 1, "cursor count " +
context.spy_key_cursor.callCount);
/*
equal(context.spy_cursor_delete.callCount, 0, "delete count " +
context.spy_cursor_delete.callCount);
......@@ -1678,6 +1711,8 @@
delete context.spy_create_index;
context.spy_cursor.restore();
delete context.spy_cursor;
context.spy_key_cursor.restore();
delete context.spy_key_cursor;
context.spy_cursor_delete.restore();
delete context.spy_cursor_delete;
context.spy_key_range.restore();
......
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