1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_INDEX_ON_DISK_H_ 6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_INDEX_ON_DISK_H_ 7 8#include <map> 9#include <set> 10#include <string> 11#include <vector> 12 13#include "chrome/browser/sync_file_system/drive_backend/metadata_database_index_interface.h" 14#include "chrome/browser/sync_file_system/drive_backend/tracker_id_set.h" 15 16namespace sync_file_system { 17namespace drive_backend { 18 19class FileMetadata; 20class FileTracker; 21class LevelDBWrapper; 22class ServiceMetadata; 23struct DatabaseContents; 24// TODO(peria): Migrate implementation of ParentIDAndTitle structure from 25// metadata_database_index.{cc,h} to here, on removing the files. 26struct ParentIDAndTitle; 27 28// Maintains indexes of MetadataDatabase on disk. 29class MetadataDatabaseIndexOnDisk : public MetadataDatabaseIndexInterface { 30 public: 31 static scoped_ptr<MetadataDatabaseIndexOnDisk> Create(LevelDBWrapper* db); 32 33 virtual ~MetadataDatabaseIndexOnDisk(); 34 35 // MetadataDatabaseIndexInterface overrides. 36 virtual bool GetFileMetadata( 37 const std::string& file_id, FileMetadata* metadata) const OVERRIDE; 38 virtual bool GetFileTracker( 39 int64 tracker_id, FileTracker* tracker) const OVERRIDE; 40 virtual void StoreFileMetadata(scoped_ptr<FileMetadata> metadata) OVERRIDE; 41 virtual void StoreFileTracker(scoped_ptr<FileTracker> tracker) OVERRIDE; 42 virtual void RemoveFileMetadata(const std::string& file_id) OVERRIDE; 43 virtual void RemoveFileTracker(int64 tracker_id) OVERRIDE; 44 virtual TrackerIDSet GetFileTrackerIDsByFileID( 45 const std::string& file_id) const OVERRIDE; 46 virtual int64 GetAppRootTracker(const std::string& app_id) const OVERRIDE; 47 virtual TrackerIDSet GetFileTrackerIDsByParentAndTitle( 48 int64 parent_tracker_id, const std::string& title) const OVERRIDE; 49 virtual std::vector<int64> GetFileTrackerIDsByParent( 50 int64 parent_tracker_id) const OVERRIDE; 51 virtual std::string PickMultiTrackerFileID() const OVERRIDE; 52 virtual ParentIDAndTitle PickMultiBackingFilePath() const OVERRIDE; 53 virtual int64 PickDirtyTracker() const OVERRIDE; 54 virtual void DemoteDirtyTracker(int64 tracker_id) OVERRIDE; 55 virtual bool HasDemotedDirtyTracker() const OVERRIDE; 56 virtual void PromoteDemotedDirtyTracker(int64 tracker_id) OVERRIDE; 57 virtual bool PromoteDemotedDirtyTrackers() OVERRIDE; 58 virtual size_t CountDirtyTracker() const OVERRIDE; 59 virtual size_t CountFileMetadata() const OVERRIDE; 60 virtual size_t CountFileTracker() const OVERRIDE; 61 virtual void SetSyncRootTrackerID(int64 sync_root_id) const OVERRIDE; 62 virtual void SetLargestChangeID(int64 largest_change_id) const OVERRIDE; 63 virtual void SetNextTrackerID(int64 next_tracker_id) const OVERRIDE; 64 virtual int64 GetSyncRootTrackerID() const OVERRIDE; 65 virtual int64 GetLargestChangeID() const OVERRIDE; 66 virtual int64 GetNextTrackerID() const OVERRIDE; 67 virtual std::vector<std::string> GetRegisteredAppIDs() const OVERRIDE; 68 virtual std::vector<int64> GetAllTrackerIDs() const OVERRIDE; 69 virtual std::vector<std::string> GetAllMetadataIDs() const OVERRIDE; 70 71 // Builds on-disk indexes from FileTracker entries on disk. 72 // Returns the number of newly added entries for indexing. 73 int64 BuildTrackerIndexes(); 74 75 // Deletes entries used for indexes on on-disk database. 76 // Returns the number of the deleted entries. 77 int64 DeleteTrackerIndexes(); 78 79 LevelDBWrapper* GetDBForTesting(); 80 81 private: 82 enum NumEntries { 83 NONE, // No entries are found. 84 SINGLE, // One entry is found. 85 MULTIPLE, // Two or more entires are found. 86 }; 87 88 explicit MetadataDatabaseIndexOnDisk(LevelDBWrapper* db); 89 90 // Maintain indexes from AppIDs to tracker IDs. 91 void AddToAppIDIndex(const FileTracker& new_tracker); 92 void UpdateInAppIDIndex(const FileTracker& old_tracker, 93 const FileTracker& new_tracker); 94 void RemoveFromAppIDIndex(const FileTracker& tracker); 95 96 // Maintain indexes from remote file IDs to tracker ID sets. 97 void AddToFileIDIndexes(const FileTracker& new_tracker); 98 void UpdateInFileIDIndexes(const FileTracker& old_tracker, 99 const FileTracker& new_tracker); 100 void RemoveFromFileIDIndexes(const FileTracker& tracker); 101 102 // Maintain indexes from path indexes to tracker ID sets 103 void AddToPathIndexes(const FileTracker& new_tracker); 104 void UpdateInPathIndexes(const FileTracker& old_tracker, 105 const FileTracker& new_tracker); 106 void RemoveFromPathIndexes(const FileTracker& tracker); 107 108 // Maintain dirty tracker IDs. 109 void AddToDirtyTrackerIndexes(const FileTracker& new_tracker); 110 void UpdateInDirtyTrackerIndexes(const FileTracker& old_tracker, 111 const FileTracker& new_tracker); 112 void RemoveFromDirtyTrackerIndexes(const FileTracker& tracker); 113 114 // Returns a TrackerIDSet built from IDs which are found with given key 115 // and key prefix. 116 TrackerIDSet GetTrackerIDSetByPrefix( 117 const std::string& active_tracker_key, 118 const std::string& key_prefix) const; 119 120 121 // Simulate behavior of TrackerIDSet class. 122 123 // Adds an entry with |key_prefix| and tracker ID of |tracker|. If |tracker| 124 // is active, an entry for |active_key| is added. 125 void AddToTrackerIDSetWithPrefix( 126 const std::string& active_tracker_key, 127 const std::string& key_prefix, 128 const FileTracker& tracker); 129 130 // Returns true if |tracker_id| is removed successfully. If no other entries 131 // are stored with |key_prefix|, the entry for |active_key| is also removed. 132 bool EraseInTrackerIDSetWithPrefix( 133 const std::string& active_tracker_key, 134 const std::string& key_prefix, 135 int64 tracker_id); 136 137 // Adds an entry for |active_key| on DB, if |tracker_id| has an entry with 138 // |key_prefix|. 139 void ActivateInTrackerIDSetWithPrefix( 140 const std::string& active_tracker_key, 141 const std::string& key_prefix, 142 int64 tracker_id); 143 144 // Removes an entry for |active_key| on DB, if the value of |active_key| key 145 // is |tracker_id|. 146 void DeactivateInTrackerIDSetWithPrefix( 147 const std::string& active_tracker_key, 148 const std::string& key_prefix, 149 int64 tracker_id); 150 151 // Checks if |db_| has an entry whose key is |key|. 152 bool DBHasKey(const std::string& key); 153 154 // Returns the number of dirty trackers, actually counting them. 155 size_t CountDirtyTrackerInternal() const; 156 157 // Returns the number of entries starting with |prefix| in NumEntries format. 158 // Entries for |ignored_id| are not counted in. 159 NumEntries CountWithPrefix(const std::string& prefix, int64 ignored_id); 160 161 // Deletes entries whose keys start from |prefix|. 162 void DeleteKeyStartsWith(const std::string& prefix); 163 164 LevelDBWrapper* db_; // Not owned. 165 scoped_ptr<ServiceMetadata> service_metadata_; 166 167 size_t num_dirty_trackers_; 168 169 DISALLOW_COPY_AND_ASSIGN(MetadataDatabaseIndexOnDisk); 170}; 171 172} // namespace drive_backend 173} // namespace sync_file_system 174 175#endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_INDEX_ON_DISK_H_ 176