| Index: components/previews/core/previews_opt_out_store_sql.cc
|
| diff --git a/components/previews/core/previews_opt_out_store_sql.cc b/components/previews/core/previews_opt_out_store_sql.cc
|
| index 6feb4a16ffa5ed3a4f0d7d8b6cca03d52efae9ee..a6fe9e7ea690cdba8274d72cbb8c90c75c69e98f 100644
|
| --- a/components/previews/core/previews_opt_out_store_sql.cc
|
| +++ b/components/previews/core/previews_opt_out_store_sql.cc
|
| @@ -4,14 +4,18 @@
|
|
|
| #include "components/previews/core/previews_opt_out_store_sql.h"
|
|
|
| +#include <string>
|
| +
|
| #include "base/bind.h"
|
| #include "base/bind_helpers.h"
|
| +#include "base/command_line.h"
|
| #include "base/files/file_util.h"
|
| #include "base/location.h"
|
| #include "base/logging.h"
|
| #include "base/memory/ptr_util.h"
|
| #include "base/metrics/histogram_macros.h"
|
| #include "base/sequenced_task_runner.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| #include "base/threading/thread_task_runner_handle.h"
|
| #include "components/previews/core/previews_black_list.h"
|
| #include "components/previews/core/previews_black_list_item.h"
|
| @@ -25,18 +29,36 @@ namespace previews {
|
|
|
| namespace {
|
|
|
| +// Command line switch to change the previews per row DB size.
|
| +const char kMaxRowsPerHost[] = "previews-max-opt-out-rows-per-host";
|
| +
|
| +// Command line switch to change the previews DB size.
|
| +const char kMaxRows[] = "previews-max-opt-out-rows";
|
| +
|
| +// Returns the maximum number of table rows allowed per host for the previews
|
| +// opt out store. This is enforced during insertion of new navigation entries.
|
| +int MaxRowsPerHostInOptOutDB() {
|
| + std::string max_rows =
|
| + base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
|
| + kMaxRowsPerHost);
|
| + int value;
|
| + return base::StringToInt(max_rows, &value) ? value : 32;
|
| +}
|
| +
|
| +// Returns the maximum number of table rows allowed for the previews opt out
|
| +// store. This is enforced during load time; thus the database can grow
|
| +// larger than this temporarily.
|
| +int MaxRowsInOptOutDB() {
|
| + std::string max_rows =
|
| + base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kMaxRows);
|
| + int value;
|
| + return base::StringToInt(max_rows, &value) ? value : 3200;
|
| +}
|
| +
|
| // This is a macro instead of a const, so it can be used inline in other SQL
|
| // statements below.
|
| #define PREVIEWS_TABLE_NAME "previews_v1"
|
|
|
| -// The maximum number of entries allowed per host. Entries are evicted based on
|
| -// entry time.
|
| -const int kMaxRowsPerHost = 32;
|
| -
|
| -// The maximum number of entries allowed in the data base. Entries are evicted
|
| -// based on entry time.
|
| -const int kMaxRowsInDB = 3200;
|
| -
|
| void CreateSchema(sql::Connection* db) {
|
| const char kSql[] = "CREATE TABLE IF NOT EXISTS " PREVIEWS_TABLE_NAME
|
| " (host_name VARCHAR NOT NULL,"
|
| @@ -129,8 +151,8 @@ void AddPreviewNavigationToDataBase(sql::Connection* db,
|
| // Removes entries if per data base row limit is exceeded.
|
| void MaybeEvictHostEntryFromDataBase(sql::Connection* db,
|
| const std::string& host_name) {
|
| - // Delete the oldest entries if there are more than |kMaxRowsPerHost| for
|
| - // |host_name|.
|
| + // Delete the oldest entries if there are more than |MaxRowsPerHostInOptOutDB|
|
| + // for |host_name|.
|
| // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries.
|
| const char kSqlDeleteByHost[] = "DELETE FROM " PREVIEWS_TABLE_NAME
|
| " WHERE ROWID IN"
|
| @@ -142,7 +164,7 @@ void MaybeEvictHostEntryFromDataBase(sql::Connection* db,
|
| sql::Statement statement_delete_by_host(
|
| db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByHost));
|
| statement_delete_by_host.BindString(0, host_name);
|
| - statement_delete_by_host.BindInt(1, kMaxRowsPerHost);
|
| + statement_delete_by_host.BindInt(1, MaxRowsPerHostInOptOutDB());
|
| statement_delete_by_host.Run();
|
| }
|
|
|
| @@ -151,14 +173,17 @@ void LoadBlackListFromDataBase(
|
| scoped_refptr<base::SingleThreadTaskRunner> runner,
|
| LoadBlackListCallback callback) {
|
| // Gets the table sorted by host and time. Limits the number of hosts using
|
| - // most recent opt_out time as the limiting function.
|
| + // most recent opt_out time as the limiting function. Sorting is free due to
|
| + // the table structure, and it improves performance in the loop below.
|
| const char kSql[] =
|
| "SELECT host_name, time, opt_out"
|
| - " FROM " PREVIEWS_TABLE_NAME;
|
| + " FROM " PREVIEWS_TABLE_NAME " ORDER BY host_name, time DESC";
|
|
|
| sql::Statement statement(db->GetUniqueStatement(kSql));
|
|
|
| std::unique_ptr<BlackListItemMap> black_list_item_map(new BlackListItemMap());
|
| + std::unique_ptr<PreviewsBlackListItem> host_indifferent_black_list_item =
|
| + PreviewsBlackList::CreateHostIndifferentBlackListItem();
|
| int count = 0;
|
| // Add the host name, the visit time, and opt out history to
|
| // |black_list_item_map|.
|
| @@ -166,8 +191,8 @@ void LoadBlackListFromDataBase(
|
| ++count;
|
| std::string host_name = statement.ColumnString(0);
|
| PreviewsBlackListItem* black_list_item =
|
| - PreviewsBlackList::GetOrCreateBlackListItem(black_list_item_map.get(),
|
| - host_name);
|
| + PreviewsBlackList::GetOrCreateBlackListItemForMap(
|
| + black_list_item_map.get(), host_name);
|
| DCHECK_LE(black_list_item_map->size(),
|
| params::MaxInMemoryHostsInBlackList());
|
| // Allows the internal logic of PreviewsBlackListItem to determine how to
|
| @@ -176,11 +201,16 @@ void LoadBlackListFromDataBase(
|
| black_list_item->AddPreviewNavigation(
|
| statement.ColumnBool(2),
|
| base::Time::FromInternalValue(statement.ColumnInt64(1)));
|
| + // Allows the internal logic of PreviewsBlackListItem to determine what
|
| + // items to evict.
|
| + host_indifferent_black_list_item->AddPreviewNavigation(
|
| + statement.ColumnBool(2),
|
| + base::Time::FromInternalValue(statement.ColumnInt64(1)));
|
| }
|
|
|
| UMA_HISTOGRAM_COUNTS_10000("Previews.OptOut.DBRowCount", count);
|
|
|
| - if (count > kMaxRowsInDB) {
|
| + if (count > MaxRowsInOptOutDB()) {
|
| // Delete the oldest entries if there are more than |kMaxEntriesInDB|.
|
| // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries.
|
| const char kSqlDeleteByDBSize[] = "DELETE FROM " PREVIEWS_TABLE_NAME
|
| @@ -191,12 +221,13 @@ void LoadBlackListFromDataBase(
|
|
|
| sql::Statement statement_delete(
|
| db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByDBSize));
|
| - statement_delete.BindInt(0, kMaxRowsInDB);
|
| + statement_delete.BindInt(0, MaxRowsInOptOutDB());
|
| statement_delete.Run();
|
| }
|
|
|
| runner->PostTask(FROM_HERE,
|
| - base::Bind(callback, base::Passed(&black_list_item_map)));
|
| + base::Bind(callback, base::Passed(&black_list_item_map),
|
| + base::Passed(&host_indifferent_black_list_item)));
|
| }
|
|
|
| // Synchronous implementations, these are run on the background thread
|
|
|