Skip to content

Instantly share code, notes, and snippets.

@satyr
Created March 1, 2009 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satyr/72345 to your computer and use it in GitHub Desktop.
Save satyr/72345 to your computer and use it in GitHub Desktop.
Performs VACUUM and REINDEX on your Firefox storage.
const noun_storage_file = {
_name: 'storage_file',
suggest: function(txt, htm, cb, sx){
if(sx || !txt) return [];
return matchSuggs(this.list, txt);
},
list: (function({directoryEntries}, ls){
while(directoryEntries.hasMoreElements()){
var f = directoryEntries.getNext().QueryInterface(Ci.nsIFile);
if(f.exists() && f.isFile() && /\.sqlite$/.test(f.path)){
var name = f.leafName.slice(0, -7);
ls.push({text: name, data: f, summary: name, index: ls.length});
}
}
return ls;
}(Cc['@mozilla.org/file/directory_service;1']
.getService(Ci.nsIProperties).get('ProfD', Ci.nsIFile), [])),
fbytes: function(b) (b +'').match(/.+?(?=(?:...)*$)/g).join(',') + ' B',
get html(){
delete this.html;
var {fbytes} = this;
return this.html = ''+ this.list.reduce(function(tbl, s){
tbl.* += (<tr><td>{s.text}
</td><td align="right">{fbytes(s.data.fileSize)}</td></tr>);
return tbl;
}, <table/>);
},
};
function matchSuggs(suggList, input, caseAware) {
var flag = caseAware ? '' : 'i';
for (var a = [], i = 0, l = suggList.length; i < l; ++i) {
var sg = suggList[i];
try { var x = sg.text.search(input, flag) }
catch (e) {
x = ((caseAware ? sg.text : sg.text.toLowerCase())
.indexOf(caseAware ? input : input.toLowerCase()));
}
if (x >= 0) a[x * l + i] = sg;
}
return a.reduce(function(r, s)(r.push(s), r), []);
}
CmdUtils.CreateCommand({
name: 'vacuum-and-reindex',
description: 'Performs VACUUM and REINDEX on a Firefox storage.',
icon: 'http://www.sqlite.org/favicon.ico',
takes: {storage: noun_storage_file},
execute: function({data: f}){
var con = (Cc['@mozilla.org/storage/service;1']
.getService(Ci.mozIStorageService).openDatabase(f));
try {
for each(var q in ['REINDEX', 'VACUUM'])
try { con.executeSimpleSQL(q) } catch(e){
CmdUtils.log(e);
this._puts('Error on '+ q);
return;
}
} finally { con.close() }
var F = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
F.initWithPath(f.path);
var {fbytes} = noun_storage_file, before = f.fileSize, after = F.fileSize;
this._puts(f.leafName +': '+ fbytes(before) +' => '+ fbytes(after) +
' (-'+ fbytes(before - after) +')');
},
preview: function(pbl, {index}){
pbl.innerHTML = noun_storage_file.html;
jQuery('tr', pbl).eq(index).css('fontWeight', 'bold');
},
previewDelay: 0,
_puts: function(x)
displayMessage({icon: this.icon, title: this.name, text: x +''}),
author: ['powchin'.link('http://tako3.com/search/powchin'),
'satyr'.link('http://d.hatena.ne.jp/murky-satyr')].join(', '),
license: 'MIT',
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment