Chromium Code Reviews| Index: sync/syncable/directory_backing_store.cc |
| diff --git a/sync/syncable/directory_backing_store.cc b/sync/syncable/directory_backing_store.cc |
| index 9b8e0dba855edc22128e0cb4aab84d96ec485da5..86044442467e2c843dcf70da43e19440cb7d3549 100644 |
| --- a/sync/syncable/directory_backing_store.cc |
| +++ b/sync/syncable/directory_backing_store.cc |
| @@ -22,6 +22,7 @@ |
| #include "sql/connection.h" |
| #include "sql/statement.h" |
| #include "sql/transaction.h" |
| +#include "sync/internal_api/public/base/node_ordinal.h" |
| #include "sync/protocol/bookmark_specifics.pb.h" |
| #include "sync/protocol/sync.pb.h" |
| #include "sync/syncable/syncable-inl.h" |
| @@ -39,7 +40,7 @@ static const string::size_type kUpdateStatementBufferSize = 2048; |
| // Increment this version whenever updating DB tables. |
| extern const int32 kCurrentDBVersion; // Global visibility for our unittest. |
| -const int32 kCurrentDBVersion = 80; |
| +const int32 kCurrentDBVersion = 81; |
| // Iterate over the fields of |entry| and bind each to |statement| for |
| // updating. Returns the number of args bound. |
| @@ -69,10 +70,15 @@ void BindFields(const EntryKernel& entry, |
| entry.ref(static_cast<ProtoField>(i)).SerializeToString(&temp); |
| statement->BindBlob(index++, temp.data(), temp.length()); |
| } |
| + for( ; i < ORDINAL_FIELDS_END; ++i) { |
| + temp = entry.ref(static_cast<OrdinalField>(i)).ToInternalValue(); |
| + statement->BindBlob(index++, temp.data(), temp.length()); |
| + } |
| } |
| // The caller owns the returned EntryKernel*. Assumes the statement currently |
| -// points to a valid row in the metas table. |
| +// points to a valid row in the metas table. Returns null to indicate that |
| +// it detected a corruption in the data on unpacking. |
| EntryKernel* UnpackEntry(sql::Statement* statement) { |
| EntryKernel* kernel = new EntryKernel(); |
| DCHECK_EQ(statement->ColumnCount(), static_cast<int>(FIELD_COUNT)); |
| @@ -99,6 +105,20 @@ EntryKernel* UnpackEntry(sql::Statement* statement) { |
| kernel->mutable_ref(static_cast<ProtoField>(i)).ParseFromArray( |
| statement->ColumnBlob(i), statement->ColumnByteLength(i)); |
| } |
| + for( ; i < ORDINAL_FIELDS_END; ++i) { |
| + std::string temp; |
| + statement->ColumnBlobAsString(i, &temp); |
| + NodeOrdinal unpacked_ord(temp); |
| + |
| + // Its safe to assume that an invalid ordinal is a sign that |
| + // some external corruption has occurred. Return null to force |
| + // a re-download of the sync data. |
| + if(!unpacked_ord.IsValid()) { |
| + DVLOG(1) <<"Unpacked invalid ordinal. Signaling that the DB is corrupt"; |
|
akalin
2012/10/05 22:40:27
space after <<
|
| + return NULL; |
|
akalin
2012/10/05 22:40:27
you're leaking kernel here! Looks like a good tim
vishwath
2012/10/08 20:17:49
Done.
|
| + } |
| + kernel->mutable_ref(static_cast<OrdinalField>(i)) = unpacked_ord; |
| + } |
| return kernel; |
| } |
| @@ -323,6 +343,13 @@ bool DirectoryBackingStore::InitializeTables() { |
| version_on_disk = 80; |
| } |
| + // Version 81 changes the server_position_in_parent_field from an int64 |
| + // to a blob. |
| + if (version_on_disk == 80) { |
| + if (MigrateVersion80To81()) |
| + version_on_disk = 81; |
| + } |
| + |
| // If one of the migrations requested it, drop columns that aren't current. |
| // It's only safe to do this after migrating all the way to the current |
| // version. |
| @@ -429,6 +456,9 @@ bool DirectoryBackingStore::LoadEntries(MetahandlesIndex* entry_bucket) { |
| while (s.Step()) { |
| EntryKernel *kernel = UnpackEntry(&s); |
|
akalin
2012/10/05 22:40:27
' *' -> '* ', but this should be a scoped_ptr anyw
vishwath
2012/10/08 20:17:49
Done.
|
| + // A null kernel is evidence of external data corruption. |
| + if(!kernel) |
| + return false; |
| entry_bucket->insert(kernel); |
| } |
| return s.Succeeded(); |
| @@ -503,7 +533,7 @@ bool DirectoryBackingStore::SaveEntryToDB(const EntryKernel& entry) { |
| values.append("VALUES "); |
| const char* separator = "( "; |
| int i = 0; |
| - for (i = BEGIN_FIELDS; i < PROTO_FIELDS_END; ++i) { |
| + for (i = BEGIN_FIELDS; i < END_FIELDS; ++i) { |
| query.append(separator); |
| values.append(separator); |
| separator = ", "; |
| @@ -968,6 +998,7 @@ bool DirectoryBackingStore::MigrateVersion78To79() { |
| SetVersion(79); |
| return true; |
| } |
| + |
| bool DirectoryBackingStore::MigrateVersion79To80() { |
| if (!db_->Execute( |
| "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB")) |
| @@ -982,6 +1013,36 @@ bool DirectoryBackingStore::MigrateVersion79To80() { |
| return true; |
| } |
| +bool DirectoryBackingStore::MigrateVersion80To81() { |
| + if(!db_->Execute( |
| + "ALTER TABLE metas ADD COLUMN server_ordinal_in_parent BLOB")) |
| + return false; |
| + |
| + sql::Statement get_positions(db_->GetUniqueStatement( |
| + "SELECT metahandle, server_position_in_parent FROM metas")); |
| + |
| + sql::Statement put_ordinals(db_->GetUniqueStatement( |
| + "UPDATE metas SET server_ordinal_in_parent = ?" |
| + "WHERE metahandle = ?")); |
| + |
| + while(get_positions.Step()) { |
| + int64 metahandle = get_positions.ColumnInt64(0); |
| + int64 position = get_positions.ColumnInt64(1); |
| + |
| + const std::string& ordinal = Int64ToNodeOrdinal(position).ToInternalValue(); |
| + put_ordinals.BindBlob(0, ordinal.data(), ordinal.length()); |
| + put_ordinals.BindInt64(1, metahandle); |
| + |
| + if(!put_ordinals.Run()) |
| + return false; |
| + put_ordinals.Reset(true); |
| + } |
| + |
| + SetVersion(81); |
| + needs_column_refresh_ = true; |
| + return true; |
| +} |
| + |
| bool DirectoryBackingStore::CreateTables() { |
| DVLOG(1) << "First run, creating tables"; |
| // Create two little tables share_version and share_info |
| @@ -1044,10 +1105,12 @@ bool DirectoryBackingStore::CreateTables() { |
| const int64 now = TimeToProtoTime(base::Time::Now()); |
| sql::Statement s(db_->GetUniqueStatement( |
| "INSERT INTO metas " |
| - "( id, metahandle, is_dir, ctime, mtime) " |
| - "VALUES ( \"r\", 1, 1, ?, ?)")); |
| + "( id, metahandle, is_dir, ctime, mtime, server_ordinal_in_parent) " |
| + "VALUES ( \"r\", 1, 1, ?, ?, ?)")); |
| s.BindInt64(0, now); |
| s.BindInt64(1, now); |
| + std::string ord = NodeOrdinal::CreateInitialOrdinal().ToInternalValue(); |
| + s.BindBlob(2, ord.data(), ord.length()); |
| if (!s.Run()) |
| return false; |