Bug 834449 - Part 2: clean up cached statements in Sqlite.jsm. r=gps

This commit is contained in:
Richard Newman 2013-01-25 00:38:59 -08:00
parent c80442d777
commit d27027b2fd
2 changed files with 67 additions and 0 deletions

View File

@ -731,6 +731,26 @@ OpenedConnection.prototype = Object.freeze({
return this.execute("PRAGMA shrink_memory").then(onShrunk, onShrunk);
},
/**
* Discard all inactive cached statements.
*
* @return (integer) the number of statements discarded.
*/
discardCachedStatements: function () {
let count = 0;
for (let [k, statement] of this._cachedStatements) {
if (this.inProgress(statement)) {
continue;
}
++count;
this._cachedStatements.delete(k);
statement.finalize();
}
this._log.debug("Discarded " + count + " cached statements.");
return count;
},
_executeStatement: function (sql, statement, params, onRow) {
if (statement.state != statement.MOZ_STORAGE_STATEMENT_READY) {
throw new Error("Statement is not ready for execution.");

View File

@ -528,3 +528,50 @@ add_task(function test_in_progress_counts() {
yield c.close();
});
add_task(function test_discard_while_active() {
let c = yield getDummyDatabase("discard_while_active");
yield c.executeCached("INSERT INTO dirs (path) VALUES ('foo')");
yield c.executeCached("INSERT INTO dirs (path) VALUES ('bar')");
let discarded = -1;
let first = true;
let sql = "SELECT * FROM dirs";
yield c.executeCached(sql, null, function onRow(row) {
if (!first) {
return;
}
first = false;
discarded = c.discardCachedStatements();
});
// We didn't discard the SELECT, only the two INSERTs.
do_check_eq(2, discarded);
// But we got the remainder when it became inactive.
do_check_eq(1, c.discardCachedStatements());
// And again is safe.
do_check_eq(0, c.discardCachedStatements());
yield c.close();
});
add_task(function test_discard_cached() {
let c = yield getDummyDatabase("discard_cached");
yield c.executeCached("SELECT * from dirs");
do_check_eq(1, c._cachedStatements.size);
yield c.executeCached("SELECT * from files");
do_check_eq(2, c._cachedStatements.size);
yield c.executeCached("SELECT * from dirs");
do_check_eq(2, c._cachedStatements.size);
c.discardCachedStatements();
do_check_eq(0, c._cachedStatements.size);
yield c.close();
});