]> Sergey Matveev's repositories - btrtrc.git/blob - storage/sqlite/sql
Removed unused sqlite "provider" storage
[btrtrc.git] / storage / sqlite / sql
1 pragma auto_vacuum=incremental;
2 create table if not exists blob(
3         name text,
4         last_used timestamp default (datetime('now')),
5         data blob,
6         primary key (name)
7 );
8
9 create view if not exists deletable_blob as
10 with recursive excess_blob(
11         usage_with,
12         last_used,
13         blob_rowid,
14         data_length
15 ) as (
16         select * from (select (select sum(length(data)) from blob) as usage_with, last_used, rowid, length(data) from blob order by last_used, rowid limit 1)
17                 where usage_with >= (select value from setting where name='capacity')
18         union all
19         select usage_with-data_length, blob.last_used, blob.rowid, length(data) from excess_blob join blob
20                 on blob.rowid=(select rowid from blob where (last_used, rowid) > (excess.last_used, blob_rowid))
21         where usage_with >= (select value from setting where name='capacity')
22 ) select * from excess;
23
24 CREATE TRIGGER if not exists trim_blobs_to_capacity_after_update after update on blob begin
25         delete from blob where rowid in (select blob_rowid from deletable_blob);
26 end;
27 CREATE TRIGGER if not exists trim_blobs_to_capacity_after_insert after insert on blob begin
28         delete from blob where rowid in (select blob_rowid from deletable_blob);
29 end;