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