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," | |
brettw
2017/03/13 19:58:59
I don't understand the bigger picture about why au
Gang Wu
2017/03/14 01:18:49
For sync propose, we need to have an unique key to
| |
605 "url LONGVARCHAR," | 587 "url LONGVARCHAR," |
606 "title LONGVARCHAR," | 588 "title LONGVARCHAR," |
607 "visit_count INTEGER DEFAULT 0 NOT NULL," | 589 "visit_count INTEGER DEFAULT 0 NOT NULL," |
608 "typed_count INTEGER DEFAULT 0 NOT NULL," | 590 "typed_count INTEGER DEFAULT 0 NOT NULL," |
609 "last_visit_time INTEGER NOT NULL," | 591 "last_visit_time INTEGER NOT NULL," |
610 "hidden INTEGER DEFAULT 0 NOT NULL," | 592 "hidden INTEGER DEFAULT 0 NOT NULL)"); |
611 "favicon_id INTEGER DEFAULT 0 NOT NULL)"); // favicon_id is not used now. | |
612 | 593 |
613 return GetDB().Execute(sql.c_str()); | 594 return GetDB().Execute(sql.c_str()); |
614 } | 595 } |
615 | 596 |
616 bool URLDatabase::CreateMainURLIndex() { | 597 bool URLDatabase::CreateMainURLIndex() { |
617 return GetDB().Execute( | 598 return GetDB().Execute( |
618 "CREATE INDEX IF NOT EXISTS urls_url_index ON urls (url)"); | 599 "CREATE INDEX IF NOT EXISTS urls_url_index ON urls (url)"); |
619 } | 600 } |
620 | 601 |
602 bool URLDatabase::RecreateURLTableWithAllContents() { | |
603 // Create a temporary table to contain the new URLs table. | |
604 if (!CreateTemporaryURLTable()) { | |
605 NOTREACHED(); | |
606 return false; | |
607 } | |
608 | |
609 // Copy the contents. | |
610 if (!GetDB().Execute( | |
611 "INSERT INTO temp_urls (id, url, title, visit_count, typed_count, " | |
612 "last_visit_time, hidden) " | |
613 "SELECT id, url, title, visit_count, typed_count, last_visit_time, " | |
614 "hidden FROM urls")) { | |
615 NOTREACHED() << GetDB().GetErrorMessage(); | |
616 return false; | |
617 } | |
618 | |
619 // Rename/commit the tmp table. | |
620 CommitTemporaryURLTable(); | |
621 | |
622 return true; | |
623 } | |
624 | |
621 const int kLowQualityMatchTypedLimit = 1; | 625 const int kLowQualityMatchTypedLimit = 1; |
622 const int kLowQualityMatchVisitLimit = 4; | 626 const int kLowQualityMatchVisitLimit = 4; |
623 const int kLowQualityMatchAgeLimitInDays = 3; | 627 const int kLowQualityMatchAgeLimitInDays = 3; |
624 | 628 |
625 base::Time AutocompleteAgeThreshold() { | 629 base::Time AutocompleteAgeThreshold() { |
626 return (base::Time::Now() - | 630 return (base::Time::Now() - |
627 base::TimeDelta::FromDays(kLowQualityMatchAgeLimitInDays)); | 631 base::TimeDelta::FromDays(kLowQualityMatchAgeLimitInDays)); |
628 } | 632 } |
629 | 633 |
630 bool RowQualifiesAsSignificant(const URLRow& row, | 634 bool RowQualifiesAsSignificant(const URLRow& row, |
631 const base::Time& threshold) { | 635 const base::Time& threshold) { |
632 const base::Time& real_threshold = | 636 const base::Time& real_threshold = |
633 threshold.is_null() ? AutocompleteAgeThreshold() : threshold; | 637 threshold.is_null() ? AutocompleteAgeThreshold() : threshold; |
634 return (row.typed_count() >= kLowQualityMatchTypedLimit) || | 638 return (row.typed_count() >= kLowQualityMatchTypedLimit) || |
635 (row.visit_count() >= kLowQualityMatchVisitLimit) || | 639 (row.visit_count() >= kLowQualityMatchVisitLimit) || |
636 (row.last_visit() >= real_threshold); | 640 (row.last_visit() >= real_threshold); |
637 } | 641 } |
638 | 642 |
639 } // namespace history | 643 } // namespace history |
OLD | NEW |