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/ordinal.h" | |
akalin
2012/10/02 22:43:01
i think you can include just node_ordinal.h, and n
| |
26 #include "sync/internal_api/public/base/node_ordinal.h" | |
25 #include "sync/protocol/bookmark_specifics.pb.h" | 27 #include "sync/protocol/bookmark_specifics.pb.h" |
26 #include "sync/protocol/sync.pb.h" | 28 #include "sync/protocol/sync.pb.h" |
27 #include "sync/syncable/syncable-inl.h" | 29 #include "sync/syncable/syncable-inl.h" |
28 #include "sync/syncable/syncable_columns.h" | 30 #include "sync/syncable/syncable_columns.h" |
29 #include "sync/util/time.h" | 31 #include "sync/util/time.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 int64s. | |
55 // See crbug.com/145412 | |
rlarocque
2012/10/02 21:48:21
I think there should be a period here, too.
akalin
2012/10/02 22:43:01
preferably with a space before (so the link doesn'
| |
56 if (i == SERVER_POSITION_IN_PARENT) { | |
rlarocque
2012/10/02 21:48:21
What will we do when SERVER_POSITION_IN_PARENT is
akalin
2012/10/02 22:43:01
I think we should get rid of SERVER_POSITION_IN_PA
| |
57 std::string ordinal = Int64ToNodeOrdinal( | |
58 entry.ref(SERVER_POSITION_IN_PARENT)).ToInternalValue(); | |
59 statement->BindBlob(index++, ordinal.data(), ordinal.size()); | |
60 } else { | |
61 statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i))); | |
62 } | |
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 blob for eventual shift |
93 // to using ordinals instead of int64s. | |
94 if(i == SERVER_POSITION_IN_PARENT) { | |
95 int64 server_position; | |
96 // Null-valued columns get a default ordinal. | |
97 if (statement->ColumnType(i) == sql::COLUMN_TYPE_NULL) | |
98 server_position = NodeOrdinalToInt64(( | |
99 NodeOrdinal::CreateInitialOrdinal())); | |
100 else { | |
101 std::string ordinal_blob; | |
102 DCHECK(statement->ColumnBlobAsString(i, &ordinal_blob)); | |
103 server_position = NodeOrdinalToInt64(NodeOrdinal(ordinal_blob)); | |
104 kernel->put(static_cast<Int64Field>(SERVER_POSITION_IN_PARENT), | |
105 server_position); | |
106 } | |
107 } else { | |
108 kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i)); | |
109 } | |
82 } | 110 } |
83 for ( ; i < TIME_FIELDS_END; ++i) { | 111 for ( ; i < TIME_FIELDS_END; ++i) { |
84 kernel->put(static_cast<TimeField>(i), | 112 kernel->put(static_cast<TimeField>(i), |
85 ProtoTimeToTime(statement->ColumnInt64(i))); | 113 ProtoTimeToTime(statement->ColumnInt64(i))); |
86 } | 114 } |
87 for ( ; i < ID_FIELDS_END; ++i) { | 115 for ( ; i < ID_FIELDS_END; ++i) { |
88 kernel->mutable_ref(static_cast<IdField>(i)).s_ = | 116 kernel->mutable_ref(static_cast<IdField>(i)).s_ = |
89 statement->ColumnString(i); | 117 statement->ColumnString(i); |
90 } | 118 } |
91 for ( ; i < BIT_FIELDS_END; ++i) { | 119 for ( ; i < BIT_FIELDS_END; ++i) { |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
316 if (MigrateVersion78To79()) | 344 if (MigrateVersion78To79()) |
317 version_on_disk = 79; | 345 version_on_disk = 79; |
318 } | 346 } |
319 | 347 |
320 // Version 80 migration is adding the bag_of_chips column. | 348 // Version 80 migration is adding the bag_of_chips column. |
321 if (version_on_disk == 79) { | 349 if (version_on_disk == 79) { |
322 if (MigrateVersion79To80()) | 350 if (MigrateVersion79To80()) |
323 version_on_disk = 80; | 351 version_on_disk = 80; |
324 } | 352 } |
325 | 353 |
354 // Version 81 changes the server_position_in_parent_field from an int64 | |
355 // to a string. | |
356 if (version_on_disk == 80) { | |
357 if (MigrateVersion80To81()) | |
358 version_on_disk = 81; | |
359 } | |
360 | |
326 // If one of the migrations requested it, drop columns that aren't current. | 361 // 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 | 362 // It's only safe to do this after migrating all the way to the current |
328 // version. | 363 // version. |
329 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { | 364 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { |
330 if (!RefreshColumns()) | 365 if (!RefreshColumns()) |
331 version_on_disk = 0; | 366 version_on_disk = 0; |
332 } | 367 } |
333 | 368 |
334 // A final, alternative catch-all migration to simply re-sync everything. | 369 // A final, alternative catch-all migration to simply re-sync everything. |
335 // | 370 // |
(...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 | 996 // 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. | 997 // perform this one-time fixup on all users to help the few that are stuck. |
963 // See crbug.com/142987 for details. | 998 // See crbug.com/142987 for details. |
964 if (!db_->Execute( | 999 if (!db_->Execute( |
965 "UPDATE share_info SET next_id = next_id - 65536")) { | 1000 "UPDATE share_info SET next_id = next_id - 65536")) { |
966 return false; | 1001 return false; |
967 } | 1002 } |
968 SetVersion(79); | 1003 SetVersion(79); |
969 return true; | 1004 return true; |
970 } | 1005 } |
1006 | |
971 bool DirectoryBackingStore::MigrateVersion79To80() { | 1007 bool DirectoryBackingStore::MigrateVersion79To80() { |
972 if (!db_->Execute( | 1008 if (!db_->Execute( |
973 "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB")) | 1009 "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB")) |
974 return false; | 1010 return false; |
975 sql::Statement update(db_->GetUniqueStatement( | 1011 sql::Statement update(db_->GetUniqueStatement( |
976 "UPDATE share_info SET bag_of_chips = ?")); | 1012 "UPDATE share_info SET bag_of_chips = ?")); |
977 // An empty message is serialized to an empty string. | 1013 // An empty message is serialized to an empty string. |
978 update.BindBlob(0, NULL, 0); | 1014 update.BindBlob(0, NULL, 0); |
979 if (!update.Run()) | 1015 if (!update.Run()) |
980 return false; | 1016 return false; |
981 SetVersion(80); | 1017 SetVersion(80); |
982 return true; | 1018 return true; |
983 } | 1019 } |
984 | 1020 |
1021 namespace { | |
1022 // Some definitions to help with the 80->81 migration. | |
1023 void Version80ColumnList(std::string& str) { | |
1024 str.append( | |
1025 "metahandle," | |
1026 "base_version," | |
1027 "server_version," | |
1028 "server_position_in_parent," | |
1029 "local_external_id," | |
1030 "mtime," | |
1031 "server_mtime," | |
1032 "ctime," | |
1033 "server_ctime," | |
1034 "id," | |
1035 "parent_id," | |
1036 "server_parent_id," | |
1037 "prev_id," | |
1038 "next_id," | |
1039 "is_unsynced," | |
1040 "is_unapplied_update," | |
1041 "is_del," | |
1042 "is_dir," | |
1043 "server_is_dir," | |
1044 "server_is_del," | |
1045 "non_unique_name," | |
1046 "server_non_unique_name," | |
1047 "unique_server_tag," | |
1048 "unique_client_tag," | |
1049 "specifics," | |
1050 "server_specifics," | |
1051 "base_server_specifics"); | |
1052 } | |
1053 | |
1054 const int V80_COLUMN_COUNT = 27; | |
1055 const int V80_ORDINAL_FIELD = 3; | |
1056 | |
1057 void Version81ColumnList(std::string& str) { | |
1058 str.append( | |
1059 "metahandle," | |
1060 "base_version," | |
1061 "server_version," | |
1062 "server_ordinal_in_parent," | |
1063 "local_external_id," | |
1064 "mtime," | |
1065 "server_mtime," | |
1066 "ctime," | |
1067 "server_ctime," | |
1068 "id," | |
1069 "parent_id," | |
1070 "server_parent_id," | |
1071 "prev_id," | |
1072 "next_id," | |
1073 "is_unsynced," | |
1074 "is_unapplied_update," | |
1075 "is_del," | |
1076 "is_dir," | |
1077 "server_is_dir," | |
1078 "server_is_del," | |
1079 "non_unique_name," | |
1080 "server_non_unique_name," | |
1081 "unique_server_tag," | |
1082 "unique_client_tag," | |
1083 "specifics," | |
1084 "server_specifics," | |
1085 "base_server_specifics"); | |
1086 } | |
1087 | |
1088 } //namespace. | |
1089 | |
1090 bool DirectoryBackingStore::MigrateVersion80To81() { | |
akalin
2012/10/02 22:43:01
whoa, this is scary. might be a stupid question,
| |
1091 // Version 81 changes server_position_in_parent from a potentially lossy | |
1092 // int64 to an unbounded fidelity ordinal (stored as a blob). | |
1093 // We make the current metas table temporary, create a new table where | |
1094 // server_position_in_parent is of the right type, and transfer values | |
1095 // after converting from int64's to Ordinals. | |
1096 SafeDropTable("temp_metas"); | |
1097 if(!db_->Execute("ALTER TABLE metas RENAME TO temp_metas")) { | |
1098 return false; | |
1099 } | |
1100 if(!CreateMetasTable(false)) | |
1101 return false; | |
1102 | |
1103 std::string old_info_string = "SELECT "; | |
1104 Version80ColumnList(old_info_string); | |
1105 old_info_string.append(" FROM temp_metas"); | |
1106 | |
1107 std::string new_info_string = "INSERT INTO metas ("; | |
1108 Version81ColumnList(new_info_string); | |
1109 new_info_string.append(") VALUES ("); | |
1110 for(int i = 0; i < V80_COLUMN_COUNT - 1; ++i) { | |
1111 new_info_string.append("?, "); | |
1112 } | |
1113 new_info_string.append("?)"); | |
1114 | |
1115 sql::Statement old_info(db_->GetUniqueStatement( | |
1116 old_info_string.c_str())); | |
1117 sql::Statement new_info(db_->GetUniqueStatement( | |
1118 new_info_string.c_str())); | |
1119 | |
1120 while(old_info.Step()) { | |
1121 int64 int_ordinal = old_info.ColumnInt(3); | |
1122 std::string new_ordinal = | |
1123 Int64ToNodeOrdinal(int_ordinal).ToInternalValue(); | |
1124 //Loop and bind each new column based on the old column type. | |
1125 for(int i = 0; i < V80_COLUMN_COUNT; ++i) { | |
1126 if (i == V80_ORDINAL_FIELD) { | |
1127 new_info.BindBlob(i, new_ordinal.data(), new_ordinal.length()); | |
1128 continue; | |
1129 } | |
1130 switch(old_info.ColumnType(i)) { | |
1131 case sql::COLUMN_TYPE_INTEGER: | |
1132 new_info.BindInt64(i, old_info.ColumnInt64(i)); | |
1133 break; | |
1134 case sql::COLUMN_TYPE_FLOAT: | |
1135 new_info.BindDouble(i, old_info.ColumnDouble(i)); | |
1136 break; | |
1137 case sql::COLUMN_TYPE_TEXT: | |
1138 new_info.BindString(i, old_info.ColumnString(i)); | |
1139 break; | |
1140 case sql::COLUMN_TYPE_BLOB: | |
1141 new_info.BindBlob(i, | |
1142 old_info.ColumnBlob(i), | |
1143 old_info.ColumnByteLength(i)); | |
1144 break; | |
1145 case sql::COLUMN_TYPE_NULL: | |
1146 new_info.BindNull(i); | |
1147 } | |
1148 } | |
1149 | |
1150 if(!new_info.Run()) | |
1151 return false; | |
1152 new_info.Reset(true); | |
1153 } | |
1154 | |
1155 SetVersion(81); | |
1156 return true; | |
1157 } | |
1158 | |
985 bool DirectoryBackingStore::CreateTables() { | 1159 bool DirectoryBackingStore::CreateTables() { |
986 DVLOG(1) << "First run, creating tables"; | 1160 DVLOG(1) << "First run, creating tables"; |
987 // Create two little tables share_version and share_info | 1161 // Create two little tables share_version and share_info |
988 if (!db_->Execute( | 1162 if (!db_->Execute( |
989 "CREATE TABLE share_version (" | 1163 "CREATE TABLE share_version (" |
990 "id VARCHAR(128) primary key, data INT)")) { | 1164 "id VARCHAR(128) primary key, data INT)")) { |
991 return false; | 1165 return false; |
992 } | 1166 } |
993 | 1167 |
994 { | 1168 { |
(...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); | 1327 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); | 1328 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); | 1329 bool next_exists = (ids_set.find(entry->ref(NEXT_ID).value()) != end); |
1156 is_ok = is_ok && prev_exists && parent_exists && next_exists; | 1330 is_ok = is_ok && prev_exists && parent_exists && next_exists; |
1157 } | 1331 } |
1158 return is_ok; | 1332 return is_ok; |
1159 } | 1333 } |
1160 | 1334 |
1161 } // namespace syncable | 1335 } // namespace syncable |
1162 } // namespace syncer | 1336 } // namespace syncer |
OLD | NEW |