+
+ sqlite3_extended_result_codes(db, 1);
+
+ /* Insert this snapshot into the database, and determine the integer key
+ * which will be used to identify it. */
+ sqlite3_stmt *stmt = Prepare("insert into "
+ "snapshots(name, scheme, timestamp) "
+ "values (?, ?, julianday('now'))");
+ sqlite3_bind_text(stmt, 1, snapshot_name, strlen(snapshot_name),
+ SQLITE_TRANSIENT);
+ if (snapshot_scheme == NULL)
+ sqlite3_bind_null(stmt, 2);
+ else
+ sqlite3_bind_text(stmt, 2, snapshot_scheme, strlen(snapshot_scheme),
+ SQLITE_TRANSIENT);
+
+ rc = sqlite3_step(stmt);
+ if (rc != SQLITE_DONE) {
+ ReportError(rc);
+ sqlite3_close(db);
+ throw IOException("Database execution error!");
+ }
+
+ snapshotid = sqlite3_last_insert_rowid(db);
+ sqlite3_finalize(stmt);
+ if (snapshotid == 0) {
+ ReportError(rc);
+ sqlite3_close(db);
+ throw IOException("Find snapshot id");
+ }
+
+ /* Create a temporary table which will be used to keep track of the objects
+ * used by this snapshot. When the database is closed, we will summarize
+ * the results of this table into segments_used. */
+ rc = sqlite3_exec(db,
+ "create temporary table snapshot_refs ("
+ " segmentid integer not null,"
+ " object text not null,"
+ " size integer not null"
+ ")", NULL, NULL, NULL);
+ if (rc != SQLITE_OK) {
+ ReportError(rc);
+ sqlite3_close(db);
+ throw IOException("Database initialization");
+ }
+ rc = sqlite3_exec(db,
+ "create unique index snapshot_refs_index "
+ "on snapshot_refs(segmentid, object)",
+ NULL, NULL, NULL);
+ if (rc != SQLITE_OK) {
+ ReportError(rc);
+ sqlite3_close(db);
+ throw IOException("Database initialization");
+ }