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 "components/history/core/browser/history_database.h" | 5 #include "components/history/core/browser/history_database.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <utility> | 12 #include <utility> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/containers/hash_tables.h" | 16 #include "base/containers/hash_tables.h" |
17 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
18 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
19 #include "base/numerics/safe_conversions.h" | 19 #include "base/numerics/safe_conversions.h" |
20 #include "base/rand_util.h" | 20 #include "base/rand_util.h" |
21 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
24 #include "components/history/core/browser/url_utils.h" | 24 #include "components/history/core/browser/url_utils.h" |
| 25 #include "sql/meta_table.h" |
25 #include "sql/statement.h" | 26 #include "sql/statement.h" |
26 #include "sql/transaction.h" | 27 #include "sql/transaction.h" |
27 | 28 |
28 #if defined(OS_MACOSX) && !defined(OS_IOS) | 29 #if defined(OS_MACOSX) && !defined(OS_IOS) |
29 #include "base/mac/mac_util.h" | 30 #include "base/mac/mac_util.h" |
30 #endif | 31 #endif |
31 | 32 |
32 namespace history { | 33 namespace history { |
33 | 34 |
34 namespace { | 35 namespace { |
35 | 36 |
36 // Current version number. We write databases at the "current" version number, | 37 // Current version number. We write databases at the "current" version number, |
37 // but any previous version that can read the "compatible" one can make do with | 38 // but any previous version that can read the "compatible" one can make do with |
38 // our database without *too* many bad effects. | 39 // our database without *too* many bad effects. |
39 const int kCurrentVersionNumber = 34; | 40 const int kCurrentVersionNumber = 35; |
40 const int kCompatibleVersionNumber = 16; | 41 const int kCompatibleVersionNumber = 16; |
41 const char kEarlyExpirationThresholdKey[] = "early_expiration_threshold"; | 42 const char kEarlyExpirationThresholdKey[] = "early_expiration_threshold"; |
42 const int kMaxHostsInMemory = 10000; | 43 const int kMaxHostsInMemory = 10000; |
43 | 44 |
44 } // namespace | 45 } // namespace |
45 | 46 |
46 HistoryDatabase::HistoryDatabase( | 47 HistoryDatabase::HistoryDatabase( |
47 DownloadInterruptReason download_interrupt_reason_none, | 48 DownloadInterruptReason download_interrupt_reason_none, |
48 DownloadInterruptReason download_interrupt_reason_crash) | 49 DownloadInterruptReason download_interrupt_reason_crash) |
49 : DownloadDatabase(download_interrupt_reason_none, | 50 : DownloadDatabase(download_interrupt_reason_none, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 // Prime the cache. | 90 // Prime the cache. |
90 db_.Preload(); | 91 db_.Preload(); |
91 | 92 |
92 // Create the tables and indices. | 93 // Create the tables and indices. |
93 // NOTE: If you add something here, also add it to | 94 // NOTE: If you add something here, also add it to |
94 // RecreateAllButStarAndURLTables. | 95 // RecreateAllButStarAndURLTables. |
95 if (!meta_table_.Init(&db_, GetCurrentVersion(), kCompatibleVersionNumber)) | 96 if (!meta_table_.Init(&db_, GetCurrentVersion(), kCompatibleVersionNumber)) |
96 return sql::INIT_FAILURE; | 97 return sql::INIT_FAILURE; |
97 if (!CreateURLTable(false) || !InitVisitTable() || | 98 if (!CreateURLTable(false) || !InitVisitTable() || |
98 !InitKeywordSearchTermsTable() || !InitDownloadTable() || | 99 !InitKeywordSearchTermsTable() || !InitDownloadTable() || |
99 !InitSegmentTables()) | 100 !InitSegmentTables() || !InitSyncTable()) |
100 return sql::INIT_FAILURE; | 101 return sql::INIT_FAILURE; |
101 CreateMainURLIndex(); | 102 CreateMainURLIndex(); |
102 CreateKeywordSearchTermsIndices(); | 103 CreateKeywordSearchTermsIndices(); |
103 | 104 |
104 // TODO(benjhayden) Remove at some point. | 105 // TODO(benjhayden) Remove at some point. |
105 meta_table_.DeleteKey("next_download_id"); | 106 meta_table_.DeleteKey("next_download_id"); |
106 | 107 |
107 // Version check. | 108 // Version check. |
108 sql::InitStatus version_status = EnsureCurrentVersion(); | 109 sql::InitStatus version_status = EnsureCurrentVersion(); |
109 if (version_status != sql::INIT_OK) | 110 if (version_status != sql::INIT_OK) |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 void HistoryDatabase::UpdateEarlyExpirationThreshold(base::Time threshold) { | 358 void HistoryDatabase::UpdateEarlyExpirationThreshold(base::Time threshold) { |
358 meta_table_.SetValue(kEarlyExpirationThresholdKey, | 359 meta_table_.SetValue(kEarlyExpirationThresholdKey, |
359 threshold.ToInternalValue()); | 360 threshold.ToInternalValue()); |
360 cached_early_expiration_threshold_ = threshold; | 361 cached_early_expiration_threshold_ = threshold; |
361 } | 362 } |
362 | 363 |
363 sql::Connection& HistoryDatabase::GetDB() { | 364 sql::Connection& HistoryDatabase::GetDB() { |
364 return db_; | 365 return db_; |
365 } | 366 } |
366 | 367 |
| 368 sql::MetaTable& HistoryDatabase::GetMetaTable() { |
| 369 return meta_table_; |
| 370 } |
| 371 |
367 // Migration ------------------------------------------------------------------- | 372 // Migration ------------------------------------------------------------------- |
368 | 373 |
369 sql::InitStatus HistoryDatabase::EnsureCurrentVersion() { | 374 sql::InitStatus HistoryDatabase::EnsureCurrentVersion() { |
370 // We can't read databases newer than we were designed for. | 375 // We can't read databases newer than we were designed for. |
371 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { | 376 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { |
372 LOG(WARNING) << "History database is too new."; | 377 LOG(WARNING) << "History database is too new."; |
373 return sql::INIT_TOO_NEW; | 378 return sql::INIT_TOO_NEW; |
374 } | 379 } |
375 | 380 |
376 int cur_version = meta_table_.GetVersionNumber(); | 381 int cur_version = meta_table_.GetVersionNumber(); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 | 549 |
545 if (cur_version == 33) { | 550 if (cur_version == 33) { |
546 if (!MigrateDownloadLastAccessTime()) { | 551 if (!MigrateDownloadLastAccessTime()) { |
547 LOG(WARNING) << "Unable to migrate to version 34"; | 552 LOG(WARNING) << "Unable to migrate to version 34"; |
548 return sql::INIT_FAILURE; | 553 return sql::INIT_FAILURE; |
549 } | 554 } |
550 cur_version++; | 555 cur_version++; |
551 meta_table_.SetVersionNumber(cur_version); | 556 meta_table_.SetVersionNumber(cur_version); |
552 } | 557 } |
553 | 558 |
| 559 if (cur_version == 34) { |
| 560 // AUTOINCREMENT is added to urls table PRIMARY KEY(id), need to recreate a |
| 561 // new table and copy all contents over. favicon_id is removed from urls |
| 562 // table since we never use it. Also typed_url_sync_metadata and |
| 563 // autofill_model_type_state tables are introduced, no migration needed for |
| 564 // those two tables. |
| 565 if (!RecreateURLTableWithAllContents()) { |
| 566 LOG(WARNING) << "Unable to update history database to version 35."; |
| 567 return sql::INIT_FAILURE; |
| 568 } |
| 569 cur_version++; |
| 570 meta_table_.SetVersionNumber(cur_version); |
| 571 } |
| 572 |
554 // When the version is too old, we just try to continue anyway, there should | 573 // When the version is too old, we just try to continue anyway, there should |
555 // not be a released product that makes a database too old for us to handle. | 574 // not be a released product that makes a database too old for us to handle. |
556 LOG_IF(WARNING, cur_version < GetCurrentVersion()) << | 575 LOG_IF(WARNING, cur_version < GetCurrentVersion()) << |
557 "History database version " << cur_version << " is too old to handle."; | 576 "History database version " << cur_version << " is too old to handle."; |
558 | 577 |
559 return sql::INIT_OK; | 578 return sql::INIT_OK; |
560 } | 579 } |
561 | 580 |
562 #if !defined(OS_WIN) | 581 #if !defined(OS_WIN) |
563 void HistoryDatabase::MigrateTimeEpoch() { | 582 void HistoryDatabase::MigrateTimeEpoch() { |
564 // Update all the times in the URLs and visits table in the main database. | 583 // Update all the times in the URLs and visits table in the main database. |
565 ignore_result(db_.Execute( | 584 ignore_result(db_.Execute( |
566 "UPDATE urls " | 585 "UPDATE urls " |
567 "SET last_visit_time = last_visit_time + 11644473600000000 " | 586 "SET last_visit_time = last_visit_time + 11644473600000000 " |
568 "WHERE id IN (SELECT id FROM urls WHERE last_visit_time > 0);")); | 587 "WHERE id IN (SELECT id FROM urls WHERE last_visit_time > 0);")); |
569 ignore_result(db_.Execute( | 588 ignore_result(db_.Execute( |
570 "UPDATE visits " | 589 "UPDATE visits " |
571 "SET visit_time = visit_time + 11644473600000000 " | 590 "SET visit_time = visit_time + 11644473600000000 " |
572 "WHERE id IN (SELECT id FROM visits WHERE visit_time > 0);")); | 591 "WHERE id IN (SELECT id FROM visits WHERE visit_time > 0);")); |
573 ignore_result(db_.Execute( | 592 ignore_result(db_.Execute( |
574 "UPDATE segment_usage " | 593 "UPDATE segment_usage " |
575 "SET time_slot = time_slot + 11644473600000000 " | 594 "SET time_slot = time_slot + 11644473600000000 " |
576 "WHERE id IN (SELECT id FROM segment_usage WHERE time_slot > 0);")); | 595 "WHERE id IN (SELECT id FROM segment_usage WHERE time_slot > 0);")); |
577 } | 596 } |
578 #endif | 597 #endif |
579 | 598 |
580 } // namespace history | 599 } // namespace history |
OLD | NEW |