Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/metrics/histogram.h" | |
| 9 #include "base/stringprintf.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "chrome/common/guid.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "sql/statement.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // TODO(shishir): Rename the table for consistency. | |
| 18 const char kAutocompletePredictorTableName[] = "network_action_predictor"; | |
|
Scott Hess - ex-Googler
2012/03/15 00:13:33
If kAutocompletePredictorTableName were a #define
Shishir
2012/03/15 07:55:48
The renaming is unfortunate since the current name
| |
| 19 | |
| 20 // The maximum length allowed for strings in the database. | |
| 21 const size_t kMaxDataLength = 2048; | |
| 22 | |
| 23 void BindRowToStatement( | |
| 24 const predictors::AutocompleteActionPredictorTable::Row& row, | |
| 25 sql::Statement* statement) { | |
| 26 DCHECK(guid::IsValidGUID(row.id)); | |
| 27 statement->BindString(0, row.id); | |
| 28 statement->BindString16(1, row.user_text.substr(0, kMaxDataLength)); | |
| 29 statement->BindString(2, row.url.spec().substr(0, kMaxDataLength)); | |
| 30 statement->BindInt(3, row.number_of_hits); | |
| 31 statement->BindInt(4, row.number_of_misses); | |
| 32 } | |
| 33 | |
| 34 bool StepAndInitializeRow( | |
| 35 sql::Statement* statement, | |
| 36 predictors::AutocompleteActionPredictorTable::Row* row) { | |
| 37 if (!statement->Step()) | |
| 38 return false; | |
| 39 | |
| 40 row->id = statement->ColumnString(0); | |
| 41 row->user_text = statement->ColumnString16(1); | |
| 42 row->url = GURL(statement->ColumnString(2)); | |
| 43 row->number_of_hits = statement->ColumnInt(3); | |
| 44 row->number_of_misses = statement->ColumnInt(4); | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 namespace predictors { | |
| 51 | |
| 52 AutocompleteActionPredictorTable::Row::Row() | |
| 53 : number_of_hits(0), | |
| 54 number_of_misses(0) { | |
| 55 } | |
| 56 | |
| 57 AutocompleteActionPredictorTable::Row::Row(const Row::Id& id, | |
| 58 const string16& user_text, | |
| 59 const GURL& url, | |
| 60 int number_of_hits, | |
| 61 int number_of_misses) | |
| 62 : id(id), | |
| 63 user_text(user_text), | |
| 64 url(url), | |
| 65 number_of_hits(number_of_hits), | |
| 66 number_of_misses(number_of_misses) { | |
| 67 } | |
| 68 | |
| 69 AutocompleteActionPredictorTable::Row::Row(const Row& row) | |
| 70 : id(row.id), | |
| 71 user_text(row.user_text), | |
| 72 url(row.url), | |
| 73 number_of_hits(row.number_of_hits), | |
| 74 number_of_misses(row.number_of_misses) { | |
| 75 } | |
| 76 | |
| 77 | |
| 78 void AutocompleteActionPredictorTable::GetRow(const Row::Id& id, Row* row) { | |
| 79 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 80 if (cancelled_.IsSet() || !DB()) | |
| 81 return; | |
| 82 | |
| 83 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
| 84 base::StringPrintf("SELECT * FROM %s WHERE id=?", | |
| 85 kAutocompletePredictorTableName).c_str())); | |
| 86 statement.BindString(0, id); | |
| 87 | |
| 88 bool success = StepAndInitializeRow(&statement, row); | |
| 89 DCHECK(success) << "Failed to get row " << id << " from " | |
| 90 << kAutocompletePredictorTableName; | |
| 91 } | |
| 92 | |
| 93 void AutocompleteActionPredictorTable::GetAllRows(Rows* row_buffer) { | |
| 94 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 95 if (cancelled_.IsSet() || !DB()) | |
| 96 return; | |
| 97 | |
| 98 CHECK(row_buffer); | |
| 99 row_buffer->clear(); | |
|
Scott Hess - ex-Googler
2012/03/15 00:13:33
I'm inclined to skip the CHECK for something like
Shishir
2012/03/15 07:55:48
Removed.
| |
| 100 | |
| 101 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
| 102 base::StringPrintf( | |
| 103 "SELECT * FROM %s", kAutocompletePredictorTableName).c_str())); | |
| 104 | |
| 105 Row row; | |
| 106 while (StepAndInitializeRow(&statement, &row)) | |
| 107 row_buffer->push_back(row); | |
| 108 } | |
| 109 | |
| 110 void AutocompleteActionPredictorTable::AddRow( | |
| 111 const AutocompleteActionPredictorTable::Row& row) { | |
| 112 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 113 if (cancelled_.IsSet() || !DB()) | |
| 114 return; | |
| 115 | |
| 116 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
| 117 base::StringPrintf( | |
| 118 "INSERT INTO %s " | |
| 119 "(id, user_text, url, number_of_hits, number_of_misses) " | |
| 120 "VALUES (?,?,?,?,?)", kAutocompletePredictorTableName).c_str())); | |
| 121 BindRowToStatement(row, &statement); | |
| 122 | |
| 123 bool success = statement.Run(); | |
| 124 DCHECK(success) << "Failed to insert row " << row.id << " into " | |
| 125 << kAutocompletePredictorTableName; | |
|
Scott Hess - ex-Googler
2012/03/15 00:13:33
I think that there will already be a DCHECK failur
Shishir
2012/03/15 07:55:48
Verified and Removed.
| |
| 126 } | |
| 127 | |
| 128 void AutocompleteActionPredictorTable::UpdateRow( | |
| 129 const AutocompleteActionPredictorTable::Row& row) { | |
| 130 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 131 if (cancelled_.IsSet() || !DB()) | |
| 132 return; | |
| 133 | |
| 134 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
| 135 base::StringPrintf( | |
| 136 "UPDATE %s " | |
| 137 "SET id=?, user_text=?, url=?, number_of_hits=?, number_of_misses=? " | |
| 138 "WHERE id=?1", kAutocompletePredictorTableName).c_str())); | |
| 139 BindRowToStatement(row, &statement); | |
| 140 | |
| 141 statement.Run(); | |
| 142 DCHECK_GT(DB()->GetLastChangeCount(), 0); | |
| 143 } | |
| 144 | |
| 145 void AutocompleteActionPredictorTable::AddAndUpdateRows( | |
| 146 const Rows& rows_to_add, | |
| 147 const Rows& rows_to_update) { | |
|
Scott Hess - ex-Googler
2012/03/15 00:13:33
Rather than having this call AddRow() and UpdateRo
Shishir
2012/03/15 07:55:48
Done.
| |
| 148 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 149 if (cancelled_.IsSet() || !DB()) | |
| 150 return; | |
| 151 | |
| 152 DB()->BeginTransaction(); | |
| 153 for (Rows::const_iterator it = rows_to_add.begin(); | |
| 154 it != rows_to_add.end(); ++it) { | |
| 155 AddRow(*it); | |
| 156 } | |
| 157 for (Rows::const_iterator it = rows_to_update.begin(); | |
| 158 it != rows_to_update.end(); ++it) { | |
| 159 UpdateRow(*it); | |
| 160 } | |
| 161 DB()->CommitTransaction(); | |
|
Scott Hess - ex-Googler
2012/03/15 00:13:33
You have a transaction around this, but you don't
Shishir
2012/03/15 07:55:48
Done.
| |
| 162 } | |
| 163 | |
| 164 void AutocompleteActionPredictorTable::DeleteRow(const Row::Id& id) { | |
| 165 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 166 if (cancelled_.IsSet() || !DB()) | |
| 167 return; | |
| 168 | |
| 169 DeleteRows(std::vector<Row::Id>(1, id)); | |
| 170 } | |
| 171 | |
| 172 void AutocompleteActionPredictorTable::DeleteRows( | |
| 173 const std::vector<Row::Id>& id_list) { | |
| 174 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 175 if (cancelled_.IsSet() || !DB()) | |
| 176 return; | |
| 177 | |
| 178 sql::Statement statement(DB()->GetUniqueStatement(base::StringPrintf( | |
| 179 "DELETE FROM %s WHERE id=?", | |
| 180 kAutocompletePredictorTableName).c_str())); | |
| 181 | |
| 182 DB()->BeginTransaction(); | |
| 183 for (std::vector<Row::Id>::const_iterator it = id_list.begin(); | |
| 184 it != id_list.end(); ++it) { | |
| 185 statement.BindString(0, *it); | |
| 186 statement.Run(); | |
| 187 statement.Reset(); | |
| 188 } | |
| 189 DB()->CommitTransaction(); | |
| 190 } | |
| 191 | |
| 192 void AutocompleteActionPredictorTable::DeleteAllRows() { | |
| 193 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 194 if (cancelled_.IsSet() || !DB()) | |
| 195 return; | |
| 196 | |
| 197 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
| 198 base::StringPrintf("DELETE FROM %s", | |
| 199 kAutocompletePredictorTableName).c_str())); | |
| 200 | |
| 201 statement.Run(); | |
| 202 } | |
| 203 | |
| 204 AutocompleteActionPredictorTable::AutocompleteActionPredictorTable() | |
| 205 : PredictorTableBase() { | |
| 206 } | |
| 207 | |
| 208 AutocompleteActionPredictorTable::~AutocompleteActionPredictorTable() { | |
| 209 } | |
| 210 | |
| 211 void AutocompleteActionPredictorTable::CreateTableIfNonExistent() { | |
| 212 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 213 if (cancelled_.IsSet() || !DB()) | |
| 214 return; | |
| 215 | |
| 216 if (DB()->DoesTableExist(kAutocompletePredictorTableName)) | |
| 217 return; | |
| 218 | |
| 219 bool success = DB()->Execute(base::StringPrintf( | |
| 220 "CREATE TABLE %s ( " | |
| 221 "id TEXT PRIMARY KEY, " | |
| 222 "user_text TEXT, " | |
| 223 "url TEXT, " | |
| 224 "number_of_hits INTEGER, " | |
| 225 "number_of_misses INTEGER)", kAutocompletePredictorTableName).c_str()); | |
| 226 DCHECK(success) << "Failed to create " << kAutocompletePredictorTableName | |
| 227 << " table."; | |
|
Scott Hess - ex-Googler
2012/03/15 00:13:33
I think Execute() will have DCHECK'ed before you g
Shishir
2012/03/15 07:55:48
Verified and Removed.
| |
| 228 } | |
| 229 | |
| 230 void AutocompleteActionPredictorTable::LogDatabaseStats() { | |
| 231 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 232 if (cancelled_.IsSet() || !DB()) | |
| 233 return; | |
| 234 | |
| 235 sql::Statement count_statement(DB()->GetUniqueStatement( | |
| 236 base::StringPrintf("SELECT count(id) FROM %s", | |
| 237 kAutocompletePredictorTableName).c_str())); | |
| 238 if (!count_statement.Step()) | |
| 239 return; | |
| 240 UMA_HISTOGRAM_COUNTS("AutocompleteActionPredictor.DatabaseRowCount", | |
| 241 count_statement.ColumnInt(0)); | |
| 242 } | |
| 243 | |
| 244 } // namespace predictors | |
| OLD | NEW |