Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/syncable/directory_backing_store.h" | 5 #include "sync/syncable/directory_backing_store.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| 11 #include "base/base64.h" | 11 #include "base/base64.h" |
| 12 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/hash_tables.h" | 14 #include "base/hash_tables.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/rand_util.h" | 17 #include "base/rand_util.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/string_number_conversions.h" | 19 #include "base/string_number_conversions.h" |
| 20 #include "base/stringprintf.h" | 20 #include "base/stringprintf.h" |
| 21 #include "base/time.h" | 21 #include "base/time.h" |
| 22 #include "sql/connection.h" | 22 #include "sql/connection.h" |
| 23 #include "sql/statement.h" | 23 #include "sql/statement.h" |
| 24 #include "sql/transaction.h" | 24 #include "sql/transaction.h" |
| 25 #include "sync/internal_api/public/base/node_ordinal.h" | |
| 25 #include "sync/protocol/bookmark_specifics.pb.h" | 26 #include "sync/protocol/bookmark_specifics.pb.h" |
| 26 #include "sync/protocol/sync.pb.h" | 27 #include "sync/protocol/sync.pb.h" |
| 27 #include "sync/syncable/syncable-inl.h" | 28 #include "sync/syncable/syncable-inl.h" |
| 28 #include "sync/syncable/syncable_columns.h" | 29 #include "sync/syncable/syncable_columns.h" |
| 29 #include "sync/util/time.h" | 30 #include "sync/util/time.h" |
| 30 | 31 |
| 31 using std::string; | 32 using std::string; |
| 32 | 33 |
| 33 namespace syncer { | 34 namespace syncer { |
| 34 namespace syncable { | 35 namespace syncable { |
| 35 | 36 |
| 36 // This just has to be big enough to hold an UPDATE or INSERT statement that | 37 // This just has to be big enough to hold an UPDATE or INSERT statement that |
| 37 // modifies all the columns in the entry table. | 38 // modifies all the columns in the entry table. |
| 38 static const string::size_type kUpdateStatementBufferSize = 2048; | 39 static const string::size_type kUpdateStatementBufferSize = 2048; |
| 39 | 40 |
| 40 // Increment this version whenever updating DB tables. | 41 // Increment this version whenever updating DB tables. |
| 41 extern const int32 kCurrentDBVersion; // Global visibility for our unittest. | 42 extern const int32 kCurrentDBVersion; // Global visibility for our unittest. |
| 42 const int32 kCurrentDBVersion = 80; | 43 const int32 kCurrentDBVersion = 81; |
| 43 | 44 |
| 44 // Iterate over the fields of |entry| and bind each to |statement| for | 45 // Iterate over the fields of |entry| and bind each to |statement| for |
| 45 // updating. Returns the number of args bound. | 46 // updating. Returns the number of args bound. |
| 46 void BindFields(const EntryKernel& entry, | 47 void BindFields(const EntryKernel& entry, |
| 47 sql::Statement* statement) { | 48 sql::Statement* statement) { |
| 48 int index = 0; | 49 int index = 0; |
| 49 int i = 0; | 50 int i = 0; |
| 50 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { | 51 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { |
| 51 statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i))); | 52 statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i))); |
| 52 } | 53 } |
| 53 for ( ; i < TIME_FIELDS_END; ++i) { | 54 for ( ; i < TIME_FIELDS_END; ++i) { |
| 54 statement->BindInt64(index++, | 55 statement->BindInt64(index++, |
| 55 TimeToProtoTime( | 56 TimeToProtoTime( |
| 56 entry.ref(static_cast<TimeField>(i)))); | 57 entry.ref(static_cast<TimeField>(i)))); |
| 57 } | 58 } |
| 58 for ( ; i < ID_FIELDS_END; ++i) { | 59 for ( ; i < ID_FIELDS_END; ++i) { |
| 59 statement->BindString(index++, entry.ref(static_cast<IdField>(i)).s_); | 60 statement->BindString(index++, entry.ref(static_cast<IdField>(i)).s_); |
| 60 } | 61 } |
| 61 for ( ; i < BIT_FIELDS_END; ++i) { | 62 for ( ; i < BIT_FIELDS_END; ++i) { |
| 62 statement->BindInt(index++, entry.ref(static_cast<BitField>(i))); | 63 statement->BindInt(index++, entry.ref(static_cast<BitField>(i))); |
| 63 } | 64 } |
| 64 for ( ; i < STRING_FIELDS_END; ++i) { | 65 for ( ; i < STRING_FIELDS_END; ++i) { |
| 65 statement->BindString(index++, entry.ref(static_cast<StringField>(i))); | 66 statement->BindString(index++, entry.ref(static_cast<StringField>(i))); |
| 66 } | 67 } |
| 67 std::string temp; | 68 std::string temp; |
| 68 for ( ; i < PROTO_FIELDS_END; ++i) { | 69 for ( ; i < PROTO_FIELDS_END; ++i) { |
| 69 entry.ref(static_cast<ProtoField>(i)).SerializeToString(&temp); | 70 entry.ref(static_cast<ProtoField>(i)).SerializeToString(&temp); |
| 70 statement->BindBlob(index++, temp.data(), temp.length()); | 71 statement->BindBlob(index++, temp.data(), temp.length()); |
| 71 } | 72 } |
| 73 for( ; i < ORDINAL_FIELDS_END; ++i) { | |
| 74 temp = entry.ref(static_cast<OrdinalField>(i)).ToInternalValue(); | |
| 75 statement->BindBlob(index++, temp.data(), temp.length()); | |
| 76 } | |
| 72 } | 77 } |
| 73 | 78 |
| 74 // The caller owns the returned EntryKernel*. Assumes the statement currently | 79 // The caller owns the returned EntryKernel*. Assumes the statement currently |
| 75 // points to a valid row in the metas table. | 80 // points to a valid row in the metas table. |
| 76 EntryKernel* UnpackEntry(sql::Statement* statement) { | 81 EntryKernel* UnpackEntry(sql::Statement* statement) { |
| 77 EntryKernel* kernel = new EntryKernel(); | 82 EntryKernel* kernel = new EntryKernel(); |
| 78 DCHECK_EQ(statement->ColumnCount(), static_cast<int>(FIELD_COUNT)); | 83 DCHECK_EQ(statement->ColumnCount(), static_cast<int>(FIELD_COUNT)); |
| 79 int i = 0; | 84 int i = 0; |
| 80 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { | 85 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { |
| 81 kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i)); | 86 kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i)); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 92 kernel->put(static_cast<BitField>(i), (0 != statement->ColumnInt(i))); | 97 kernel->put(static_cast<BitField>(i), (0 != statement->ColumnInt(i))); |
| 93 } | 98 } |
| 94 for ( ; i < STRING_FIELDS_END; ++i) { | 99 for ( ; i < STRING_FIELDS_END; ++i) { |
| 95 kernel->put(static_cast<StringField>(i), | 100 kernel->put(static_cast<StringField>(i), |
| 96 statement->ColumnString(i)); | 101 statement->ColumnString(i)); |
| 97 } | 102 } |
| 98 for ( ; i < PROTO_FIELDS_END; ++i) { | 103 for ( ; i < PROTO_FIELDS_END; ++i) { |
| 99 kernel->mutable_ref(static_cast<ProtoField>(i)).ParseFromArray( | 104 kernel->mutable_ref(static_cast<ProtoField>(i)).ParseFromArray( |
| 100 statement->ColumnBlob(i), statement->ColumnByteLength(i)); | 105 statement->ColumnBlob(i), statement->ColumnByteLength(i)); |
| 101 } | 106 } |
| 107 for( ; i < ORDINAL_FIELDS_END; ++i) { | |
| 108 std::string temp; | |
| 109 statement->ColumnBlobAsString(i, &temp); | |
| 110 kernel->mutable_ref(static_cast<OrdinalField>(i)) = NodeOrdinal(temp); | |
|
akalin
2012/10/05 00:57:59
how should we handle invalid NodeOrdinals? DCHECK
rlarocque
2012/10/05 01:10:50
One option would be to fail to load the directory
vishwath
2012/10/05 18:34:49
Done.
| |
| 111 } | |
| 102 return kernel; | 112 return kernel; |
| 103 } | 113 } |
| 104 | 114 |
| 105 namespace { | 115 namespace { |
| 106 | 116 |
| 107 string ComposeCreateTableColumnSpecs() { | 117 string ComposeCreateTableColumnSpecs() { |
| 108 const ColumnSpec* begin = g_metas_columns; | 118 const ColumnSpec* begin = g_metas_columns; |
| 109 const ColumnSpec* end = g_metas_columns + arraysize(g_metas_columns); | 119 const ColumnSpec* end = g_metas_columns + arraysize(g_metas_columns); |
| 110 string query; | 120 string query; |
| 111 query.reserve(kUpdateStatementBufferSize); | 121 query.reserve(kUpdateStatementBufferSize); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 if (MigrateVersion78To79()) | 326 if (MigrateVersion78To79()) |
| 317 version_on_disk = 79; | 327 version_on_disk = 79; |
| 318 } | 328 } |
| 319 | 329 |
| 320 // Version 80 migration is adding the bag_of_chips column. | 330 // Version 80 migration is adding the bag_of_chips column. |
| 321 if (version_on_disk == 79) { | 331 if (version_on_disk == 79) { |
| 322 if (MigrateVersion79To80()) | 332 if (MigrateVersion79To80()) |
| 323 version_on_disk = 80; | 333 version_on_disk = 80; |
| 324 } | 334 } |
| 325 | 335 |
| 336 // Version 81 changes the server_position_in_parent_field from an int64 | |
| 337 // to a blob. | |
| 338 if (version_on_disk == 80) { | |
| 339 if (MigrateVersion80To81()) | |
| 340 version_on_disk = 81; | |
| 341 } | |
| 342 | |
| 326 // If one of the migrations requested it, drop columns that aren't current. | 343 // If one of the migrations requested it, drop columns that aren't current. |
| 327 // It's only safe to do this after migrating all the way to the current | 344 // It's only safe to do this after migrating all the way to the current |
| 328 // version. | 345 // version. |
| 329 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { | 346 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { |
| 330 if (!RefreshColumns()) | 347 if (!RefreshColumns()) |
| 331 version_on_disk = 0; | 348 version_on_disk = 0; |
| 332 } | 349 } |
| 333 | 350 |
| 334 // A final, alternative catch-all migration to simply re-sync everything. | 351 // A final, alternative catch-all migration to simply re-sync everything. |
| 335 // | 352 // |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 496 // and cache it ourselves the first time this function is called. | 513 // and cache it ourselves the first time this function is called. |
| 497 if (!save_entry_statement_.is_valid()) { | 514 if (!save_entry_statement_.is_valid()) { |
| 498 string query; | 515 string query; |
| 499 query.reserve(kUpdateStatementBufferSize); | 516 query.reserve(kUpdateStatementBufferSize); |
| 500 query.append("INSERT OR REPLACE INTO metas "); | 517 query.append("INSERT OR REPLACE INTO metas "); |
| 501 string values; | 518 string values; |
| 502 values.reserve(kUpdateStatementBufferSize); | 519 values.reserve(kUpdateStatementBufferSize); |
| 503 values.append("VALUES "); | 520 values.append("VALUES "); |
| 504 const char* separator = "( "; | 521 const char* separator = "( "; |
| 505 int i = 0; | 522 int i = 0; |
| 506 for (i = BEGIN_FIELDS; i < PROTO_FIELDS_END; ++i) { | 523 for (i = BEGIN_FIELDS; i < ORDINAL_FIELDS_END; ++i) { |
|
akalin
2012/10/05 00:57:59
can you use FIELD_COUNT instead of ORDINAL_FIELDS_
vishwath
2012/10/05 18:34:49
Called it END_FIELDS to match with BEGIN_FIELDS, b
| |
| 507 query.append(separator); | 524 query.append(separator); |
| 508 values.append(separator); | 525 values.append(separator); |
| 509 separator = ", "; | 526 separator = ", "; |
| 510 query.append(ColumnName(i)); | 527 query.append(ColumnName(i)); |
| 511 values.append("?"); | 528 values.append("?"); |
| 512 } | 529 } |
| 513 query.append(" ) "); | 530 query.append(" ) "); |
| 514 values.append(" )"); | 531 values.append(" )"); |
| 515 query.append(values); | 532 query.append(values); |
| 516 | 533 |
| (...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 961 // Some users are stuck with a DB that causes them to reuse existing IDs. We | 978 // Some users are stuck with a DB that causes them to reuse existing IDs. We |
| 962 // perform this one-time fixup on all users to help the few that are stuck. | 979 // perform this one-time fixup on all users to help the few that are stuck. |
| 963 // See crbug.com/142987 for details. | 980 // See crbug.com/142987 for details. |
| 964 if (!db_->Execute( | 981 if (!db_->Execute( |
| 965 "UPDATE share_info SET next_id = next_id - 65536")) { | 982 "UPDATE share_info SET next_id = next_id - 65536")) { |
| 966 return false; | 983 return false; |
| 967 } | 984 } |
| 968 SetVersion(79); | 985 SetVersion(79); |
| 969 return true; | 986 return true; |
| 970 } | 987 } |
| 988 | |
| 971 bool DirectoryBackingStore::MigrateVersion79To80() { | 989 bool DirectoryBackingStore::MigrateVersion79To80() { |
| 972 if (!db_->Execute( | 990 if (!db_->Execute( |
| 973 "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB")) | 991 "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB")) |
| 974 return false; | 992 return false; |
| 975 sql::Statement update(db_->GetUniqueStatement( | 993 sql::Statement update(db_->GetUniqueStatement( |
| 976 "UPDATE share_info SET bag_of_chips = ?")); | 994 "UPDATE share_info SET bag_of_chips = ?")); |
| 977 // An empty message is serialized to an empty string. | 995 // An empty message is serialized to an empty string. |
| 978 update.BindBlob(0, NULL, 0); | 996 update.BindBlob(0, NULL, 0); |
| 979 if (!update.Run()) | 997 if (!update.Run()) |
| 980 return false; | 998 return false; |
| 981 SetVersion(80); | 999 SetVersion(80); |
| 982 return true; | 1000 return true; |
| 983 } | 1001 } |
| 984 | 1002 |
| 1003 bool DirectoryBackingStore::MigrateVersion80To81() { | |
| 1004 if(!db_->Execute( | |
| 1005 "ALTER TABLE metas ADD COLUMN server_ordinal_in_parent BLOB")) | |
| 1006 return false; | |
| 1007 | |
| 1008 sql::Statement get_positions(db_->GetUniqueStatement( | |
| 1009 "SELECT metahandle, server_position_in_parent FROM metas")); | |
| 1010 | |
| 1011 sql::Statement put_ordinals(db_->GetUniqueStatement( | |
| 1012 "UPDATE metas SET server_ordinal_in_parent = ?" | |
| 1013 "WHERE metahandle = ?")); | |
| 1014 | |
| 1015 while(get_positions.Step()) { | |
| 1016 int64 metahandle = get_positions.ColumnInt64(0); | |
| 1017 int64 position = get_positions.ColumnInt64(1); | |
| 1018 | |
| 1019 std::string ordinal = Int64ToNodeOrdinal(position).ToInternalValue(); | |
|
rlarocque
2012/10/05 19:02:55
I think you can make this string a const ref.
vishwath
2012/10/05 21:05:48
Done.
| |
| 1020 put_ordinals.BindBlob(0, ordinal.data(), ordinal.length()); | |
| 1021 put_ordinals.BindInt64(1, metahandle); | |
| 1022 | |
| 1023 if(!put_ordinals.Run()) | |
| 1024 return false; | |
| 1025 put_ordinals.Reset(true); | |
| 1026 } | |
| 1027 | |
| 1028 SetVersion(81); | |
| 1029 needs_column_refresh_ = true; | |
| 1030 return true; | |
| 1031 } | |
| 1032 | |
| 985 bool DirectoryBackingStore::CreateTables() { | 1033 bool DirectoryBackingStore::CreateTables() { |
| 986 DVLOG(1) << "First run, creating tables"; | 1034 DVLOG(1) << "First run, creating tables"; |
| 987 // Create two little tables share_version and share_info | 1035 // Create two little tables share_version and share_info |
| 988 if (!db_->Execute( | 1036 if (!db_->Execute( |
| 989 "CREATE TABLE share_version (" | 1037 "CREATE TABLE share_version (" |
| 990 "id VARCHAR(128) primary key, data INT)")) { | 1038 "id VARCHAR(128) primary key, data INT)")) { |
| 991 return false; | 1039 return false; |
| 992 } | 1040 } |
| 993 | 1041 |
| 994 { | 1042 { |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1153 bool prev_exists = (ids_set.find(entry->ref(PREV_ID).value()) != end); | 1201 bool prev_exists = (ids_set.find(entry->ref(PREV_ID).value()) != end); |
| 1154 bool parent_exists = (ids_set.find(entry->ref(PARENT_ID).value()) != end); | 1202 bool parent_exists = (ids_set.find(entry->ref(PARENT_ID).value()) != end); |
| 1155 bool next_exists = (ids_set.find(entry->ref(NEXT_ID).value()) != end); | 1203 bool next_exists = (ids_set.find(entry->ref(NEXT_ID).value()) != end); |
| 1156 is_ok = is_ok && prev_exists && parent_exists && next_exists; | 1204 is_ok = is_ok && prev_exists && parent_exists && next_exists; |
| 1157 } | 1205 } |
| 1158 return is_ok; | 1206 return is_ok; |
| 1159 } | 1207 } |
| 1160 | 1208 |
| 1161 } // namespace syncable | 1209 } // namespace syncable |
| 1162 } // namespace syncer | 1210 } // namespace syncer |
| OLD | NEW |