Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: chrome/browser/history/top_sites_database.cc

Issue 9071014: Database usage adjustment for .../history (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/string_split.h" 6 #include "base/string_split.h"
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" 8 #include "chrome/browser/diagnostics/sqlite_diagnostics.h"
9 #include "chrome/browser/history/history_types.h" 9 #include "chrome/browser/history/history_types.h"
10 #include "chrome/browser/history/top_sites.h" 10 #include "chrome/browser/history/top_sites.h"
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 } 114 }
115 115
116 void TopSitesDatabase::GetPageThumbnails(MostVisitedURLList* urls, 116 void TopSitesDatabase::GetPageThumbnails(MostVisitedURLList* urls,
117 URLToImagesMap* thumbnails) { 117 URLToImagesMap* thumbnails) {
118 sql::Statement statement(db_->GetCachedStatement( 118 sql::Statement statement(db_->GetCachedStatement(
119 SQL_FROM_HERE, 119 SQL_FROM_HERE,
120 "SELECT url, url_rank, title, thumbnail, redirects, " 120 "SELECT url, url_rank, title, thumbnail, redirects, "
121 "boring_score, good_clipping, at_top, last_updated, load_completed " 121 "boring_score, good_clipping, at_top, last_updated, load_completed "
122 "FROM thumbnails ORDER BY url_rank ")); 122 "FROM thumbnails ORDER BY url_rank "));
123 123
124 if (!statement) { 124 if (!statement.is_valid()) {
125 LOG(WARNING) << db_->GetErrorMessage(); 125 LOG(WARNING) << db_->GetErrorMessage();
126 return; 126 return;
127 } 127 }
128 128
129 urls->clear(); 129 urls->clear();
130 thumbnails->clear(); 130 thumbnails->clear();
131 131
132 while (statement.Step()) { 132 while (statement.Step()) {
133 // Results are sorted by url_rank. 133 // Results are sorted by url_rank.
134 MostVisitedURL url; 134 MostVisitedURL url;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 191
192 void TopSitesDatabase::UpdatePageThumbnail( 192 void TopSitesDatabase::UpdatePageThumbnail(
193 const MostVisitedURL& url, const Images& thumbnail) { 193 const MostVisitedURL& url, const Images& thumbnail) {
194 sql::Statement statement(db_->GetCachedStatement( 194 sql::Statement statement(db_->GetCachedStatement(
195 SQL_FROM_HERE, 195 SQL_FROM_HERE,
196 "UPDATE thumbnails SET " 196 "UPDATE thumbnails SET "
197 "title = ?, thumbnail = ?, redirects = ?, " 197 "title = ?, thumbnail = ?, redirects = ?, "
198 "boring_score = ?, good_clipping = ?, at_top = ?, last_updated = ?, " 198 "boring_score = ?, good_clipping = ?, at_top = ?, last_updated = ?, "
199 "load_completed = ? " 199 "load_completed = ? "
200 "WHERE url = ? ")); 200 "WHERE url = ? "));
201 if (!statement) 201 if (!statement.is_valid())
Scott Hess - ex-Googler 2012/01/03 23:41:57 Is this still necessary?
Greg Billock 2012/01/04 19:20:20 Done.
202 return; 202 return;
203 203
204 statement.BindString16(0, url.title); 204 statement.BindString16(0, url.title);
205 if (thumbnail.thumbnail.get() && thumbnail.thumbnail->front()) { 205 if (thumbnail.thumbnail.get() && thumbnail.thumbnail->front()) {
206 statement.BindBlob(1, thumbnail.thumbnail->front(), 206 statement.BindBlob(1, thumbnail.thumbnail->front(),
207 static_cast<int>(thumbnail.thumbnail->size())); 207 static_cast<int>(thumbnail.thumbnail->size()));
208 } 208 }
209 statement.BindString(2, GetRedirects(url)); 209 statement.BindString(2, GetRedirects(url));
210 const ThumbnailScore& score = thumbnail.thumbnail_score; 210 const ThumbnailScore& score = thumbnail.thumbnail_score;
211 statement.BindDouble(3, score.boring_score); 211 statement.BindDouble(3, score.boring_score);
212 statement.BindBool(4, score.good_clipping); 212 statement.BindBool(4, score.good_clipping);
213 statement.BindBool(5, score.at_top); 213 statement.BindBool(5, score.at_top);
214 statement.BindInt64(6, score.time_at_snapshot.ToInternalValue()); 214 statement.BindInt64(6, score.time_at_snapshot.ToInternalValue());
215 statement.BindBool(7, score.load_completed); 215 statement.BindBool(7, score.load_completed);
216 statement.BindString(8, url.url.spec()); 216 statement.BindString(8, url.url.spec());
217 if (!statement.Run()) 217 if (!statement.Run())
218 NOTREACHED() << db_->GetErrorMessage(); 218 NOTREACHED() << db_->GetErrorMessage();
Scott Hess - ex-Googler 2012/01/03 23:41:57 This line should be redundant?
Greg Billock 2012/01/04 19:20:20 Done.
219 } 219 }
220 220
221 void TopSitesDatabase::AddPageThumbnail(const MostVisitedURL& url, 221 void TopSitesDatabase::AddPageThumbnail(const MostVisitedURL& url,
222 int new_rank, 222 int new_rank,
223 const Images& thumbnail) { 223 const Images& thumbnail) {
224 int count = GetRowCount(); 224 int count = GetRowCount();
225 225
226 sql::Statement statement(db_->GetCachedStatement( 226 sql::Statement statement(db_->GetCachedStatement(
227 SQL_FROM_HERE, 227 SQL_FROM_HERE,
228 "INSERT OR REPLACE INTO thumbnails " 228 "INSERT OR REPLACE INTO thumbnails "
229 "(url, url_rank, title, thumbnail, redirects, " 229 "(url, url_rank, title, thumbnail, redirects, "
230 "boring_score, good_clipping, at_top, last_updated, load_completed) " 230 "boring_score, good_clipping, at_top, last_updated, load_completed) "
231 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); 231 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
232 if (!statement) 232 if (!statement.is_valid())
233 return; 233 return;
Scott Hess - ex-Googler 2012/01/03 23:41:57 Still necessary?
Greg Billock 2012/01/04 19:20:20 Done.
234 234
235 statement.BindString(0, url.url.spec()); 235 statement.BindString(0, url.url.spec());
236 statement.BindInt(1, count); // Make it the last url. 236 statement.BindInt(1, count); // Make it the last url.
237 statement.BindString16(2, url.title); 237 statement.BindString16(2, url.title);
238 if (thumbnail.thumbnail.get() && thumbnail.thumbnail->front()) { 238 if (thumbnail.thumbnail.get() && thumbnail.thumbnail->front()) {
239 statement.BindBlob(3, thumbnail.thumbnail->front(), 239 statement.BindBlob(3, thumbnail.thumbnail->front(),
240 static_cast<int>(thumbnail.thumbnail->size())); 240 static_cast<int>(thumbnail.thumbnail->size()));
241 } 241 }
242 statement.BindString(4, GetRedirects(url)); 242 statement.BindString(4, GetRedirects(url));
243 const ThumbnailScore& score = thumbnail.thumbnail_score; 243 const ThumbnailScore& score = thumbnail.thumbnail_score;
244 statement.BindDouble(5, score.boring_score); 244 statement.BindDouble(5, score.boring_score);
245 statement.BindBool(6, score.good_clipping); 245 statement.BindBool(6, score.good_clipping);
246 statement.BindBool(7, score.at_top); 246 statement.BindBool(7, score.at_top);
247 statement.BindInt64(8, score.time_at_snapshot.ToInternalValue()); 247 statement.BindInt64(8, score.time_at_snapshot.ToInternalValue());
248 statement.BindBool(9, score.load_completed); 248 statement.BindBool(9, score.load_completed);
249 if (!statement.Run()) 249 if (!statement.Run())
250 NOTREACHED() << db_->GetErrorMessage(); 250 NOTREACHED() << db_->GetErrorMessage();
Scott Hess - ex-Googler 2012/01/03 23:41:57 The error message is redundant?
Greg Billock 2012/01/04 19:20:20 Done. (Missed a bunch in this file, whether later
251 251
252 UpdatePageRankNoTransaction(url, new_rank); 252 UpdatePageRankNoTransaction(url, new_rank);
253 } 253 }
254 254
255 void TopSitesDatabase::UpdatePageRank(const MostVisitedURL& url, 255 void TopSitesDatabase::UpdatePageRank(const MostVisitedURL& url,
256 int new_rank) { 256 int new_rank) {
257 sql::Transaction transaction(db_.get()); 257 sql::Transaction transaction(db_.get());
258 transaction.Begin(); 258 transaction.Begin();
259 UpdatePageRankNoTransaction(url, new_rank); 259 UpdatePageRankNoTransaction(url, new_rank);
260 transaction.Commit(); 260 transaction.Commit();
(...skipping 11 matching lines...) Expand all
272 // Shift the ranks. 272 // Shift the ranks.
273 if (prev_rank > new_rank) { 273 if (prev_rank > new_rank) {
274 // Shift up 274 // Shift up
275 sql::Statement shift_statement(db_->GetCachedStatement( 275 sql::Statement shift_statement(db_->GetCachedStatement(
276 SQL_FROM_HERE, 276 SQL_FROM_HERE,
277 "UPDATE thumbnails " 277 "UPDATE thumbnails "
278 "SET url_rank = url_rank + 1 " 278 "SET url_rank = url_rank + 1 "
279 "WHERE url_rank >= ? AND url_rank < ?")); 279 "WHERE url_rank >= ? AND url_rank < ?"));
280 shift_statement.BindInt(0, new_rank); 280 shift_statement.BindInt(0, new_rank);
281 shift_statement.BindInt(1, prev_rank); 281 shift_statement.BindInt(1, prev_rank);
282 if (shift_statement) 282 shift_statement.Run();
283 shift_statement.Run();
284 } else if (prev_rank < new_rank) { 283 } else if (prev_rank < new_rank) {
285 // Shift down 284 // Shift down
286 sql::Statement shift_statement(db_->GetCachedStatement( 285 sql::Statement shift_statement(db_->GetCachedStatement(
287 SQL_FROM_HERE, 286 SQL_FROM_HERE,
288 "UPDATE thumbnails " 287 "UPDATE thumbnails "
289 "SET url_rank = url_rank - 1 " 288 "SET url_rank = url_rank - 1 "
290 "WHERE url_rank > ? AND url_rank <= ?")); 289 "WHERE url_rank > ? AND url_rank <= ?"));
291 shift_statement.BindInt(0, prev_rank); 290 shift_statement.BindInt(0, prev_rank);
292 shift_statement.BindInt(1, new_rank); 291 shift_statement.BindInt(1, new_rank);
293 if (shift_statement) 292 shift_statement.Run();
294 shift_statement.Run();
295 } 293 }
296 294
297 // Set the url's rank. 295 // Set the url's rank.
298 sql::Statement set_statement(db_->GetCachedStatement( 296 sql::Statement set_statement(db_->GetCachedStatement(
299 SQL_FROM_HERE, 297 SQL_FROM_HERE,
300 "UPDATE thumbnails " 298 "UPDATE thumbnails "
301 "SET url_rank = ? " 299 "SET url_rank = ? "
302 "WHERE url == ?")); 300 "WHERE url == ?"));
303 set_statement.BindInt(0, new_rank); 301 set_statement.BindInt(0, new_rank);
304 set_statement.BindString(1, url.url.spec()); 302 set_statement.BindString(1, url.url.spec());
305 if (set_statement) 303 set_statement.Run();
306 set_statement.Run();
307 } 304 }
308 305
309 bool TopSitesDatabase::GetPageThumbnail(const GURL& url, 306 bool TopSitesDatabase::GetPageThumbnail(const GURL& url,
310 Images* thumbnail) { 307 Images* thumbnail) {
311 sql::Statement statement(db_->GetCachedStatement( 308 sql::Statement statement(db_->GetCachedStatement(
312 SQL_FROM_HERE, 309 SQL_FROM_HERE,
313 "SELECT thumbnail, boring_score, good_clipping, at_top, last_updated " 310 "SELECT thumbnail, boring_score, good_clipping, at_top, last_updated "
314 "FROM thumbnails WHERE url=?")); 311 "FROM thumbnails WHERE url=?"));
315
316 if (!statement) {
317 LOG(WARNING) << db_->GetErrorMessage();
318 return false;
319 }
320
321 statement.BindString(0, url.spec()); 312 statement.BindString(0, url.spec());
322 if (!statement.Step()) 313 if (!statement.Step())
323 return false; 314 return false;
324 315
325 std::vector<unsigned char> data; 316 std::vector<unsigned char> data;
326 statement.ColumnBlobAsVector(0, &data); 317 statement.ColumnBlobAsVector(0, &data);
327 thumbnail->thumbnail = RefCountedBytes::TakeVector(&data); 318 thumbnail->thumbnail = RefCountedBytes::TakeVector(&data);
328 thumbnail->thumbnail_score.boring_score = statement.ColumnDouble(1); 319 thumbnail->thumbnail_score.boring_score = statement.ColumnDouble(1);
329 thumbnail->thumbnail_score.good_clipping = statement.ColumnBool(2); 320 thumbnail->thumbnail_score.good_clipping = statement.ColumnBool(2);
330 thumbnail->thumbnail_score.at_top = statement.ColumnBool(3); 321 thumbnail->thumbnail_score.at_top = statement.ColumnBool(3);
331 thumbnail->thumbnail_score.time_at_snapshot = 322 thumbnail->thumbnail_score.time_at_snapshot =
332 base::Time::FromInternalValue(statement.ColumnInt64(4)); 323 base::Time::FromInternalValue(statement.ColumnInt64(4));
333 return true; 324 return true;
334 } 325 }
335 326
336 int TopSitesDatabase::GetRowCount() { 327 int TopSitesDatabase::GetRowCount() {
337 int result = 0;
338 sql::Statement select_statement(db_->GetCachedStatement( 328 sql::Statement select_statement(db_->GetCachedStatement(
339 SQL_FROM_HERE, 329 SQL_FROM_HERE,
340 "SELECT COUNT (url) FROM thumbnails")); 330 "SELECT COUNT (url) FROM thumbnails"));
341 if (!select_statement) { 331 if (select_statement.Step())
342 LOG(WARNING) << db_->GetErrorMessage(); 332 return select_statement.ColumnInt(0);
Scott Hess - ex-Googler 2012/01/03 23:41:57 AFAICT, having this return anything other than a s
Greg Billock 2012/01/04 19:20:20 This is returning 0 on error, which is a bit of an
Scott Hess - ex-Googler 2012/01/10 22:04:56 Yeah, I think it doesn't matter (or, at least, it'
343 return result;
344 }
345 333
346 if (select_statement.Step()) 334 return 0;
347 result = select_statement.ColumnInt(0);
348
349 return result;
350 } 335 }
351 336
352 int TopSitesDatabase::GetURLRank(const MostVisitedURL& url) { 337 int TopSitesDatabase::GetURLRank(const MostVisitedURL& url) {
353 int result = -1;
354 sql::Statement select_statement(db_->GetCachedStatement( 338 sql::Statement select_statement(db_->GetCachedStatement(
355 SQL_FROM_HERE, 339 SQL_FROM_HERE,
356 "SELECT url_rank " 340 "SELECT url_rank "
357 "FROM thumbnails WHERE url=?")); 341 "FROM thumbnails WHERE url=?"));
358 if (!select_statement) {
359 LOG(WARNING) << db_->GetErrorMessage();
360 return result;
361 }
362
363 select_statement.BindString(0, url.url.spec()); 342 select_statement.BindString(0, url.url.spec());
364 if (select_statement.Step()) 343 if (select_statement.Step())
365 result = select_statement.ColumnInt(0); 344 return select_statement.ColumnInt(0);
366 345
367 return result; 346 return -1;
368 } 347 }
369 348
370 // Remove the record for this URL. Returns true iff removed successfully. 349 // Remove the record for this URL. Returns true iff removed successfully.
371 bool TopSitesDatabase::RemoveURL(const MostVisitedURL& url) { 350 bool TopSitesDatabase::RemoveURL(const MostVisitedURL& url) {
372 int old_rank = GetURLRank(url); 351 int old_rank = GetURLRank(url);
373 if (old_rank < 0) 352 if (old_rank < 0)
374 return false; 353 return false;
375 354
376 sql::Transaction transaction(db_.get()); 355 sql::Transaction transaction(db_.get());
377 transaction.Begin(); 356 transaction.Begin();
378 // Decrement all following ranks. 357 // Decrement all following ranks.
379 sql::Statement shift_statement(db_->GetCachedStatement( 358 sql::Statement shift_statement(db_->GetCachedStatement(
380 SQL_FROM_HERE, 359 SQL_FROM_HERE,
381 "UPDATE thumbnails " 360 "UPDATE thumbnails "
382 "SET url_rank = url_rank - 1 " 361 "SET url_rank = url_rank - 1 "
383 "WHERE url_rank > ?")); 362 "WHERE url_rank > ?"));
384 if (!shift_statement) 363 shift_statement.BindInt(0, old_rank);
364
365 if (!shift_statement.Run())
385 return false; 366 return false;
386 shift_statement.BindInt(0, old_rank);
387 shift_statement.Run();
388 367
389 sql::Statement delete_statement( 368 sql::Statement delete_statement(
390 db_->GetCachedStatement(SQL_FROM_HERE, 369 db_->GetCachedStatement(SQL_FROM_HERE,
391 "DELETE FROM thumbnails WHERE url = ?")); 370 "DELETE FROM thumbnails WHERE url = ?"));
392 if (!delete_statement) 371 delete_statement.BindString(0, url.url.spec());
372
373 if (!delete_statement.Run())
393 return false; 374 return false;
394 delete_statement.BindString(0, url.url.spec());
395 delete_statement.Run();
396 375
397 return transaction.Commit(); 376 return transaction.Commit();
398 } 377 }
399 378
400 sql::Connection* TopSitesDatabase::CreateDB(const FilePath& db_name) { 379 sql::Connection* TopSitesDatabase::CreateDB(const FilePath& db_name) {
401 scoped_ptr<sql::Connection> db(new sql::Connection()); 380 scoped_ptr<sql::Connection> db(new sql::Connection());
402 // Settings copied from ThumbnailDatabase. 381 // Settings copied from ThumbnailDatabase.
403 db->set_error_delegate(GetErrorHandlerForThumbnailDb()); 382 db->set_error_delegate(GetErrorHandlerForThumbnailDb());
404 db->set_page_size(4096); 383 db->set_page_size(4096);
405 db->set_cache_size(32); 384 db->set_cache_size(32);
406 385
407 if (!db->Open(db_name)) { 386 if (!db->Open(db_name)) {
408 LOG(ERROR) << db->GetErrorMessage(); 387 LOG(ERROR) << db->GetErrorMessage();
409 return NULL; 388 return NULL;
410 } 389 }
411 390
412 return db.release(); 391 return db.release();
413 } 392 }
414 393
415 } // namespace history 394 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698