1 /* LBS: An LFS-inspired filesystem backup system
2 * Copyright (C) 2007 Michael Vrable
4 * When creating backup snapshots, maintain a local database of data blocks and
5 * checksums, in addition to the data contents (which may be stored remotely).
6 * This database is consulted when attempting to build incremental snapshots,
7 * as it says which objects can be reused.
9 * The database is implemented as an SQLite3 database, but this implementation
10 * detail is kept internal to this file, so that the storage format may be
25 /* Helper function to prepare a statement for execution in the current
27 sqlite3_stmt *LocalDb::Prepare(const char *sql)
33 rc = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, &tail);
34 if (rc != SQLITE_OK) {
36 throw IOException(string("Error preparing statement: ") + sql);
42 void LocalDb::ReportError(int rc)
44 fprintf(stderr, "Result code: %d\n", rc);
45 fprintf(stderr, "Error message: %s\n", sqlite3_errmsg(db));
48 void LocalDb::Open(const char *path, const char *snapshot_name,
49 const char *snapshot_scheme, double intent)
53 rc = sqlite3_open(path, &db);
55 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
57 throw IOException("Error opening local database");
60 rc = sqlite3_exec(db, "begin", NULL, NULL, NULL);
62 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
64 throw IOException("Error starting transaction");
67 sqlite3_extended_result_codes(db, 1);
69 /* Insert this snapshot into the database, and determine the integer key
70 * which will be used to identify it. */
71 sqlite3_stmt *stmt = Prepare("insert into "
72 "snapshots(name, scheme, timestamp, intent) "
73 "values (?, ?, julianday('now'), ?)");
74 sqlite3_bind_text(stmt, 1, snapshot_name, strlen(snapshot_name),
76 if (snapshot_scheme == NULL)
77 sqlite3_bind_null(stmt, 2);
79 sqlite3_bind_text(stmt, 2, snapshot_scheme, strlen(snapshot_scheme),
81 sqlite3_bind_double(stmt, 3, intent);
83 rc = sqlite3_step(stmt);
84 if (rc != SQLITE_DONE) {
87 throw IOException("Database execution error!");
90 snapshotid = sqlite3_last_insert_rowid(db);
91 sqlite3_finalize(stmt);
92 if (snapshotid == 0) {
95 throw IOException("Find snapshot id");
98 /* Create a temporary table which will be used to keep track of the objects
99 * used by this snapshot. When the database is closed, we will summarize
100 * the results of this table into segments_used. */
101 rc = sqlite3_exec(db,
102 "create temporary table snapshot_refs ("
103 " segmentid integer not null,"
104 " object text not null,"
105 " size integer not null"
106 ")", NULL, NULL, NULL);
107 if (rc != SQLITE_OK) {
110 throw IOException("Database initialization");
112 rc = sqlite3_exec(db,
113 "create unique index snapshot_refs_index "
114 "on snapshot_refs(segmentid, object)",
116 if (rc != SQLITE_OK) {
119 throw IOException("Database initialization");
123 void LocalDb::Close()
127 /* Summarize the snapshot_refs table into segments_used. */
128 sqlite3_stmt *stmt = Prepare(
129 "insert or replace into segments_used "
130 "select ? as snapshotid, segmentid, max(utilization) from ("
131 " select segmentid, cast(used as real) / size as utilization "
133 " (select segmentid, sum(size) as used from snapshot_refs "
134 " group by segmentid) "
135 " join segments using (segmentid) "
137 " select segmentid, utilization from segments_used "
138 " where snapshotid = ? "
139 ") group by segmentid"
141 sqlite3_bind_int64(stmt, 1, snapshotid);
142 sqlite3_bind_int64(stmt, 2, snapshotid);
143 rc = sqlite3_step(stmt);
144 if (rc != SQLITE_OK && rc != SQLITE_DONE) {
147 fprintf(stderr, "DATABASE ERROR: Unable to create segment summary!\n");
149 sqlite3_finalize(stmt);
151 /* Commit changes to the database and close. */
152 rc = sqlite3_exec(db, "commit", NULL, NULL, NULL);
153 if (rc != SQLITE_OK) {
154 fprintf(stderr, "DATABASE ERROR: Can't commit database!\n");
160 int64_t LocalDb::SegmentToId(const string &segment)
166 stmt = Prepare("insert or ignore into segments(segment) values (?)");
167 sqlite3_bind_text(stmt, 1, segment.c_str(), segment.size(),
169 rc = sqlite3_step(stmt);
170 if (rc != SQLITE_DONE) {
171 throw IOException("Could not execute INSERT statement!");
173 sqlite3_finalize(stmt);
175 stmt = Prepare("select segmentid from segments where segment = ?");
176 sqlite3_bind_text(stmt, 1, segment.c_str(), segment.size(),
179 rc = sqlite3_step(stmt);
180 if (rc == SQLITE_DONE) {
181 throw IOException("No segment found by id");
182 } else if (rc == SQLITE_ROW) {
183 result = sqlite3_column_int64(stmt, 0);
185 throw IOException("Error executing find segment by id query");
188 sqlite3_finalize(stmt);
193 string LocalDb::IdToSegment(int64_t segmentid)
199 stmt = Prepare("select segment from segments where segmentid = ?");
200 sqlite3_bind_int64(stmt, 1, segmentid);
202 rc = sqlite3_step(stmt);
203 if (rc == SQLITE_DONE) {
204 throw IOException("No segment found by id");
205 } else if (rc == SQLITE_ROW) {
206 result = (const char *)sqlite3_column_text(stmt, 0);
208 throw IOException("Error executing find segment by id query");
211 sqlite3_finalize(stmt);
216 void LocalDb::StoreObject(const ObjectReference& ref,
217 const string &checksum, int64_t size,
224 stmt = Prepare("insert into block_index("
225 "segmentid, object, checksum, size, timestamp) "
226 "values (?, ?, ?, ?, julianday('now'))");
228 stmt = Prepare("insert into block_index("
229 "segmentid, object, checksum, size, timestamp) "
230 "values (?, ?, ?, ?, ?)");
233 sqlite3_bind_int64(stmt, 1, SegmentToId(ref.get_segment()));
234 string obj = ref.get_sequence();
235 sqlite3_bind_text(stmt, 2, obj.c_str(), obj.size(), SQLITE_TRANSIENT);
236 sqlite3_bind_text(stmt, 3, checksum.c_str(), checksum.size(),
238 sqlite3_bind_int64(stmt, 4, size);
240 sqlite3_bind_double(stmt, 5, age);
242 rc = sqlite3_step(stmt);
243 if (rc != SQLITE_DONE) {
244 fprintf(stderr, "Could not execute INSERT statement!\n");
248 sqlite3_finalize(stmt);
251 stmt = Prepare("update segments set mtime = max(mtime, ?) "
252 "where segmentid = ?");
253 sqlite3_bind_double(stmt, 1, age);
254 sqlite3_bind_int64(stmt, 2, SegmentToId(ref.get_segment()));
255 rc = sqlite3_step(stmt);
256 sqlite3_finalize(stmt);
260 ObjectReference LocalDb::FindObject(const string &checksum, int64_t size)
266 stmt = Prepare("select segmentid, object from block_index "
267 "where checksum = ? and size = ? and expired is null");
268 sqlite3_bind_text(stmt, 1, checksum.c_str(), checksum.size(),
270 sqlite3_bind_int64(stmt, 2, size);
272 rc = sqlite3_step(stmt);
273 if (rc == SQLITE_DONE) {
274 } else if (rc == SQLITE_ROW) {
275 ref = ObjectReference(IdToSegment(sqlite3_column_int64(stmt, 0)),
276 (const char *)sqlite3_column_text(stmt, 1));
277 ref.set_range(0, size);
279 fprintf(stderr, "Could not execute SELECT statement!\n");
283 sqlite3_finalize(stmt);
288 bool LocalDb::IsOldObject(const string &checksum, int64_t size, double *age,
295 stmt = Prepare("select segmentid, object, timestamp, expired "
296 "from block_index where checksum = ? and size = ?");
297 sqlite3_bind_text(stmt, 1, checksum.c_str(), checksum.size(),
299 sqlite3_bind_int64(stmt, 2, size);
301 rc = sqlite3_step(stmt);
302 if (rc == SQLITE_DONE) {
304 } else if (rc == SQLITE_ROW) {
306 *age = sqlite3_column_double(stmt, 2);
307 *group = sqlite3_column_int(stmt, 3);
309 fprintf(stderr, "Could not execute SELECT statement!\n");
313 sqlite3_finalize(stmt);
318 /* Does this object still exist in the database (and not expired)? */
319 bool LocalDb::IsAvailable(const ObjectReference &ref)
325 // Special objects (such as the zero object) aren't stored in segments, and
326 // so are always available.
327 if (!ref.is_normal())
330 stmt = Prepare("select count(*) from block_index "
331 "where segmentid = ? and object = ? and expired is null");
332 sqlite3_bind_int64(stmt, 1, SegmentToId(ref.get_segment()));
333 sqlite3_bind_text(stmt, 2, ref.get_sequence().c_str(),
334 ref.get_sequence().size(), SQLITE_TRANSIENT);
336 rc = sqlite3_step(stmt);
337 if (rc == SQLITE_DONE) {
339 } else if (rc == SQLITE_ROW) {
340 if (sqlite3_column_int(stmt, 0) > 0)
343 fprintf(stderr, "Could not execute SELECT statement!\n");
347 sqlite3_finalize(stmt);
352 void LocalDb::UseObject(const ObjectReference& ref)
357 if (!ref.is_normal())
360 stmt = Prepare("insert or ignore into snapshot_refs "
361 "select segmentid, object, size from block_index "
362 "where segmentid = ? and object = ?");
363 sqlite3_bind_int64(stmt, 1, SegmentToId(ref.get_segment()));
364 string obj = ref.get_sequence();
365 sqlite3_bind_text(stmt, 2, obj.c_str(), obj.size(), SQLITE_TRANSIENT);
367 rc = sqlite3_step(stmt);
368 if (rc != SQLITE_DONE) {
369 fprintf(stderr, "Could not execute INSERT statement!\n");
373 sqlite3_finalize(stmt);
376 void LocalDb::UseSegment(const std::string &segment, double utilization)
381 stmt = Prepare("insert or replace "
382 "into segments_used(snapshotid, segmentid, utilization) "
384 sqlite3_bind_int64(stmt, 1, snapshotid);
385 sqlite3_bind_int64(stmt, 2, SegmentToId(segment));
386 sqlite3_bind_double(stmt, 3, utilization);
388 rc = sqlite3_step(stmt);
389 if (rc != SQLITE_DONE) {
390 fprintf(stderr, "Could not insert segment use record!\n");
394 sqlite3_finalize(stmt);
397 void LocalDb::SetSegmentChecksum(const std::string &segment,
398 const std::string &path,
399 const std::string &checksum,
405 stmt = Prepare("update segments set path = ?, checksum = ?, size = ?, "
406 "mtime = coalesce(mtime, julianday('now')) "
407 "where segmentid = ?");
408 sqlite3_bind_text(stmt, 1, path.c_str(), path.size(),
410 sqlite3_bind_text(stmt, 2, checksum.c_str(), checksum.size(),
412 sqlite3_bind_int64(stmt, 3, size);
413 sqlite3_bind_int64(stmt, 4, SegmentToId(segment));
415 rc = sqlite3_step(stmt);
416 if (rc != SQLITE_DONE) {
417 fprintf(stderr, "Could not update segment checksum in database!\n");
421 sqlite3_finalize(stmt);
424 bool LocalDb::GetSegmentChecksum(const string &segment,
426 string *seg_checksum)
433 stmt = Prepare("select path, checksum from segments where segment = ?");
434 sqlite3_bind_text(stmt, 1, segment.c_str(), segment.size(),
437 rc = sqlite3_step(stmt);
438 if (rc == SQLITE_DONE) {
439 } else if (rc == SQLITE_ROW) {
443 val = (const char *)sqlite3_column_text(stmt, 0);
449 val = (const char *)sqlite3_column_text(stmt, 1);
455 fprintf(stderr, "Could not execute SELECT statement!\n");
459 sqlite3_finalize(stmt);