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/protocol/bookmark_specifics.pb.h" | 25 #include "sync/protocol/bookmark_specifics.pb.h" |
| 26 #include "sync/protocol/sync.pb.h" | 26 #include "sync/protocol/sync.pb.h" |
| 27 #include "sync/syncable/syncable-inl.h" | 27 #include "sync/syncable/syncable-inl.h" |
| 28 #include "sync/syncable/syncable_columns.h" | 28 #include "sync/syncable/syncable_columns.h" |
| 29 #include "sync/util/time.h" | 29 #include "sync/util/time.h" |
| 30 #include "sync/internal_api/public/base/ordinal.h" | |
|
rlarocque
2012/10/01 21:45:55
These should be in alphabetical order.
vishwath
2012/10/02 01:06:33
Done.
| |
| 31 #include "sync/internal_api/public/base/node_ordinal.h" | |
| 30 | 32 |
| 31 using std::string; | 33 using std::string; |
| 32 | 34 |
| 33 namespace syncer { | 35 namespace syncer { |
| 34 namespace syncable { | 36 namespace syncable { |
| 35 | 37 |
| 36 // This just has to be big enough to hold an UPDATE or INSERT statement that | 38 // This just has to be big enough to hold an UPDATE or INSERT statement that |
| 37 // modifies all the columns in the entry table. | 39 // modifies all the columns in the entry table. |
| 38 static const string::size_type kUpdateStatementBufferSize = 2048; | 40 static const string::size_type kUpdateStatementBufferSize = 2048; |
| 39 | 41 |
| 40 // Increment this version whenever updating DB tables. | 42 // Increment this version whenever updating DB tables. |
| 41 extern const int32 kCurrentDBVersion; // Global visibility for our unittest. | 43 extern const int32 kCurrentDBVersion; // Global visibility for our unittest. |
| 42 const int32 kCurrentDBVersion = 80; | 44 const int32 kCurrentDBVersion = 81; |
| 43 | 45 |
| 44 // Iterate over the fields of |entry| and bind each to |statement| for | 46 // Iterate over the fields of |entry| and bind each to |statement| for |
| 45 // updating. Returns the number of args bound. | 47 // updating. Returns the number of args bound. |
| 46 void BindFields(const EntryKernel& entry, | 48 void BindFields(const EntryKernel& entry, |
| 47 sql::Statement* statement) { | 49 sql::Statement* statement) { |
| 48 int index = 0; | 50 int index = 0; |
| 49 int i = 0; | 51 int i = 0; |
| 50 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { | 52 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { |
| 51 statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i))); | 53 // Store SERVER_POSITION_IN_PARENT as a blob for eventual shift |
| 54 // to using ordinals instead of int64's | |
|
rlarocque
2012/10/01 21:45:55
Comments should end with a period.
akalin
2012/10/01 23:13:35
int64's int64s
vishwath
2012/10/02 01:06:33
Done.
vishwath
2012/10/02 01:06:33
Done.
| |
| 55 // See crbug.com/145412 | |
| 56 if (i == SERVER_POSITION_IN_PARENT) { | |
| 57 std::string ordinal = Int64ToNodeOrdinal( | |
| 58 entry.ref(SERVER_POSITION_IN_PARENT)).ToInternalValue(); | |
| 59 statement->BindBlob(index++, ordinal.data(), ordinal.size()); | |
| 60 } | |
| 61 else | |
|
rlarocque
2012/10/01 21:45:55
That's unusual formatting. You should put the els
vishwath
2012/10/02 01:06:33
An unfortunate take-away from too much python prog
| |
| 62 statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i))); | |
| 52 } | 63 } |
| 53 for ( ; i < TIME_FIELDS_END; ++i) { | 64 for ( ; i < TIME_FIELDS_END; ++i) { |
| 54 statement->BindInt64(index++, | 65 statement->BindInt64(index++, |
| 55 TimeToProtoTime( | 66 TimeToProtoTime( |
| 56 entry.ref(static_cast<TimeField>(i)))); | 67 entry.ref(static_cast<TimeField>(i)))); |
| 57 } | 68 } |
| 58 for ( ; i < ID_FIELDS_END; ++i) { | 69 for ( ; i < ID_FIELDS_END; ++i) { |
| 59 statement->BindString(index++, entry.ref(static_cast<IdField>(i)).s_); | 70 statement->BindString(index++, entry.ref(static_cast<IdField>(i)).s_); |
| 60 } | 71 } |
| 61 for ( ; i < BIT_FIELDS_END; ++i) { | 72 for ( ; i < BIT_FIELDS_END; ++i) { |
| 62 statement->BindInt(index++, entry.ref(static_cast<BitField>(i))); | 73 statement->BindInt(index++, entry.ref(static_cast<BitField>(i))); |
| 63 } | 74 } |
| 64 for ( ; i < STRING_FIELDS_END; ++i) { | 75 for ( ; i < STRING_FIELDS_END; ++i) { |
| 65 statement->BindString(index++, entry.ref(static_cast<StringField>(i))); | 76 statement->BindString(index++, entry.ref(static_cast<StringField>(i))); |
| 66 } | 77 } |
| 67 std::string temp; | 78 std::string temp; |
| 68 for ( ; i < PROTO_FIELDS_END; ++i) { | 79 for ( ; i < PROTO_FIELDS_END; ++i) { |
| 69 entry.ref(static_cast<ProtoField>(i)).SerializeToString(&temp); | 80 entry.ref(static_cast<ProtoField>(i)).SerializeToString(&temp); |
| 70 statement->BindBlob(index++, temp.data(), temp.length()); | 81 statement->BindBlob(index++, temp.data(), temp.length()); |
| 71 } | 82 } |
| 72 } | 83 } |
| 73 | 84 |
| 74 // The caller owns the returned EntryKernel*. Assumes the statement currently | 85 // The caller owns the returned EntryKernel*. Assumes the statement currently |
| 75 // points to a valid row in the metas table. | 86 // points to a valid row in the metas table. |
| 76 EntryKernel* UnpackEntry(sql::Statement* statement) { | 87 EntryKernel* UnpackEntry(sql::Statement* statement) { |
| 77 EntryKernel* kernel = new EntryKernel(); | 88 EntryKernel* kernel = new EntryKernel(); |
| 78 DCHECK_EQ(statement->ColumnCount(), static_cast<int>(FIELD_COUNT)); | 89 DCHECK_EQ(statement->ColumnCount(), static_cast<int>(FIELD_COUNT)); |
| 79 int i = 0; | 90 int i = 0; |
| 80 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { | 91 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { |
| 81 kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i)); | 92 // Store SERVER_POSITION_IN_PARENT as a string for eventual shift |
| 93 // to using ordinals instead of int64's //VISH_TODO: CONTINUE HERE | |
|
rlarocque
2012/10/01 21:45:55
Did you intend to leave this comment here?
vishwath
2012/10/02 01:06:33
That would be an embarrassed no.
On 2012/10/01 21:
| |
| 94 if(i == SERVER_POSITION_IN_PARENT && | |
| 95 statement->ColumnType(i) == sql::COLUMN_TYPE_BLOB) { | |
|
rlarocque
2012/10/01 21:45:55
Make this second expression a DCHECK? We shouldn'
vishwath
2012/10/02 01:06:33
I changed the check to a test for a non-null colum
| |
| 96 std::string ordinal_blob; | |
| 97 DCHECK(statement->ColumnBlobAsString(i, &ordinal_blob)); | |
| 98 int64 server_position = NodeOrdinalToInt64( | |
| 99 NodeOrdinal(ordinal_blob)); | |
| 100 kernel->put( | |
| 101 static_cast<Int64Field>(SERVER_POSITION_IN_PARENT), | |
|
rlarocque
2012/10/01 21:45:55
The parameters to put() should be aligned.
vishwath
2012/10/02 01:06:33
Done.
| |
| 102 server_position); | |
| 103 } | |
| 104 else | |
|
rlarocque
2012/10/01 21:45:55
Change this formatting, too.
vishwath
2012/10/02 01:06:33
Done.
| |
| 105 kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i)); | |
| 82 } | 106 } |
| 83 for ( ; i < TIME_FIELDS_END; ++i) { | 107 for ( ; i < TIME_FIELDS_END; ++i) { |
| 84 kernel->put(static_cast<TimeField>(i), | 108 kernel->put(static_cast<TimeField>(i), |
| 85 ProtoTimeToTime(statement->ColumnInt64(i))); | 109 ProtoTimeToTime(statement->ColumnInt64(i))); |
| 86 } | 110 } |
| 87 for ( ; i < ID_FIELDS_END; ++i) { | 111 for ( ; i < ID_FIELDS_END; ++i) { |
| 88 kernel->mutable_ref(static_cast<IdField>(i)).s_ = | 112 kernel->mutable_ref(static_cast<IdField>(i)).s_ = |
| 89 statement->ColumnString(i); | 113 statement->ColumnString(i); |
| 90 } | 114 } |
| 91 for ( ; i < BIT_FIELDS_END; ++i) { | 115 for ( ; i < BIT_FIELDS_END; ++i) { |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 if (MigrateVersion78To79()) | 340 if (MigrateVersion78To79()) |
| 317 version_on_disk = 79; | 341 version_on_disk = 79; |
| 318 } | 342 } |
| 319 | 343 |
| 320 // Version 80 migration is adding the bag_of_chips column. | 344 // Version 80 migration is adding the bag_of_chips column. |
| 321 if (version_on_disk == 79) { | 345 if (version_on_disk == 79) { |
| 322 if (MigrateVersion79To80()) | 346 if (MigrateVersion79To80()) |
| 323 version_on_disk = 80; | 347 version_on_disk = 80; |
| 324 } | 348 } |
| 325 | 349 |
| 350 // Version 81 changes the server_position_in_parent_field from an int64 | |
| 351 // to a string. | |
| 352 if (version_on_disk == 80) { | |
| 353 if (MigrateVersion80To81()) | |
| 354 version_on_disk = 81; | |
| 355 } | |
| 356 | |
| 326 // If one of the migrations requested it, drop columns that aren't current. | 357 // 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 | 358 // It's only safe to do this after migrating all the way to the current |
| 328 // version. | 359 // version. |
| 329 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { | 360 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { |
| 330 if (!RefreshColumns()) | 361 if (!RefreshColumns()) |
| 331 version_on_disk = 0; | 362 version_on_disk = 0; |
| 332 } | 363 } |
| 333 | 364 |
| 334 // A final, alternative catch-all migration to simply re-sync everything. | 365 // A final, alternative catch-all migration to simply re-sync everything. |
| 335 // | 366 // |
| (...skipping 625 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 | 992 // 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. | 993 // perform this one-time fixup on all users to help the few that are stuck. |
| 963 // See crbug.com/142987 for details. | 994 // See crbug.com/142987 for details. |
| 964 if (!db_->Execute( | 995 if (!db_->Execute( |
| 965 "UPDATE share_info SET next_id = next_id - 65536")) { | 996 "UPDATE share_info SET next_id = next_id - 65536")) { |
| 966 return false; | 997 return false; |
| 967 } | 998 } |
| 968 SetVersion(79); | 999 SetVersion(79); |
| 969 return true; | 1000 return true; |
| 970 } | 1001 } |
| 1002 | |
| 971 bool DirectoryBackingStore::MigrateVersion79To80() { | 1003 bool DirectoryBackingStore::MigrateVersion79To80() { |
| 972 if (!db_->Execute( | 1004 if (!db_->Execute( |
| 973 "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB")) | 1005 "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB")) |
| 974 return false; | 1006 return false; |
| 975 sql::Statement update(db_->GetUniqueStatement( | 1007 sql::Statement update(db_->GetUniqueStatement( |
| 976 "UPDATE share_info SET bag_of_chips = ?")); | 1008 "UPDATE share_info SET bag_of_chips = ?")); |
| 977 // An empty message is serialized to an empty string. | 1009 // An empty message is serialized to an empty string. |
| 978 update.BindBlob(0, NULL, 0); | 1010 update.BindBlob(0, NULL, 0); |
| 979 if (!update.Run()) | 1011 if (!update.Run()) |
| 980 return false; | 1012 return false; |
| 981 SetVersion(80); | 1013 SetVersion(80); |
| 982 return true; | 1014 return true; |
| 983 } | 1015 } |
| 984 | 1016 |
| 1017 bool DirectoryBackingStore::MigrateVersion80To81() { | |
| 1018 // Version 81 changes server_position_in_parent from a potentially lossy | |
| 1019 // int64 to an unbounded fidelity ordinal (stored as a blob) | |
|
rlarocque
2012/10/01 21:45:55
Append period here.
vishwath
2012/10/02 01:06:33
Done.
| |
| 1020 // We make the current metas table temporary, create a new table where | |
| 1021 // server_position_in_parent is of the right type, and transfer values | |
| 1022 // after converting from int64's to Ordinals. | |
| 1023 SafeDropTable("temp_metas"); | |
| 1024 if(!db_->Execute( | |
|
rlarocque
2012/10/01 21:45:55
Does this not fit on one line?
vishwath
2012/10/02 01:06:33
Done.
| |
| 1025 "ALTER TABLE metas RENAME TO temp_metas")) { | |
| 1026 return false; | |
| 1027 } | |
| 1028 if(!CreateMetasTable(false)) | |
| 1029 return false; | |
| 1030 | |
|
rlarocque
2012/10/01 21:45:55
Remove extra whitespace.
vishwath
2012/10/02 01:06:33
Done.
| |
| 1031 | |
| 1032 std::string old_info_string = "SELECT "; | |
| 1033 AppendColumnList(&old_info_string); | |
|
rlarocque
2012/10/01 21:45:55
This is dangerous. Someone might update g_metas_c
vishwath
2012/10/02 01:06:33
Done.
| |
| 1034 old_info_string.append(" FROM temp_metas"); | |
| 1035 | |
| 1036 std::string new_info_string = "INSERT INTO metas ("; | |
| 1037 AppendColumnList(&new_info_string); | |
|
rlarocque
2012/10/01 21:45:55
Same warning here, except that this call expects t
vishwath
2012/10/02 01:06:33
Done.
| |
| 1038 new_info_string.append(") VALUES ("); | |
| 1039 for(int i = BEGIN_FIELDS; i < BEGIN_FIELDS + FIELD_COUNT - 1; ++i) { | |
|
rlarocque
2012/10/01 21:45:55
Similar warnings apply to this loop, too. And oth
vishwath
2012/10/02 01:06:33
Done.
| |
| 1040 new_info_string.append("?, "); | |
| 1041 } | |
| 1042 new_info_string.append("?)"); | |
| 1043 | |
| 1044 sql::Statement old_info(db_->GetUniqueStatement( | |
| 1045 old_info_string.c_str())); | |
| 1046 sql::Statement new_info(db_->GetUniqueStatement( | |
| 1047 new_info_string.c_str())); | |
| 1048 | |
| 1049 while(old_info.Step()) { | |
| 1050 int64 int_ordinal = old_info.ColumnInt(3); | |
| 1051 std::string new_ordinal = | |
| 1052 Int64ToNodeOrdinal(int_ordinal).ToInternalValue(); | |
| 1053 //Loop and bind each new column based on the old column type | |
| 1054 for(int i = 0; i < FIELD_COUNT; ++i) { | |
| 1055 if (i == SERVER_POSITION_IN_PARENT) { | |
| 1056 new_info.BindBlob(i, new_ordinal.data(), new_ordinal.length()); | |
| 1057 continue; | |
| 1058 } | |
| 1059 switch(old_info.ColumnType(i)) { | |
| 1060 case sql::COLUMN_TYPE_INTEGER: | |
| 1061 new_info.BindInt64(i, old_info.ColumnInt64(i)); | |
| 1062 break; | |
| 1063 case sql::COLUMN_TYPE_FLOAT: | |
| 1064 new_info.BindDouble(i, old_info.ColumnDouble(i)); | |
| 1065 break; | |
| 1066 case sql::COLUMN_TYPE_TEXT: | |
| 1067 new_info.BindString(i, old_info.ColumnString(i)); | |
| 1068 break; | |
| 1069 case sql::COLUMN_TYPE_BLOB: | |
| 1070 new_info.BindBlob(i, | |
| 1071 old_info.ColumnBlob(i), | |
| 1072 old_info.ColumnByteLength(i)); | |
| 1073 break; | |
| 1074 case sql::COLUMN_TYPE_NULL: | |
| 1075 new_info.BindNull(i); | |
| 1076 } | |
| 1077 } | |
| 1078 | |
| 1079 if(!new_info.Run()) | |
| 1080 return false; | |
| 1081 new_info.Reset(true); | |
| 1082 } | |
| 1083 | |
| 1084 SetVersion(81); | |
| 1085 return true; | |
| 1086 } | |
| 1087 | |
| 985 bool DirectoryBackingStore::CreateTables() { | 1088 bool DirectoryBackingStore::CreateTables() { |
| 986 DVLOG(1) << "First run, creating tables"; | 1089 DVLOG(1) << "First run, creating tables"; |
| 987 // Create two little tables share_version and share_info | 1090 // Create two little tables share_version and share_info |
| 988 if (!db_->Execute( | 1091 if (!db_->Execute( |
| 989 "CREATE TABLE share_version (" | 1092 "CREATE TABLE share_version (" |
| 990 "id VARCHAR(128) primary key, data INT)")) { | 1093 "id VARCHAR(128) primary key, data INT)")) { |
| 991 return false; | 1094 return false; |
| 992 } | 1095 } |
| 993 | 1096 |
| 994 { | 1097 { |
| (...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); | 1256 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); | 1257 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); | 1258 bool next_exists = (ids_set.find(entry->ref(NEXT_ID).value()) != end); |
| 1156 is_ok = is_ok && prev_exists && parent_exists && next_exists; | 1259 is_ok = is_ok && prev_exists && parent_exists && next_exists; |
| 1157 } | 1260 } |
| 1158 return is_ok; | 1261 return is_ok; |
| 1159 } | 1262 } |
| 1160 | 1263 |
| 1161 } // namespace syncable | 1264 } // namespace syncable |
| 1162 } // namespace syncer | 1265 } // namespace syncer |
| OLD | NEW |