X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=localdb.cc;h=27fec5a8b9b38ffc8c51d5d93451230d9cc28909;hb=da87780779a2f165503d019ee0b59d10e5d31ec8;hp=63a9f8349531e0fb13531b8ba7931dc4300c112d;hpb=ed5a047c1b8a8f5c43b0192e8e774b91497c9706;p=cumulus.git diff --git a/localdb.cc b/localdb.cc index 63a9f83..27fec5a 100644 --- a/localdb.cc +++ b/localdb.cc @@ -22,10 +22,12 @@ using std::string; -void LocalDb::Open(const char *path) +void LocalDb::Open(const char *path, const char *snapshot_name) { int rc; + snapshot = snapshot_name; + rc = sqlite3_open(path, &db); if (rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); @@ -81,3 +83,67 @@ void LocalDb::StoreObject(const ObjectReference& ref, sqlite3_finalize(stmt); } + +ObjectReference LocalDb::FindObject(const string &checksum, int64_t size) +{ + int rc; + sqlite3_stmt *stmt; + static const char s[] = + "select segment, object from block_index " + "where checksum = ? and size = ?"; + const char *tail; + + ObjectReference ref; + + rc = sqlite3_prepare_v2(db, s, strlen(s), &stmt, &tail); + if (rc != SQLITE_OK) { + return ref; + } + + sqlite3_bind_text(stmt, 1, checksum.c_str(), checksum.size(), + SQLITE_TRANSIENT); + sqlite3_bind_int64(stmt, 2, size); + + rc = sqlite3_step(stmt); + if (rc == SQLITE_DONE) { + } else if (rc == SQLITE_ROW) { + ref = ObjectReference((const char *)sqlite3_column_text(stmt, 0), + (const char *)sqlite3_column_text(stmt, 1)); + } else { + fprintf(stderr, "Could not execute SELECT statement!\n"); + } + + sqlite3_finalize(stmt); + + return ref; +} + +void LocalDb::UseObject(const ObjectReference& ref) +{ + int rc; + sqlite3_stmt *stmt; + static const char s[] = + "insert into snapshot_contents " + "select blockid, ? as snapshot from block_index " + "where segment = ? and object = ?"; + const char *tail; + + rc = sqlite3_prepare_v2(db, s, strlen(s), &stmt, &tail); + if (rc != SQLITE_OK) { + return; + } + + sqlite3_bind_text(stmt, 1, snapshot.c_str(), snapshot.size(), + SQLITE_TRANSIENT); + string seg = ref.get_segment(); + sqlite3_bind_text(stmt, 2, seg.c_str(), seg.size(), SQLITE_TRANSIENT); + string obj = ref.get_sequence(); + sqlite3_bind_text(stmt, 3, obj.c_str(), obj.size(), SQLITE_TRANSIENT); + + rc = sqlite3_step(stmt); + if (rc != SQLITE_DONE) { + fprintf(stderr, "Could not execute INSERT statement!\n"); + } + + sqlite3_finalize(stmt); +}