From 7813fa6a3b699996503c96e54087d139d45a65d0 Mon Sep 17 00:00:00 2001 From: "peria@chromium.org" Date: Thu, 17 Jul 2014 11:57:37 +0000 Subject: [PATCH] [SyncFS] Build indexes from FileTracker entries on disk. It is assumed that no indexes exist before. BUG=347425 TEST=./unit_tests --gtest_filter="MetadataDatabaseIndexOnDiskTest.*" Review URL: https://codereview.chromium.org/391623002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283759 0039d316-1c4b-4281-b951-d872f2087c98 --- .../metadata_database_index_on_disk.cc | 21 +++++++++++++++ .../metadata_database_index_on_disk.h | 3 +++ .../metadata_database_index_on_disk_unittest.cc | 31 +++++++++++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk.cc b/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk.cc index a051a25e9dfa..9925445a86ae 100644 --- a/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk.cc +++ b/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk.cc @@ -556,6 +556,27 @@ MetadataDatabaseIndexOnDisk::GetAllMetadataIDs() const { return file_ids; } +void MetadataDatabaseIndexOnDisk::BuildTrackerIndexes( + leveldb::WriteBatch* batch) { + scoped_ptr itr(db_->NewIterator(leveldb::ReadOptions())); + for (itr->Seek(kFileTrackerKeyPrefix); itr->Valid(); itr->Next()) { + if (!RemovePrefix(itr->key().ToString(), kFileTrackerKeyPrefix, NULL)) + break; + + FileTracker tracker; + if (!tracker.ParseFromString(itr->value().ToString())) { + util::Log(logging::LOG_WARNING, FROM_HERE, + "Failed to parse a Tracker"); + continue; + } + + AddToAppIDIndex(tracker, batch); + AddToFileIDIndexes(tracker, batch); + AddToPathIndexes(tracker, batch); + AddToDirtyTrackerIndexes(tracker, batch); + } +} + MetadataDatabaseIndexOnDisk::MetadataDatabaseIndexOnDisk(leveldb::DB* db) : db_(db) { // TODO(peria): Add UMA to measure the number of FileMetadata, FileTracker, diff --git a/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk.h b/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk.h index 7a9b066e8781..72193d19d23f 100644 --- a/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk.h +++ b/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk.h @@ -79,6 +79,9 @@ class MetadataDatabaseIndexOnDisk : public MetadataDatabaseIndexInterface { virtual std::vector GetAllTrackerIDs() const OVERRIDE; virtual std::vector GetAllMetadataIDs() const OVERRIDE; + // Builds on-disk indexes from FileTracker entries on disk. + void BuildTrackerIndexes(leveldb::WriteBatch* batch); + private: enum NumEntries { NONE, // No entries are found. diff --git a/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk_unittest.cc b/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk_unittest.cc index 66cc0064b840..36eeb6675d24 100644 --- a/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk_unittest.cc +++ b/chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk_unittest.cc @@ -183,6 +183,35 @@ TEST_F(MetadataDatabaseIndexOnDiskTest, SetEntryTest) { EXPECT_FALSE(index()->GetFileTracker(tracker_id, NULL)); } +TEST_F(MetadataDatabaseIndexOnDiskTest, BuildIndexTest) { + CreateTestDatabase(false); + + TrackerIDSet tracker_ids; + // Before building indexes, no references exist. + EXPECT_EQ(kInvalidTrackerID, index()->GetAppRootTracker("app_id")); + tracker_ids = index()->GetFileTrackerIDsByFileID("file_id"); + EXPECT_TRUE(tracker_ids.empty()); + tracker_ids = index()->GetFileTrackerIDsByParentAndTitle( + kAppRootTrackerID, "file"); + EXPECT_TRUE(tracker_ids.empty()); + EXPECT_EQ(0U, index()->CountDirtyTracker()); + + scoped_ptr batch(new leveldb::WriteBatch); + index()->BuildTrackerIndexes(batch.get()); + WriteToDB(batch.Pass()); + + // After building indexes, we should have correct indexes. + EXPECT_EQ(kAppRootTrackerID, index()->GetAppRootTracker("app_id")); + tracker_ids = index()->GetFileTrackerIDsByFileID("file_id"); + EXPECT_EQ(1U, tracker_ids.size()); + EXPECT_EQ(kFileTrackerID, tracker_ids.active_tracker()); + tracker_ids = index()->GetFileTrackerIDsByParentAndTitle( + kAppRootTrackerID, "file"); + EXPECT_EQ(1U, tracker_ids.size()); + EXPECT_EQ(kFileTrackerID, tracker_ids.active_tracker()); + EXPECT_EQ(1U, index()->CountDirtyTracker()); +} + TEST_F(MetadataDatabaseIndexOnDiskTest, AllEntriesTest) { CreateTestDatabase(true); @@ -352,7 +381,7 @@ TEST_F(MetadataDatabaseIndexOnDiskTest, TrackerIDSetByParentIDAndTitleTest) { tracker_ids = index()->GetFileTrackerIDsByParentAndTitle( kAppRootTrackerID, "file2"); - EXPECT_EQ(0U, tracker_ids.size()); + EXPECT_TRUE(tracker_ids.empty()); const int64 tracker_id = 72; // Testing AddToFileIDIndexes -- 2.11.4.GIT