Created
April 13, 2011 16:13
-
-
Save mason-larobina/917848 to your computer and use it in GitHub Desktop.
clean all luakit sqlite3 databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---------------------------------------------------------- | |
-- :dbclean command to vacuum all open sqlite databases -- | |
-- (C) 2011 Mason Larobina <[email protected]> -- | |
---------------------------------------------------------- | |
-- Note: this needs to be required in your config before any databases | |
-- are created (i.e. before the history and cookies libs) | |
require "coroutine" | |
require "lfs" | |
-- Table of all database objects | |
local all_dbs = setmetatable({}, { __mode = "kv" }) | |
-- Watch for new sqlite3 database instances | |
sqlite3.add_signal("new", function (db) | |
print("New db", db) | |
all_dbs[db] = db | |
end) | |
local cmd = lousy.bind.cmd | |
add_cmds{ | |
-- Vacuum all dbs | |
-- http://sqlite.org/lang_vacuum.html | |
cmd("dbclean", function (w) | |
function vacuum() | |
local reaped, count = 0, 0 | |
-- Clone db list to prevent GC of items while iterating | |
for _, db in pairs(lousy.util.table.clone(all_dbs)) do | |
if db.open then | |
count = count + 1 | |
local file = db.filename | |
local size = lfs.attributes(file).size | |
w:set_prompt(string.format("Re-constructing database: " | |
.. "%s (%.2fMB)", file, size / (1024 * 1024))) | |
-- Wait for GUI update (so that the prompt becomes visible) | |
-- and then start the vacuum. | |
coroutine.yield() | |
db:exec("VACUUM;") | |
reaped = reaped + (size - lfs.attributes(file).size) | |
end | |
end | |
-- Print stats | |
w:set_prompt(string.format("Vacuums complete, %.2fMB reclaimed " | |
.. "over %d open databases.", reaped, count)) | |
end | |
co = coroutine.create(vacuum) | |
luakit.idle_add(function () | |
return coroutine.resume(co) | |
end) | |
end) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment