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("insert into segments_used "
129 "select ? as snapshotid, segmentid, "
130 "cast(used as real) / size as utilization "
132 "(select segmentid, sum(size) as used "
133 "from snapshot_refs group by segmentid) "
134 "join segments using (segmentid)");
135 sqlite3_bind_int64(stmt, 1, snapshotid);
136 rc = sqlite3_step(stmt);
137 if (rc != SQLITE_OK && rc != SQLITE_DONE) {
140 fprintf(stderr, "DATABASE ERROR: Unable to create segment summary!\n");
142 sqlite3_finalize(stmt);
144 /* Commit changes to the database and close. */
145 rc = sqlite3_exec(db, "commit", NULL, NULL, NULL);
146 if (rc != SQLITE_OK) {
147 fprintf(stderr, "DATABASE ERROR: Can't commit database!\n");
153 int64_t LocalDb::SegmentToId(const string &segment)
159 stmt = Prepare("insert or ignore into segments(segment) values (?)");
160 sqlite3_bind_text(stmt, 1, segment.c_str(), segment.size(),
162 rc = sqlite3_step(stmt);
163 if (rc != SQLITE_DONE) {
164 throw IOException("Could not execute INSERT statement!");
166 sqlite3_finalize(stmt);
168 stmt = Prepare("select segmentid from segments where segment = ?");
169 sqlite3_bind_text(stmt, 1, segment.c_str(), segment.size(),
172 rc = sqlite3_step(stmt);
173 if (rc == SQLITE_DONE) {
174 throw IOException("No segment found by id");
175 } else if (rc == SQLITE_ROW) {
176 result = sqlite3_column_int64(stmt, 0);
178 throw IOException("Error executing find segment by id query");
181 sqlite3_finalize(stmt);
186 string LocalDb::IdToSegment(int64_t segmentid)
192 stmt = Prepare("select segment from segments where segmentid = ?");
193 sqlite3_bind_int64(stmt, 1, segmentid);
195 rc = sqlite3_step(stmt);
196 if (rc == SQLITE_DONE) {
197 throw IOException("No segment found by id");
198 } else if (rc == SQLITE_ROW) {
199 result = (const char *)sqlite3_column_text(stmt, 0);
201 throw IOException("Error executing find segment by id query");
204 sqlite3_finalize(stmt);
209 void LocalDb::StoreObject(const ObjectReference& ref,
210 const string &checksum, int64_t size,
217 stmt = Prepare("insert into block_index("
218 "segmentid, object, checksum, size, timestamp) "
219 "values (?, ?, ?, ?, julianday('now'))");
221 stmt = Prepare("insert into block_index("
222 "segmentid, object, checksum, size, timestamp) "
223 "values (?, ?, ?, ?, ?)");
226 sqlite3_bind_int64(stmt, 1, SegmentToId(ref.get_segment()));
227 string obj = ref.get_sequence();
228 sqlite3_bind_text(stmt, 2, obj.c_str(), obj.size(), SQLITE_TRANSIENT);
229 sqlite3_bind_text(stmt, 3, checksum.c_str(), checksum.size(),
231 sqlite3_bind_int64(stmt, 4, size);
233 sqlite3_bind_double(stmt, 5, age);
235 rc = sqlite3_step(stmt);
236 if (rc != SQLITE_DONE) {
237 fprintf(stderr, "Could not execute INSERT statement!\n");
241 sqlite3_finalize(stmt);
244 ObjectReference LocalDb::FindObject(const string &checksum, int64_t size)
250 stmt = Prepare("select segmentid, object from block_index "
251 "where checksum = ? and size = ? and expired is null");
252 sqlite3_bind_text(stmt, 1, checksum.c_str(), checksum.size(),
254 sqlite3_bind_int64(stmt, 2, size);
256 rc = sqlite3_step(stmt);
257 if (rc == SQLITE_DONE) {
258 } else if (rc == SQLITE_ROW) {
259 ref = ObjectReference(IdToSegment(sqlite3_column_int64(stmt, 0)),
260 (const char *)sqlite3_column_text(stmt, 1));
262 fprintf(stderr, "Could not execute SELECT statement!\n");
266 sqlite3_finalize(stmt);
271 bool LocalDb::IsOldObject(const string &checksum, int64_t size, double *age,
278 stmt = Prepare("select segmentid, object, timestamp, expired "
279 "from block_index where checksum = ? and size = ?");
280 sqlite3_bind_text(stmt, 1, checksum.c_str(), checksum.size(),
282 sqlite3_bind_int64(stmt, 2, size);
284 rc = sqlite3_step(stmt);
285 if (rc == SQLITE_DONE) {
287 } else if (rc == SQLITE_ROW) {
289 *age = sqlite3_column_double(stmt, 2);
290 *group = sqlite3_column_int(stmt, 3);
292 fprintf(stderr, "Could not execute SELECT statement!\n");
296 sqlite3_finalize(stmt);
301 /* Does this object still exist in the database (and not expired)? */
302 bool LocalDb::IsAvailable(const ObjectReference &ref)
308 // Special objects (such as the zero object) aren't stored in segments, and
309 // so are always available.
310 if (!ref.is_normal())
313 stmt = Prepare("select count(*) from block_index "
314 "where segmentid = ? and object = ? and expired is null");
315 sqlite3_bind_int64(stmt, 1, SegmentToId(ref.get_segment()));
316 sqlite3_bind_text(stmt, 2, ref.get_sequence().c_str(),
317 ref.get_sequence().size(), SQLITE_TRANSIENT);
319 rc = sqlite3_step(stmt);
320 if (rc == SQLITE_DONE) {
322 } else if (rc == SQLITE_ROW) {
323 if (sqlite3_column_int(stmt, 0) > 0)
326 fprintf(stderr, "Could not execute SELECT statement!\n");
330 sqlite3_finalize(stmt);
335 void LocalDb::UseObject(const ObjectReference& ref)
340 if (!ref.is_normal())
343 stmt = Prepare("insert or ignore into snapshot_refs "
344 "select segmentid, object, size from block_index "
345 "where segmentid = ? and object = ?");
346 sqlite3_bind_int64(stmt, 1, SegmentToId(ref.get_segment()));
347 string obj = ref.get_sequence();
348 sqlite3_bind_text(stmt, 2, obj.c_str(), obj.size(), SQLITE_TRANSIENT);
350 rc = sqlite3_step(stmt);
351 if (rc != SQLITE_DONE) {
352 fprintf(stderr, "Could not execute INSERT statement!\n");
356 sqlite3_finalize(stmt);
359 void LocalDb::SetSegmentChecksum(const std::string &segment,
360 const std::string &path,
361 const std::string &checksum)
366 stmt = Prepare("update segments set path = ?, checksum = ?, "
367 "size = (select sum(size) from block_index "
368 " where segmentid = ?) "
369 "where segmentid = ?");
370 sqlite3_bind_text(stmt, 1, path.c_str(), path.size(),
372 sqlite3_bind_text(stmt, 2, checksum.c_str(), checksum.size(),
374 sqlite3_bind_int64(stmt, 3, SegmentToId(segment));
375 sqlite3_bind_int64(stmt, 4, SegmentToId(segment));
377 rc = sqlite3_step(stmt);
378 if (rc != SQLITE_DONE) {
379 fprintf(stderr, "Could not update segment checksum in database!\n");
383 sqlite3_finalize(stmt);
386 bool LocalDb::GetSegmentChecksum(const string &segment,
388 string *seg_checksum)
395 stmt = Prepare("select path, checksum from segments where segment = ?");
396 sqlite3_bind_text(stmt, 1, segment.c_str(), segment.size(),
399 rc = sqlite3_step(stmt);
400 if (rc == SQLITE_DONE) {
401 } else if (rc == SQLITE_ROW) {
405 val = (const char *)sqlite3_column_text(stmt, 0);
411 val = (const char *)sqlite3_column_text(stmt, 1);
417 fprintf(stderr, "Could not execute SELECT statement!\n");
421 sqlite3_finalize(stmt);