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 string for eventual shift |
| 54 // to using ordinals instead of int64's |
| 55 if(i == SERVER_POSITION_IN_PARENT) { |
| 56 NodeOrdinal ordinal = Int64ToNodeOrdinal( |
| 57 entry.ref(SERVER_POSITION_IN_PARENT)); |
| 58 statement->BindString(index++, ordinal.ToInternalValue()); |
| 59 continue; |
| 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 int64 server_position = NodeOrdinalToInt64( |
| 95 NodeOrdinal(statement->ColumnString(i))); |
| 96 kernel->put(static_cast<Int64Field>(i), server_position); |
| 97 continue; |
| 98 } |
81 kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i)); | 99 kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i)); |
82 } | 100 } |
83 for ( ; i < TIME_FIELDS_END; ++i) { | 101 for ( ; i < TIME_FIELDS_END; ++i) { |
84 kernel->put(static_cast<TimeField>(i), | 102 kernel->put(static_cast<TimeField>(i), |
85 ProtoTimeToTime(statement->ColumnInt64(i))); | 103 ProtoTimeToTime(statement->ColumnInt64(i))); |
86 } | 104 } |
87 for ( ; i < ID_FIELDS_END; ++i) { | 105 for ( ; i < ID_FIELDS_END; ++i) { |
88 kernel->mutable_ref(static_cast<IdField>(i)).s_ = | 106 kernel->mutable_ref(static_cast<IdField>(i)).s_ = |
89 statement->ColumnString(i); | 107 statement->ColumnString(i); |
90 } | 108 } |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 if (MigrateVersion78To79()) | 334 if (MigrateVersion78To79()) |
317 version_on_disk = 79; | 335 version_on_disk = 79; |
318 } | 336 } |
319 | 337 |
320 // Version 80 migration is adding the bag_of_chips column. | 338 // Version 80 migration is adding the bag_of_chips column. |
321 if (version_on_disk == 79) { | 339 if (version_on_disk == 79) { |
322 if (MigrateVersion79To80()) | 340 if (MigrateVersion79To80()) |
323 version_on_disk = 80; | 341 version_on_disk = 80; |
324 } | 342 } |
325 | 343 |
| 344 // Version 81 changes the server_position_in_parent_field from an int64 |
| 345 // to a string. |
| 346 if (version_on_disk == 80) { |
| 347 if (MigrateVersion80To81()) |
| 348 version_on_disk = 81; |
| 349 } |
| 350 |
326 // If one of the migrations requested it, drop columns that aren't current. | 351 // 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 | 352 // It's only safe to do this after migrating all the way to the current |
328 // version. | 353 // version. |
329 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { | 354 if (version_on_disk == kCurrentDBVersion && needs_column_refresh_) { |
330 if (!RefreshColumns()) | 355 if (!RefreshColumns()) |
331 version_on_disk = 0; | 356 version_on_disk = 0; |
332 } | 357 } |
333 | 358 |
334 // A final, alternative catch-all migration to simply re-sync everything. | 359 // A final, alternative catch-all migration to simply re-sync everything. |
335 // | 360 // |
(...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 | 986 // 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. | 987 // perform this one-time fixup on all users to help the few that are stuck. |
963 // See crbug.com/142987 for details. | 988 // See crbug.com/142987 for details. |
964 if (!db_->Execute( | 989 if (!db_->Execute( |
965 "UPDATE share_info SET next_id = next_id - 65536")) { | 990 "UPDATE share_info SET next_id = next_id - 65536")) { |
966 return false; | 991 return false; |
967 } | 992 } |
968 SetVersion(79); | 993 SetVersion(79); |
969 return true; | 994 return true; |
970 } | 995 } |
| 996 |
971 bool DirectoryBackingStore::MigrateVersion79To80() { | 997 bool DirectoryBackingStore::MigrateVersion79To80() { |
972 if (!db_->Execute( | 998 if (!db_->Execute( |
973 "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB")) | 999 "ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB")) |
974 return false; | 1000 return false; |
975 sql::Statement update(db_->GetUniqueStatement( | 1001 sql::Statement update(db_->GetUniqueStatement( |
976 "UPDATE share_info SET bag_of_chips = ?")); | 1002 "UPDATE share_info SET bag_of_chips = ?")); |
977 // An empty message is serialized to an empty string. | 1003 // An empty message is serialized to an empty string. |
978 update.BindBlob(0, NULL, 0); | 1004 update.BindBlob(0, NULL, 0); |
979 if (!update.Run()) | 1005 if (!update.Run()) |
980 return false; | 1006 return false; |
981 SetVersion(80); | 1007 SetVersion(80); |
982 return true; | 1008 return true; |
983 } | 1009 } |
984 | 1010 |
| 1011 bool DirectoryBackingStore::MigrateVersion80To81() { |
| 1012 // Version 81 changes server_position_in_parent from a potentially lossy |
| 1013 // bigint to an unbounded fidelity ordinal represented as a varchar |
| 1014 if (!db_->Execute( |
| 1015 "ALTER TABLE metas" |
| 1016 "ALTER COLUMN server_position_in_parent VARCHAR(255)")) { |
| 1017 return false; |
| 1018 } |
| 1019 SetVersion(81); |
| 1020 return true; |
| 1021 } |
| 1022 |
985 bool DirectoryBackingStore::CreateTables() { | 1023 bool DirectoryBackingStore::CreateTables() { |
986 DVLOG(1) << "First run, creating tables"; | 1024 DVLOG(1) << "First run, creating tables"; |
987 // Create two little tables share_version and share_info | 1025 // Create two little tables share_version and share_info |
988 if (!db_->Execute( | 1026 if (!db_->Execute( |
989 "CREATE TABLE share_version (" | 1027 "CREATE TABLE share_version (" |
990 "id VARCHAR(128) primary key, data INT)")) { | 1028 "id VARCHAR(128) primary key, data INT)")) { |
991 return false; | 1029 return false; |
992 } | 1030 } |
993 | 1031 |
994 { | 1032 { |
(...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); | 1191 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); | 1192 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); | 1193 bool next_exists = (ids_set.find(entry->ref(NEXT_ID).value()) != end); |
1156 is_ok = is_ok && prev_exists && parent_exists && next_exists; | 1194 is_ok = is_ok && prev_exists && parent_exists && next_exists; |
1157 } | 1195 } |
1158 return is_ok; | 1196 return is_ok; |
1159 } | 1197 } |
1160 | 1198 |
1161 } // namespace syncable | 1199 } // namespace syncable |
1162 } // namespace syncer | 1200 } // namespace syncer |
OLD | NEW |