I'm building a cache backed by a sqlite3 database. To limit disk space used, I'm periodically checking the size of the db file. If it's over my max limit, I batch delete the oldest items and vacuum, repeating until the size is below 80% of the max.
However, the file size hasn't changed immediately after the vacuum, and I usually end up deleting the whole table before the size goes down. All calls return SQLITE_OK
with no errors to be seen anywhere.
while (too_many_rows) {
// delete N rows
sqlite3_exec(database, "VACUUM;", NULL, NULL, &error);
sqlite3_close(database);
// check file size
printf("%d deleted, new size %d KB\n", N, size);
}
Produces something like:
200 deleted, new size 400 KB
200 deleted, new size 400 KB
200 deleted, new size 360 KB
200 deleted, new size 360 KB
156 deleted, new size 360 KB
0 deleted, new size 360 KB
0 deleted, new size 360 KB
0 deleted, new size 360 KB
0 deleted, new size 360 KB
0 deleted, new size 16 KB
How can I tell when the vacuum has finished, and I can check the file size again?
Can sqlite tell me or be made to block? Do I need to be using inotify
or something?