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" | |
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) { |
53 // Store SERVER_POSITION_IN_PARENT as a blob for eventual shift | |
54 // to using ordinals instead of int64's | |
rlarocque
2012/09/29 01:13:42
nit: put period after comment.
Also, you might wa
vishwath
2012/10/01 20:39:12
Done.
| |
55 if(i == SERVER_POSITION_IN_PARENT) { | |
rlarocque
2012/09/29 01:13:42
nit: space between if and parenthesis.
vishwath
2012/10/01 20:39:12
Done.
| |
56 std::string ordinal = Int64ToNodeOrdinal( | |
57 entry.ref(SERVER_POSITION_IN_PARENT)).ToInternalValue(); | |
58 statement->BindBlob(index++, ordinal.data(), ordinal.size()); | |
59 continue; | |
rlarocque
2012/09/29 01:13:42
I'd prefer an 'else' branch to 'continue'.
vishwath
2012/10/01 20:39:12
Done.
| |
60 } | |
51 statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i))); | 61 statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i))); |
52 } | 62 } |
53 for ( ; i < TIME_FIELDS_END; ++i) { | 63 for ( ; i < TIME_FIELDS_END; ++i) { |
54 statement->BindInt64(index++, | 64 statement->BindInt64(index++, |
55 TimeToProtoTime( | 65 TimeToProtoTime( |
56 entry.ref(static_cast<TimeField>(i)))); | 66 entry.ref(static_cast<TimeField>(i)))); |
57 } | 67 } |
58 for ( ; i < ID_FIELDS_END; ++i) { | 68 for ( ; i < ID_FIELDS_END; ++i) { |
59 statement->BindString(index++, entry.ref(static_cast<IdField>(i)).s_); | 69 statement->BindString(index++, entry.ref(static_cast<IdField>(i)).s_); |
60 } | 70 } |
(...skipping 10 matching lines...) Expand all Loading... | |
71 } | 81 } |
72 } | 82 } |
73 | 83 |
74 // The caller owns the returned EntryKernel*. Assumes the statement currently | 84 // The caller owns the returned EntryKernel*. Assumes the statement currently |
75 // points to a valid row in the metas table. | 85 // points to a valid row in the metas table. |
76 EntryKernel* UnpackEntry(sql::Statement* statement) { | 86 EntryKernel* UnpackEntry(sql::Statement* statement) { |
77 EntryKernel* kernel = new EntryKernel(); | 87 EntryKernel* kernel = new EntryKernel(); |
78 DCHECK_EQ(statement->ColumnCount(), static_cast<int>(FIELD_COUNT)); | 88 DCHECK_EQ(statement->ColumnCount(), static_cast<int>(FIELD_COUNT)); |
79 int i = 0; | 89 int i = 0; |
80 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { | 90 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { |
91 // Store SERVER_POSITION_IN_PARENT as a string for eventual shift | |
92 // to using ordinals instead of int64's | |
93 if(i == SERVER_POSITION_IN_PARENT && | |
94 statement->ColumnType(i) == sql::COLUMN_TYPE_BLOB) { | |
rlarocque
2012/09/29 01:13:42
We shouldn't get here unless the database is migra
| |
95 std::string ordinal_blob; | |
96 DCHECK(statement->ColumnBlobAsString(i, &ordinal_blob)); | |
97 int64 server_position = NodeOrdinalToInt64( | |
98 NodeOrdinal(ordinal_blob)); | |
99 kernel->put(static_cast<Int64Field>(i), server_position); | |
rlarocque
2012/09/29 01:13:42
nit: I'd prefer that the first argument to put be
vishwath
2012/10/01 20:39:12
Done.
| |
100 continue; | |
rlarocque
2012/09/29 01:13:42
I'd prefer an else branch here.
vishwath
2012/10/01 20:39:12
Done.
| |
101 } | |
81 kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i)); | 102 kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i)); |
82 } | 103 } |
83 for ( ; i < TIME_FIELDS_END; ++i) { | 104 for ( ; i < TIME_FIELDS_END; ++i) { |
84 kernel->put(static_cast<TimeField>(i), | 105 kernel->put(static_cast<TimeField>(i), |
85 ProtoTimeToTime(statement->ColumnInt64(i))); | 106 ProtoTimeToTime(statement->ColumnInt64(i))); |
86 } | 107 } |
87 for ( ; i < ID_FIELDS_END; ++i) { | 108 for ( ; i < ID_FIELDS_END; ++i) { |
88 kernel->mutable_ref(static_cast<IdField>(i)).s_ = | 109 kernel->mutable_ref(static_cast<IdField>(i)).s_ = |
89 statement->ColumnString(i); | 110 statement->ColumnString(i); |
90 } | 111 } |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
316 if (MigrateVersion78To79()) | 337 if (MigrateVersion78To79()) |
317 version_on_disk = 79; | 338 version_on_disk = 79; |
318 } | 339 } |
319 | 340 |
320 // Version 80 migration is adding the bag_of_chips column. | 341 // Version 80 migration is adding the bag_of_chips column. |
321 if (version_on_disk == 79) { | 342 if (version_on_disk == 79) { |
322 if (MigrateVersion79To80()) | 343 if (MigrateVersion79To80()) |
323 version_on_disk = 80; | 344 version_on_disk = 80; |
324 } | 345 } |
325 | 346 |
347 // Version 81 changes the server_position_in_parent_field from an int64 | |
348 // to a string. | |
349 if (version_on_disk == 80) { | |
350 if (MigrateVersion80To81()) | |
351 version_on_disk = 81; | |
352 } | |
353 | |
326 // If one of the migrations requested it, drop columns that aren't current. | 354 // 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 | 355 // It's only safe to do this after migrating all the way to the current |
328 // version. | 356 // version. |
329 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { | 357 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { |
330 if (!RefreshColumns()) | 358 if (!RefreshColumns()) |
331 version_on_disk = 0; | 359 version_on_disk = 0; |
332 } | 360 } |
333 | 361 |
334 // A final, alternative catch-all migration to simply re-sync everything. | 362 // A final, alternative catch-all migration to simply re-sync everything. |
335 // | 363 // |
(...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 | 989 // 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. | 990 // perform this one-time fixup on all users to help the few that are stuck. |
963 // See crbug.com/142987 for details. | 991 // See crbug.com/142987 for details. |
964 if (!db_->Execute( | 992 if (!db_->Execute( |
965 "UPDATE share_info SET next_id = next_id - 65536")) { | 993 "UPDATE share_info SET next_id = next_id - 65536")) { |
966 return false; | 994 return false; |
967 } | 995 } |
968 SetVersion(79); | 996 SetVersion(79); |
969 return true; | 997 return true; |
970 } | 998 } |
999 | |
971 bool DirectoryBackingStore::MigrateVersion79To80() { | 1000 bool DirectoryBackingStore::MigrateVersion79To80() { |
972 if (!db_->Execute( | 1001 if (!db_->Execute( |
973 "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB")) | 1002 "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB")) |
974 return false; | 1003 return false; |
975 sql::Statement update(db_->GetUniqueStatement( | 1004 sql::Statement update(db_->GetUniqueStatement( |
976 "UPDATE share_info SET bag_of_chips = ?")); | 1005 "UPDATE share_info SET bag_of_chips = ?")); |
977 // An empty message is serialized to an empty string. | 1006 // An empty message is serialized to an empty string. |
978 update.BindBlob(0, NULL, 0); | 1007 update.BindBlob(0, NULL, 0); |
979 if (!update.Run()) | 1008 if (!update.Run()) |
980 return false; | 1009 return false; |
981 SetVersion(80); | 1010 SetVersion(80); |
982 return true; | 1011 return true; |
983 } | 1012 } |
984 | 1013 |
1014 bool DirectoryBackingStore::MigrateVersion80To81() { | |
1015 // Version 81 changes server_position_in_parent from a potentially lossy | |
1016 // bigint to an unbounded fidelity ordinal (stored as a blob) | |
rlarocque
2012/09/29 01:13:42
I wouldn't call it a bigint, that term could be ov
vishwath
2012/10/01 20:39:12
Done.
| |
1017 // We make the current metas table temporary, create a new table where | |
1018 // server_position_in_parent is of the right type, and transfer values | |
1019 // after converting from int64's to Ordinals. | |
1020 SafeDropTable("temp_metas"); | |
rlarocque
2012/09/29 01:13:42
Is there a transaction open at this point? If not
vishwath
2012/10/01 20:39:12
I think all the Migrate*() functions rely on the c
| |
1021 if(!db_->Execute( | |
1022 "ALTER TABLE metas RENAME TO temp_metas")) { | |
1023 return false; | |
1024 } | |
1025 if(!CreateMetasTable(false)) | |
1026 return false; | |
1027 | |
1028 std::string old_info_string = "SELECT "; | |
rlarocque
2012/09/29 01:13:42
I was hoping we could perform the migration withou
vishwath
2012/10/01 20:39:12
That'a actually the way I'd prefer to do it as wel
| |
1029 AppendColumnList(&old_info_string); | |
1030 old_info_string.append(" FROM temp_metas"); | |
1031 | |
1032 std::string new_info_string = "INSERT INTO metas ("; | |
1033 AppendColumnList(&new_info_string); | |
1034 new_info_string.append(") VALUES ("); | |
1035 for(int i = BEGIN_FIELDS; i < BEGIN_FIELDS + FIELD_COUNT - 1; ++i) { | |
rlarocque
2012/09/29 01:13:42
I'd really like to avoid this sort of thing, thoug
vishwath
2012/10/01 20:39:12
I see the inherent vulnerabilities in misusing thi
| |
1036 new_info_string.append("?, "); | |
1037 } | |
1038 new_info_string.append("?)"); | |
1039 | |
1040 sql::Statement old_info(db_->GetUniqueStatement( | |
1041 old_info_string.c_str())); | |
1042 sql::Statement new_info(db_->GetUniqueStatement( | |
1043 new_info_string.c_str())); | |
1044 | |
1045 while(old_info.Step()) { | |
1046 int64 int_ordinal = old_info.ColumnInt(3); | |
1047 std::string new_ordinal = | |
1048 Int64ToNodeOrdinal(int_ordinal).ToInternalValue(); | |
1049 //Loop and bind each new column based on the old column type | |
1050 for(int i = 0; i < FIELD_COUNT; ++i) { | |
1051 if (i == SERVER_POSITION_IN_PARENT) { | |
1052 new_info.BindBlob(i, new_ordinal.data(), new_ordinal.length()); | |
1053 continue; | |
1054 } | |
1055 switch(old_info.ColumnType(i)) { | |
1056 case sql::COLUMN_TYPE_INTEGER: | |
1057 new_info.BindInt64(i, old_info.ColumnInt64(i)); | |
1058 break; | |
1059 case sql::COLUMN_TYPE_FLOAT: | |
1060 new_info.BindDouble(i, old_info.ColumnDouble(i)); | |
1061 break; | |
1062 case sql::COLUMN_TYPE_TEXT: | |
1063 new_info.BindString(i, old_info.ColumnString(i)); | |
1064 break; | |
1065 case sql::COLUMN_TYPE_BLOB: | |
1066 new_info.BindBlob(i, | |
1067 old_info.ColumnBlob(i), | |
1068 old_info.ColumnByteLength(i)); | |
1069 break; | |
1070 case sql::COLUMN_TYPE_NULL: | |
1071 new_info.BindNull(i); | |
1072 } | |
1073 } | |
1074 | |
1075 if(!new_info.Run()) | |
1076 return false; | |
1077 new_info.Reset(true); | |
1078 } | |
1079 SetVersion(81); | |
1080 return true; | |
1081 } | |
1082 | |
985 bool DirectoryBackingStore::CreateTables() { | 1083 bool DirectoryBackingStore::CreateTables() { |
986 DVLOG(1) << "First run, creating tables"; | 1084 DVLOG(1) << "First run, creating tables"; |
987 // Create two little tables share_version and share_info | 1085 // Create two little tables share_version and share_info |
988 if (!db_->Execute( | 1086 if (!db_->Execute( |
989 "CREATE TABLE share_version (" | 1087 "CREATE TABLE share_version (" |
990 "id VARCHAR(128) primary key, data INT)")) { | 1088 "id VARCHAR(128) primary key, data INT)")) { |
991 return false; | 1089 return false; |
992 } | 1090 } |
993 | 1091 |
994 { | 1092 { |
(...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); | 1251 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); | 1252 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); | 1253 bool next_exists = (ids_set.find(entry->ref(NEXT_ID).value()) != end); |
1156 is_ok = is_ok && prev_exists && parent_exists && next_exists; | 1254 is_ok = is_ok && prev_exists && parent_exists && next_exists; |
1157 } | 1255 } |
1158 return is_ok; | 1256 return is_ok; |
1159 } | 1257 } |
1160 | 1258 |
1161 } // namespace syncable | 1259 } // namespace syncable |
1162 } // namespace syncer | 1260 } // namespace syncer |
OLD | NEW |