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

Side by Side Diff: chrome/browser/history/thumbnail_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 "chrome/browser/history/thumbnail_database.h" 5 #include "chrome/browser/history/thumbnail_database.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 const char* alterations[] = { 169 const char* alterations[] = {
170 "ALTER TABLE thumbnails ADD boring_score DOUBLE DEFAULT 1.0", 170 "ALTER TABLE thumbnails ADD boring_score DOUBLE DEFAULT 1.0",
171 "ALTER TABLE thumbnails ADD good_clipping INTEGER DEFAULT 0", 171 "ALTER TABLE thumbnails ADD good_clipping INTEGER DEFAULT 0",
172 "ALTER TABLE thumbnails ADD at_top INTEGER DEFAULT 0", 172 "ALTER TABLE thumbnails ADD at_top INTEGER DEFAULT 0",
173 "ALTER TABLE thumbnails ADD last_updated INTEGER DEFAULT 0", 173 "ALTER TABLE thumbnails ADD last_updated INTEGER DEFAULT 0",
174 NULL 174 NULL
175 }; 175 };
176 176
177 for (int i = 0; alterations[i] != NULL; ++i) { 177 for (int i = 0; alterations[i] != NULL; ++i) {
178 if (!db_.Execute(alterations[i])) { 178 if (!db_.Execute(alterations[i])) {
179 NOTREACHED();
180 return false; 179 return false;
181 } 180 }
182 } 181 }
183 182
184 meta_table_.SetVersionNumber(3); 183 meta_table_.SetVersionNumber(3);
185 meta_table_.SetCompatibleVersionNumber(std::min(3, kCompatibleVersionNumber)); 184 meta_table_.SetCompatibleVersionNumber(std::min(3, kCompatibleVersionNumber));
186 return true; 185 return true;
187 } 186 }
188 187
189 bool ThumbnailDatabase::RecreateThumbnailTable() { 188 bool ThumbnailDatabase::RecreateThumbnailTable() {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 void ThumbnailDatabase::CommitTransaction() { 231 void ThumbnailDatabase::CommitTransaction() {
233 db_.CommitTransaction(); 232 db_.CommitTransaction();
234 } 233 }
235 234
236 void ThumbnailDatabase::Vacuum() { 235 void ThumbnailDatabase::Vacuum() {
237 DCHECK(db_.transaction_nesting() == 0) << 236 DCHECK(db_.transaction_nesting() == 0) <<
238 "Can not have a transaction when vacuuming."; 237 "Can not have a transaction when vacuuming.";
239 ignore_result(db_.Execute("VACUUM")); 238 ignore_result(db_.Execute("VACUUM"));
240 } 239 }
241 240
242 void ThumbnailDatabase::SetPageThumbnail( 241 bool ThumbnailDatabase::SetPageThumbnail(
243 const GURL& url, 242 const GURL& url,
244 URLID id, 243 URLID id,
245 const gfx::Image* thumbnail, 244 const gfx::Image* thumbnail,
246 const ThumbnailScore& score, 245 const ThumbnailScore& score,
247 base::Time time) { 246 base::Time time) {
248 if (use_top_sites_) { 247 if (use_top_sites_) {
249 LOG(WARNING) << "Use TopSites instead."; 248 LOG(WARNING) << "Use TopSites instead.";
250 return; // Not possible after migration to TopSites. 249 return false; // Not possible after migration to TopSites.
251 } 250 }
252 251
253 if (thumbnail) { 252 if (!thumbnail)
254 bool add_thumbnail = true; 253 return DeleteThumbnail(id);
255 ThumbnailScore current_score;
256 if (ThumbnailScoreForId(id, &current_score)) {
257 add_thumbnail = ShouldReplaceThumbnailWith(current_score, score);
258 }
259 254
260 if (add_thumbnail) { 255 bool add_thumbnail = true;
261 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 256 ThumbnailScore current_score;
262 "INSERT OR REPLACE INTO thumbnails " 257 if (ThumbnailScoreForId(id, &current_score)) {
263 "(url_id, boring_score, good_clipping, at_top, last_updated, data) " 258 add_thumbnail = ShouldReplaceThumbnailWith(current_score, score);
264 "VALUES (?,?,?,?,?,?)")); 259 }
265 if (!statement)
266 return;
267 260
268 std::vector<unsigned char> jpeg_data; 261 if (!add_thumbnail)
269 bool encoded = gfx::JPEGEncodedDataFromImage(*thumbnail, kImageQuality, 262 return true;
270 &jpeg_data);
271 if (encoded) {
272 statement.BindInt64(0, id);
273 statement.BindDouble(1, score.boring_score);
274 statement.BindBool(2, score.good_clipping);
275 statement.BindBool(3, score.at_top);
276 statement.BindInt64(4, score.time_at_snapshot.ToTimeT());
277 statement.BindBlob(5, &jpeg_data[0],
278 static_cast<int>(jpeg_data.size()));
279 if (!statement.Run())
280 NOTREACHED() << db_.GetErrorMessage();
281 }
282 263
283 // Publish the thumbnail to any indexers listening to us. 264 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
284 // The tests may send an invalid url. Hence avoid publishing those. 265 "INSERT OR REPLACE INTO thumbnails "
285 if (url.is_valid() && history_publisher_ != NULL) 266 "(url_id, boring_score, good_clipping, at_top, last_updated, data) "
286 history_publisher_->PublishPageThumbnail(jpeg_data, url, time); 267 "VALUES (?,?,?,?,?,?)"));
287 } 268 if (!statement.is_valid())
288 } else { 269 return false;
Scott Hess - ex-Googler 2012/01/03 23:41:57 Is this likely enough to happen to be worth bother
Greg Billock 2012/01/04 19:20:20 Agreed.
289 if (!DeleteThumbnail(id) ) 270
290 DLOG(WARNING) << "Unable to delete thumbnail"; 271 std::vector<unsigned char> jpeg_data;
272 bool encoded = gfx::JPEGEncodedDataFromImage(
273 *thumbnail, kImageQuality, &jpeg_data);
274 if (encoded) {
275 statement.BindInt64(0, id);
276 statement.BindDouble(1, score.boring_score);
277 statement.BindBool(2, score.good_clipping);
278 statement.BindBool(3, score.at_top);
279 statement.BindInt64(4, score.time_at_snapshot.ToTimeT());
280 statement.BindBlob(5, &jpeg_data[0],
281 static_cast<int>(jpeg_data.size()));
Scott Hess - ex-Googler 2012/01/03 23:41:57 Sigh, the parameter should have been a size_t in t
Greg Billock 2012/01/04 19:20:20 Shall I add a todo?
Scott Hess - ex-Googler 2012/01/10 22:04:56 Over in BindBlob()? I think that makes sense. SQ
282 if (!statement.Run())
283 return false;
291 } 284 }
285
286 // Publish the thumbnail to any indexers listening to us.
287 // The tests may send an invalid url. Hence avoid publishing those.
288 if (url.is_valid() && history_publisher_ != NULL)
289 history_publisher_->PublishPageThumbnail(jpeg_data, url, time);
290
291 return true;
292 } 292 }
293 293
294 bool ThumbnailDatabase::GetPageThumbnail(URLID id, 294 bool ThumbnailDatabase::GetPageThumbnail(URLID id,
295 std::vector<unsigned char>* data) { 295 std::vector<unsigned char>* data) {
296 if (use_top_sites_) { 296 if (use_top_sites_) {
297 LOG(WARNING) << "Use TopSites instead."; 297 LOG(WARNING) << "Use TopSites instead.";
298 return false; // Not possible after migration to TopSites. 298 return false; // Not possible after migration to TopSites.
299 } 299 }
300 300
301 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 301 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
302 "SELECT data FROM thumbnails WHERE url_id=?")); 302 "SELECT data FROM thumbnails WHERE url_id=?"));
303 if (!statement) 303 statement.BindInt64(0, id);
304 return false;
305 304
306 statement.BindInt64(0, id);
307 if (!statement.Step()) 305 if (!statement.Step())
308 return false; // don't have a thumbnail for this ID 306 return false; // don't have a thumbnail for this ID
309 307
310 statement.ColumnBlobAsVector(0, data); 308 statement.ColumnBlobAsVector(0, data);
311 return true; 309 return true;
312 } 310 }
313 311
314 bool ThumbnailDatabase::DeleteThumbnail(URLID id) { 312 bool ThumbnailDatabase::DeleteThumbnail(URLID id) {
315 if (use_top_sites_) { 313 if (use_top_sites_) {
316 return true; // Not possible after migration to TopSites. 314 return true; // Not possible after migration to TopSites.
317 } 315 }
318 316
319 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 317 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
320 "DELETE FROM thumbnails WHERE url_id = ?")); 318 "DELETE FROM thumbnails WHERE url_id = ?"));
321 if (!statement) 319 statement.BindInt64(0, id);
322 return false;
323 320
324 statement.BindInt64(0, id);
325 return statement.Run(); 321 return statement.Run();
326 } 322 }
327 323
328 bool ThumbnailDatabase::ThumbnailScoreForId(URLID id, 324 bool ThumbnailDatabase::ThumbnailScoreForId(URLID id,
329 ThumbnailScore* score) { 325 ThumbnailScore* score) {
330 if (use_top_sites_) { 326 if (use_top_sites_) {
331 LOG(WARNING) << "Use TopSites instead."; 327 LOG(WARNING) << "Use TopSites instead.";
332 return false; // Not possible after migration to TopSites. 328 return false; // Not possible after migration to TopSites.
333 } 329 }
330 DCHECK(score);
Scott Hess - ex-Googler 2012/01/03 23:41:57 If you're going to add this, push it above the if(
Greg Billock 2012/01/04 19:20:20 Done.
334 331
335 // Fetch the current thumbnail's information to make sure we 332 // Fetch the current thumbnail's information to make sure we
336 // aren't replacing a good thumbnail with one that's worse. 333 // aren't replacing a good thumbnail with one that's worse.
337 sql::Statement select_statement(db_.GetCachedStatement(SQL_FROM_HERE, 334 sql::Statement select_statement(db_.GetCachedStatement(SQL_FROM_HERE,
338 "SELECT boring_score, good_clipping, at_top, last_updated " 335 "SELECT boring_score, good_clipping, at_top, last_updated "
339 "FROM thumbnails WHERE url_id=?")); 336 "FROM thumbnails WHERE url_id=?"));
340 if (!select_statement) { 337 select_statement.BindInt64(0, id);
341 NOTREACHED() << "Couldn't build select statement!";
342 } else {
343 select_statement.BindInt64(0, id);
344 if (select_statement.Step()) {
345 double current_boring_score = select_statement.ColumnDouble(0);
346 bool current_clipping = select_statement.ColumnBool(1);
347 bool current_at_top = select_statement.ColumnBool(2);
348 base::Time last_updated =
349 base::Time::FromTimeT(select_statement.ColumnInt64(3));
350 *score = ThumbnailScore(current_boring_score, current_clipping,
351 current_at_top, last_updated);
352 return true;
353 }
354 }
355 338
356 return false; 339 if (!select_statement.Step())
340 return false;
341
342 double current_boring_score = select_statement.ColumnDouble(0);
343 bool current_clipping = select_statement.ColumnBool(1);
344 bool current_at_top = select_statement.ColumnBool(2);
345 base::Time last_updated =
346 base::Time::FromTimeT(select_statement.ColumnInt64(3));
347 *score = ThumbnailScore(current_boring_score, current_clipping,
348 current_at_top, last_updated);
349 return true;
357 } 350 }
358 351
359 bool ThumbnailDatabase::SetFavicon(URLID icon_id, 352 bool ThumbnailDatabase::SetFavicon(URLID icon_id,
360 scoped_refptr<RefCountedMemory> icon_data, 353 scoped_refptr<RefCountedMemory> icon_data,
361 base::Time time) { 354 base::Time time) {
362 DCHECK(icon_id); 355 DCHECK(icon_id);
363 if (icon_data->size()) { 356 if (icon_data->size()) {
364 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 357 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
365 "UPDATE favicons SET image_data=?, last_updated=? WHERE id=?")); 358 "UPDATE favicons SET image_data=?, last_updated=? WHERE id=?"));
366 if (!statement)
367 return 0;
368
369 statement.BindBlob(0, icon_data->front(), 359 statement.BindBlob(0, icon_data->front(),
370 static_cast<int>(icon_data->size())); 360 static_cast<int>(icon_data->size()));
371 statement.BindInt64(1, time.ToTimeT()); 361 statement.BindInt64(1, time.ToTimeT());
372 statement.BindInt64(2, icon_id); 362 statement.BindInt64(2, icon_id);
363
373 return statement.Run(); 364 return statement.Run();
374 } else { 365 } else {
375 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 366 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
376 "UPDATE favicons SET image_data=NULL, last_updated=? WHERE id=?")); 367 "UPDATE favicons SET image_data=NULL, last_updated=? WHERE id=?"));
Scott Hess - ex-Googler 2012/01/03 23:41:57 This would probably be cleaner with a single state
Greg Billock 2012/01/04 19:20:20 Done.
377 if (!statement)
378 return 0;
379
380 statement.BindInt64(0, time.ToTimeT()); 368 statement.BindInt64(0, time.ToTimeT());
381 statement.BindInt64(1, icon_id); 369 statement.BindInt64(1, icon_id);
370
382 return statement.Run(); 371 return statement.Run();
383 } 372 }
384 } 373 }
385 374
386 bool ThumbnailDatabase::SetFaviconLastUpdateTime(FaviconID icon_id, 375 bool ThumbnailDatabase::SetFaviconLastUpdateTime(FaviconID icon_id,
387 base::Time time) { 376 base::Time time) {
388 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 377 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
389 "UPDATE favicons SET last_updated=? WHERE id=?")); 378 "UPDATE favicons SET last_updated=? WHERE id=?"));
390 if (!statement)
391 return 0;
392
393 statement.BindInt64(0, time.ToTimeT()); 379 statement.BindInt64(0, time.ToTimeT());
394 statement.BindInt64(1, icon_id); 380 statement.BindInt64(1, icon_id);
381
395 return statement.Run(); 382 return statement.Run();
396 } 383 }
397 384
398 FaviconID ThumbnailDatabase::GetFaviconIDForFaviconURL(const GURL& icon_url, 385 FaviconID ThumbnailDatabase::GetFaviconIDForFaviconURL(const GURL& icon_url,
399 int required_icon_type, 386 int required_icon_type,
400 IconType* icon_type) { 387 IconType* icon_type) {
401 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 388 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
402 "SELECT id, icon_type FROM favicons WHERE url=? AND (icon_type & ? > 0) " 389 "SELECT id, icon_type FROM favicons WHERE url=? AND (icon_type & ? > 0) "
403 "ORDER BY icon_type DESC")); 390 "ORDER BY icon_type DESC"));
404 if (!statement)
405 return 0;
406
407 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); 391 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url));
408 statement.BindInt(1, required_icon_type); 392 statement.BindInt(1, required_icon_type);
393
409 if (!statement.Step()) 394 if (!statement.Step())
410 return 0; // not cached 395 return 0; // not cached
411 396
412 if (icon_type) 397 if (icon_type)
413 *icon_type = static_cast<IconType>(statement.ColumnInt(1)); 398 *icon_type = static_cast<IconType>(statement.ColumnInt(1));
414 return statement.ColumnInt64(0); 399 return statement.ColumnInt64(0);
415 } 400 }
416 401
417 bool ThumbnailDatabase::GetFavicon( 402 bool ThumbnailDatabase::GetFavicon(
418 FaviconID icon_id, 403 FaviconID icon_id,
419 base::Time* last_updated, 404 base::Time* last_updated,
420 std::vector<unsigned char>* png_icon_data, 405 std::vector<unsigned char>* png_icon_data,
421 GURL* icon_url) { 406 GURL* icon_url) {
422 DCHECK(icon_id); 407 DCHECK(icon_id);
423 408
424 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 409 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
425 "SELECT last_updated, image_data, url FROM favicons WHERE id=?")); 410 "SELECT last_updated, image_data, url FROM favicons WHERE id=?"));
426 if (!statement)
427 return 0;
428
429 statement.BindInt64(0, icon_id); 411 statement.BindInt64(0, icon_id);
430 412
431 if (!statement.Step()) 413 if (!statement.Step())
432 return false; // No entry for the id. 414 return false; // No entry for the id.
433 415
434 *last_updated = base::Time::FromTimeT(statement.ColumnInt64(0)); 416 *last_updated = base::Time::FromTimeT(statement.ColumnInt64(0));
435 if (statement.ColumnByteLength(1) > 0) 417 if (statement.ColumnByteLength(1) > 0)
436 statement.ColumnBlobAsVector(1, png_icon_data); 418 statement.ColumnBlobAsVector(1, png_icon_data);
437 if (icon_url) 419 if (icon_url)
438 *icon_url = GURL(statement.ColumnString(2)); 420 *icon_url = GURL(statement.ColumnString(2));
439 421
440 return true; 422 return true;
441 } 423 }
442 424
443 FaviconID ThumbnailDatabase::AddFavicon(const GURL& icon_url, 425 FaviconID ThumbnailDatabase::AddFavicon(const GURL& icon_url,
444 IconType icon_type) { 426 IconType icon_type) {
445 427
446 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 428 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
447 "INSERT INTO favicons (url, icon_type) VALUES (?, ?)")); 429 "INSERT INTO favicons (url, icon_type) VALUES (?, ?)"));
448 if (!statement)
449 return 0;
450
451 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); 430 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url));
452 statement.BindInt(1, icon_type); 431 statement.BindInt(1, icon_type);
432
453 if (!statement.Run()) 433 if (!statement.Run())
454 return 0; 434 return 0;
455 return db_.GetLastInsertRowId(); 435 return db_.GetLastInsertRowId();
456 } 436 }
457 437
458 bool ThumbnailDatabase::DeleteFavicon(FaviconID id) { 438 bool ThumbnailDatabase::DeleteFavicon(FaviconID id) {
459 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 439 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
460 "DELETE FROM favicons WHERE id = ?")); 440 "DELETE FROM favicons WHERE id = ?"));
461 if (!statement) 441 statement.BindInt64(0, id);
462 return false;
463 442
464 statement.BindInt64(0, id);
465 return statement.Run(); 443 return statement.Run();
466 } 444 }
467 445
468 bool ThumbnailDatabase::GetIconMappingForPageURL(const GURL& page_url, 446 bool ThumbnailDatabase::GetIconMappingForPageURL(const GURL& page_url,
469 IconType required_icon_type, 447 IconType required_icon_type,
470 IconMapping* icon_mapping) { 448 IconMapping* icon_mapping) {
471 std::vector<IconMapping> icon_mappings; 449 std::vector<IconMapping> icon_mappings;
472 if (!GetIconMappingsForPageURL(page_url, &icon_mappings)) 450 if (!GetIconMappingsForPageURL(page_url, &icon_mappings))
473 return false; 451 return false;
474 452
(...skipping 12 matching lines...) Expand all
487 bool ThumbnailDatabase::GetIconMappingsForPageURL( 465 bool ThumbnailDatabase::GetIconMappingsForPageURL(
488 const GURL& page_url, 466 const GURL& page_url,
489 std::vector<IconMapping>* mapping_data) { 467 std::vector<IconMapping>* mapping_data) {
490 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 468 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
491 "SELECT icon_mapping.id, icon_mapping.icon_id, favicons.icon_type " 469 "SELECT icon_mapping.id, icon_mapping.icon_id, favicons.icon_type "
492 "FROM icon_mapping " 470 "FROM icon_mapping "
493 "INNER JOIN favicons " 471 "INNER JOIN favicons "
494 "ON icon_mapping.icon_id = favicons.id " 472 "ON icon_mapping.icon_id = favicons.id "
495 "WHERE icon_mapping.page_url=? " 473 "WHERE icon_mapping.page_url=? "
496 "ORDER BY favicons.icon_type DESC")); 474 "ORDER BY favicons.icon_type DESC"));
497 if (!statement)
498 return false;
499
500 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url)); 475 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url));
501 476
502 bool result = false; 477 bool result = false;
503 while (statement.Step()) { 478 while (statement.Step()) {
504 result = true; 479 result = true;
505 if (!mapping_data) 480 if (!mapping_data)
506 return result; 481 return result;
507 482
508 IconMapping icon_mapping; 483 IconMapping icon_mapping;
509 FillIconMapping(statement, page_url, &icon_mapping); 484 FillIconMapping(statement, page_url, &icon_mapping);
510 mapping_data->push_back(icon_mapping); 485 mapping_data->push_back(icon_mapping);
511 } 486 }
512 return result; 487 return result;
513 } 488 }
514 489
515 IconMappingID ThumbnailDatabase::AddIconMapping(const GURL& page_url, 490 IconMappingID ThumbnailDatabase::AddIconMapping(const GURL& page_url,
516 FaviconID icon_id) { 491 FaviconID icon_id) {
517 return AddIconMapping(page_url, icon_id, false); 492 return AddIconMapping(page_url, icon_id, false);
518 } 493 }
519 494
520 bool ThumbnailDatabase::UpdateIconMapping(IconMappingID mapping_id, 495 bool ThumbnailDatabase::UpdateIconMapping(IconMappingID mapping_id,
521 FaviconID icon_id) { 496 FaviconID icon_id) {
522 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 497 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
523 "UPDATE icon_mapping SET icon_id=? WHERE id=?")); 498 "UPDATE icon_mapping SET icon_id=? WHERE id=?"));
524 if (!statement)
525 return 0;
526
527 statement.BindInt64(0, icon_id); 499 statement.BindInt64(0, icon_id);
528 statement.BindInt64(1, mapping_id); 500 statement.BindInt64(1, mapping_id);
501
529 return statement.Run(); 502 return statement.Run();
530 } 503 }
531 504
532 bool ThumbnailDatabase::DeleteIconMappings(const GURL& page_url) { 505 bool ThumbnailDatabase::DeleteIconMappings(const GURL& page_url) {
533 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 506 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
534 "DELETE FROM icon_mapping WHERE page_url = ?")); 507 "DELETE FROM icon_mapping WHERE page_url = ?"));
535 if (!statement) 508 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url));
536 return false;
537 509
538 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url));
539 return statement.Run(); 510 return statement.Run();
540 } 511 }
541 512
542 bool ThumbnailDatabase::HasMappingFor(FaviconID id) { 513 bool ThumbnailDatabase::HasMappingFor(FaviconID id) {
543 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 514 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
544 "SELECT id FROM icon_mapping " 515 "SELECT id FROM icon_mapping "
545 "WHERE icon_id=?")); 516 "WHERE icon_id=?"));
546 if (!statement) 517 statement.BindInt64(0, id);
547 return false;
548 518
549 statement.BindInt64(0, id);
550 return statement.Step(); 519 return statement.Step();
551 } 520 }
552 521
553 bool ThumbnailDatabase::CloneIconMapping(const GURL& old_page_url, 522 bool ThumbnailDatabase::CloneIconMapping(const GURL& old_page_url,
554 const GURL& new_page_url) { 523 const GURL& new_page_url) {
555 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 524 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
556 "SELECT icon_id FROM icon_mapping " 525 "SELECT icon_id FROM icon_mapping "
557 "WHERE page_url=?")); 526 "WHERE page_url=?"));
558 if (!statement) 527 if (!statement.is_valid())
559 return false; 528 return false;
560 529
561 // Do nothing if there are existing bindings 530 // Do nothing if there are existing bindings
562 statement.BindString(0, URLDatabase::GURLToDatabaseURL(new_page_url)); 531 statement.BindString(0, URLDatabase::GURLToDatabaseURL(new_page_url));
563 if (statement.Step()) 532 if (statement.Step())
564 return true; 533 return true;
565 534
566 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE, 535 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE,
567 "INSERT INTO icon_mapping (page_url, icon_id) " 536 "INSERT INTO icon_mapping (page_url, icon_id) "
568 "SELECT ?, icon_id FROM icon_mapping " 537 "SELECT ?, icon_id FROM icon_mapping "
569 "WHERE page_url = ?")); 538 "WHERE page_url = ?"));
570 if (!statement) 539 if (!statement.is_valid())
571 return false; 540 return false;
Scott Hess - ex-Googler 2012/01/03 23:41:57 Is this still necessary?
Greg Billock 2012/01/04 19:20:20 Removed. On 2012/01/03 23:41:57, shess wrote:
572 541
573 statement.BindString(0, URLDatabase::GURLToDatabaseURL(new_page_url)); 542 statement.BindString(0, URLDatabase::GURLToDatabaseURL(new_page_url));
574 statement.BindString(1, URLDatabase::GURLToDatabaseURL(old_page_url)); 543 statement.BindString(1, URLDatabase::GURLToDatabaseURL(old_page_url));
575 return statement.Run(); 544 return statement.Run();
576 } 545 }
577 546
578 547
579 bool ThumbnailDatabase::MigrateIconMappingData(URLDatabase* url_db) { 548 bool ThumbnailDatabase::MigrateIconMappingData(URLDatabase* url_db) {
580 URLDatabase::IconMappingEnumerator e; 549 URLDatabase::IconMappingEnumerator e;
581 if (!url_db->InitIconMappingEnumeratorForEverything(&e)) 550 if (!url_db->InitIconMappingEnumeratorForEverything(&e))
(...skipping 24 matching lines...) Expand all
606 575
607 // The renamed table needs the index (the temporary table doesn't have one). 576 // The renamed table needs the index (the temporary table doesn't have one).
608 return InitIconMappingIndex(); 577 return InitIconMappingIndex();
609 } 578 }
610 579
611 FaviconID ThumbnailDatabase::CopyToTemporaryFaviconTable(FaviconID source) { 580 FaviconID ThumbnailDatabase::CopyToTemporaryFaviconTable(FaviconID source) {
612 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 581 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
613 "INSERT INTO temp_favicons (url, last_updated, image_data, icon_type)" 582 "INSERT INTO temp_favicons (url, last_updated, image_data, icon_type)"
614 "SELECT url, last_updated, image_data, icon_type " 583 "SELECT url, last_updated, image_data, icon_type "
615 "FROM favicons WHERE id = ?")); 584 "FROM favicons WHERE id = ?"));
616 if (!statement)
617 return 0;
618 statement.BindInt64(0, source); 585 statement.BindInt64(0, source);
586
619 if (!statement.Run()) 587 if (!statement.Run())
620 return 0; 588 return 0;
621 589
622 // We return the ID of the newly inserted favicon. 590 // We return the ID of the newly inserted favicon.
623 return db_.GetLastInsertRowId(); 591 return db_.GetLastInsertRowId();
624 } 592 }
625 593
626 bool ThumbnailDatabase::CommitTemporaryFaviconTable() { 594 bool ThumbnailDatabase::CommitTemporaryFaviconTable() {
627 // Delete the old favicons table. 595 // Delete the old favicons table.
628 if (!db_.Execute("DROP TABLE favicons")) 596 if (!db_.Execute("DROP TABLE favicons"))
(...skipping 13 matching lines...) Expand all
642 610
643 bool ThumbnailDatabase::RenameAndDropThumbnails(const FilePath& old_db_file, 611 bool ThumbnailDatabase::RenameAndDropThumbnails(const FilePath& old_db_file,
644 const FilePath& new_db_file) { 612 const FilePath& new_db_file) {
645 // Init favicons table - same schema as the thumbnails. 613 // Init favicons table - same schema as the thumbnails.
646 sql::Connection favicons; 614 sql::Connection favicons;
647 if (OpenDatabase(&favicons, new_db_file) != sql::INIT_OK) 615 if (OpenDatabase(&favicons, new_db_file) != sql::INIT_OK)
648 return false; 616 return false;
649 617
650 if (!InitFaviconsTable(&favicons, false) || 618 if (!InitFaviconsTable(&favicons, false) ||
651 !InitIconMappingTable(&favicons, false)) { 619 !InitIconMappingTable(&favicons, false)) {
652 NOTREACHED() << "Couldn't init favicons and icon-mapping table.";
653 favicons.Close(); 620 favicons.Close();
654 return false; 621 return false;
655 } 622 }
656 favicons.Close(); 623 favicons.Close();
657 624
658 // Can't attach within a transaction. 625 // Can't attach within a transaction.
659 if (transaction_nesting()) 626 if (transaction_nesting())
660 CommitTransaction(); 627 CommitTransaction();
661 628
662 // Attach new DB. 629 // Attach new DB.
663 { 630 {
664 // This block is needed because otherwise the attach statement is 631 // This block is needed because otherwise the attach statement is
665 // never cleared from cache and we can't close the DB :P 632 // never cleared from cache and we can't close the DB :P
666 sql::Statement attach(db_.GetUniqueStatement("ATTACH ? AS new_favicons")); 633 sql::Statement attach(db_.GetUniqueStatement("ATTACH ? AS new_favicons"));
667 if (!attach) { 634 if (!attach.is_valid()) {
668 NOTREACHED() << "Unable to attach database."; 635 DLOG(FATAL) << "Unable to attach database.";
669 // Keep the transaction open, even though we failed. 636 // Keep the transaction open, even though we failed.
670 BeginTransaction(); 637 BeginTransaction();
671 return false; 638 return false;
672 } 639 }
673 640
674 #if defined(OS_POSIX) 641 #if defined(OS_POSIX)
675 attach.BindString(0, new_db_file.value()); 642 attach.BindString(0, new_db_file.value());
676 #else 643 #else
677 attach.BindString(0, WideToUTF8(new_db_file.value())); 644 attach.BindString(0, WideToUTF8(new_db_file.value()));
678 #endif 645 #endif
679 646
680 if (!attach.Run()) { 647 if (!attach.Run()) {
681 NOTREACHED() << db_.GetErrorMessage(); 648 DLOG(FATAL) << db_.GetErrorMessage();
Scott Hess - ex-Googler 2012/01/03 23:41:57 I can't tell if the Execute() cases would already
Greg Billock 2012/01/04 19:20:20 I think you are right. We have a TODO to DLOG(FATA
Scott Hess - ex-Googler 2012/01/10 22:04:56 If Execute() is currently not doing FATAL, then le
682 BeginTransaction(); 649 BeginTransaction();
683 return false; 650 return false;
684 } 651 }
685 } 652 }
686 653
687 // Move favicons to the new DB. 654 // Move favicons to the new DB.
688 if (!db_.Execute("INSERT OR REPLACE INTO new_favicons.favicons " 655 if (!db_.Execute("INSERT OR REPLACE INTO new_favicons.favicons "
689 "SELECT * FROM favicons")) { 656 "SELECT * FROM favicons")) {
690 NOTREACHED() << "Unable to copy favicons."; 657 DLOG(FATAL) << "Unable to copy favicons.";
691 BeginTransaction(); 658 BeginTransaction();
692 return false; 659 return false;
693 } 660 }
694 661
695 if (!db_.Execute("DETACH new_favicons")) { 662 if (!db_.Execute("DETACH new_favicons")) {
696 NOTREACHED() << "Unable to detach database."; 663 DLOG(FATAL) << "Unable to detach database.";
697 BeginTransaction(); 664 BeginTransaction();
698 return false; 665 return false;
699 } 666 }
700 667
701 db_.Close(); 668 db_.Close();
702 669
703 // Reset the DB to point to new file. 670 // Reset the DB to point to new file.
704 if (OpenDatabase(&db_, new_db_file) != sql::INIT_OK) 671 if (OpenDatabase(&db_, new_db_file) != sql::INIT_OK)
705 return false; 672 return false;
706 673
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
752 const char* statement_name = 719 const char* statement_name =
753 is_temporary ? "add_temp_icon_mapping" : "add_icon_mapping"; 720 is_temporary ? "add_temp_icon_mapping" : "add_icon_mapping";
754 721
755 std::string sql; 722 std::string sql;
756 sql.append("INSERT INTO "); 723 sql.append("INSERT INTO ");
757 sql.append(name); 724 sql.append(name);
758 sql.append("(page_url, icon_id) VALUES (?, ?)"); 725 sql.append("(page_url, icon_id) VALUES (?, ?)");
759 726
760 sql::Statement statement( 727 sql::Statement statement(
761 db_.GetCachedStatement(sql::StatementID(statement_name), sql.c_str())); 728 db_.GetCachedStatement(sql::StatementID(statement_name), sql.c_str()));
762 if (!statement)
763 return 0;
764
765 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url)); 729 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url));
766 statement.BindInt64(1, icon_id); 730 statement.BindInt64(1, icon_id);
767 731
768 if (!statement.Run()) 732 if (!statement.Run())
769 return 0; 733 return 0;
770 734
771 return db_.GetLastInsertRowId(); 735 return db_.GetLastInsertRowId();
772 } 736 }
773 737
774 bool ThumbnailDatabase::IsLatestVersion() { 738 bool ThumbnailDatabase::IsLatestVersion() {
775 return meta_table_.GetVersionNumber() == kCurrentVersionNumber; 739 return meta_table_.GetVersionNumber() == kCurrentVersionNumber;
776 } 740 }
777 741
778 bool ThumbnailDatabase::UpgradeToVersion4() { 742 bool ThumbnailDatabase::UpgradeToVersion4() {
779 // Set the default icon type as favicon, so the current data are set 743 // Set the default icon type as favicon, so the current data are set
780 // correctly. 744 // correctly.
781 if (!db_.Execute("ALTER TABLE favicons ADD icon_type INTEGER DEFAULT 1")) { 745 if (!db_.Execute("ALTER TABLE favicons ADD icon_type INTEGER DEFAULT 1")) {
782 NOTREACHED();
783 return false; 746 return false;
784 } 747 }
785 meta_table_.SetVersionNumber(4); 748 meta_table_.SetVersionNumber(4);
786 meta_table_.SetCompatibleVersionNumber(std::min(4, kCompatibleVersionNumber)); 749 meta_table_.SetCompatibleVersionNumber(std::min(4, kCompatibleVersionNumber));
787 return true; 750 return true;
788 } 751 }
789 752
790 bool ThumbnailDatabase::UpgradeToVersion5() { 753 bool ThumbnailDatabase::UpgradeToVersion5() {
791 if (!db_.Execute("ALTER TABLE favicons ADD sizes LONGVARCHAR")) { 754 if (!db_.Execute("ALTER TABLE favicons ADD sizes LONGVARCHAR")) {
792 NOTREACHED();
793 return false; 755 return false;
794 } 756 }
795 meta_table_.SetVersionNumber(5); 757 meta_table_.SetVersionNumber(5);
796 meta_table_.SetCompatibleVersionNumber(std::min(5, kCompatibleVersionNumber)); 758 meta_table_.SetCompatibleVersionNumber(std::min(5, kCompatibleVersionNumber));
797 return true; 759 return true;
798 } 760 }
799 761
800 } // namespace history 762 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698