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. | |
|
dominich
2012/05/08 20:35:28
Why not do this? we'll lose two weeks of data but
Shishir
2012/05/08 21:49:03
We talked about this last time. The idea is that w
| |
| 18 const char kAutocompletePredictorTableName[] = "network_action_predictor"; | |
| 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()) | |
|
dominich
2012/05/08 20:35:28
where does cancelled_ get set?
dominich
2012/05/08 20:35:28
can this check be combined into a single check? Ta
Shishir
2012/05/08 21:49:03
The predictor_database sets it when it is asked to
Shishir
2012/05/08 21:49:03
The semantics might be unclear. The DB is set to n
dominich
2012/05/08 22:49:39
I understand. But throughout this class you have
Shishir
2012/05/09 17:40:10
Done.
| |
| 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 row_buffer->clear(); | |
| 99 | |
| 100 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
| 101 base::StringPrintf( | |
| 102 "SELECT * FROM %s", kAutocompletePredictorTableName).c_str())); | |
| 103 | |
| 104 Row row; | |
| 105 while (StepAndInitializeRow(&statement, &row)) | |
| 106 row_buffer->push_back(row); | |
| 107 } | |
| 108 | |
| 109 void AutocompleteActionPredictorTable::AddRow( | |
| 110 const AutocompleteActionPredictorTable::Row& row) { | |
| 111 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 112 if (cancelled_.IsSet() || !DB()) | |
| 113 return; | |
| 114 | |
| 115 AddAndUpdateRows(Rows(1, row), Rows()); | |
| 116 } | |
| 117 | |
| 118 void AutocompleteActionPredictorTable::UpdateRow( | |
| 119 const AutocompleteActionPredictorTable::Row& row) { | |
| 120 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 121 if (cancelled_.IsSet() || !DB()) | |
| 122 return; | |
| 123 | |
| 124 AddAndUpdateRows(Rows(), Rows(1, row)); | |
| 125 } | |
| 126 | |
| 127 void AutocompleteActionPredictorTable::AddAndUpdateRows( | |
| 128 const Rows& rows_to_add, | |
| 129 const Rows& rows_to_update) { | |
| 130 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 131 if (cancelled_.IsSet() || !DB()) | |
| 132 return; | |
| 133 | |
| 134 if (!DB()->BeginTransaction()) | |
| 135 return; | |
| 136 for (Rows::const_iterator it = rows_to_add.begin(); | |
| 137 it != rows_to_add.end(); ++it) { | |
| 138 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
| 139 base::StringPrintf( | |
| 140 "INSERT INTO %s " | |
| 141 "(id, user_text, url, number_of_hits, number_of_misses) " | |
| 142 "VALUES (?,?,?,?,?)", kAutocompletePredictorTableName).c_str())); | |
| 143 BindRowToStatement(*it, &statement); | |
| 144 if (!statement.Run()) { | |
| 145 DB()->RollbackTransaction(); | |
|
dominich
2012/05/08 20:35:28
change of behaviour: in the old code, we never rol
Shishir
2012/05/08 21:49:03
statement.Run() will DCHECK.
| |
| 146 return; | |
| 147 } | |
| 148 } | |
| 149 for (Rows::const_iterator it = rows_to_update.begin(); | |
| 150 it != rows_to_update.end(); ++it) { | |
| 151 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
| 152 base::StringPrintf( | |
| 153 "UPDATE %s " | |
| 154 "SET id=?, user_text=?, url=?, number_of_hits=?, number_of_misses=?" | |
| 155 " WHERE id=?1", kAutocompletePredictorTableName).c_str())); | |
| 156 BindRowToStatement(*it, &statement); | |
| 157 if (!statement.Run()) { | |
| 158 DB()->RollbackTransaction(); | |
|
dominich
2012/05/08 20:35:28
same as above: DCHECK.
Shishir
2012/05/08 21:49:03
statement.Run() will DCHECK.
| |
| 159 return; | |
| 160 } | |
| 161 DCHECK_GT(DB()->GetLastChangeCount(), 0); | |
| 162 } | |
| 163 DB()->CommitTransaction(); | |
| 164 } | |
| 165 | |
| 166 void AutocompleteActionPredictorTable::DeleteRows( | |
| 167 const std::vector<Row::Id>& id_list) { | |
| 168 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 169 if (cancelled_.IsSet() || !DB()) | |
| 170 return; | |
| 171 | |
| 172 sql::Statement statement(DB()->GetUniqueStatement(base::StringPrintf( | |
| 173 "DELETE FROM %s WHERE id=?", | |
| 174 kAutocompletePredictorTableName).c_str())); | |
| 175 | |
| 176 if (!DB()->BeginTransaction()) | |
| 177 return; | |
| 178 for (std::vector<Row::Id>::const_iterator it = id_list.begin(); | |
| 179 it != id_list.end(); ++it) { | |
| 180 statement.BindString(0, *it); | |
| 181 if (!statement.Run()) { | |
| 182 DB()->RollbackTransaction(); | |
|
dominich
2012/05/08 20:35:28
ditto: DCHECK
Shishir
2012/05/08 21:49:03
statement.Run() will DCHECK.
| |
| 183 return; | |
| 184 } | |
| 185 statement.Reset(true); | |
| 186 } | |
| 187 DB()->CommitTransaction(); | |
| 188 } | |
| 189 | |
| 190 void AutocompleteActionPredictorTable::DeleteAllRows() { | |
| 191 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 192 if (cancelled_.IsSet() || !DB()) | |
| 193 return; | |
| 194 | |
| 195 sql::Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
| 196 base::StringPrintf("DELETE FROM %s", | |
| 197 kAutocompletePredictorTableName).c_str())); | |
| 198 | |
| 199 statement.Run(); | |
| 200 } | |
| 201 | |
| 202 AutocompleteActionPredictorTable::AutocompleteActionPredictorTable() | |
| 203 : PredictorTableBase() { | |
| 204 } | |
| 205 | |
| 206 AutocompleteActionPredictorTable::~AutocompleteActionPredictorTable() { | |
| 207 } | |
| 208 | |
| 209 void AutocompleteActionPredictorTable::CreateTableIfNonExistent() { | |
| 210 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 211 if (cancelled_.IsSet() || !DB()) | |
| 212 return; | |
| 213 | |
| 214 if (DB()->DoesTableExist(kAutocompletePredictorTableName)) | |
| 215 return; | |
| 216 | |
| 217 bool success = DB()->Execute(base::StringPrintf( | |
| 218 "CREATE TABLE %s ( " | |
| 219 "id TEXT PRIMARY KEY, " | |
| 220 "user_text TEXT, " | |
| 221 "url TEXT, " | |
| 222 "number_of_hits INTEGER, " | |
| 223 "number_of_misses INTEGER)", kAutocompletePredictorTableName).c_str()); | |
| 224 if (!success) | |
| 225 ResetDB(); | |
| 226 } | |
| 227 | |
| 228 void AutocompleteActionPredictorTable::LogDatabaseStats() { | |
| 229 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
| 230 if (cancelled_.IsSet() || !DB()) | |
| 231 return; | |
| 232 | |
| 233 sql::Statement count_statement(DB()->GetUniqueStatement( | |
| 234 base::StringPrintf("SELECT count(id) FROM %s", | |
| 235 kAutocompletePredictorTableName).c_str())); | |
| 236 if (!count_statement.Step()) | |
| 237 return; | |
| 238 UMA_HISTOGRAM_COUNTS("AutocompleteActionPredictor.DatabaseRowCount", | |
| 239 count_statement.ColumnInt(0)); | |
| 240 } | |
| 241 | |
| 242 } // namespace predictors | |
| OLD | NEW |