| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/metrics/field_trial.h" |
| 13 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
| 17 #include "sql/connection.h" | 18 #include "sql/connection.h" |
| 18 #include "sql/statement.h" | 19 #include "sql/statement.h" |
| 19 #include "sql/transaction.h" | 20 #include "sql/transaction.h" |
| 20 #include "sync/internal_api/public/base/node_ordinal.h" | 21 #include "sync/internal_api/public/base/node_ordinal.h" |
| 21 #include "sync/protocol/bookmark_specifics.pb.h" | 22 #include "sync/protocol/bookmark_specifics.pb.h" |
| 22 #include "sync/protocol/sync.pb.h" | 23 #include "sync/protocol/sync.pb.h" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 } // namespace | 169 } // namespace |
| 169 | 170 |
| 170 /////////////////////////////////////////////////////////////////////////////// | 171 /////////////////////////////////////////////////////////////////////////////// |
| 171 // DirectoryBackingStore implementation. | 172 // DirectoryBackingStore implementation. |
| 172 | 173 |
| 173 DirectoryBackingStore::DirectoryBackingStore(const string& dir_name) | 174 DirectoryBackingStore::DirectoryBackingStore(const string& dir_name) |
| 174 : db_(new sql::Connection()), | 175 : db_(new sql::Connection()), |
| 175 dir_name_(dir_name), | 176 dir_name_(dir_name), |
| 176 needs_column_refresh_(false) { | 177 needs_column_refresh_(false) { |
| 177 db_->set_histogram_tag("SyncDirectory"); | 178 db_->set_histogram_tag("SyncDirectory"); |
| 178 db_->set_page_size(4096); | |
| 179 db_->set_cache_size(32); | 179 db_->set_cache_size(32); |
| 180 if (IsSyncBackingDatabase32KEnabled()) { |
| 181 db_->set_page_size(32768); |
| 182 } else { |
| 183 db_->set_page_size(4096); |
| 184 } |
| 180 } | 185 } |
| 181 | 186 |
| 182 DirectoryBackingStore::DirectoryBackingStore(const string& dir_name, | 187 DirectoryBackingStore::DirectoryBackingStore(const string& dir_name, |
| 183 sql::Connection* db) | 188 sql::Connection* db) |
| 184 : db_(db), | 189 : db_(db), |
| 185 dir_name_(dir_name), | 190 dir_name_(dir_name), |
| 186 needs_column_refresh_(false) { | 191 needs_column_refresh_(false) { |
| 187 } | 192 } |
| 188 | 193 |
| 189 DirectoryBackingStore::~DirectoryBackingStore() { | 194 DirectoryBackingStore::~DirectoryBackingStore() { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 return false; | 306 return false; |
| 302 DCHECK_EQ(db_->GetLastChangeCount(), 1); | 307 DCHECK_EQ(db_->GetLastChangeCount(), 1); |
| 303 s2.Reset(true); | 308 s2.Reset(true); |
| 304 } | 309 } |
| 305 } | 310 } |
| 306 | 311 |
| 307 return transaction.Commit(); | 312 return transaction.Commit(); |
| 308 } | 313 } |
| 309 | 314 |
| 310 bool DirectoryBackingStore::InitializeTables() { | 315 bool DirectoryBackingStore::InitializeTables() { |
| 316 int page_size = 0; |
| 317 if (IsSyncBackingDatabase32KEnabled() && |
| 318 GetDatabasePageSize(&page_size) && |
| 319 page_size == 4096) { |
| 320 IncreasePageSizeTo32K(); |
| 321 } |
| 311 sql::Transaction transaction(db_.get()); | 322 sql::Transaction transaction(db_.get()); |
| 312 if (!transaction.Begin()) | 323 if (!transaction.Begin()) |
| 313 return false; | 324 return false; |
| 314 | 325 |
| 315 int version_on_disk = GetVersion(); | 326 int version_on_disk = GetVersion(); |
| 316 | 327 |
| 317 // Upgrade from version 67. Version 67 was widely distributed as the original | 328 // Upgrade from version 67. Version 67 was widely distributed as the original |
| 318 // Bookmark Sync release. Version 68 removed unique naming. | 329 // Bookmark Sync release. Version 68 removed unique naming. |
| 319 if (version_on_disk == 67) { | 330 if (version_on_disk == 67) { |
| 320 if (MigrateVersion67To68()) | 331 if (MigrateVersion67To68()) |
| (...skipping 1265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1586 query.append(ColumnName(i)); | 1597 query.append(ColumnName(i)); |
| 1587 values.append("?"); | 1598 values.append("?"); |
| 1588 } | 1599 } |
| 1589 query.append(" ) "); | 1600 query.append(" ) "); |
| 1590 values.append(" )"); | 1601 values.append(" )"); |
| 1591 query.append(values); | 1602 query.append(values); |
| 1592 save_statement->Assign(db_->GetUniqueStatement( | 1603 save_statement->Assign(db_->GetUniqueStatement( |
| 1593 base::StringPrintf(query.c_str(), "metas").c_str())); | 1604 base::StringPrintf(query.c_str(), "metas").c_str())); |
| 1594 } | 1605 } |
| 1595 | 1606 |
| 1607 // Get page size for the database. |
| 1608 bool DirectoryBackingStore::GetDatabasePageSize(int* page_size) { |
| 1609 sql::Statement s(db_->GetUniqueStatement("PRAGMA page_size")); |
| 1610 if (!s.Step()) |
| 1611 return false; |
| 1612 *page_size = s.ColumnInt(0); |
| 1613 return true; |
| 1614 } |
| 1615 |
| 1616 bool DirectoryBackingStore::IsSyncBackingDatabase32KEnabled() { |
| 1617 const std::string group_name = |
| 1618 base::FieldTrialList::FindFullName("SyncBackingDatabase32K"); |
| 1619 return group_name == "Enabled"; |
| 1620 } |
| 1621 |
| 1622 bool DirectoryBackingStore::IncreasePageSizeTo32K() { |
| 1623 if (!db_->Execute("PRAGMA page_size=32768;") || |
| 1624 !Vacuum()) { |
| 1625 return false; |
| 1626 } |
| 1627 return true; |
| 1628 } |
| 1629 |
| 1630 bool DirectoryBackingStore::Vacuum() { |
| 1631 DCHECK_EQ(db_->transaction_nesting(), 0); |
| 1632 if (!db_->Execute("VACUUM;")) { |
| 1633 return false; |
| 1634 } |
| 1635 return true; |
| 1636 } |
| 1637 |
| 1596 } // namespace syncable | 1638 } // namespace syncable |
| 1597 } // namespace syncer | 1639 } // namespace syncer |
| OLD | NEW |