Compute checksums of segments and store them 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     path text,
19     checksum text
20 );
21
22 -- Index of all blocks which have been stored in a snapshot, by checksum.
23 create table block_index (
24     blockid integer primary key,
25     segmentid integer not null,
26     object text not null,
27     checksum text,
28     size integer,
29     timestamp real,
30     expired integer
31 );
32 create index block_content_index on block_index(checksum);
33 create unique index block_name_index on block_index(segmentid, object);
34
35 -- Index tracking which blocks are used by which snapshots.
36 create table snapshot_contents (
37     blockid integer,
38     snapshotid integer
39 );
40 create unique index snapshot_contents_unique
41     on snapshot_contents(blockid, snapshotid);
42
43 -- Summary statistics for each segment.
44 create view segment_info as select * from
45     (select segmentid, max(timestamp) as mtime,
46             sum(size) as size, count(*) as objects
47        from block_index natural join segments group by segmentid)
48 natural join
49     (select segmentid, sum(size) as used, count(*) as objects_used
50        from block_index where blockid in
51             (select blockid from snapshot_contents) group by segmentid);
52
53 -- Ranking of segments to be cleaned, using a benefit function of
54 -- (fraction free space)*(age of youngest object).
55 create view cleaning_order as select *, (1-u)*age/(u+0.1) as benefit from
56     (select segmentid,
57             cast(used as real) / size as u, julianday('now') - mtime as age
58         from segment_info)
59 where benefit > 0;