35b2c9d0c2cf16b96579e78d2720eb300ecb4d8c
[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 not null,
11     timestamp real,
12     intent real
13 );
14
15 -- List of segments which have been created.
16 create table segments (
17     segmentid integer primary key,
18     segment text unique not null,
19     path text,
20     checksum text,
21     mtime real,
22     size integer,
23     expire_time integer         -- snapshotid of latest snapshot when expired
24 );
25
26 -- Index of all blocks which have been stored, by checksum.
27 create table block_index (
28     blockid integer primary key,
29     segmentid integer not null,
30     object text not null,
31     checksum text,
32     size integer,
33     timestamp real,
34     expired integer
35 );
36 create index block_content_index on block_index(checksum);
37 create unique index block_name_index on block_index(segmentid, object);
38
39 -- Checksums for the decomposition of blocks into even smaller chunks
40 -- (variable-sized, but generally ~4 kB, and maximum 64 kB).  Chunk boundaries
41 -- are determined based on the contents using Rabin fingerprints.  These
42 -- checksums can be used for computing sub-file incrementals.
43 --
44 -- Each block stored in block_index may have an entry in the
45 -- subblock_signatures table.  The signatures field is a binary blob consisting
46 -- of a packed sequence of (chunk length [16-bit unsigned, big-endian],
47 -- checksum [20 bytes if SHA-1]) tuples that should cover the entire block.
48 --
49 -- algorithm specifies the method used for computing break points as well as
50 -- the hash function used, so that signatures can be discarded if the algorithm
51 -- changes.  The current algorithm used is 'lbfs-4096/sha1', which specifies a
52 -- target 4 kB block size with parameters set to match LBFS, and SHA-1 as the
53 -- hash algorithm.
54 create table subblock_signatures (
55     blockid integer primary key,
56     algorithm text not null,
57     signatures blob not null
58 );
59
60 -- Summary of segment utilization for each snapshots.
61 create table segments_used (
62     snapshotid integer not null,
63     segmentid integer not null,
64     utilization real
65 );
66 create unique index segments_used_index
67     on segments_used(snapshotid, segmentid);
68
69 -- Overall estimate of segment utilization, for all snapshots combined.
70 create view segment_info as
71 select segmentid, mtime, size, expire_time,
72        cast(size * utilization as integer) as used, utilization
73 from segments join
74      (select segmentid, max(utilization) as utilization
75       from segments_used group by segmentid)
76 using (segmentid);