Rename --name to --scheme, and store the scheme name in the local database.
[cumulus.git] / schema.sql
1 -- We maintain a local index of data blocks that have been previously stored
2 -- for constructing incremental snapshots.
3 --
4 -- The index is stored in an SQLite3 database.  This is its schema.
5
6 -- List of snapshots which have been created.
7 create table snapshots (
8     snapshotid integer primary key,
9     name text not null,
10     scheme text,
11     timestamp real
12 );
13
14 -- List of segments which have been created.
15 create table segments (
16     segmentid integer primary key,
17     segment text unique not null
18 );
19
20 -- Index of all blocks which have been stored in a snapshot, by checksum.
21 create table block_index (
22     blockid integer primary key,
23     segmentid integer not null,
24     object text not null,
25     checksum text,
26     size integer,
27     timestamp real,
28     expired integer
29 );
30 create index block_content_index on block_index(checksum);
31 create unique index block_name_index on block_index(segmentid, object);
32
33 -- Index tracking which blocks are used by which snapshots.
34 create table snapshot_contents (
35     blockid integer,
36     snapshotid integer
37 );
38 create unique index snapshot_contents_unique
39     on snapshot_contents(blockid, snapshotid);
40
41 -- Summary statistics for each segment.
42 create view segment_info as select * from
43     (select segmentid, max(timestamp) as mtime,
44             sum(size) as size, count(*) as objects
45        from block_index natural join segments group by segmentid)
46 natural join
47     (select segmentid, sum(size) as used, count(*) as objects_used
48        from block_index where blockid in
49             (select blockid from snapshot_contents) group by segmentid);
50
51 -- Ranking of segments to be cleaned, using a benefit function of
52 -- (fraction free space)*(age of youngest object).
53 create view cleaning_order as select *, (1-u)*age/(u+0.1) as benefit from
54     (select segmentid,
55             cast(used as real) / size as u, julianday('now') - mtime as age
56         from segment_info)
57 where benefit > 0;