Chromium Code Reviews| 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..ee93a21a15d673e8019783bc01791df4c2c30076 100644 |
| --- a/components/previews/core/previews_opt_out_store_sql.cc |
| +++ b/components/previews/core/previews_opt_out_store_sql.cc |
| @@ -1,49 +1,70 @@ |
| // Copyright 2016 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| #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" |
| #include "components/previews/core/previews_experiments.h" |
| #include "sql/connection.h" |
| #include "sql/recovery.h" |
| #include "sql/statement.h" |
| #include "sql/transaction.h" |
| 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. |
|
tbansal1
2016/11/07 17:10:26
May be add a comment that says that the limit is e
RyanSturm
2016/11/07 18:24:28
Done.
|
| +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. |
|
tbansal1
2016/11/07 17:10:26
Can you add a comment that says that the limit is
RyanSturm
2016/11/07 18:24:28
Done.
|
| +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," |
| " time INTEGER NOT NULL," |
| " opt_out INTEGER NOT NULL," |
| " type INTEGER NOT NULL," |
| " PRIMARY KEY(host_name, time DESC, opt_out, type))"; |
| if (!db->Execute(kSql)) |
| return; |
| } |
| @@ -122,34 +143,34 @@ void AddPreviewNavigationToDataBase(sql::Connection* db, |
| statement_insert.BindInt64(1, now.ToInternalValue()); |
| statement_insert.BindBool(2, opt_out); |
| statement_insert.BindInt(3, static_cast<int>(type)); |
| statement_insert.Run(); |
| } |
| // Removes entries for |host_name| if the per-host row limit is exceeded. |
| // 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" |
| " (SELECT ROWID from " PREVIEWS_TABLE_NAME |
| " WHERE host_name == ?" |
| " ORDER BY time DESC" |
| " LIMIT -1 OFFSET ?)"; |
| 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(); |
| } |
| void LoadBlackListFromDataBase( |
| sql::Connection* db, |
| 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. |
| const char kSql[] = |
| @@ -173,32 +194,32 @@ void LoadBlackListFromDataBase( |
| // Allows the internal logic of PreviewsBlackListItem to determine how to |
| // evict entries when there are more than |
| // |StoredHistoryLengthForBlackList()| for the host. |
| 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 |
| " WHERE ROWID IN" |
| " (SELECT ROWID from " PREVIEWS_TABLE_NAME |
| " ORDER BY time DESC" |
| " LIMIT -1 OFFSET ?)"; |
| 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))); |
| } |
| // Synchronous implementations, these are run on the background thread |
| // and actually do the work to access the SQL data base. |
| void LoadBlackListSync(sql::Connection* db, |