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

Side by Side Diff: components/history/core/browser/history_database.cc

Issue 2721713002: [sync] Add typed url sync metadata to the history db (Closed)
Patch Set: move meta table into typed_url_metadata_database Created 3 years, 9 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "components/history/core/browser/history_database.h" 5 #include "components/history/core/browser/history_database.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/command_line.h" 15 #include "base/command_line.h"
16 #include "base/containers/hash_tables.h" 16 #include "base/containers/hash_tables.h"
17 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
18 #include "base/metrics/histogram_macros.h" 18 #include "base/metrics/histogram_macros.h"
19 #include "base/numerics/safe_conversions.h" 19 #include "base/numerics/safe_conversions.h"
20 #include "base/rand_util.h" 20 #include "base/rand_util.h"
21 #include "base/strings/string_util.h" 21 #include "base/strings/string_util.h"
22 #include "base/time/time.h" 22 #include "base/time/time.h"
23 #include "build/build_config.h" 23 #include "build/build_config.h"
24 #include "components/history/core/browser/url_utils.h" 24 #include "components/history/core/browser/url_utils.h"
25 #include "sql/meta_table.h"
25 #include "sql/statement.h" 26 #include "sql/statement.h"
26 #include "sql/transaction.h" 27 #include "sql/transaction.h"
27 28
28 #if defined(OS_MACOSX) && !defined(OS_IOS) 29 #if defined(OS_MACOSX) && !defined(OS_IOS)
29 #include "base/mac/mac_util.h" 30 #include "base/mac/mac_util.h"
30 #endif 31 #endif
31 32
32 namespace history { 33 namespace history {
33 34
34 namespace { 35 namespace {
35 36
36 // Current version number. We write databases at the "current" version number, 37 // Current version number. We write databases at the "current" version number,
37 // but any previous version that can read the "compatible" one can make do with 38 // but any previous version that can read the "compatible" one can make do with
38 // our database without *too* many bad effects. 39 // our database without *too* many bad effects.
39 const int kCurrentVersionNumber = 34; 40 const int kCurrentVersionNumber = 35;
40 const int kCompatibleVersionNumber = 16; 41 const int kCompatibleVersionNumber = 16;
41 const char kEarlyExpirationThresholdKey[] = "early_expiration_threshold"; 42 const char kEarlyExpirationThresholdKey[] = "early_expiration_threshold";
42 const int kMaxHostsInMemory = 10000; 43 const int kMaxHostsInMemory = 10000;
43 44
44 } // namespace 45 } // namespace
45 46
46 HistoryDatabase::HistoryDatabase( 47 HistoryDatabase::HistoryDatabase(
47 DownloadInterruptReason download_interrupt_reason_none, 48 DownloadInterruptReason download_interrupt_reason_none,
48 DownloadInterruptReason download_interrupt_reason_crash) 49 DownloadInterruptReason download_interrupt_reason_crash)
49 : DownloadDatabase(download_interrupt_reason_none, 50 : DownloadDatabase(download_interrupt_reason_none,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 // Exclude the history file from backups. 86 // Exclude the history file from backups.
86 base::mac::SetFileBackupExclusion(history_name); 87 base::mac::SetFileBackupExclusion(history_name);
87 #endif 88 #endif
88 89
89 // Prime the cache. 90 // Prime the cache.
90 db_.Preload(); 91 db_.Preload();
91 92
92 // Create the tables and indices. 93 // Create the tables and indices.
93 // NOTE: If you add something here, also add it to 94 // NOTE: If you add something here, also add it to
94 // RecreateAllButStarAndURLTables. 95 // RecreateAllButStarAndURLTables.
95 if (!meta_table_.Init(&db_, GetCurrentVersion(), kCompatibleVersionNumber)) 96 if (!GetMetaTable()->Init(&db_, GetCurrentVersion(),
97 kCompatibleVersionNumber))
96 return sql::INIT_FAILURE; 98 return sql::INIT_FAILURE;
97 if (!CreateURLTable(false) || !InitVisitTable() || 99 if (!CreateURLTable(false) || !InitVisitTable() ||
98 !InitKeywordSearchTermsTable() || !InitDownloadTable() || 100 !InitKeywordSearchTermsTable() || !InitDownloadTable() ||
99 !InitSegmentTables()) 101 !InitSegmentTables() || !InitSyncTable())
100 return sql::INIT_FAILURE; 102 return sql::INIT_FAILURE;
101 CreateMainURLIndex(); 103 CreateMainURLIndex();
102 CreateKeywordSearchTermsIndices(); 104 CreateKeywordSearchTermsIndices();
103 105
104 // TODO(benjhayden) Remove at some point. 106 // TODO(benjhayden) Remove at some point.
105 meta_table_.DeleteKey("next_download_id"); 107 GetMetaTable()->DeleteKey("next_download_id");
106 108
107 // Version check. 109 // Version check.
108 sql::InitStatus version_status = EnsureCurrentVersion(); 110 sql::InitStatus version_status = EnsureCurrentVersion();
109 if (version_status != sql::INIT_OK) 111 if (version_status != sql::INIT_OK)
110 return version_status; 112 return version_status;
111 113
112 return committer.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; 114 return committer.Commit() ? sql::INIT_OK : sql::INIT_FAILURE;
113 } 115 }
114 116
115 void HistoryDatabase::ComputeDatabaseMetrics( 117 void HistoryDatabase::ComputeDatabaseMetrics(
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 return s.ColumnInt64(0); 339 return s.ColumnInt64(0);
338 } 340 }
339 return 0; 341 return 0;
340 } 342 }
341 343
342 base::Time HistoryDatabase::GetEarlyExpirationThreshold() { 344 base::Time HistoryDatabase::GetEarlyExpirationThreshold() {
343 if (!cached_early_expiration_threshold_.is_null()) 345 if (!cached_early_expiration_threshold_.is_null())
344 return cached_early_expiration_threshold_; 346 return cached_early_expiration_threshold_;
345 347
346 int64_t threshold; 348 int64_t threshold;
347 if (!meta_table_.GetValue(kEarlyExpirationThresholdKey, &threshold)) { 349 if (!GetMetaTable()->GetValue(kEarlyExpirationThresholdKey, &threshold)) {
348 // Set to a very early non-zero time, so it's before all history, but not 350 // Set to a very early non-zero time, so it's before all history, but not
349 // zero to avoid re-retrieval. 351 // zero to avoid re-retrieval.
350 threshold = 1L; 352 threshold = 1L;
351 } 353 }
352 354
353 cached_early_expiration_threshold_ = base::Time::FromInternalValue(threshold); 355 cached_early_expiration_threshold_ = base::Time::FromInternalValue(threshold);
354 return cached_early_expiration_threshold_; 356 return cached_early_expiration_threshold_;
355 } 357 }
356 358
357 void HistoryDatabase::UpdateEarlyExpirationThreshold(base::Time threshold) { 359 void HistoryDatabase::UpdateEarlyExpirationThreshold(base::Time threshold) {
358 meta_table_.SetValue(kEarlyExpirationThresholdKey, 360 GetMetaTable()->SetValue(kEarlyExpirationThresholdKey,
359 threshold.ToInternalValue()); 361 threshold.ToInternalValue());
360 cached_early_expiration_threshold_ = threshold; 362 cached_early_expiration_threshold_ = threshold;
361 } 363 }
362 364
363 sql::Connection& HistoryDatabase::GetDB() { 365 sql::Connection& HistoryDatabase::GetDB() {
364 return db_; 366 return db_;
365 } 367 }
366 368
367 // Migration ------------------------------------------------------------------- 369 // Migration -------------------------------------------------------------------
368 370
369 sql::InitStatus HistoryDatabase::EnsureCurrentVersion() { 371 sql::InitStatus HistoryDatabase::EnsureCurrentVersion() {
370 // We can't read databases newer than we were designed for. 372 // We can't read databases newer than we were designed for.
371 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { 373 if (GetMetaTable()->GetCompatibleVersionNumber() > kCurrentVersionNumber) {
372 LOG(WARNING) << "History database is too new."; 374 LOG(WARNING) << "History database is too new.";
373 return sql::INIT_TOO_NEW; 375 return sql::INIT_TOO_NEW;
374 } 376 }
375 377
376 int cur_version = meta_table_.GetVersionNumber(); 378 int cur_version = GetMetaTable()->GetVersionNumber();
377 379
378 // Put migration code here 380 // Put migration code here
379 381
380 if (cur_version == 15) { 382 if (cur_version == 15) {
381 if (!db_.Execute("DROP TABLE starred") || !DropStarredIDFromURLs()) { 383 if (!db_.Execute("DROP TABLE starred") || !DropStarredIDFromURLs()) {
382 LOG(WARNING) << "Unable to update history database to version 16."; 384 LOG(WARNING) << "Unable to update history database to version 16.";
383 return sql::INIT_FAILURE; 385 return sql::INIT_FAILURE;
384 } 386 }
385 ++cur_version; 387 ++cur_version;
386 meta_table_.SetVersionNumber(cur_version); 388 GetMetaTable()->SetVersionNumber(cur_version);
387 meta_table_.SetCompatibleVersionNumber( 389 GetMetaTable()->SetCompatibleVersionNumber(
388 std::min(cur_version, kCompatibleVersionNumber)); 390 std::min(cur_version, kCompatibleVersionNumber));
389 } 391 }
390 392
391 if (cur_version == 16) { 393 if (cur_version == 16) {
392 #if !defined(OS_WIN) 394 #if !defined(OS_WIN)
393 // In this version we bring the time format on Mac & Linux in sync with the 395 // In this version we bring the time format on Mac & Linux in sync with the
394 // Windows version so that profiles can be moved between computers. 396 // Windows version so that profiles can be moved between computers.
395 MigrateTimeEpoch(); 397 MigrateTimeEpoch();
396 #endif 398 #endif
397 // On all platforms we bump the version number, so on Windows this 399 // On all platforms we bump the version number, so on Windows this
398 // migration is a NOP. We keep the compatible version at 16 since things 400 // migration is a NOP. We keep the compatible version at 16 since things
399 // will basically still work, just history will be in the future if an 401 // will basically still work, just history will be in the future if an
400 // old version reads it. 402 // old version reads it.
401 ++cur_version; 403 ++cur_version;
402 meta_table_.SetVersionNumber(cur_version); 404 GetMetaTable()->SetVersionNumber(cur_version);
403 } 405 }
404 406
405 if (cur_version == 17) { 407 if (cur_version == 17) {
406 // Version 17 was for thumbnails to top sites migration. We ended up 408 // Version 17 was for thumbnails to top sites migration. We ended up
407 // disabling it though, so 17->18 does nothing. 409 // disabling it though, so 17->18 does nothing.
408 ++cur_version; 410 ++cur_version;
409 meta_table_.SetVersionNumber(cur_version); 411 GetMetaTable()->SetVersionNumber(cur_version);
410 } 412 }
411 413
412 if (cur_version == 18) { 414 if (cur_version == 18) {
413 // This is the version prior to adding url_source column. We need to 415 // This is the version prior to adding url_source column. We need to
414 // migrate the database. 416 // migrate the database.
415 cur_version = 19; 417 cur_version = 19;
416 meta_table_.SetVersionNumber(cur_version); 418 GetMetaTable()->SetVersionNumber(cur_version);
417 } 419 }
418 420
419 if (cur_version == 19) { 421 if (cur_version == 19) {
420 cur_version++; 422 cur_version++;
421 meta_table_.SetVersionNumber(cur_version); 423 GetMetaTable()->SetVersionNumber(cur_version);
422 // This was the thumbnail migration. Obsolete. 424 // This was the thumbnail migration. Obsolete.
423 } 425 }
424 426
425 if (cur_version == 20) { 427 if (cur_version == 20) {
426 // This is the version prior to adding the visit_duration field in visits 428 // This is the version prior to adding the visit_duration field in visits
427 // database. We need to migrate the database. 429 // database. We need to migrate the database.
428 if (!MigrateVisitsWithoutDuration()) { 430 if (!MigrateVisitsWithoutDuration()) {
429 LOG(WARNING) << "Unable to update history database to version 21."; 431 LOG(WARNING) << "Unable to update history database to version 21.";
430 return sql::INIT_FAILURE; 432 return sql::INIT_FAILURE;
431 } 433 }
432 ++cur_version; 434 ++cur_version;
433 meta_table_.SetVersionNumber(cur_version); 435 GetMetaTable()->SetVersionNumber(cur_version);
434 } 436 }
435 437
436 if (cur_version == 21) { 438 if (cur_version == 21) {
437 // The android_urls table's data schemal was changed in version 21. 439 // The android_urls table's data schemal was changed in version 21.
438 #if defined(OS_ANDROID) 440 #if defined(OS_ANDROID)
439 if (!MigrateToVersion22()) { 441 if (!MigrateToVersion22()) {
440 LOG(WARNING) << "Unable to migrate the android_urls table to version 22"; 442 LOG(WARNING) << "Unable to migrate the android_urls table to version 22";
441 } 443 }
442 #endif 444 #endif
443 ++cur_version; 445 ++cur_version;
444 meta_table_.SetVersionNumber(cur_version); 446 GetMetaTable()->SetVersionNumber(cur_version);
445 } 447 }
446 448
447 if (cur_version == 22) { 449 if (cur_version == 22) {
448 if (!MigrateDownloadsState()) { 450 if (!MigrateDownloadsState()) {
449 LOG(WARNING) << "Unable to fix invalid downloads state values"; 451 LOG(WARNING) << "Unable to fix invalid downloads state values";
450 // Invalid state values may cause crashes. 452 // Invalid state values may cause crashes.
451 return sql::INIT_FAILURE; 453 return sql::INIT_FAILURE;
452 } 454 }
453 cur_version++; 455 cur_version++;
454 meta_table_.SetVersionNumber(cur_version); 456 GetMetaTable()->SetVersionNumber(cur_version);
455 } 457 }
456 458
457 if (cur_version == 23) { 459 if (cur_version == 23) {
458 if (!MigrateDownloadsReasonPathsAndDangerType()) { 460 if (!MigrateDownloadsReasonPathsAndDangerType()) {
459 LOG(WARNING) << "Unable to upgrade download interrupt reason and paths"; 461 LOG(WARNING) << "Unable to upgrade download interrupt reason and paths";
460 // Invalid state values may cause crashes. 462 // Invalid state values may cause crashes.
461 return sql::INIT_FAILURE; 463 return sql::INIT_FAILURE;
462 } 464 }
463 cur_version++; 465 cur_version++;
464 meta_table_.SetVersionNumber(cur_version); 466 GetMetaTable()->SetVersionNumber(cur_version);
465 } 467 }
466 468
467 if (cur_version == 24) { 469 if (cur_version == 24) {
468 if (!MigratePresentationIndex()) { 470 if (!MigratePresentationIndex()) {
469 LOG(WARNING) << "Unable to migrate history to version 25"; 471 LOG(WARNING) << "Unable to migrate history to version 25";
470 return sql::INIT_FAILURE; 472 return sql::INIT_FAILURE;
471 } 473 }
472 cur_version++; 474 cur_version++;
473 meta_table_.SetVersionNumber(cur_version); 475 GetMetaTable()->SetVersionNumber(cur_version);
474 } 476 }
475 477
476 if (cur_version == 25) { 478 if (cur_version == 25) {
477 if (!MigrateReferrer()) { 479 if (!MigrateReferrer()) {
478 LOG(WARNING) << "Unable to migrate history to version 26"; 480 LOG(WARNING) << "Unable to migrate history to version 26";
479 return sql::INIT_FAILURE; 481 return sql::INIT_FAILURE;
480 } 482 }
481 cur_version++; 483 cur_version++;
482 meta_table_.SetVersionNumber(cur_version); 484 GetMetaTable()->SetVersionNumber(cur_version);
483 } 485 }
484 486
485 if (cur_version == 26) { 487 if (cur_version == 26) {
486 if (!MigrateDownloadedByExtension()) { 488 if (!MigrateDownloadedByExtension()) {
487 LOG(WARNING) << "Unable to migrate history to version 27"; 489 LOG(WARNING) << "Unable to migrate history to version 27";
488 return sql::INIT_FAILURE; 490 return sql::INIT_FAILURE;
489 } 491 }
490 cur_version++; 492 cur_version++;
491 meta_table_.SetVersionNumber(cur_version); 493 GetMetaTable()->SetVersionNumber(cur_version);
492 } 494 }
493 495
494 if (cur_version == 27) { 496 if (cur_version == 27) {
495 if (!MigrateDownloadValidators()) { 497 if (!MigrateDownloadValidators()) {
496 LOG(WARNING) << "Unable to migrate history to version 28"; 498 LOG(WARNING) << "Unable to migrate history to version 28";
497 return sql::INIT_FAILURE; 499 return sql::INIT_FAILURE;
498 } 500 }
499 cur_version++; 501 cur_version++;
500 meta_table_.SetVersionNumber(cur_version); 502 GetMetaTable()->SetVersionNumber(cur_version);
501 } 503 }
502 504
503 if (cur_version == 28) { 505 if (cur_version == 28) {
504 if (!MigrateMimeType()) { 506 if (!MigrateMimeType()) {
505 LOG(WARNING) << "Unable to migrate history to version 29"; 507 LOG(WARNING) << "Unable to migrate history to version 29";
506 return sql::INIT_FAILURE; 508 return sql::INIT_FAILURE;
507 } 509 }
508 cur_version++; 510 cur_version++;
509 meta_table_.SetVersionNumber(cur_version); 511 GetMetaTable()->SetVersionNumber(cur_version);
510 } 512 }
511 513
512 if (cur_version == 29) { 514 if (cur_version == 29) {
513 if (!MigrateHashHttpMethodAndGenerateGuids()) { 515 if (!MigrateHashHttpMethodAndGenerateGuids()) {
514 LOG(WARNING) << "Unable to migrate history to version 30"; 516 LOG(WARNING) << "Unable to migrate history to version 30";
515 return sql::INIT_FAILURE; 517 return sql::INIT_FAILURE;
516 } 518 }
517 cur_version++; 519 cur_version++;
518 meta_table_.SetVersionNumber(cur_version); 520 GetMetaTable()->SetVersionNumber(cur_version);
519 } 521 }
520 522
521 if (cur_version == 30) { 523 if (cur_version == 30) {
522 if (!MigrateDownloadTabUrl()) { 524 if (!MigrateDownloadTabUrl()) {
523 LOG(WARNING) << "Unable to migrate history to version 31"; 525 LOG(WARNING) << "Unable to migrate history to version 31";
524 return sql::INIT_FAILURE; 526 return sql::INIT_FAILURE;
525 } 527 }
526 cur_version++; 528 cur_version++;
527 meta_table_.SetVersionNumber(cur_version); 529 GetMetaTable()->SetVersionNumber(cur_version);
528 } 530 }
529 531
530 if (cur_version == 31) { 532 if (cur_version == 31) {
531 if (!MigrateDownloadSiteInstanceUrl()) { 533 if (!MigrateDownloadSiteInstanceUrl()) {
532 LOG(WARNING) << "Unable to migrate history to version 32"; 534 LOG(WARNING) << "Unable to migrate history to version 32";
533 return sql::INIT_FAILURE; 535 return sql::INIT_FAILURE;
534 } 536 }
535 cur_version++; 537 cur_version++;
536 meta_table_.SetVersionNumber(cur_version); 538 GetMetaTable()->SetVersionNumber(cur_version);
537 } 539 }
538 540
539 if (cur_version == 32) { 541 if (cur_version == 32) {
540 // New download slices table is introduced, no migration needed. 542 // New download slices table is introduced, no migration needed.
541 cur_version++; 543 cur_version++;
542 meta_table_.SetVersionNumber(cur_version); 544 GetMetaTable()->SetVersionNumber(cur_version);
543 } 545 }
544 546
545 if (cur_version == 33) { 547 if (cur_version == 33) {
546 if (!MigrateDownloadLastAccessTime()) { 548 if (!MigrateDownloadLastAccessTime()) {
547 LOG(WARNING) << "Unable to migrate to version 34"; 549 LOG(WARNING) << "Unable to migrate to version 34";
548 return sql::INIT_FAILURE; 550 return sql::INIT_FAILURE;
549 } 551 }
550 cur_version++; 552 cur_version++;
551 meta_table_.SetVersionNumber(cur_version); 553 GetMetaTable()->SetVersionNumber(cur_version);
554 }
555
556 if (cur_version == 34) {
557 // AUTOINCREMENT is added to urls table PRIMARY KEY(id), need to recreate a
558 // new table and copy all contents over. favicon_id is removed from urls
559 // table since we never use it. Also typed_url_sync_metadata and
560 // autofill_model_type_state tables are introduced, no migration needed for
561 // those two tables.
562 if (!RecreateURLTableWithAllContents()) {
563 LOG(WARNING) << "Unable to update history database to version 35.";
564 return sql::INIT_FAILURE;
565 }
566 cur_version++;
567 GetMetaTable()->SetVersionNumber(cur_version);
552 } 568 }
553 569
554 // When the version is too old, we just try to continue anyway, there should 570 // When the version is too old, we just try to continue anyway, there should
555 // not be a released product that makes a database too old for us to handle. 571 // not be a released product that makes a database too old for us to handle.
556 LOG_IF(WARNING, cur_version < GetCurrentVersion()) << 572 LOG_IF(WARNING, cur_version < GetCurrentVersion()) <<
557 "History database version " << cur_version << " is too old to handle."; 573 "History database version " << cur_version << " is too old to handle.";
558 574
559 return sql::INIT_OK; 575 return sql::INIT_OK;
560 } 576 }
561 577
562 #if !defined(OS_WIN) 578 #if !defined(OS_WIN)
563 void HistoryDatabase::MigrateTimeEpoch() { 579 void HistoryDatabase::MigrateTimeEpoch() {
564 // Update all the times in the URLs and visits table in the main database. 580 // Update all the times in the URLs and visits table in the main database.
565 ignore_result(db_.Execute( 581 ignore_result(db_.Execute(
566 "UPDATE urls " 582 "UPDATE urls "
567 "SET last_visit_time = last_visit_time + 11644473600000000 " 583 "SET last_visit_time = last_visit_time + 11644473600000000 "
568 "WHERE id IN (SELECT id FROM urls WHERE last_visit_time > 0);")); 584 "WHERE id IN (SELECT id FROM urls WHERE last_visit_time > 0);"));
569 ignore_result(db_.Execute( 585 ignore_result(db_.Execute(
570 "UPDATE visits " 586 "UPDATE visits "
571 "SET visit_time = visit_time + 11644473600000000 " 587 "SET visit_time = visit_time + 11644473600000000 "
572 "WHERE id IN (SELECT id FROM visits WHERE visit_time > 0);")); 588 "WHERE id IN (SELECT id FROM visits WHERE visit_time > 0);"));
573 ignore_result(db_.Execute( 589 ignore_result(db_.Execute(
574 "UPDATE segment_usage " 590 "UPDATE segment_usage "
575 "SET time_slot = time_slot + 11644473600000000 " 591 "SET time_slot = time_slot + 11644473600000000 "
576 "WHERE id IN (SELECT id FROM segment_usage WHERE time_slot > 0);")); 592 "WHERE id IN (SELECT id FROM segment_usage WHERE time_slot > 0);"));
577 } 593 }
578 #endif 594 #endif
579 595
580 } // namespace history 596 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698