X-Git-Url: http://git.vrable.net/?p=cumulus.git;a=blobdiff_plain;f=localdb.cc;h=fd86605a18860fda2d8b8da448e7917410086099;hp=ca8355951bedd9f102c957208cec5e472d79424c;hb=HEAD;hpb=3d780590edec4583eb3ef0ca16120afd0f7451f9 diff --git a/localdb.cc b/localdb.cc index ca83559..fd86605 100644 --- a/localdb.cc +++ b/localdb.cc @@ -32,12 +32,14 @@ #include #include +#include #include #include "localdb.h" #include "store.h" #include "util.h" +using std::map; using std::max; using std::min; using std::set; @@ -70,7 +72,7 @@ void LocalDb::ReportError(int rc) } void LocalDb::Open(const char *path, const char *snapshot_name, - const char *snapshot_scheme, double intent) + const char *snapshot_scheme) { int rc; @@ -115,13 +117,12 @@ void LocalDb::Open(const char *path, const char *snapshot_name, /* Insert this snapshot into the database, and determine the integer key * which will be used to identify it. */ - stmt = Prepare("insert into snapshots(name, scheme, timestamp, intent) " - "values (?, ?, julianday('now'), ?)"); + stmt = Prepare("insert into snapshots(name, scheme, timestamp) " + "values (?, ?, julianday('now'))"); sqlite3_bind_text(stmt, 1, snapshot_name, strlen(snapshot_name), SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 2, snapshot_scheme, strlen(snapshot_scheme), SQLITE_TRANSIENT); - sqlite3_bind_double(stmt, 3, intent); rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) { @@ -283,17 +284,6 @@ void LocalDb::StoreObject(const ObjectReference& ref, double age) } sqlite3_finalize(stmt); - - if (age != 0.0) { - stmt = Prepare("update segments " - "set mtime = coalesce(max(mtime, ?), ?) " - "where segmentid = ?"); - sqlite3_bind_double(stmt, 1, age); - sqlite3_bind_double(stmt, 2, age); - sqlite3_bind_int64(stmt, 3, SegmentToId(ref.get_segment())); - rc = sqlite3_step(stmt); - sqlite3_finalize(stmt); - } } ObjectReference LocalDb::FindObject(const string &checksum, int64_t size) @@ -331,7 +321,7 @@ bool LocalDb::IsOldObject(const string &checksum, int64_t size, double *age, sqlite3_stmt *stmt; bool found = false; - stmt = Prepare("select segmentid, object, timestamp, expired " + stmt = Prepare("select segmentid, object, julianday(timestamp), expired " "from block_index where checksum = ? and size = ?"); sqlite3_bind_text(stmt, 1, checksum.c_str(), checksum.size(), SQLITE_TRANSIENT); @@ -464,7 +454,7 @@ void LocalDb::UseObject(const ObjectReference& ref) // size will have a reference size capped at just less than the full object // size (we can't tell if some bytes were referenced multiple times, and // thus we conservatively assume some bytes might still be unreferenced). - int64_t new_refs = old_size; + int64_t new_refs; if (ref.has_range()) { new_refs = ref.get_range_length(); } else { @@ -495,25 +485,29 @@ void LocalDb::UseObject(const ObjectReference& ref) } } -void LocalDb::SetSegmentChecksum(const std::string &segment, +void LocalDb::SetSegmentMetadata(const std::string &segment, const std::string &path, const std::string &checksum, + const std::string &type, int data_size, int disk_size) { int rc; sqlite3_stmt *stmt; stmt = Prepare("update segments set path = ?, checksum = ?, " - "data_size = ?, disk_size = ?, " - "mtime = coalesce(mtime, julianday('now')) " + "type = ?, data_size = ?, disk_size = ?, " + "timestamp = coalesce(julianday(timestamp), " + " julianday('now')) " "where segmentid = ?"); sqlite3_bind_text(stmt, 1, path.c_str(), path.size(), SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 2, checksum.c_str(), checksum.size(), SQLITE_TRANSIENT); - sqlite3_bind_int64(stmt, 3, data_size); - sqlite3_bind_int64(stmt, 4, disk_size); - sqlite3_bind_int64(stmt, 5, SegmentToId(segment)); + sqlite3_bind_text(stmt, 3, type.c_str(), type.size(), + SQLITE_TRANSIENT); + sqlite3_bind_int64(stmt, 4, data_size); + sqlite3_bind_int64(stmt, 5, disk_size); + sqlite3_bind_int64(stmt, 6, SegmentToId(segment)); rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) { @@ -524,36 +518,33 @@ void LocalDb::SetSegmentChecksum(const std::string &segment, sqlite3_finalize(stmt); } -bool LocalDb::GetSegmentChecksum(const string &segment, - string *seg_path, - string *seg_checksum) +map LocalDb::GetSegmentMetadata(const string &segment) { int rc; sqlite3_stmt *stmt; - ObjectReference ref; - int found = false; + map info; + + // Names in the returned map, in the order returned from the select + // statement below. + static const char *fields[] = { + "datetime", "path", "checksum", "data_size", "disk_size", "type", NULL + }; - stmt = Prepare("select path, checksum from segments where segment = ?"); + stmt = Prepare("select datetime(timestamp), path, checksum, " + " data_size, disk_size, type " + "from segments where segment = ?"); sqlite3_bind_text(stmt, 1, segment.c_str(), segment.size(), SQLITE_TRANSIENT); rc = sqlite3_step(stmt); if (rc == SQLITE_DONE) { } else if (rc == SQLITE_ROW) { - found = true; - const char *val; - - val = (const char *)sqlite3_column_text(stmt, 0); - if (val == NULL) - found = false; - else - *seg_path = val; - - val = (const char *)sqlite3_column_text(stmt, 1); - if (val == NULL) - found = false; - else - *seg_checksum = val; + info["segment"] = segment; + for (int i = 0; fields[i] != NULL; i++) { + const char *val = (const char *)sqlite3_column_text(stmt, i); + if (val != NULL) + info[fields[i]] = val; + } } else { fprintf(stderr, "Could not execute SELECT statement!\n"); ReportError(rc); @@ -561,7 +552,7 @@ bool LocalDb::GetSegmentChecksum(const string &segment, sqlite3_finalize(stmt); - return found; + return info; } /* Look up and return the packed representation of the subblock chunk