Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(552)

Side by Side Diff: sync/syncable/directory_backing_store.cc

Issue 10989063: Changed DB to store node positions as Ordinals. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Minor changes and corrections Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
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
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 } else
rlarocque 2012/10/02 01:39:50 Opening brace should be on the same line.
61 {
62 statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i)));
63 }
52 } 64 }
53 for ( ; i < TIME_FIELDS_END; ++i) { 65 for ( ; i < TIME_FIELDS_END; ++i) {
54 statement->BindInt64(index++, 66 statement->BindInt64(index++,
55 TimeToProtoTime( 67 TimeToProtoTime(
56 entry.ref(static_cast<TimeField>(i)))); 68 entry.ref(static_cast<TimeField>(i))));
57 } 69 }
58 for ( ; i < ID_FIELDS_END; ++i) { 70 for ( ; i < ID_FIELDS_END; ++i) {
59 statement->BindString(index++, entry.ref(static_cast<IdField>(i)).s_); 71 statement->BindString(index++, entry.ref(static_cast<IdField>(i)).s_);
60 } 72 }
61 for ( ; i < BIT_FIELDS_END; ++i) { 73 for ( ; i < BIT_FIELDS_END; ++i) {
62 statement->BindInt(index++, entry.ref(static_cast<BitField>(i))); 74 statement->BindInt(index++, entry.ref(static_cast<BitField>(i)));
63 } 75 }
64 for ( ; i < STRING_FIELDS_END; ++i) { 76 for ( ; i < STRING_FIELDS_END; ++i) {
65 statement->BindString(index++, entry.ref(static_cast<StringField>(i))); 77 statement->BindString(index++, entry.ref(static_cast<StringField>(i)));
66 } 78 }
67 std::string temp; 79 std::string temp;
68 for ( ; i < PROTO_FIELDS_END; ++i) { 80 for ( ; i < PROTO_FIELDS_END; ++i) {
69 entry.ref(static_cast<ProtoField>(i)).SerializeToString(&temp); 81 entry.ref(static_cast<ProtoField>(i)).SerializeToString(&temp);
70 statement->BindBlob(index++, temp.data(), temp.length()); 82 statement->BindBlob(index++, temp.data(), temp.length());
71 } 83 }
72 } 84 }
73 85
74 // The caller owns the returned EntryKernel*. Assumes the statement currently 86 // The caller owns the returned EntryKernel*. Assumes the statement currently
75 // points to a valid row in the metas table. 87 // points to a valid row in the metas table.
76 EntryKernel* UnpackEntry(sql::Statement* statement) { 88 EntryKernel* UnpackEntry(sql::Statement* statement) {
77 EntryKernel* kernel = new EntryKernel(); 89 EntryKernel* kernel = new EntryKernel();
78 DCHECK_EQ(statement->ColumnCount(), static_cast<int>(FIELD_COUNT)); 90 DCHECK_EQ(statement->ColumnCount(), static_cast<int>(FIELD_COUNT));
79 int i = 0; 91 int i = 0;
80 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { 92 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) {
81 kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i)); 93 // Store SERVER_POSITION_IN_PARENT as a string for eventual shift
94 // to using ordinals instead of int64s.
95 // Columns with a null value for the ordinal (like the root node)
rlarocque 2012/10/02 01:39:50 If you're going to have to special case it here, t
96 // are left as is.
97 if(i == SERVER_POSITION_IN_PARENT &&
98 statement->ColumnType(i) != sql::COLUMN_TYPE_NULL) {
99 std::string ordinal_blob;
100 DCHECK(statement->ColumnBlobAsString(i, &ordinal_blob));
101 int64 server_position = NodeOrdinalToInt64(
102 NodeOrdinal(ordinal_blob));
103 kernel->put(
104 static_cast<Int64Field>(SERVER_POSITION_IN_PARENT),
105 server_position);
106 } else
rlarocque 2012/10/02 01:39:50 Opening brace should be on the same line.
107 {
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
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
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 } //namespace.
1057
1058 bool DirectoryBackingStore::MigrateVersion80To81() {
1059 // Version 81 changes server_position_in_parent from a potentially lossy
1060 // int64 to an unbounded fidelity ordinal (stored as a blob).
1061 // We make the current metas table temporary, create a new table where
1062 // server_position_in_parent is of the right type, and transfer values
1063 // after converting from int64's to Ordinals.
1064 SafeDropTable("temp_metas");
1065 if(!db_->Execute("ALTER TABLE metas RENAME TO temp_metas")) {
1066 return false;
1067 }
1068 if(!CreateMetasTable(false))
1069 return false;
1070
1071 std::string old_info_string = "SELECT ";
1072 Version80ColumnList(old_info_string);
1073 old_info_string.append(" FROM temp_metas");
1074
1075 std::string new_info_string = "INSERT INTO metas (";
1076 Version80ColumnList(new_info_string);
1077 new_info_string.append(") VALUES (");
1078 for(int i = 0; i < V80_COLUMN_COUNT - 1; ++i) {
1079 new_info_string.append("?, ");
1080 }
1081 new_info_string.append("?)");
1082
1083 sql::Statement old_info(db_->GetUniqueStatement(
1084 old_info_string.c_str()));
1085 sql::Statement new_info(db_->GetUniqueStatement(
1086 new_info_string.c_str()));
1087
1088 while(old_info.Step()) {
1089 int64 int_ordinal = old_info.ColumnInt(3);
1090 std::string new_ordinal =
1091 Int64ToNodeOrdinal(int_ordinal).ToInternalValue();
1092 //Loop and bind each new column based on the old column type.
1093 for(int i = 0; i < V80_COLUMN_COUNT; ++i) {
1094 if (i == V80_ORDINAL_FIELD) {
1095 new_info.BindBlob(i, new_ordinal.data(), new_ordinal.length());
1096 continue;
1097 }
1098 switch(old_info.ColumnType(i)) {
1099 case sql::COLUMN_TYPE_INTEGER:
1100 new_info.BindInt64(i, old_info.ColumnInt64(i));
1101 break;
1102 case sql::COLUMN_TYPE_FLOAT:
1103 new_info.BindDouble(i, old_info.ColumnDouble(i));
1104 break;
1105 case sql::COLUMN_TYPE_TEXT:
1106 new_info.BindString(i, old_info.ColumnString(i));
1107 break;
1108 case sql::COLUMN_TYPE_BLOB:
1109 new_info.BindBlob(i,
1110 old_info.ColumnBlob(i),
1111 old_info.ColumnByteLength(i));
1112 break;
1113 case sql::COLUMN_TYPE_NULL:
1114 new_info.BindNull(i);
1115 }
1116 }
1117
1118 if(!new_info.Run())
1119 return false;
1120 new_info.Reset(true);
1121 }
1122
1123 SetVersion(81);
1124 return true;
1125 }
1126
985 bool DirectoryBackingStore::CreateTables() { 1127 bool DirectoryBackingStore::CreateTables() {
986 DVLOG(1) << "First run, creating tables"; 1128 DVLOG(1) << "First run, creating tables";
987 // Create two little tables share_version and share_info 1129 // Create two little tables share_version and share_info
988 if (!db_->Execute( 1130 if (!db_->Execute(
989 "CREATE TABLE share_version (" 1131 "CREATE TABLE share_version ("
990 "id VARCHAR(128) primary key, data INT)")) { 1132 "id VARCHAR(128) primary key, data INT)")) {
991 return false; 1133 return false;
992 } 1134 }
993 1135
994 { 1136 {
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
1153 bool prev_exists = (ids_set.find(entry->ref(PREV_ID).value()) != end); 1295 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); 1296 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); 1297 bool next_exists = (ids_set.find(entry->ref(NEXT_ID).value()) != end);
1156 is_ok = is_ok && prev_exists && parent_exists && next_exists; 1298 is_ok = is_ok && prev_exists && parent_exists && next_exists;
1157 } 1299 }
1158 return is_ok; 1300 return is_ok;
1159 } 1301 }
1160 1302
1161 } // namespace syncable 1303 } // namespace syncable
1162 } // namespace syncer 1304 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698