OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/url_database.h" | 5 #include "components/history/core/browser/url_database.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 sql::Statement statement(GetDB().GetCachedStatement( | 561 sql::Statement statement(GetDB().GetCachedStatement( |
562 SQL_FROM_HERE, "DELETE FROM keyword_search_terms WHERE url_id=?")); | 562 SQL_FROM_HERE, "DELETE FROM keyword_search_terms WHERE url_id=?")); |
563 statement.BindInt64(0, url_id); | 563 statement.BindInt64(0, url_id); |
564 return statement.Run(); | 564 return statement.Run(); |
565 } | 565 } |
566 | 566 |
567 bool URLDatabase::DropStarredIDFromURLs() { | 567 bool URLDatabase::DropStarredIDFromURLs() { |
568 if (!GetDB().DoesColumnExist("urls", "starred_id")) | 568 if (!GetDB().DoesColumnExist("urls", "starred_id")) |
569 return true; // urls is already updated, no need to continue. | 569 return true; // urls is already updated, no need to continue. |
570 | 570 |
571 // Create a temporary table to contain the new URLs table. | 571 return RecreateURLTableWithAllContents(); |
572 if (!CreateTemporaryURLTable()) { | |
573 NOTREACHED(); | |
574 return false; | |
575 } | |
576 | |
577 // Copy the contents. | |
578 if (!GetDB().Execute( | |
579 "INSERT INTO temp_urls (id, url, title, visit_count, typed_count, " | |
580 "last_visit_time, hidden, favicon_id) " | |
581 "SELECT id, url, title, visit_count, typed_count, last_visit_time, " | |
582 "hidden, favicon_id FROM urls")) { | |
583 NOTREACHED() << GetDB().GetErrorMessage(); | |
584 return false; | |
585 } | |
586 | |
587 // Rename/commit the tmp table. | |
588 CommitTemporaryURLTable(); | |
589 | |
590 return true; | |
591 } | 572 } |
592 | 573 |
593 bool URLDatabase::CreateURLTable(bool is_temporary) { | 574 bool URLDatabase::CreateURLTable(bool is_temporary) { |
594 const char* name = is_temporary ? "temp_urls" : "urls"; | 575 const char* name = is_temporary ? "temp_urls" : "urls"; |
595 if (GetDB().DoesTableExist(name)) | 576 if (GetDB().DoesTableExist(name)) |
596 return true; | 577 return true; |
597 | 578 |
598 // Note: revise implementation for InsertOrUpdateURLRowByID() if you add any | 579 // Note: revise implementation for InsertOrUpdateURLRowByID() if you add any |
599 // new constraints to the schema. | 580 // new constraints to the schema. |
600 std::string sql; | 581 std::string sql; |
601 sql.append("CREATE TABLE "); | 582 sql.append("CREATE TABLE "); |
602 sql.append(name); | 583 sql.append(name); |
603 sql.append("(" | 584 sql.append( |
604 "id INTEGER PRIMARY KEY," | 585 "(" |
| 586 "id INTEGER PRIMARY KEY AUTOINCREMENT," |
| 587 // Using AUTOINCREMENT is for sync propose. Sync uses this |id| as an |
| 588 // unique key to identify the URLs. If here did not use AUTOINCREMENT, and |
| 589 // Sync was not working somehow, a ROWID could be deleted and re-used |
| 590 // during this period. Once Sync come back, Sync would use ROWIDs and |
| 591 // timestamps to see if there are any updates need to be synced. And sync |
| 592 // will only see the new URL, but missed the deleted URL. |
605 "url LONGVARCHAR," | 593 "url LONGVARCHAR," |
606 "title LONGVARCHAR," | 594 "title LONGVARCHAR," |
607 "visit_count INTEGER DEFAULT 0 NOT NULL," | 595 "visit_count INTEGER DEFAULT 0 NOT NULL," |
608 "typed_count INTEGER DEFAULT 0 NOT NULL," | 596 "typed_count INTEGER DEFAULT 0 NOT NULL," |
609 "last_visit_time INTEGER NOT NULL," | 597 "last_visit_time INTEGER NOT NULL," |
610 "hidden INTEGER DEFAULT 0 NOT NULL," | 598 "hidden INTEGER DEFAULT 0 NOT NULL)"); |
611 "favicon_id INTEGER DEFAULT 0 NOT NULL)"); // favicon_id is not used now. | |
612 | 599 |
613 return GetDB().Execute(sql.c_str()); | 600 return GetDB().Execute(sql.c_str()); |
614 } | 601 } |
615 | 602 |
616 bool URLDatabase::CreateMainURLIndex() { | 603 bool URLDatabase::CreateMainURLIndex() { |
617 return GetDB().Execute( | 604 return GetDB().Execute( |
618 "CREATE INDEX IF NOT EXISTS urls_url_index ON urls (url)"); | 605 "CREATE INDEX IF NOT EXISTS urls_url_index ON urls (url)"); |
619 } | 606 } |
620 | 607 |
| 608 bool URLDatabase::RecreateURLTableWithAllContents() { |
| 609 // Create a temporary table to contain the new URLs table. |
| 610 if (!CreateTemporaryURLTable()) { |
| 611 NOTREACHED(); |
| 612 return false; |
| 613 } |
| 614 |
| 615 // Copy the contents. |
| 616 if (!GetDB().Execute( |
| 617 "INSERT INTO temp_urls (id, url, title, visit_count, typed_count, " |
| 618 "last_visit_time, hidden) " |
| 619 "SELECT id, url, title, visit_count, typed_count, last_visit_time, " |
| 620 "hidden FROM urls")) { |
| 621 NOTREACHED() << GetDB().GetErrorMessage(); |
| 622 return false; |
| 623 } |
| 624 |
| 625 // Rename/commit the tmp table. |
| 626 CommitTemporaryURLTable(); |
| 627 |
| 628 return true; |
| 629 } |
| 630 |
621 const int kLowQualityMatchTypedLimit = 1; | 631 const int kLowQualityMatchTypedLimit = 1; |
622 const int kLowQualityMatchVisitLimit = 4; | 632 const int kLowQualityMatchVisitLimit = 4; |
623 const int kLowQualityMatchAgeLimitInDays = 3; | 633 const int kLowQualityMatchAgeLimitInDays = 3; |
624 | 634 |
625 base::Time AutocompleteAgeThreshold() { | 635 base::Time AutocompleteAgeThreshold() { |
626 return (base::Time::Now() - | 636 return (base::Time::Now() - |
627 base::TimeDelta::FromDays(kLowQualityMatchAgeLimitInDays)); | 637 base::TimeDelta::FromDays(kLowQualityMatchAgeLimitInDays)); |
628 } | 638 } |
629 | 639 |
630 bool RowQualifiesAsSignificant(const URLRow& row, | 640 bool RowQualifiesAsSignificant(const URLRow& row, |
631 const base::Time& threshold) { | 641 const base::Time& threshold) { |
632 const base::Time& real_threshold = | 642 const base::Time& real_threshold = |
633 threshold.is_null() ? AutocompleteAgeThreshold() : threshold; | 643 threshold.is_null() ? AutocompleteAgeThreshold() : threshold; |
634 return (row.typed_count() >= kLowQualityMatchTypedLimit) || | 644 return (row.typed_count() >= kLowQualityMatchTypedLimit) || |
635 (row.visit_count() >= kLowQualityMatchVisitLimit) || | 645 (row.visit_count() >= kLowQualityMatchVisitLimit) || |
636 (row.last_visit() >= real_threshold); | 646 (row.last_visit() >= real_threshold); |
637 } | 647 } |
638 | 648 |
639 } // namespace history | 649 } // namespace history |
OLD | NEW |