Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/previews/core/previews_opt_out_store_sql.h" | 5 #include "components/previews/core/previews_opt_out_store_sql.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 | |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" | |
| 9 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 10 #include "base/location.h" | 13 #include "base/location.h" |
| 11 #include "base/logging.h" | 14 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 13 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "components/previews/core/previews_black_list.h" | 20 #include "components/previews/core/previews_black_list.h" |
| 17 #include "components/previews/core/previews_black_list_item.h" | 21 #include "components/previews/core/previews_black_list_item.h" |
| 18 #include "components/previews/core/previews_experiments.h" | 22 #include "components/previews/core/previews_experiments.h" |
| 19 #include "sql/connection.h" | 23 #include "sql/connection.h" |
| 20 #include "sql/recovery.h" | 24 #include "sql/recovery.h" |
| 21 #include "sql/statement.h" | 25 #include "sql/statement.h" |
| 22 #include "sql/transaction.h" | 26 #include "sql/transaction.h" |
| 23 | 27 |
| 24 namespace previews { | 28 namespace previews { |
| 25 | 29 |
| 26 namespace { | 30 namespace { |
| 27 | 31 |
| 32 // Command line switch to change the previews per row DB size. | |
| 33 const char kMaxRowsPerHost[] = "previews-max-opt-out-rows-per-host"; | |
| 34 | |
| 35 // Command line switch to change the previews DB size. | |
| 36 const char kMaxRows[] = "previews-max-opt-out-rows"; | |
| 37 | |
| 38 // Returns the maximum number of table rows allowed per host for the previews | |
| 39 // 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.
| |
| 40 int MaxRowsPerHostInOptOutDB() { | |
| 41 std::string max_rows = | |
| 42 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 43 kMaxRowsPerHost); | |
| 44 int value; | |
| 45 return base::StringToInt(max_rows, &value) ? value : 32; | |
| 46 } | |
| 47 | |
| 48 // Returns the maximum number of table rows allowed for the previews opt out | |
| 49 // 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.
| |
| 50 int MaxRowsInOptOutDB() { | |
| 51 std::string max_rows = | |
| 52 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kMaxRows); | |
| 53 int value; | |
| 54 return base::StringToInt(max_rows, &value) ? value : 3200; | |
| 55 } | |
| 56 | |
| 28 // This is a macro instead of a const, so it can be used inline in other SQL | 57 // This is a macro instead of a const, so it can be used inline in other SQL |
| 29 // statements below. | 58 // statements below. |
| 30 #define PREVIEWS_TABLE_NAME "previews_v1" | 59 #define PREVIEWS_TABLE_NAME "previews_v1" |
| 31 | 60 |
| 32 // The maximum number of entries allowed per host. Entries are evicted based on | |
| 33 // entry time. | |
| 34 const int kMaxRowsPerHost = 32; | |
| 35 | |
| 36 // The maximum number of entries allowed in the data base. Entries are evicted | |
| 37 // based on entry time. | |
| 38 const int kMaxRowsInDB = 3200; | |
| 39 | |
| 40 void CreateSchema(sql::Connection* db) { | 61 void CreateSchema(sql::Connection* db) { |
| 41 const char kSql[] = "CREATE TABLE IF NOT EXISTS " PREVIEWS_TABLE_NAME | 62 const char kSql[] = "CREATE TABLE IF NOT EXISTS " PREVIEWS_TABLE_NAME |
| 42 " (host_name VARCHAR NOT NULL," | 63 " (host_name VARCHAR NOT NULL," |
| 43 " time INTEGER NOT NULL," | 64 " time INTEGER NOT NULL," |
| 44 " opt_out INTEGER NOT NULL," | 65 " opt_out INTEGER NOT NULL," |
| 45 " type INTEGER NOT NULL," | 66 " type INTEGER NOT NULL," |
| 46 " PRIMARY KEY(host_name, time DESC, opt_out, type))"; | 67 " PRIMARY KEY(host_name, time DESC, opt_out, type))"; |
| 47 if (!db->Execute(kSql)) | 68 if (!db->Execute(kSql)) |
| 48 return; | 69 return; |
| 49 } | 70 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 statement_insert.BindInt64(1, now.ToInternalValue()); | 143 statement_insert.BindInt64(1, now.ToInternalValue()); |
| 123 statement_insert.BindBool(2, opt_out); | 144 statement_insert.BindBool(2, opt_out); |
| 124 statement_insert.BindInt(3, static_cast<int>(type)); | 145 statement_insert.BindInt(3, static_cast<int>(type)); |
| 125 statement_insert.Run(); | 146 statement_insert.Run(); |
| 126 } | 147 } |
| 127 | 148 |
| 128 // Removes entries for |host_name| if the per-host row limit is exceeded. | 149 // Removes entries for |host_name| if the per-host row limit is exceeded. |
| 129 // Removes entries if per data base row limit is exceeded. | 150 // Removes entries if per data base row limit is exceeded. |
| 130 void MaybeEvictHostEntryFromDataBase(sql::Connection* db, | 151 void MaybeEvictHostEntryFromDataBase(sql::Connection* db, |
| 131 const std::string& host_name) { | 152 const std::string& host_name) { |
| 132 // Delete the oldest entries if there are more than |kMaxRowsPerHost| for | 153 // Delete the oldest entries if there are more than |MaxRowsPerHostInOptOutDB| |
| 133 // |host_name|. | 154 // for |host_name|. |
| 134 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. | 155 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. |
| 135 const char kSqlDeleteByHost[] = "DELETE FROM " PREVIEWS_TABLE_NAME | 156 const char kSqlDeleteByHost[] = "DELETE FROM " PREVIEWS_TABLE_NAME |
| 136 " WHERE ROWID IN" | 157 " WHERE ROWID IN" |
| 137 " (SELECT ROWID from " PREVIEWS_TABLE_NAME | 158 " (SELECT ROWID from " PREVIEWS_TABLE_NAME |
| 138 " WHERE host_name == ?" | 159 " WHERE host_name == ?" |
| 139 " ORDER BY time DESC" | 160 " ORDER BY time DESC" |
| 140 " LIMIT -1 OFFSET ?)"; | 161 " LIMIT -1 OFFSET ?)"; |
| 141 | 162 |
| 142 sql::Statement statement_delete_by_host( | 163 sql::Statement statement_delete_by_host( |
| 143 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByHost)); | 164 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByHost)); |
| 144 statement_delete_by_host.BindString(0, host_name); | 165 statement_delete_by_host.BindString(0, host_name); |
| 145 statement_delete_by_host.BindInt(1, kMaxRowsPerHost); | 166 statement_delete_by_host.BindInt(1, MaxRowsPerHostInOptOutDB()); |
| 146 statement_delete_by_host.Run(); | 167 statement_delete_by_host.Run(); |
| 147 } | 168 } |
| 148 | 169 |
| 149 void LoadBlackListFromDataBase( | 170 void LoadBlackListFromDataBase( |
| 150 sql::Connection* db, | 171 sql::Connection* db, |
| 151 scoped_refptr<base::SingleThreadTaskRunner> runner, | 172 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 152 LoadBlackListCallback callback) { | 173 LoadBlackListCallback callback) { |
| 153 // Gets the table sorted by host and time. Limits the number of hosts using | 174 // Gets the table sorted by host and time. Limits the number of hosts using |
| 154 // most recent opt_out time as the limiting function. | 175 // most recent opt_out time as the limiting function. |
| 155 const char kSql[] = | 176 const char kSql[] = |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 173 // Allows the internal logic of PreviewsBlackListItem to determine how to | 194 // Allows the internal logic of PreviewsBlackListItem to determine how to |
| 174 // evict entries when there are more than | 195 // evict entries when there are more than |
| 175 // |StoredHistoryLengthForBlackList()| for the host. | 196 // |StoredHistoryLengthForBlackList()| for the host. |
| 176 black_list_item->AddPreviewNavigation( | 197 black_list_item->AddPreviewNavigation( |
| 177 statement.ColumnBool(2), | 198 statement.ColumnBool(2), |
| 178 base::Time::FromInternalValue(statement.ColumnInt64(1))); | 199 base::Time::FromInternalValue(statement.ColumnInt64(1))); |
| 179 } | 200 } |
| 180 | 201 |
| 181 UMA_HISTOGRAM_COUNTS_10000("Previews.OptOut.DBRowCount", count); | 202 UMA_HISTOGRAM_COUNTS_10000("Previews.OptOut.DBRowCount", count); |
| 182 | 203 |
| 183 if (count > kMaxRowsInDB) { | 204 if (count > MaxRowsInOptOutDB()) { |
| 184 // Delete the oldest entries if there are more than |kMaxEntriesInDB|. | 205 // Delete the oldest entries if there are more than |kMaxEntriesInDB|. |
| 185 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. | 206 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. |
| 186 const char kSqlDeleteByDBSize[] = "DELETE FROM " PREVIEWS_TABLE_NAME | 207 const char kSqlDeleteByDBSize[] = "DELETE FROM " PREVIEWS_TABLE_NAME |
| 187 " WHERE ROWID IN" | 208 " WHERE ROWID IN" |
| 188 " (SELECT ROWID from " PREVIEWS_TABLE_NAME | 209 " (SELECT ROWID from " PREVIEWS_TABLE_NAME |
| 189 " ORDER BY time DESC" | 210 " ORDER BY time DESC" |
| 190 " LIMIT -1 OFFSET ?)"; | 211 " LIMIT -1 OFFSET ?)"; |
| 191 | 212 |
| 192 sql::Statement statement_delete( | 213 sql::Statement statement_delete( |
| 193 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByDBSize)); | 214 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByDBSize)); |
| 194 statement_delete.BindInt(0, kMaxRowsInDB); | 215 statement_delete.BindInt(0, MaxRowsInOptOutDB()); |
| 195 statement_delete.Run(); | 216 statement_delete.Run(); |
| 196 } | 217 } |
| 197 | 218 |
| 198 runner->PostTask(FROM_HERE, | 219 runner->PostTask(FROM_HERE, |
| 199 base::Bind(callback, base::Passed(&black_list_item_map))); | 220 base::Bind(callback, base::Passed(&black_list_item_map))); |
| 200 } | 221 } |
| 201 | 222 |
| 202 // Synchronous implementations, these are run on the background thread | 223 // Synchronous implementations, these are run on the background thread |
| 203 // and actually do the work to access the SQL data base. | 224 // and actually do the work to access the SQL data base. |
| 204 void LoadBlackListSync(sql::Connection* db, | 225 void LoadBlackListSync(sql::Connection* db, |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { | 299 void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { |
| 279 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 300 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 280 if (!db_) | 301 if (!db_) |
| 281 db_ = base::MakeUnique<sql::Connection>(); | 302 db_ = base::MakeUnique<sql::Connection>(); |
| 282 background_task_runner_->PostTask( | 303 background_task_runner_->PostTask( |
| 283 FROM_HERE, base::Bind(&LoadBlackListSync, db_.get(), db_file_path_, | 304 FROM_HERE, base::Bind(&LoadBlackListSync, db_.get(), db_file_path_, |
| 284 base::ThreadTaskRunnerHandle::Get(), callback)); | 305 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 285 } | 306 } |
| 286 | 307 |
| 287 } // namespace previews | 308 } // namespace previews |
| OLD | NEW |