| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/autocomplete/network_action_predictor_database.h" | 5 #include "chrome/browser/predictors/autocomplete_action_predictor_database.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/guid.h" | 14 #include "chrome/common/guid.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const char kNetworkActionPredictorTableName[] = "network_action_predictor"; | 20 const char kAutocompleteActionPredictorTableName[] = "network_action_predictor"; |
| 21 const FilePath::CharType kNetworkActionPredictorDatabaseName[] = | 21 const FilePath::CharType kAutocompleteActionPredictorDatabaseName[] = |
| 22 FILE_PATH_LITERAL("Network Action Predictor"); | 22 FILE_PATH_LITERAL("Network Action Predictor"); |
| 23 | 23 |
| 24 // The maximum length allowed for strings in the database. | 24 // The maximum length allowed for strings in the database. |
| 25 const size_t kMaxDataLength = 2048; | 25 const size_t kMaxDataLength = 2048; |
| 26 | 26 |
| 27 void BindRowToStatement(const NetworkActionPredictorDatabase::Row& row, | 27 void BindRowToStatement(const AutocompleteActionPredictorDatabase::Row& row, |
| 28 sql::Statement* statement) { | 28 sql::Statement* statement) { |
| 29 DCHECK(guid::IsValidGUID(row.id)); | 29 DCHECK(guid::IsValidGUID(row.id)); |
| 30 statement->BindString(0, row.id); | 30 statement->BindString(0, row.id); |
| 31 statement->BindString16(1, row.user_text.substr(0, kMaxDataLength)); | 31 statement->BindString16(1, row.user_text.substr(0, kMaxDataLength)); |
| 32 statement->BindString(2, row.url.spec().substr(0, kMaxDataLength)); | 32 statement->BindString(2, row.url.spec().substr(0, kMaxDataLength)); |
| 33 statement->BindInt(3, row.number_of_hits); | 33 statement->BindInt(3, row.number_of_hits); |
| 34 statement->BindInt(4, row.number_of_misses); | 34 statement->BindInt(4, row.number_of_misses); |
| 35 } | 35 } |
| 36 | 36 |
| 37 bool StepAndInitializeRow(sql::Statement* statement, | 37 bool StepAndInitializeRow(sql::Statement* statement, |
| 38 NetworkActionPredictorDatabase::Row* row) { | 38 AutocompleteActionPredictorDatabase::Row* row) { |
| 39 if (!statement->Step()) | 39 if (!statement->Step()) |
| 40 return false; | 40 return false; |
| 41 | 41 |
| 42 row->id = statement->ColumnString(0); | 42 row->id = statement->ColumnString(0); |
| 43 row->user_text = statement->ColumnString16(1); | 43 row->user_text = statement->ColumnString16(1); |
| 44 row->url = GURL(statement->ColumnString(2)); | 44 row->url = GURL(statement->ColumnString(2)); |
| 45 row->number_of_hits = statement->ColumnInt(3); | 45 row->number_of_hits = statement->ColumnInt(3); |
| 46 row->number_of_misses = statement->ColumnInt(4); | 46 row->number_of_misses = statement->ColumnInt(4); |
| 47 return true; | 47 return true; |
| 48 } | 48 } |
| 49 | 49 |
| 50 void LogDatabaseStats(const FilePath& db_path, sql::Connection* db) { | 50 void LogDatabaseStats(const FilePath& db_path, sql::Connection* db) { |
| 51 int64 db_size; | 51 int64 db_size; |
| 52 bool success = file_util::GetFileSize(db_path, &db_size); | 52 bool success = file_util::GetFileSize(db_path, &db_size); |
| 53 DCHECK(success) << "Failed to get file size for " << db_path.value(); | 53 DCHECK(success) << "Failed to get file size for " << db_path.value(); |
| 54 UMA_HISTOGRAM_MEMORY_KB("NetworkActionPredictor.DatabaseSizeKB", | 54 UMA_HISTOGRAM_MEMORY_KB("NetworkActionPredictor.DatabaseSizeKB", |
| 55 static_cast<int>(db_size / 1024)); | 55 static_cast<int>(db_size / 1024)); |
| 56 | 56 |
| 57 sql::Statement count_statement(db->GetUniqueStatement( | 57 sql::Statement count_statement(db->GetUniqueStatement( |
| 58 base::StringPrintf("SELECT count(id) FROM %s", | 58 base::StringPrintf("SELECT count(id) FROM %s", |
| 59 kNetworkActionPredictorTableName).c_str())); | 59 kAutocompleteActionPredictorTableName).c_str())); |
| 60 if (!count_statement.Step()) | 60 if (!count_statement.Step()) |
| 61 return; | 61 return; |
| 62 UMA_HISTOGRAM_COUNTS("NetworkActionPredictor.DatabaseRowCount", | 62 UMA_HISTOGRAM_COUNTS("NetworkActionPredictor.DatabaseRowCount", |
| 63 count_statement.ColumnInt(0)); | 63 count_statement.ColumnInt(0)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 } | 66 } |
| 67 | 67 |
| 68 NetworkActionPredictorDatabase::Row::Row() | 68 AutocompleteActionPredictorDatabase::Row::Row() |
| 69 : number_of_hits(0), | 69 : number_of_hits(0), |
| 70 number_of_misses(0) { | 70 number_of_misses(0) { |
| 71 } | 71 } |
| 72 | 72 |
| 73 NetworkActionPredictorDatabase::Row::Row(const Row::Id& id, | 73 AutocompleteActionPredictorDatabase::Row::Row(const Row::Id& id, |
| 74 const string16& user_text, | 74 const string16& user_text, |
| 75 const GURL& url, | 75 const GURL& url, |
| 76 int number_of_hits, | 76 int number_of_hits, |
| 77 int number_of_misses) | 77 int number_of_misses) |
| 78 : id(id), | 78 : id(id), |
| 79 user_text(user_text), | 79 user_text(user_text), |
| 80 url(url), | 80 url(url), |
| 81 number_of_hits(number_of_hits), | 81 number_of_hits(number_of_hits), |
| 82 number_of_misses(number_of_misses) { | 82 number_of_misses(number_of_misses) { |
| 83 } | 83 } |
| 84 | 84 |
| 85 NetworkActionPredictorDatabase::NetworkActionPredictorDatabase(Profile* profile) | 85 AutocompleteActionPredictorDatabase::AutocompleteActionPredictorDatabase( |
| 86 : db_path_(profile->GetPath().Append(kNetworkActionPredictorDatabaseName)) { | 86 Profile* profile) |
| 87 : db_path_(profile->GetPath().Append( |
| 88 kAutocompleteActionPredictorDatabaseName)) { |
| 87 } | 89 } |
| 88 | 90 |
| 89 NetworkActionPredictorDatabase::~NetworkActionPredictorDatabase() { | 91 AutocompleteActionPredictorDatabase::~AutocompleteActionPredictorDatabase() { |
| 90 } | 92 } |
| 91 | 93 |
| 92 void NetworkActionPredictorDatabase::Initialize() { | 94 void AutocompleteActionPredictorDatabase::Initialize() { |
| 93 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 95 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 94 | 96 |
| 95 if (canceled_.IsSet()) | 97 if (canceled_.IsSet()) |
| 96 return; | 98 return; |
| 97 | 99 |
| 98 db_.set_exclusive_locking(); | 100 db_.set_exclusive_locking(); |
| 99 if (!db_.Open(db_path_)) { | 101 if (!db_.Open(db_path_)) { |
| 100 canceled_.Set(); | 102 canceled_.Set(); |
| 101 return; | 103 return; |
| 102 } | 104 } |
| 103 | 105 |
| 104 if (!db_.DoesTableExist(kNetworkActionPredictorTableName)) | 106 if (!db_.DoesTableExist(kAutocompleteActionPredictorTableName)) |
| 105 CreateTable(); | 107 CreateTable(); |
| 106 | 108 |
| 107 LogDatabaseStats(db_path_, &db_); | 109 LogDatabaseStats(db_path_, &db_); |
| 108 } | 110 } |
| 109 | 111 |
| 110 void NetworkActionPredictorDatabase::GetRow(const Row::Id& id, Row* row) { | 112 void AutocompleteActionPredictorDatabase::GetRow(const Row::Id& id, Row* row) { |
| 111 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 113 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 112 | 114 |
| 113 if (canceled_.IsSet()) | 115 if (canceled_.IsSet()) |
| 114 return; | 116 return; |
| 115 | 117 |
| 116 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 118 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
| 117 base::StringPrintf( | 119 base::StringPrintf( |
| 118 "SELECT * FROM %s WHERE id=?", | 120 "SELECT * FROM %s WHERE id=?", |
| 119 kNetworkActionPredictorTableName).c_str())); | 121 kAutocompleteActionPredictorTableName).c_str())); |
| 120 statement.BindString(0, id); | 122 statement.BindString(0, id); |
| 121 | 123 |
| 122 bool success = StepAndInitializeRow(&statement, row); | 124 bool success = StepAndInitializeRow(&statement, row); |
| 123 DCHECK(success) << "Failed to get row " << id << " from " | 125 DCHECK(success) << "Failed to get row " << id << " from " |
| 124 << kNetworkActionPredictorTableName; | 126 << kAutocompleteActionPredictorTableName; |
| 125 } | 127 } |
| 126 | 128 |
| 127 void NetworkActionPredictorDatabase::GetAllRows( | 129 void AutocompleteActionPredictorDatabase::GetAllRows( |
| 128 std::vector<NetworkActionPredictorDatabase::Row>* row_buffer) { | 130 std::vector<AutocompleteActionPredictorDatabase::Row>* row_buffer) { |
| 129 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 131 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 130 CHECK(row_buffer); | 132 CHECK(row_buffer); |
| 131 row_buffer->clear(); | 133 row_buffer->clear(); |
| 132 | 134 |
| 133 if (canceled_.IsSet()) | 135 if (canceled_.IsSet()) |
| 134 return; | 136 return; |
| 135 | 137 |
| 136 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 138 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
| 137 base::StringPrintf( | 139 base::StringPrintf( |
| 138 "SELECT * FROM %s", kNetworkActionPredictorTableName).c_str())); | 140 "SELECT * FROM %s", kAutocompleteActionPredictorTableName).c_str())); |
| 139 | 141 |
| 140 Row row; | 142 Row row; |
| 141 while (StepAndInitializeRow(&statement, &row)) | 143 while (StepAndInitializeRow(&statement, &row)) |
| 142 row_buffer->push_back(row); | 144 row_buffer->push_back(row); |
| 143 } | 145 } |
| 144 | 146 |
| 145 void NetworkActionPredictorDatabase::AddRow( | 147 void AutocompleteActionPredictorDatabase::AddRow( |
| 146 const NetworkActionPredictorDatabase::Row& row) { | 148 const AutocompleteActionPredictorDatabase::Row& row) { |
| 147 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 149 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 148 | 150 |
| 149 if (canceled_.IsSet()) | 151 if (canceled_.IsSet()) |
| 150 return; | 152 return; |
| 151 | 153 |
| 152 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 154 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
| 153 base::StringPrintf( | 155 base::StringPrintf( |
| 154 "INSERT INTO %s " | 156 "INSERT INTO %s " |
| 155 "(id, user_text, url, number_of_hits, number_of_misses) " | 157 "(id, user_text, url, number_of_hits, number_of_misses) " |
| 156 "VALUES (?,?,?,?,?)", kNetworkActionPredictorTableName).c_str())); | 158 "VALUES (?,?,?,?,?)", |
| 159 kAutocompleteActionPredictorTableName).c_str())); |
| 157 BindRowToStatement(row, &statement); | 160 BindRowToStatement(row, &statement); |
| 158 | 161 |
| 159 bool success = statement.Run(); | 162 bool success = statement.Run(); |
| 160 DCHECK(success) << "Failed to insert row " << row.id << " into " | 163 DCHECK(success) << "Failed to insert row " << row.id << " into " |
| 161 << kNetworkActionPredictorTableName; | 164 << kAutocompleteActionPredictorTableName; |
| 162 } | 165 } |
| 163 | 166 |
| 164 void NetworkActionPredictorDatabase::UpdateRow( | 167 void AutocompleteActionPredictorDatabase::UpdateRow( |
| 165 const NetworkActionPredictorDatabase::Row& row) { | 168 const AutocompleteActionPredictorDatabase::Row& row) { |
| 166 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 169 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 167 | 170 |
| 168 if (canceled_.IsSet()) | 171 if (canceled_.IsSet()) |
| 169 return; | 172 return; |
| 170 | 173 |
| 171 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 174 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
| 172 base::StringPrintf( | 175 base::StringPrintf( |
| 173 "UPDATE %s " | 176 "UPDATE %s " |
| 174 "SET id=?, user_text=?, url=?, number_of_hits=?, number_of_misses=? " | 177 "SET id=?, user_text=?, url=?, number_of_hits=?, number_of_misses=? " |
| 175 "WHERE id=?1", kNetworkActionPredictorTableName).c_str())); | 178 "WHERE id=?1", kAutocompleteActionPredictorTableName).c_str())); |
| 176 BindRowToStatement(row, &statement); | 179 BindRowToStatement(row, &statement); |
| 177 | 180 |
| 178 statement.Run(); | 181 statement.Run(); |
| 179 DCHECK_GT(db_.GetLastChangeCount(), 0); | 182 DCHECK_GT(db_.GetLastChangeCount(), 0); |
| 180 } | 183 } |
| 181 | 184 |
| 182 void NetworkActionPredictorDatabase::DeleteRow(const Row::Id& id) { | 185 void AutocompleteActionPredictorDatabase::DeleteRow(const Row::Id& id) { |
| 183 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 186 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 184 | 187 |
| 185 if (canceled_.IsSet()) | 188 if (canceled_.IsSet()) |
| 186 return; | 189 return; |
| 187 | 190 |
| 188 DeleteRows(std::vector<Row::Id>(1, id)); | 191 DeleteRows(std::vector<Row::Id>(1, id)); |
| 189 } | 192 } |
| 190 | 193 |
| 191 void NetworkActionPredictorDatabase::DeleteRows( | 194 void AutocompleteActionPredictorDatabase::DeleteRows( |
| 192 const std::vector<Row::Id>& id_list) { | 195 const std::vector<Row::Id>& id_list) { |
| 193 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 196 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 194 | 197 |
| 195 if (canceled_.IsSet()) | 198 if (canceled_.IsSet()) |
| 196 return; | 199 return; |
| 197 | 200 |
| 198 sql::Statement statement(db_.GetUniqueStatement(base::StringPrintf( | 201 sql::Statement statement(db_.GetUniqueStatement(base::StringPrintf( |
| 199 "DELETE FROM %s WHERE id=?", | 202 "DELETE FROM %s WHERE id=?", |
| 200 kNetworkActionPredictorTableName).c_str())); | 203 kAutocompleteActionPredictorTableName).c_str())); |
| 201 | 204 |
| 202 db_.BeginTransaction(); | 205 db_.BeginTransaction(); |
| 203 for (std::vector<Row::Id>::const_iterator it = id_list.begin(); | 206 for (std::vector<Row::Id>::const_iterator it = id_list.begin(); |
| 204 it != id_list.end(); ++it) { | 207 it != id_list.end(); ++it) { |
| 205 statement.BindString(0, *it); | 208 statement.BindString(0, *it); |
| 206 statement.Run(); | 209 statement.Run(); |
| 207 statement.Reset(); | 210 statement.Reset(); |
| 208 } | 211 } |
| 209 db_.CommitTransaction(); | 212 db_.CommitTransaction(); |
| 210 } | 213 } |
| 211 | 214 |
| 212 void NetworkActionPredictorDatabase::DeleteAllRows() { | 215 void AutocompleteActionPredictorDatabase::DeleteAllRows() { |
| 213 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 216 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 214 | 217 |
| 215 if (canceled_.IsSet()) | 218 if (canceled_.IsSet()) |
| 216 return; | 219 return; |
| 217 | 220 |
| 218 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 221 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
| 219 base::StringPrintf("DELETE FROM %s", | 222 base::StringPrintf("DELETE FROM %s", |
| 220 kNetworkActionPredictorTableName).c_str())); | 223 kAutocompleteActionPredictorTableName).c_str())); |
| 221 | 224 |
| 222 statement.Run(); | 225 statement.Run(); |
| 223 } | 226 } |
| 224 | 227 |
| 225 void NetworkActionPredictorDatabase::BeginTransaction() { | 228 void AutocompleteActionPredictorDatabase::BeginTransaction() { |
| 226 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 229 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 227 | 230 |
| 228 if (canceled_.IsSet()) | 231 if (canceled_.IsSet()) |
| 229 return; | 232 return; |
| 230 | 233 |
| 231 db_.BeginTransaction(); | 234 db_.BeginTransaction(); |
| 232 } | 235 } |
| 233 | 236 |
| 234 void NetworkActionPredictorDatabase::CommitTransaction() { | 237 void AutocompleteActionPredictorDatabase::CommitTransaction() { |
| 235 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 238 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 236 | 239 |
| 237 if (canceled_.IsSet()) | 240 if (canceled_.IsSet()) |
| 238 return; | 241 return; |
| 239 | 242 |
| 240 db_.CommitTransaction(); | 243 db_.CommitTransaction(); |
| 241 } | 244 } |
| 242 | 245 |
| 243 void NetworkActionPredictorDatabase::OnPredictorDestroyed() { | 246 void AutocompleteActionPredictorDatabase::OnPredictorDestroyed() { |
| 244 canceled_.Set(); | 247 canceled_.Set(); |
| 245 } | 248 } |
| 246 | 249 |
| 247 void NetworkActionPredictorDatabase::CreateTable() { | 250 void AutocompleteActionPredictorDatabase::CreateTable() { |
| 248 bool success = db_.Execute(base::StringPrintf( | 251 bool success = db_.Execute(base::StringPrintf( |
| 249 "CREATE TABLE %s ( " | 252 "CREATE TABLE %s ( " |
| 250 "id TEXT PRIMARY KEY, " | 253 "id TEXT PRIMARY KEY, " |
| 251 "user_text TEXT, " | 254 "user_text TEXT, " |
| 252 "url TEXT, " | 255 "url TEXT, " |
| 253 "number_of_hits INTEGER, " | 256 "number_of_hits INTEGER, " |
| 254 "number_of_misses INTEGER)", kNetworkActionPredictorTableName).c_str()); | 257 "number_of_misses INTEGER)", |
| 255 DCHECK(success) << "Failed to create " << kNetworkActionPredictorTableName | 258 kAutocompleteActionPredictorTableName).c_str()); |
| 256 << " table."; | 259 DCHECK(success) << "Failed to create " |
| 260 << kAutocompleteActionPredictorTableName << " table."; |
| 257 } | 261 } |
| OLD | NEW |