Browse Source

SQlite: importing changes from uBlock

pull/2/head
gorhill 10 years ago
parent
commit
6f842724be
  1. 43
      platform/firefox/vapi-background.js

43
platform/firefox/vapi-background.js

@ -134,12 +134,16 @@ window.addEventListener('unload', function() {
vAPI.storage = (function() { vAPI.storage = (function() {
var db = null; var db = null;
var vacuumTimer = null;
var close = function() { var close = function() {
if ( vacuumTimer !== null ) {
clearTimeout(vacuumTimer);
vacuumTimer = null;
}
if ( db === null ) { if ( db === null ) {
return; return;
} }
db.createAsyncStatement('VACUUM').executeAsync();
db.asyncClose(); db.asyncClose();
db = null; db = null;
}; };
@ -178,12 +182,33 @@ vAPI.storage = (function() {
cleanupTasks.push(close); cleanupTasks.push(close);
// Setup database // Setup database
db.createAsyncStatement('CREATE TABLE IF NOT EXISTS settings(name TEXT PRIMARY KEY NOT NULL, value TEXT);')
db.createAsyncStatement('CREATE TABLE IF NOT EXISTS "settings" ("name" TEXT PRIMARY KEY NOT NULL, "value" TEXT);')
.executeAsync(); .executeAsync();
if ( vacuum !== null ) {
vacuumTimer = vAPI.setTimeout(vacuum, 60000);
}
return db; return db;
}; };
// https://developer.mozilla.org/en-US/docs/Storage/Performance#Vacuuming_and_zero-fill
// Vacuum only once, and only while idle
var vacuum = function() {
vacuumTimer = null;
if ( db === null ) {
return;
}
var idleSvc = Cc['@mozilla.org/widget/idleservice;1']
.getService(Ci.nsIIdleService);
if ( idleSvc.idleTime < 60000 ) {
vacuumTimer = vAPI.setTimeout(vacuum, 60000);
return;
}
db.createAsyncStatement('VACUUM').executeAsync();
vacuum = null;
};
// Execute a query // Execute a query
var runStatement = function(stmt, callback) { var runStatement = function(stmt, callback) {
var result = {}; var result = {};
@ -238,7 +263,7 @@ vAPI.storage = (function() {
} }
return; return;
} }
runStatement(db.createAsyncStatement('DELETE FROM settings; VACUUM;'), callback);
runStatement(db.createAsyncStatement('DELETE FROM "settings";'), callback);
}; };
var getBytesInUse = function(keys, callback) { var getBytesInUse = function(keys, callback) {
@ -253,10 +278,10 @@ vAPI.storage = (function() {
var stmt; var stmt;
if ( Array.isArray(keys) ) { if ( Array.isArray(keys) ) {
stmt = db.createAsyncStatement('SELECT "size" AS size, SUM(LENGTH(value)) FROM settings WHERE name = :name');
stmt = db.createAsyncStatement('SELECT "size" AS "size", SUM(LENGTH("value")) FROM "settings" WHERE "name" = :name');
bindNames(keys); bindNames(keys);
} else { } else {
stmt = db.createAsyncStatement('SELECT "size" AS size, SUM(LENGTH(value)) FROM settings');
stmt = db.createAsyncStatement('SELECT "size" AS "size", SUM(LENGTH("value")) FROM "settings"');
} }
runStatement(stmt, function(result) { runStatement(stmt, function(result) {
@ -305,9 +330,9 @@ vAPI.storage = (function() {
var stmt; var stmt;
if ( names.length === 0 ) { if ( names.length === 0 ) {
stmt = db.createAsyncStatement('SELECT * FROM settings');
stmt = db.createAsyncStatement('SELECT * FROM "settings"');
} else { } else {
stmt = db.createAsyncStatement('SELECT * FROM settings WHERE name = :name');
stmt = db.createAsyncStatement('SELECT * FROM "settings" WHERE "name" = :name');
bindNames(stmt, names); bindNames(stmt, names);
} }
@ -321,7 +346,7 @@ vAPI.storage = (function() {
} }
return; return;
} }
var stmt = db.createAsyncStatement('DELETE FROM settings WHERE name = :name');
var stmt = db.createAsyncStatement('DELETE FROM "settings" WHERE "name" = :name');
bindNames(stmt, typeof keys === 'string' ? [keys] : keys); bindNames(stmt, typeof keys === 'string' ? [keys] : keys);
runStatement(stmt, callback); runStatement(stmt, callback);
}; };
@ -334,7 +359,7 @@ vAPI.storage = (function() {
return; return;
} }
var stmt = db.createAsyncStatement('INSERT OR REPLACE INTO settings (name, value) VALUES(:name, :value)');
var stmt = db.createAsyncStatement('INSERT OR REPLACE INTO "settings" ("name", "value") VALUES(:name, :value)');
var params = stmt.newBindingParamsArray(), bp; var params = stmt.newBindingParamsArray(), bp;
for ( var key in details ) { for ( var key in details ) {
if ( details.hasOwnProperty(key) === false ) { if ( details.hasOwnProperty(key) === false ) {

Loading…
Cancel
Save