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 22ba0a0f5f204b7a227da159756bc478752ddfa3..a6fe9e7ea690cdba8274d72cbb8c90c75c69e98f 100644 |
--- a/components/previews/core/previews_opt_out_store_sql.cc |
+++ b/components/previews/core/previews_opt_out_store_sql.cc |
@@ -166,66 +166,75 @@ void MaybeEvictHostEntryFromDataBase(sql::Connection* db, |
statement_delete_by_host.BindString(0, host_name); |
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. |
+ // 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|. |
while (statement.Step()) { |
++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 |
// evict entries when there are more than |
// |StoredHistoryLengthForBlackList()| for the host. |
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 > 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, 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 |
// and actually do the work to access the SQL data base. |
void LoadBlackListSync(sql::Connection* db, |
const base::FilePath& path, |
scoped_refptr<base::SingleThreadTaskRunner> runner, |
LoadBlackListCallback callback) { |
if (!db->is_open()) |
InitDatabase(db, path); |