1 -- We have to opt into this before creating any tables, or before a vacuum to enable it. It means we
2 -- can trim the database file size with partial vacuums without having to do a full vacuum, which
4 pragma auto_vacuum=incremental;
6 create table if not exists blob (
8 last_used timestamp default (datetime('now')),
13 create table if not exists blob_meta (
18 create index if not exists blob_last_used on blob(last_used);
20 -- While sqlite *seems* to be faster to get sum(length(data)) instead of
21 -- sum(length(data)), it may still require a large table scan at start-up or with a
22 -- cold-cache. With this we can be assured that it doesn't.
23 insert or ignore into blob_meta values ('size', 0);
25 create table if not exists setting (
26 name primary key on conflict replace,
30 create table if not exists tag (
31 blob_name references blob(name),
34 primary key (blob_name, tag_name)
37 create view if not exists deletable_blob as
38 with recursive excess (
47 (select value from blob_meta where key='size') as usage_with,
51 from blob order by last_used, rowid limit 1
53 where usage_with > (select value from setting where name='capacity')
56 usage_with-data_length as new_usage_with,
61 on blob.rowid=(select rowid from blob where (last_used, rowid) > (excess.last_used, blob_rowid))
62 where new_usage_with > (select value from setting where name='capacity')