Commit 0b0e0149 authored by lucas.parsy's avatar lucas.parsy

removed useless table in websqlstorage

added indexes to increase queries speed.
parent fbdd12ad
...@@ -55,21 +55,17 @@ ...@@ -55,21 +55,17 @@
"CREATE TABLE IF NOT EXISTS document" + "CREATE TABLE IF NOT EXISTS document" +
"(id VARCHAR PRIMARY KEY NOT NULL, data TEXT)", "(id VARCHAR PRIMARY KEY NOT NULL, data TEXT)",
"CREATE TABLE IF NOT EXISTS attachment" + "CREATE TABLE IF NOT EXISTS attachment" +
"(id VARCHAR, attachment VARCHAR, " +
"CONSTRAINT uniq UNIQUE (id, attachment))",
"CREATE TABLE IF NOT EXISTS blob" +
"(id VARCHAR, attachment VARCHAR, part INT, blob TEXT)", "(id VARCHAR, attachment VARCHAR, part INT, blob TEXT)",
"CREATE TRIGGER IF NOT EXISTS remove " +
"BEFORE DELETE ON document FOR EACH ROW BEGIN DELETE " +
"FROM attachment WHERE id = OLD.id;END;",
"CREATE TRIGGER IF NOT EXISTS removeAttachment " + "CREATE TRIGGER IF NOT EXISTS removeAttachment " +
"BEFORE DELETE ON attachment FOR EACH ROW " + "BEFORE DELETE ON document FOR EACH ROW " +
"BEGIN DELETE from blob WHERE id = OLD.id " + "BEGIN DELETE from attachment WHERE id = OLD.id;END;",
"AND attachment = OLD.attachment;END;" "CREATE INDEX IF NOT EXISTS index_document ON document (id);",
"CREATE INDEX IF NOT EXISTS index_attachment " +
"ON attachment (id, attachment);"
]; ];
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return queueSql(db, query_list, [[], [], [], [], []]); return queueSql(db, query_list, []);
}); });
} }
...@@ -159,7 +155,7 @@ ...@@ -159,7 +155,7 @@
.push(function () { .push(function () {
return queueSql(db, [ return queueSql(db, [
"SELECT id FROM document WHERE id = ?", "SELECT id FROM document WHERE id = ?",
"SELECT attachment FROM attachment WHERE id = ?" "SELECT DISTINCT attachment FROM attachment WHERE id = ?"
], [[id], [id]]); ], [[id], [id]]);
}) })
.push(function (result_list) { .push(function (result_list) {
...@@ -183,7 +179,7 @@ ...@@ -183,7 +179,7 @@
return jIO.util.readBlobAsDataURL(blob); return jIO.util.readBlobAsDataURL(blob);
}) })
.push(function (strBlob) { .push(function (strBlob) {
argument_list[index + 3].push(strBlob.currentTarget.result); argument_list[index + 2].push(strBlob.currentTarget.result);
return; return;
}); });
} }
...@@ -211,18 +207,16 @@ ...@@ -211,18 +207,16 @@
if (result[0].rows.length === 0) { if (result[0].rows.length === 0) {
throw new jIO.util.jIOError("Cannot access subdocument", 404); throw new jIO.util.jIOError("Cannot access subdocument", 404);
} }
query_list.push("INSERT OR REPLACE INTO attachment(id, attachment)" + query_list.push("DELETE FROM attachment WHERE id = ? " +
" VALUES(?, ?)"); "AND attachment = ?");
argument_list.push([id, name]); argument_list.push([id, name]);
query_list.push("DELETE FROM blob WHERE id = ? AND attachment = ?"); query_list.push("INSERT INTO attachment(id, attachment, part, blob)" +
argument_list.push([id, name]);
query_list.push("INSERT INTO blob(id, attachment, part, blob)" +
"VALUES(?, ?, ?, ?)"); "VALUES(?, ?, ?, ?)");
argument_list.push([id, name, -1, argument_list.push([id, name, -1,
blob.type || "application/octet-stream"]); blob.type || "application/octet-stream"]);
for (i = 0, index = 0; i < blob_size; i += part_size, index += 1) { for (i = 0, index = 0; i < blob_size; i += part_size, index += 1) {
query_list.push("INSERT INTO blob(id, attachment, part, blob)" + query_list.push("INSERT INTO attachment(id, attachment, part, blob)" +
"VALUES(?, ?, ?, ?)"); "VALUES(?, ?, ?, ?)");
argument_list.push([id, name, index]); argument_list.push([id, name, index]);
sendBlobPart(blob.slice(i, i + part_size), argument_list, index, sendBlobPart(blob.slice(i, i + part_size), argument_list, index,
...@@ -269,7 +263,7 @@ ...@@ -269,7 +263,7 @@
return that._init_db_promise; return that._init_db_promise;
}) })
.push(function () { .push(function () {
var command = "SELECT part, blob FROM blob WHERE id = ? AND " + var command = "SELECT part, blob FROM attachment WHERE id = ? AND " +
"attachment = ? AND part >= ?", "attachment = ? AND part >= ?",
argument_list = [id, name, start_index]; argument_list = [id, name, start_index];
......
...@@ -47,18 +47,15 @@ ...@@ -47,18 +47,15 @@
'KEY NOT NULL, data TEXT)'); 'KEY NOT NULL, data TEXT)');
equal(context.spy.args[1][0], equal(context.spy.args[1][0],
'CREATE TABLE IF NOT EXISTS attachment(id VARCHAR, attachment' + 'CREATE TABLE IF NOT EXISTS attachment(id VARCHAR, attachment' +
' VARCHAR, CONSTRAINT uniq UNIQUE (id, attachment))');
equal(context.spy.args[2][0],
'CREATE TABLE IF NOT EXISTS blob(id VARCHAR, attachment' +
' VARCHAR, part INT, blob TEXT)'); ' VARCHAR, part INT, blob TEXT)');
equal(context.spy.args[2][0],
"CREATE TRIGGER IF NOT EXISTS removeAttachment " +
"BEFORE DELETE ON document FOR EACH ROW " +
"BEGIN DELETE from attachment WHERE id = OLD.id;END;");
equal(context.spy.args[3][0], equal(context.spy.args[3][0],
'CREATE TRIGGER IF NOT EXISTS remove BEFORE DELETE ON ' + "CREATE INDEX IF NOT EXISTS index_document ON document (id);");
'document FOR EACH ROW BEGIN DELETE FROM attachment ' + equal(context.spy.args[4][0], "CREATE INDEX IF NOT EXISTS " +
'WHERE id = OLD.id;END;'); "index_attachment ON attachment (id, attachment);");
equal(context.spy.args[4][0],
'CREATE TRIGGER IF NOT EXISTS removeAttachment BEFORE DELETE ON ' +
'attachment FOR EACH ROW BEGIN DELETE from blob WHERE id = OLD.id ' +
'AND attachment = OLD.attachment;END;');
} }
function deleteRow(it, queue, result) { function deleteRow(it, queue, result) {
...@@ -542,7 +539,7 @@ ...@@ -542,7 +539,7 @@
'SELECT id FROM document WHERE id = ?'); 'SELECT id FROM document WHERE id = ?');
deepEqual(context.spy.args[6][1], ['foo']); deepEqual(context.spy.args[6][1], ['foo']);
equal(context.spy.args[7][0], equal(context.spy.args[7][0],
'SELECT attachment FROM attachment WHERE id = ?'); 'SELECT DISTINCT attachment FROM attachment WHERE id = ?');
deepEqual(context.spy.args[7][1], ['foo']); deepEqual(context.spy.args[7][1], ['foo']);
}) })
.then(function () { .then(function () {
...@@ -769,7 +766,7 @@ ...@@ -769,7 +766,7 @@
test("spy webSQL usage with two attachments", function () { test("spy webSQL usage with two attachments", function () {
var context = this; var context = this;
stop(); stop();
expect(30); expect(26);
context.spy.then(function (value) { context.spy.then(function (value) {
context.spy = value; context.spy = value;
return; return;
...@@ -795,41 +792,36 @@ ...@@ -795,41 +792,36 @@
'SELECT id FROM document WHERE id = ?'); 'SELECT id FROM document WHERE id = ?');
deepEqual(context.spy.args[6][1], ['foo']); deepEqual(context.spy.args[6][1], ['foo']);
equal(context.spy.args[7][0], equal(context.spy.args[7][0],
'INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)'); 'DELETE FROM attachment WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[7][1], ['foo', 'attachment1']); deepEqual(context.spy.args[7][1], ['foo', 'attachment1']);
equal(context.spy.args[8][0], equal(context.spy.args[8][0],
'DELETE FROM blob WHERE id = ? AND attachment = ?'); 'INSERT INTO attachment(id, attachment, ' +
deepEqual(context.spy.args[8][1], ['foo', 'attachment1']); 'part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[8][1],
['foo', 'attachment1', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[9][0], equal(context.spy.args[9][0],
'INSERT INTO blob(id, attachment, ' + 'INSERT INTO attachment(id, attachment, ' +
'part, blob)VALUES(?, ?, ?, ?)'); 'part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[9][1], deepEqual(context.spy.args[9][1],
['foo', 'attachment1', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[10][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[10][1],
['foo', 'attachment1', 0, 'data:;base64,YmFy']); ['foo', 'attachment1', 0, 'data:;base64,YmFy']);
equal(context.spy.args[11][0], equal(context.spy.args[10][0],
'SELECT id FROM document WHERE id = ?'); 'SELECT id FROM document WHERE id = ?');
deepEqual(context.spy.args[11][1], ['foo']); deepEqual(context.spy.args[10][1], ['foo']);
equal(context.spy.args[11][0],
'DELETE FROM attachment WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[11][1], ['foo', 'attachment2']);
equal(context.spy.args[12][0], equal(context.spy.args[12][0],
'INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)'); 'INSERT INTO attachment(id, attachment, ' +
deepEqual(context.spy.args[12][1], ['foo', 'attachment2']);
equal(context.spy.args[13][0],
'DELETE FROM blob WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[13][1], ['foo', 'attachment2']);
equal(context.spy.args[14][0],
'INSERT INTO blob(id, attachment, ' +
'part, blob)VALUES(?, ?, ?, ?)'); 'part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[14][1], deepEqual(context.spy.args[12][1],
['foo', 'attachment2', -1, 'text/plain;charset=utf-8']); ['foo', 'attachment2', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[15][0], equal(context.spy.args[13][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)'); 'INSERT INTO attachment(id, attachment, ' +
deepEqual(context.spy.args[15][1], 'part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[13][1],
['foo', 'attachment2', 0, 'data:;base64,YmFyMg==']); ['foo', 'attachment2', 0, 'data:;base64,YmFyMg==']);
equal(context.spy.args[16][0], 'DELETE FROM document WHERE id = ?'); equal(context.spy.args[14][0], 'DELETE FROM document WHERE id = ?');
deepEqual(context.spy.args[16][1], ['foo']); deepEqual(context.spy.args[14][1], ['foo']);
}) })
.then(function () { .then(function () {
return deleteWebsql(); return deleteWebsql();
...@@ -902,7 +894,7 @@ ...@@ -902,7 +894,7 @@
test("spy webSQL usage", function () { test("spy webSQL usage", function () {
var context = this; var context = this;
stop(); stop();
expect(22); expect(20);
context.spy.then(function (value) { context.spy.then(function (value) {
context.spy = value; context.spy = value;
return; return;
...@@ -926,30 +918,29 @@ ...@@ -926,30 +918,29 @@
'SELECT id FROM document WHERE id = ?'); 'SELECT id FROM document WHERE id = ?');
deepEqual(context.spy.args[6][1], ['foo']); deepEqual(context.spy.args[6][1], ['foo']);
equal(context.spy.args[7][0], equal(context.spy.args[7][0],
'INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)'); 'DELETE FROM attachment WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[7][1], ['foo', 'attachment']); deepEqual(context.spy.args[7][1], ['foo', 'attachment']);
equal(context.spy.args[8][0], equal(context.spy.args[8][0],
'DELETE FROM blob WHERE id = ? AND attachment = ?'); 'INSERT INTO attachment(id, attachment, part, blob)' +
deepEqual(context.spy.args[8][1], ['foo', 'attachment']);
equal(context.spy.args[9][0],
'INSERT INTO blob(id, attachment, part, blob)' +
'VALUES(?, ?, ?, ?)'); 'VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[9][1], deepEqual(context.spy.args[8][1],
['foo', 'attachment', -1, 'text/plain;charset=utf-8']); ['foo', 'attachment', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[10][0], equal(context.spy.args[9][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)'); 'INSERT INTO attachment(id, attachment, ' +
deepEqual(context.spy.args[10][1], 'part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[9][1],
['foo', 'attachment', 0, ['foo', 'attachment', 0,
'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']); 'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']);
equal(context.spy.args[11][0], equal(context.spy.args[10][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)'); 'INSERT INTO attachment(id, attachment, ' +
deepEqual(context.spy.args[11][1], 'part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[10][1],
['foo', 'attachment', 1, ['foo', 'attachment', 1,
'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']); 'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']);
equal(context.spy.args[12][0], equal(context.spy.args[11][0],
'SELECT part, blob FROM blob WHERE id = ? ' + 'SELECT part, blob FROM attachment WHERE id = ? ' +
'AND attachment = ? AND part >= ?'); 'AND attachment = ? AND part >= ?');
deepEqual(context.spy.args[12][1], ['foo', 'attachment', -1]); deepEqual(context.spy.args[11][1], ['foo', 'attachment', -1]);
}) })
.then(function () { .then(function () {
return deleteWebsql(); return deleteWebsql();
...@@ -1058,7 +1049,7 @@ ...@@ -1058,7 +1049,7 @@
test("spy webSQL usage", function () { test("spy webSQL usage", function () {
var context = this; var context = this;
stop(); stop();
expect(20); expect(18);
context.spy.then(function (value) { context.spy.then(function (value) {
context.spy = value; context.spy = value;
return; return;
...@@ -1082,25 +1073,23 @@ ...@@ -1082,25 +1073,23 @@
'SELECT id FROM document WHERE id = ?'); 'SELECT id FROM document WHERE id = ?');
deepEqual(context.spy.args[6][1], ['foo']); deepEqual(context.spy.args[6][1], ['foo']);
equal(context.spy.args[7][0], equal(context.spy.args[7][0],
'INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)'); 'DELETE FROM attachment WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[7][1], ['foo', 'attachment']); deepEqual(context.spy.args[7][1], ['foo', 'attachment']);
equal(context.spy.args[8][0], equal(context.spy.args[8][0],
'DELETE FROM blob WHERE id = ? AND attachment = ?'); 'INSERT INTO attachment(id, attachment, part, blob)' +
deepEqual(context.spy.args[8][1], ['foo', 'attachment']);
equal(context.spy.args[9][0],
'INSERT INTO blob(id, attachment, part, blob)' +
'VALUES(?, ?, ?, ?)'); 'VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[9][1], deepEqual(context.spy.args[8][1],
['foo', 'attachment', -1, 'text/plain;charset=utf-8']); ['foo', 'attachment', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[10][0], equal(context.spy.args[9][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)'); 'INSERT INTO attachment(id, attachment, ' +
deepEqual(context.spy.args[10][1], 'part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[9][1],
['foo', 'attachment', 0, ['foo', 'attachment', 0,
"data:;base64,YWFhYWFhYWFhYWFhYWFhYWF" + "data:;base64,YWFhYWFhYWFhYWFhYWFhYWF" +
"hYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ=="]); "hYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ=="]);
equal(context.spy.args[11][0], equal(context.spy.args[10][0],
'DELETE FROM attachment WHERE id = ? AND attachment = ?'); 'DELETE FROM attachment WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[11][1], ['foo', 'attachment']); deepEqual(context.spy.args[10][1], ['foo', 'attachment']);
}) })
.then(function () { .then(function () {
return deleteWebsql(); return deleteWebsql();
...@@ -1132,19 +1121,17 @@ ...@@ -1132,19 +1121,17 @@
return context.jio.putAttachment("foo", attachment, big_string); return context.jio.putAttachment("foo", attachment, big_string);
}) })
.then(function () { .then(function () {
return exec("SELECT id, attachment FROM attachment UNION ALL" + return exec("SELECT id, attachment FROM attachment", []);
" SELECT id, attachment FROM blob", []);
}) })
.then(function (selectResult) { .then(function (selectResult) {
equal(selectResult.rows.length, 3, "putAttachment done"); equal(selectResult.rows.length, 2, "putAttachment done");
}) })
.then(function () { .then(function () {
return context.jio.removeAttachment("foo", attachment); return context.jio.removeAttachment("foo", attachment);
}) })
.then(function (result) { .then(function (result) {
equal(result, attachment); equal(result, attachment);
return exec("SELECT id, attachment FROM attachment UNION ALL" + return exec("SELECT id, attachment FROM attachment", []);
" SELECT id, attachment FROM blob", []);
}) })
.then(function (selectResult) { .then(function (selectResult) {
equal(selectResult.rows.length, 0, "removeAttachment done"); equal(selectResult.rows.length, 0, "removeAttachment done");
...@@ -1178,7 +1165,7 @@ ...@@ -1178,7 +1165,7 @@
test("spy webSQL usage", function () { test("spy webSQL usage", function () {
var context = this; var context = this;
stop(); stop();
expect(20); expect(18);
context.spy.then(function (value) { context.spy.then(function (value) {
context.spy = value; context.spy = value;
return; return;
...@@ -1199,24 +1186,23 @@ ...@@ -1199,24 +1186,23 @@
'SELECT id FROM document WHERE id = ?'); 'SELECT id FROM document WHERE id = ?');
deepEqual(context.spy.args[6][1], ['foo']); deepEqual(context.spy.args[6][1], ['foo']);
equal(context.spy.args[7][0], equal(context.spy.args[7][0],
'INSERT OR REPLACE INTO attachment(id, attachment) VALUES(?, ?)'); 'DELETE FROM attachment WHERE id = ? AND attachment = ?');
deepEqual(context.spy.args[7][1], ['foo', 'attachment']); deepEqual(context.spy.args[7][1], ['foo', 'attachment']);
equal(context.spy.args[8][0], equal(context.spy.args[8][0],
'DELETE FROM blob WHERE id = ? AND attachment = ?'); 'INSERT INTO attachment(id, attachment, part, blob)' +
deepEqual(context.spy.args[8][1], ['foo', 'attachment']);
equal(context.spy.args[9][0],
'INSERT INTO blob(id, attachment, part, blob)' +
'VALUES(?, ?, ?, ?)'); 'VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[9][1], deepEqual(context.spy.args[8][1],
['foo', 'attachment', -1, 'text/plain;charset=utf-8']); ['foo', 'attachment', -1, 'text/plain;charset=utf-8']);
equal(context.spy.args[10][0], equal(context.spy.args[9][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)'); 'INSERT INTO attachment(id, attachment, ' +
deepEqual(context.spy.args[10][1], 'part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[9][1],
['foo', 'attachment', 0, ['foo', 'attachment', 0,
'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']); 'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']);
equal(context.spy.args[11][0], equal(context.spy.args[10][0],
'INSERT INTO blob(id, attachment, part, blob)VALUES(?, ?, ?, ?)'); 'INSERT INTO attachment(id, attachment, ' +
deepEqual(context.spy.args[11][1], 'part, blob)VALUES(?, ?, ?, ?)');
deepEqual(context.spy.args[10][1],
['foo', 'attachment', 1, ['foo', 'attachment', 1,
'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']); 'data:;base64,YWFhYWFhYWFhYWFhYWFhYWFhYWE=']);
}) })
...@@ -1250,11 +1236,10 @@ ...@@ -1250,11 +1236,10 @@
return context.jio.putAttachment("foo", attachment, big_string); return context.jio.putAttachment("foo", attachment, big_string);
}) })
.then(function () { .then(function () {
return exec("SELECT id, attachment FROM attachment UNION ALL" + return exec("SELECT id, attachment FROM attachment", []);
" SELECT id, attachment FROM blob", []);
}) })
.then(function (selectResult) { .then(function (selectResult) {
equal(selectResult.rows.length, 4, "putAttachment done"); equal(selectResult.rows.length, 3, "putAttachment done");
}) })
.then(function () { .then(function () {
return context.jio.getAttachment("foo", attachment); return context.jio.getAttachment("foo", attachment);
......
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