| 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 "chrome/browser/history/history_database.h" | 5 #include "chrome/browser/history/history_database.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" | 16 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" |
| 17 #include "chrome/browser/history/starred_url_database.h" | |
| 18 #include "sql/transaction.h" | 17 #include "sql/transaction.h" |
| 19 | 18 |
| 20 #if defined(OS_MACOSX) | 19 #if defined(OS_MACOSX) |
| 21 #include "base/mac/mac_util.h" | 20 #include "base/mac/mac_util.h" |
| 22 #endif | 21 #endif |
| 23 | 22 |
| 24 namespace history { | 23 namespace history { |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 | 60 |
| 62 } // namespace | 61 } // namespace |
| 63 | 62 |
| 64 HistoryDatabase::HistoryDatabase() | 63 HistoryDatabase::HistoryDatabase() |
| 65 : needs_version_17_migration_(false) { | 64 : needs_version_17_migration_(false) { |
| 66 } | 65 } |
| 67 | 66 |
| 68 HistoryDatabase::~HistoryDatabase() { | 67 HistoryDatabase::~HistoryDatabase() { |
| 69 } | 68 } |
| 70 | 69 |
| 71 sql::InitStatus HistoryDatabase::Init(const FilePath& history_name, | 70 sql::InitStatus HistoryDatabase::Init(const FilePath& history_name) { |
| 72 const FilePath& bookmarks_path) { | |
| 73 // Set the exceptional sqlite error handler. | 71 // Set the exceptional sqlite error handler. |
| 74 db_.set_error_delegate(GetErrorHandlerForHistoryDb()); | 72 db_.set_error_delegate(GetErrorHandlerForHistoryDb()); |
| 75 | 73 |
| 76 // Set the database page size to something a little larger to give us | 74 // Set the database page size to something a little larger to give us |
| 77 // better performance (we're typically seek rather than bandwidth limited). | 75 // better performance (we're typically seek rather than bandwidth limited). |
| 78 // This only has an effect before any tables have been created, otherwise | 76 // This only has an effect before any tables have been created, otherwise |
| 79 // this is a NOP. Must be a power of 2 and a max of 8192. | 77 // this is a NOP. Must be a power of 2 and a max of 8192. |
| 80 db_.set_page_size(4096); | 78 db_.set_page_size(4096); |
| 81 | 79 |
| 82 // Increase the cache size. The page size, plus a little extra, times this | 80 // Increase the cache size. The page size, plus a little extra, times this |
| (...skipping 29 matching lines...) Expand all Loading... |
| 112 if (!meta_table_.Init(&db_, GetCurrentVersion(), kCompatibleVersionNumber)) | 110 if (!meta_table_.Init(&db_, GetCurrentVersion(), kCompatibleVersionNumber)) |
| 113 return sql::INIT_FAILURE; | 111 return sql::INIT_FAILURE; |
| 114 if (!CreateURLTable(false) || !InitVisitTable() || | 112 if (!CreateURLTable(false) || !InitVisitTable() || |
| 115 !InitKeywordSearchTermsTable() || !InitDownloadTable() || | 113 !InitKeywordSearchTermsTable() || !InitDownloadTable() || |
| 116 !InitSegmentTables()) | 114 !InitSegmentTables()) |
| 117 return sql::INIT_FAILURE; | 115 return sql::INIT_FAILURE; |
| 118 CreateMainURLIndex(); | 116 CreateMainURLIndex(); |
| 119 CreateKeywordSearchTermsIndices(); | 117 CreateKeywordSearchTermsIndices(); |
| 120 | 118 |
| 121 // Version check. | 119 // Version check. |
| 122 sql::InitStatus version_status = EnsureCurrentVersion(bookmarks_path); | 120 sql::InitStatus version_status = EnsureCurrentVersion(); |
| 123 if (version_status != sql::INIT_OK) | 121 if (version_status != sql::INIT_OK) |
| 124 return version_status; | 122 return version_status; |
| 125 | 123 |
| 126 ComputeDatabaseMetrics(history_name, db_); | 124 ComputeDatabaseMetrics(history_name, db_); |
| 127 return committer.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; | 125 return committer.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; |
| 128 } | 126 } |
| 129 | 127 |
| 130 void HistoryDatabase::BeginExclusiveMode() { | 128 void HistoryDatabase::BeginExclusiveMode() { |
| 131 // We can't use set_exclusive_locking() since that only has an effect before | 129 // We can't use set_exclusive_locking() since that only has an effect before |
| 132 // the DB is opened. | 130 // the DB is opened. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 sql::Connection& HistoryDatabase::GetDB() { | 235 sql::Connection& HistoryDatabase::GetDB() { |
| 238 return db_; | 236 return db_; |
| 239 } | 237 } |
| 240 | 238 |
| 241 sql::MetaTable& HistoryDatabase::GetMetaTable() { | 239 sql::MetaTable& HistoryDatabase::GetMetaTable() { |
| 242 return meta_table_; | 240 return meta_table_; |
| 243 } | 241 } |
| 244 | 242 |
| 245 // Migration ------------------------------------------------------------------- | 243 // Migration ------------------------------------------------------------------- |
| 246 | 244 |
| 247 sql::InitStatus HistoryDatabase::EnsureCurrentVersion( | 245 sql::InitStatus HistoryDatabase::EnsureCurrentVersion() { |
| 248 const FilePath& tmp_bookmarks_path) { | |
| 249 // We can't read databases newer than we were designed for. | 246 // We can't read databases newer than we were designed for. |
| 250 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { | 247 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { |
| 251 LOG(WARNING) << "History database is too new."; | 248 LOG(WARNING) << "History database is too new."; |
| 252 return sql::INIT_TOO_NEW; | 249 return sql::INIT_TOO_NEW; |
| 253 } | 250 } |
| 254 | 251 |
| 255 // NOTICE: If you are changing structures for things shared with the archived | 252 // NOTICE: If you are changing structures for things shared with the archived |
| 256 // history file like URLs, visits, or downloads, that will need migration as | 253 // history file like URLs, visits, or downloads, that will need migration as |
| 257 // well. Instead of putting such migration code in this class, it should be | 254 // well. Instead of putting such migration code in this class, it should be |
| 258 // in the corresponding file (url_database.cc, etc.) and called from here and | 255 // in the corresponding file (url_database.cc, etc.) and called from here and |
| 259 // from the archived_database.cc. | 256 // from the archived_database.cc. |
| 260 | 257 |
| 261 int cur_version = meta_table_.GetVersionNumber(); | 258 int cur_version = meta_table_.GetVersionNumber(); |
| 262 | 259 |
| 263 // Put migration code here | 260 // Put migration code here |
| 264 | 261 |
| 265 if (cur_version == 15) { | 262 if (cur_version == 15) { |
| 266 StarredURLDatabase starred_url_database(&db_); | 263 if (!db_.Execute("DROP TABLE starred") || !DropStarredIDFromURLs()) { |
| 267 if (!starred_url_database.MigrateBookmarksToFile(tmp_bookmarks_path) || | |
| 268 !DropStarredIDFromURLs()) { | |
| 269 LOG(WARNING) << "Unable to update history database to version 16."; | 264 LOG(WARNING) << "Unable to update history database to version 16."; |
| 270 return sql::INIT_FAILURE; | 265 return sql::INIT_FAILURE; |
| 271 } | 266 } |
| 272 ++cur_version; | 267 ++cur_version; |
| 273 meta_table_.SetVersionNumber(cur_version); | 268 meta_table_.SetVersionNumber(cur_version); |
| 274 meta_table_.SetCompatibleVersionNumber( | 269 meta_table_.SetCompatibleVersionNumber( |
| 275 std::min(cur_version, kCompatibleVersionNumber)); | 270 std::min(cur_version, kCompatibleVersionNumber)); |
| 276 } | 271 } |
| 277 | 272 |
| 278 if (cur_version == 16) { | 273 if (cur_version == 16) { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 "WHERE id IN (SELECT id FROM segment_usage WHERE time_slot > 0);")); | 365 "WHERE id IN (SELECT id FROM segment_usage WHERE time_slot > 0);")); |
| 371 | 366 |
| 372 // Erase all the full text index files. These will take a while to update and | 367 // Erase all the full text index files. These will take a while to update and |
| 373 // are less important, so we just blow them away. Same with the archived | 368 // are less important, so we just blow them away. Same with the archived |
| 374 // database. | 369 // database. |
| 375 needs_version_17_migration_ = true; | 370 needs_version_17_migration_ = true; |
| 376 } | 371 } |
| 377 #endif | 372 #endif |
| 378 | 373 |
| 379 } // namespace history | 374 } // namespace history |
| OLD | NEW |