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