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

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

Powered by Google App Engine
This is Rietveld 408576698