| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/sync_file_system/drive_backend/metadata_database.h" | 5 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.
h" | 12 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.
h" |
| 13 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_test_util.
h" | 13 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_test_util.
h" |
| 14 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h" | 14 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h" |
| 15 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" | 15 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" |
| 16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h" | 16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h" |
| 17 #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index.
h" | 17 #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index.
h" |
| 18 #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index_
interface.h" | 18 #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index_
interface.h" |
| 19 #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index_
on_disk.h" |
| 19 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" | 20 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" |
| 20 #include "google_apis/drive/drive_api_parser.h" | 21 #include "google_apis/drive/drive_api_parser.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 23 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
| 23 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 24 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 24 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 25 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
| 25 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 26 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 26 | 27 |
| 27 #define FPL(a) FILE_PATH_LITERAL(a) | 28 #define FPL(a) FILE_PATH_LITERAL(a) |
| 28 | 29 |
| (...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 PutFileMetadataToDB(file, db); | 456 PutFileMetadataToDB(file, db); |
| 456 return db->Commit(); | 457 return db->Commit(); |
| 457 } | 458 } |
| 458 | 459 |
| 459 leveldb::Status PutTrackerToDB(LevelDBWrapper* db, | 460 leveldb::Status PutTrackerToDB(LevelDBWrapper* db, |
| 460 const FileTracker& tracker) { | 461 const FileTracker& tracker) { |
| 461 PutFileTrackerToDB(tracker, db); | 462 PutFileTrackerToDB(tracker, db); |
| 462 return db->Commit(); | 463 return db->Commit(); |
| 463 } | 464 } |
| 464 | 465 |
| 466 void VerifyReloadConsistencyForOnMemory(MetadataDatabaseIndex* index1, |
| 467 MetadataDatabaseIndex* index2) { |
| 468 ExpectEquivalentServiceMetadata(index1, index2); |
| 469 { |
| 470 SCOPED_TRACE("Expect equivalent metadata_by_id_ contents."); |
| 471 ExpectEquivalent(index1->metadata_by_id_, index2->metadata_by_id_); |
| 472 } |
| 473 { |
| 474 SCOPED_TRACE("Expect equivalent tracker_by_id_ contents."); |
| 475 ExpectEquivalent(index1->tracker_by_id_, index2->tracker_by_id_); |
| 476 } |
| 477 { |
| 478 SCOPED_TRACE("Expect equivalent trackers_by_file_id_ contents."); |
| 479 ExpectEquivalent(index1->trackers_by_file_id_, |
| 480 index2->trackers_by_file_id_); |
| 481 } |
| 482 { |
| 483 SCOPED_TRACE("Expect equivalent app_root_by_app_id_ contents."); |
| 484 ExpectEquivalent(index1->app_root_by_app_id_, |
| 485 index2->app_root_by_app_id_); |
| 486 } |
| 487 { |
| 488 SCOPED_TRACE("Expect equivalent trackers_by_parent_and_title_ contents."); |
| 489 ExpectEquivalent(index1->trackers_by_parent_and_title_, |
| 490 index2->trackers_by_parent_and_title_); |
| 491 } |
| 492 { |
| 493 SCOPED_TRACE("Expect equivalent dirty_trackers_ contents."); |
| 494 ExpectEquivalent(index1->dirty_trackers_, index2->dirty_trackers_); |
| 495 } |
| 496 } |
| 497 |
| 498 void VerifyReloadConsistencyForOnDisk( |
| 499 MetadataDatabaseIndexOnDisk* index1, |
| 500 MetadataDatabaseIndexOnDisk* index2) { |
| 501 ExpectEquivalentServiceMetadata(index1, index2); |
| 502 scoped_ptr<LevelDBWrapper::Iterator> itr1 = |
| 503 index1->GetDBForTesting()->NewIterator(); |
| 504 scoped_ptr<LevelDBWrapper::Iterator> itr2 = |
| 505 index2->GetDBForTesting()->NewIterator(); |
| 506 for (itr1->SeekToFirst(), itr2->SeekToFirst(); |
| 507 itr1->Valid() && itr2->Valid(); |
| 508 itr1->Next(), itr2->Next()) { |
| 509 EXPECT_EQ(itr1->key().ToString(), itr2->key().ToString()); |
| 510 EXPECT_EQ(itr1->value().ToString(), itr2->value().ToString()); |
| 511 } |
| 512 EXPECT_TRUE(!itr1->Valid()); |
| 513 EXPECT_TRUE(!itr2->Valid()); |
| 514 } |
| 515 |
| 465 void VerifyReloadConsistency() { | 516 void VerifyReloadConsistency() { |
| 466 scoped_ptr<MetadataDatabase> metadata_database_2; | 517 scoped_ptr<MetadataDatabase> metadata_database_2; |
| 467 ASSERT_EQ(SYNC_STATUS_OK, | 518 ASSERT_EQ(SYNC_STATUS_OK, |
| 468 MetadataDatabase::CreateForTesting( | 519 MetadataDatabase::CreateForTesting( |
| 469 metadata_database_->db_.Pass(), | 520 metadata_database_->db_.Pass(), |
| 521 metadata_database_->enable_on_disk_index_, |
| 470 &metadata_database_2)); | 522 &metadata_database_2)); |
| 471 metadata_database_->db_ = metadata_database_2->db_.Pass(); | 523 metadata_database_->db_ = metadata_database_2->db_.Pass(); |
| 472 | 524 |
| 473 const MetadataDatabaseIndex* on_memory = | 525 MetadataDatabaseIndexInterface* index1 = metadata_database_->index_.get(); |
| 474 static_cast<MetadataDatabaseIndex*>(metadata_database_->index_.get()); | 526 MetadataDatabaseIndexInterface* index2 = metadata_database_2->index_.get(); |
| 475 const MetadataDatabaseIndex* reloaded = | 527 if (metadata_database_->enable_on_disk_index_) { |
| 476 static_cast<MetadataDatabaseIndex*>(metadata_database_2->index_.get()); | 528 VerifyReloadConsistencyForOnDisk( |
| 477 | 529 static_cast<MetadataDatabaseIndexOnDisk*>(index1), |
| 478 { | 530 static_cast<MetadataDatabaseIndexOnDisk*>(index2)); |
| 479 SCOPED_TRACE("Expect equivalent service_metadata"); | 531 } else { |
| 480 ExpectEquivalentServiceMetadata(metadata_database_->index_.get(), | 532 VerifyReloadConsistencyForOnMemory( |
| 481 metadata_database_2->index_.get()); | 533 static_cast<MetadataDatabaseIndex*>(index1), |
| 482 } | 534 static_cast<MetadataDatabaseIndex*>(index2)); |
| 483 | |
| 484 { | |
| 485 SCOPED_TRACE("Expect equivalent metadata_by_id_ contents."); | |
| 486 ExpectEquivalent(on_memory->metadata_by_id_, | |
| 487 reloaded->metadata_by_id_); | |
| 488 } | |
| 489 | |
| 490 { | |
| 491 SCOPED_TRACE("Expect equivalent tracker_by_id_ contents."); | |
| 492 ExpectEquivalent(on_memory->tracker_by_id_, | |
| 493 reloaded->tracker_by_id_); | |
| 494 } | |
| 495 | |
| 496 { | |
| 497 SCOPED_TRACE("Expect equivalent trackers_by_file_id_ contents."); | |
| 498 ExpectEquivalent(on_memory->trackers_by_file_id_, | |
| 499 reloaded->trackers_by_file_id_); | |
| 500 } | |
| 501 | |
| 502 { | |
| 503 SCOPED_TRACE("Expect equivalent app_root_by_app_id_ contents."); | |
| 504 ExpectEquivalent(on_memory->app_root_by_app_id_, | |
| 505 reloaded->app_root_by_app_id_); | |
| 506 } | |
| 507 | |
| 508 { | |
| 509 SCOPED_TRACE("Expect equivalent trackers_by_parent_and_title_ contents."); | |
| 510 ExpectEquivalent(on_memory->trackers_by_parent_and_title_, | |
| 511 reloaded->trackers_by_parent_and_title_); | |
| 512 } | |
| 513 | |
| 514 { | |
| 515 SCOPED_TRACE("Expect equivalent dirty_trackers_ contents."); | |
| 516 ExpectEquivalent(on_memory->dirty_trackers_, | |
| 517 reloaded->dirty_trackers_); | |
| 518 } | 535 } |
| 519 } | 536 } |
| 520 | 537 |
| 521 void VerifyFile(const FileMetadata& file) { | 538 void VerifyFile(const FileMetadata& file) { |
| 522 FileMetadata file_in_metadata_database; | 539 FileMetadata file_in_metadata_database; |
| 523 ASSERT_TRUE(metadata_database()->FindFileByFileID( | 540 ASSERT_TRUE(metadata_database()->FindFileByFileID( |
| 524 file.file_id(), &file_in_metadata_database)); | 541 file.file_id(), &file_in_metadata_database)); |
| 525 | 542 |
| 526 SCOPED_TRACE("Expect equivalent " + file.file_id()); | 543 SCOPED_TRACE("Expect equivalent " + file.file_id()); |
| 527 ExpectEquivalent(&file, &file_in_metadata_database); | 544 ExpectEquivalent(&file, &file_in_metadata_database); |
| (...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1167 EXPECT_TRUE(file->HasKey("details")); | 1184 EXPECT_TRUE(file->HasKey("details")); |
| 1168 | 1185 |
| 1169 ASSERT_TRUE(files->GetDictionary(1, &file)); | 1186 ASSERT_TRUE(files->GetDictionary(1, &file)); |
| 1170 EXPECT_TRUE(file->GetString("title", &str) && str == "file_0"); | 1187 EXPECT_TRUE(file->GetString("title", &str) && str == "file_0"); |
| 1171 EXPECT_TRUE(file->GetString("type", &str) && str == "file"); | 1188 EXPECT_TRUE(file->GetString("type", &str) && str == "file"); |
| 1172 EXPECT_TRUE(file->HasKey("details")); | 1189 EXPECT_TRUE(file->HasKey("details")); |
| 1173 } | 1190 } |
| 1174 | 1191 |
| 1175 } // namespace drive_backend | 1192 } // namespace drive_backend |
| 1176 } // namespace sync_file_system | 1193 } // namespace sync_file_system |
| OLD | NEW |