metadata_database.h revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_H_ 6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_H_ 7 8#include <map> 9#include <set> 10#include <string> 11#include <vector> 12 13#include "base/callback_forward.h" 14#include "base/memory/scoped_ptr.h" 15#include "base/memory/scoped_vector.h" 16#include "base/memory/weak_ptr.h" 17#include "chrome/browser/sync_file_system/drive_backend/tracker_set.h" 18#include "chrome/browser/sync_file_system/sync_callbacks.h" 19#include "chrome/browser/sync_file_system/sync_status_code.h" 20 21namespace base { 22class FilePath; 23class SequencedTaskRunner; 24class SingleThreadTaskRunner; 25} 26 27namespace leveldb { 28class DB; 29class WriteBatch; 30} 31 32namespace google_apis { 33class ChangeResource; 34class FileResource; 35class ResourceEntry; 36} 37 38namespace sync_file_system { 39namespace drive_backend { 40 41class FileDetails; 42class FileMetadata; 43class FileTracker; 44class ServiceMetadata; 45struct DatabaseContents; 46 47// MetadataDatabase holds and maintains a LevelDB instance and its indexes, 48// which holds 1)ServiceMetadata, 2)FileMetadata and 3)FileTracker. 49// 1) ServiceMetadata is a singleton in the database which holds information for 50// the backend. 51// 2) FileMetadata represents a remote-side file and holds latest known 52// metadata of the remote file. 53// 3) FileTracker represents a synced or to-be-synced file and maintains 54// the local-side folder tree. 55// 56// The term "file" includes files, folders and other resources on Drive. 57// 58// FileTrackers form a tree structure on the database, which represents the 59// FileSystem trees of SyncFileSystem. The tree has a FileTracker named 60// sync-root as its root node, and a set of FileTracker named app-root. An 61// app-root represents a remote folder for an installed Chrome App and holds all 62// synced contents for the App. 63// 64// One FileMetadata is created for each tracked remote file, which is identified 65// by FileID. 66// One FileTracker is created for every different {parent tracker, FileID} pair, 67// excluding non-app-root inactive parent trackers. Multiple trackers may be 68// associated to one FileID when the file has multiple parents. Multiple 69// trackers may have the same {parent tracker, title} pair when the associated 70// remote files have the same title. 71// 72// Files have following state: 73// - Unknown file 74// - Has a dirty inactive tracker and empty synced_details. 75// - Is initial state of a tracker, only file_id and parent_tracker_id field 76// are known. 77// - Folder 78// - Is either one of sync-root folder, app-root folder or a regular folder. 79// - Sync-root folder holds app-root folders as its direct children, and 80// holds entire SyncFileSystem files as its descentants. Its tracker 81// should be stored in ServiceMetadata by its tracker_id. 82// - App-root folder holds all files for an application as its descendants. 83// - File 84// - Unsupported file 85// - Represents unsupported files such as hosted documents. Must be 86// inactive. 87// 88// Invariants: 89// - Any tracker in the database must either: 90// - be sync-root, 91// - have an app-root as its parent tracker, or 92// - have an active tracker as its parent. 93// That is, all trackers must be reachable from sync-root via app-root folders 94// and active trackers. 95// 96// - Any active tracker must either: 97// - have |needs_folder_listing| flag and dirty flag, or 98// - have all children at the stored largest change ID. 99// 100// - If multiple trackers have the same parent tracker and same title, they 101// must not have same |file_id|, and at most one of them may be active. 102// - If multiple trackers have the same |file_id|, at most one of them may be 103// active. 104// 105class MetadataDatabase { 106 public: 107 typedef std::map<std::string, FileMetadata*> FileByID; 108 typedef std::map<int64, FileTracker*> TrackerByID; 109 typedef std::map<std::string, TrackerSet> TrackersByFileID; 110 typedef std::map<std::string, TrackerSet> TrackersByTitle; 111 typedef std::map<int64, TrackersByTitle> TrackersByParentAndTitle; 112 typedef std::map<std::string, FileTracker*> TrackerByAppID; 113 typedef std::vector<std::string> FileIDList; 114 115 typedef base::Callback< 116 void(SyncStatusCode status, scoped_ptr<MetadataDatabase> instance)> 117 CreateCallback; 118 119 // The entry point of the MetadataDatabase for production code. 120 static void Create(base::SequencedTaskRunner* task_runner, 121 const base::FilePath& database_path, 122 const CreateCallback& callback); 123 ~MetadataDatabase(); 124 125 int64 GetLargestChangeID() const; 126 127 // Registers existing folder as the app-root for |app_id|. The folder 128 // must be an inactive folder that does not yet associated to any App. 129 // This method associates the folder with |app_id| and activates it. 130 void RegisterApp(const std::string& app_id, 131 const std::string& folder_id, 132 const SyncStatusCallback& callback); 133 134 // Inactivates the folder associated to the app to disable |app_id|. 135 // Does nothing if |app_id| is already disabled. 136 void DisableApp(const std::string& app_id, 137 const SyncStatusCallback& callback); 138 139 // Activates the folder associated to |app_id| to enable |app_id|. 140 // Does nothing if |app_id| is already enabled. 141 void EnableApp(const std::string& app_id, 142 const SyncStatusCallback& callback); 143 144 // Unregisters the folder as the app-root for |app_id|. If |app_id| does not 145 // exist, does nothing. The folder is left as an inactive regular folder. 146 // Note that the inactivation drops all descendant files since they are no 147 // longer reachable from sync-root via active folder or app-root. 148 void UnregisterApp(const std::string& app_id, 149 const SyncStatusCallback& callback); 150 151 // Finds the app-root folder for |app_id|. Returns true if exists. 152 // Copies the result to |tracker| if it is non-NULL. 153 bool FindAppRootTracker(const std::string& app_id, 154 FileTracker* tracker) const; 155 156 // Finds the file identified by |file_id|. Returns true if the file is found. 157 // Copies the metadata identified by |file_id| into |file| if exists and 158 // |file| is non-NULL. 159 bool FindFileByFileID(const std::string& file_id, FileMetadata* file) const; 160 161 // Finds the tracker identified by |tracker_id|. Returns true if the tracker 162 // is found. 163 // Copies the tracker identified by |tracker_id| into |tracker| if exists and 164 // |tracker| is non-NULL. 165 bool FindTrackerByTrackerID(int64 tracker_id, FileTracker* tracker) const; 166 167 // Finds the trackers tracking |file_id|. Returns true if the trackers are 168 // found. 169 bool FindTrackersByFileID(const std::string& file_id, 170 TrackerSet* trackers) const; 171 172 // Finds the set of trackers whose parent's tracker ID is |parent_tracker_id|, 173 // and who has |title| as its title in the synced_details. 174 // Copies the tracker set to |trackers| if it is non-NULL. 175 size_t FindTrackersByParentAndTitle( 176 int64 parent_tracker_id, 177 const std::string& title, 178 TrackerSet* trackers) const; 179 180 // Builds the file path for the given tracker. Returns true on success. 181 // |path| can be NULL. 182 // The file path is relative to app-root and have a leading path separator. 183 bool BuildPathForTracker(int64 tracker_id, base::FilePath* path) const; 184 185 // Updates database by |changes|. 186 // Marks each tracker for modified file as dirty and adds new trackers if 187 // needed. 188 void UpdateByChangeList(ScopedVector<google_apis::ChangeResource> changes, 189 const SyncStatusCallback& callback); 190 191 // Adds |child_file_ids| to |folder_id| as its children. 192 // This method affects the active tracker only. 193 // If the tracker has no further change to sync, unmarks its dirty flag. 194 void PopulateFolder(const std::string& folder_id, 195 const FileIDList& child_file_ids, 196 const SyncStatusCallback& callback); 197 198 private: 199 struct DirtyTrackerComparator { 200 bool operator()(const FileTracker* left, 201 const FileTracker* right) const; 202 }; 203 204 typedef std::set<FileTracker*, DirtyTrackerComparator> DirtyTrackers; 205 206 friend class MetadataDatabaseTest; 207 208 explicit MetadataDatabase(base::SequencedTaskRunner* task_runner); 209 static void CreateOnTaskRunner(base::SingleThreadTaskRunner* callback_runner, 210 base::SequencedTaskRunner* task_runner, 211 const base::FilePath& database_path, 212 const CreateCallback& callback); 213 static SyncStatusCode CreateForTesting( 214 scoped_ptr<leveldb::DB> db, 215 scoped_ptr<MetadataDatabase>* metadata_database_out); 216 SyncStatusCode InitializeOnTaskRunner(const base::FilePath& database_path); 217 void BuildIndexes(DatabaseContents* contents); 218 219 // Database manipulation methods. 220 void RegisterTrackerAsAppRoot(const std::string& app_id, 221 int64 tracker_id, 222 leveldb::WriteBatch* batch); 223 void MakeTrackerActive(int64 tracker_id, leveldb::WriteBatch* batch); 224 void MakeTrackerInactive(int64 tracker_id, leveldb::WriteBatch* batch); 225 void MakeAppRootDisabled(int64 tracker_id, leveldb::WriteBatch* batch); 226 void MakeAppRootEnabled(int64 tracker_id, leveldb::WriteBatch* batch); 227 228 void UnregisterTrackerAsAppRoot(const std::string& app_id, 229 leveldb::WriteBatch* batch); 230 void RemoveAllDescendantTrackers(int64 root_tracker_id, 231 leveldb::WriteBatch* batch); 232 233 void CreateTrackerForParentAndFileID(const FileTracker& parent_tracker, 234 const std::string& file_id, 235 leveldb::WriteBatch* batch); 236 void RemoveTrackerIgnoringSiblings(int64 tracker_id, 237 leveldb::WriteBatch* batch); 238 void MaybeAddTrackersForNewFile(const FileMetadata& file, 239 leveldb::WriteBatch* batch); 240 241 void MarkTrackerSetDirty(TrackerSet* trackers, 242 leveldb::WriteBatch* batch); 243 void MarkTrackersDirtyByFileID(const std::string& file_id, 244 leveldb::WriteBatch* batch); 245 void MarkTrackersDirtyByPath(int64 parent_tracker_id, 246 const std::string& title, 247 leveldb::WriteBatch* batch); 248 249 void EraseTrackerFromFileIDIndex(FileTracker* tracker, 250 leveldb::WriteBatch* batch); 251 void EraseTrackerFromPathIndex(FileTracker* tracker); 252 void EraseFileFromDatabase(const std::string& file_id, 253 leveldb::WriteBatch* batch); 254 255 int64 GetNextTrackerID(leveldb::WriteBatch* batch); 256 257 void RecursiveMarkTrackerAsDirty(int64 root_tracker_id, 258 leveldb::WriteBatch* batch); 259 bool ShouldKeepDirty(const FileTracker& tracker) const; 260 261 void WriteToDatabase(scoped_ptr<leveldb::WriteBatch> batch, 262 const SyncStatusCallback& callback); 263 264 scoped_refptr<base::SequencedTaskRunner> task_runner_; 265 scoped_ptr<leveldb::DB> db_; 266 267 scoped_ptr<ServiceMetadata> service_metadata_; 268 269 FileByID file_by_id_; // Owned. 270 TrackerByID tracker_by_id_; // Owned. 271 272 // Maps FileID to trackers. The active tracker must be unique per FileID. 273 // This must be updated when updating |active| field of a tracker. 274 TrackersByFileID trackers_by_file_id_; // Not owned. 275 276 // Maps AppID to the app-root tracker. 277 // This must be updated when a tracker is registered/unregistered as an 278 // app-root. 279 TrackerByAppID app_root_by_app_id_; // Not owned. 280 281 // Maps |tracker_id| to its children grouped by their |title|. 282 // If the title is unknown for a tracker, treats its title as empty. Empty 283 // titled file must not be active. 284 // The active tracker must be unique per its parent_tracker and its title. 285 // This must be updated when updating |title|, |active| or 286 // |parent_tracker_id|. 287 TrackersByParentAndTitle trackers_by_parent_and_title_; 288 289 // Holds all trackers which marked as dirty. 290 // This must be updated when updating |dirty| field of a tracker. 291 DirtyTrackers dirty_trackers_; // Not owned. 292 293 base::WeakPtrFactory<MetadataDatabase> weak_ptr_factory_; 294 295 DISALLOW_COPY_AND_ASSIGN(MetadataDatabase); 296}; 297 298} // namespace drive_backend 299} // namespace sync_file_system 300 301#endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_H_ 302