/
Schema.sql
290 lines (251 loc) · 7.92 KB
/
Schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
Syntax notes (Applying to schema.sql and versioning x.*.sql files):
Be careful with semicolons, it is used as a simple Split-point for statements.
For conditional schema statements, a preprocesossor exists. Example:
{#if sqlite_version >= 3.8.2} DO_SOMETHING {#else} DO_SOMETHING_ELSE {#endif}
Variables available: sqlite_version (type Version) and db_version (type int)
Nesting is possible when appending a number in the form "_x" to the #if #else #endif.
{#if sqlite_version >= 3.8.2} DO_SOMETHING_3.8 {#else} {#if_1 sqlite_version >= 3.6.5} DO_SOMETHING_3.6 {#else_1} DO_SOMETHING_ELSE {#endif_1} {#endif}
*/
/*
The operation table is a local table
that is used to record all operations
for later debug inspection, and can be
used to map log messages to an operation
*/
CREATE TABLE "Operation" (
"ID" INTEGER PRIMARY KEY,
"Description" TEXT NOT NULL,
"Timestamp" INTEGER NOT NULL
);
/*
The remote volumes table keeps track
of the state of all known volumes
*/
CREATE TABLE "Remotevolume" (
"ID" INTEGER PRIMARY KEY,
"OperationID" INTEGER NOT NULL,
"Name" TEXT NOT NULL,
"Type" TEXT NOT NULL,
"Size" INTEGER NULL,
"Hash" TEXT NULL,
"State" TEXT NOT NULL,
"VerificationCount" INTEGER NOT NULL,
"DeleteGraceTime" INTEGER NOT NULL
);
/* Index for detecting broken states */
CREATE UNIQUE INDEX "RemotevolumeName" ON "Remotevolume" ("Name", "State");
/*
The index-block table contains
references that explains what block
files a index file references.
This is used to remove index volumes,
when they no longer reference any
block volumes
*/
CREATE TABLE "IndexBlockLink" (
"IndexVolumeID" INTEGER NOT NULL,
"BlockVolumeID" INTEGER NOT NULL
);
/*
The fileset collects all files belonging to
a particular backup, and thus a remote Fileset
*/
CREATE TABLE "Fileset" (
"ID" INTEGER PRIMARY KEY,
"OperationID" INTEGER NOT NULL,
"VolumeID" INTEGER NOT NULL,
"IsFullBackup" INTEGER NOT NULL,
"Timestamp" INTEGER NOT NULL
);
/*
The OperationFileset contains an
entry for each file scanned for
a single operation. The scantime
is the time the file was last
scanned in UNIX EPOCH format
*/
CREATE TABLE "FilesetEntry" (
"FilesetID" INTEGER NOT NULL,
"FileID" INTEGER NOT NULL,
"Lastmodified" INTEGER NOT NULL,
CONSTRAINT "FilesetEntry_PK_FilesetIdFileId" PRIMARY KEY ("FilesetID", "FileID")
) {#if sqlite_version >= 3.8.2} WITHOUT ROWID {#endif};
/* Improved reverse lookup for joining Fileset and File table */
CREATE INDEX "FilesetentryFileIdIndex" on "FilesetEntry" ("FileID");
CREATE INDEX "nn_FilesetentryFile" on FilesetEntry ("FilesetID","FileID");
/*
The PathPrefix contains a set
of path prefixes, used to minimize
the space required to store paths
*/
CREATE TABLE "PathPrefix" (
"ID" INTEGER PRIMARY KEY,
"Prefix" TEXT NOT NULL
);
CREATE UNIQUE INDEX "PathPrefixPrefix" ON "PathPrefix" ("Prefix");
/*
The FileLookup table contains an ID
for each path and each version
of the data and metadata
*/
CREATE TABLE "FileLookup" (
"ID" INTEGER PRIMARY KEY,
"PrefixID" INTEGER NOT NULL,
"Path" TEXT NOT NULL,
"BlocksetID" INTEGER NOT NULL,
"MetadataID" INTEGER NOT NULL
);
/* Fast path based lookup, single properties are auto-indexed */
CREATE UNIQUE INDEX "FileLookupPath" ON "FileLookup" ("PrefixID", "Path", "BlocksetID", "MetadataID");
CREATE INDEX "nn_FileLookup_BlockMeta" ON FileLookup ("BlocksetID", "MetadataID");
/*
The File view contains an ID
for each path and each version
of the data and metadata
*/
CREATE VIEW "File" AS SELECT "A"."ID" AS "ID", "B"."Prefix" || "A"."Path" AS "Path", "A"."BlocksetID" AS "BlocksetID", "A"."MetadataID" AS "MetadataID" FROM "FileLookup" "A", "PathPrefix" "B" WHERE "A"."PrefixID" = "B"."ID";
/*
The blocklist hashes are hashes of
fragments of the blocklists.
They are grouped by the BlocksetID
and ordered by the index
*/
CREATE TABLE "BlocklistHash" (
"BlocksetID" INTEGER NOT NULL,
"Index" INTEGER NOT NULL,
"Hash" TEXT NOT NULL
);
/* Faster database recreation */
CREATE UNIQUE INDEX "BlocklistHashBlocksetIDIndex" ON "BlocklistHash" ("BlocksetID", "Index");
/*
The blockset is a list of blocks
Note that Length is actually redundant,
it can be calculated by
SUM(Blockset.Size)
The FullHash is the hash of the entire
blob when reconstructed
*/
CREATE TABLE "Blockset" (
"ID" INTEGER PRIMARY KEY,
"Length" INTEGER NOT NULL,
"FullHash" TEXT NOT NULL
);
CREATE UNIQUE INDEX "BlocksetFullHash" ON "Blockset" ("FullHash", "Length");
/*
The elements of a blocklist,
the hash is the block hash,
they are grouped by the BlocksetID
and ordered by the index
For general speed and storage improvement
we use a table with option "WITHOUT ROWID"
["WITHOUT ROWID" available since SQLite v3.8.2 (= System.Data.SQLite v1.0.90.0, rel 2013-12-23)]
*/
CREATE TABLE "BlocksetEntry" (
"BlocksetID" INTEGER NOT NULL,
"Index" INTEGER NOT NULL,
"BlockID" INTEGER NOT NULL,
CONSTRAINT "BlocksetEntry_PK_IdIndex" PRIMARY KEY ("BlocksetID", "Index")
) {#if sqlite_version >= 3.8.2} WITHOUT ROWID {#endif};
/* As this table is a cross table we need fast lookup */
CREATE INDEX "BlocksetEntry_IndexIdsBackwards" ON "BlocksetEntry" ("BlockID");
CREATE INDEX "nnc_BlocksetEntry" ON "BlocksetEntry" ("Index", "BlocksetID", "BlockID");
/*
The individual block hashes,
mapped to the containing remote volume
*/
CREATE TABLE "Block" (
"ID" INTEGER PRIMARY KEY,
"Hash" TEXT NOT NULL,
"Size" INTEGER NOT NULL,
"VolumeID" INTEGER NOT NULL
);
/* This is the most performance critical part of the database */
CREATE UNIQUE INDEX "BlockHashSize" ON "Block" ("Hash", "Size");
/* Add index for faster volume based block access (for compacting) */
CREATE INDEX "Block_IndexByVolumeId" ON "Block" ("VolumeID");
/* Speedup for recreate database */
CREATE INDEX "BlockSize" ON "Block" ("Size");
CREATE INDEX "BlockHashVolumeID" ON "Block" ("Hash", "VolumeID");
/*
The deleted block hashes,
mapped to the containing file,
used for wasted space computations
*/
CREATE TABLE "DeletedBlock" (
"ID" INTEGER PRIMARY KEY,
"Hash" TEXT NOT NULL,
"Size" INTEGER NOT NULL,
"VolumeID" INTEGER NOT NULL
);
/*
If extra copies of blocks are detected,
they are recorded here
*/
CREATE TABLE "DuplicateBlock" (
"BlockID" INTEGER NOT NULL,
"VolumeID" INTEGER NOT NULL
);
/*
A metadata set, essentially a placeholder
to easily extend metadatasets with new properties
*/
CREATE TABLE "Metadataset" (
"ID" INTEGER PRIMARY KEY,
"BlocksetID" INTEGER NOT NULL
);
CREATE INDEX "MetadatasetBlocksetID" ON "Metadataset" ("BlocksetID");
CREATE INDEX "nnc_Metadataset" ON Metadataset ("ID","BlocksetID");
/*
Operations performed on the backend,
intended to be used when constructing
an error report or when debugging
*/
CREATE TABLE "RemoteOperation" (
"ID" INTEGER PRIMARY KEY,
"OperationID" INTEGER NOT NULL,
"Timestamp" INTEGER NOT NULL,
"Operation" TEXT NOT NULL,
"Path" TEXT NOT NULL,
"Data" BLOB NULL
);
/*
Logged events, intended to be used when
constructing an error report or when
debugging
*/
CREATE TABLE "LogData" (
"ID" INTEGER PRIMARY KEY,
"OperationID" INTEGER NOT NULL,
"Timestamp" INTEGER NOT NULL,
"Type" TEXT NOT NULL,
"Message" TEXT NOT NULL,
"Exception" TEXT NULL
);
/*
Internal version tracking
*/
CREATE TABLE "Version" (
"ID" INTEGER PRIMARY KEY,
"Version" INTEGER NOT NULL
);
/*
Settings, such as hash and blocksize,
used for verification
*/
CREATE TABLE "Configuration" (
"Key" TEXT PRIMARY KEY NOT NULL,
"Value" TEXT NOT NULL
);
/*
USN tracking table
*/
CREATE TABLE "ChangeJournalData" (
"ID" INTEGER PRIMARY KEY,
"FilesetID" INTEGER NOT NULL,
"VolumeName" TEXT NOT NULL,
"JournalID" INTEGER NOT NULL,
"NextUsn" INTEGER NOT NULL,
"ConfigHash" TEXT NOT NULL
);
INSERT INTO "Version" ("Version") VALUES (12);