resource_metadata_storage.h revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_ 6#define CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/basictypes.h" 12#include "base/files/file_path.h" 13#include "base/memory/ref_counted.h" 14#include "base/memory/scoped_ptr.h" 15#include "chrome/browser/chromeos/drive/drive.pb.h" 16 17namespace base { 18class SequencedTaskRunner; 19} 20 21namespace leveldb { 22class DB; 23class Iterator; 24} 25 26namespace drive { 27 28class ResourceEntry; 29class ResourceMetadataHeader; 30 31namespace internal { 32 33// Storage for ResourceMetadata which is responsible to manage resource 34// entries and child-parent relationships between entries. 35class ResourceMetadataStorage { 36 public: 37 // This should be incremented when incompatibility change is made to DB 38 // format. 39 static const int kDBVersion = 11; 40 41 // Object to iterate over entries stored in this storage. 42 class Iterator { 43 public: 44 explicit Iterator(scoped_ptr<leveldb::Iterator> it); 45 ~Iterator(); 46 47 // Returns true if this iterator cannot advance any more and does not point 48 // to a valid entry. Get() and Advance() should not be called in such cases. 49 bool IsAtEnd() const; 50 51 // Returns the ID of the entry currently pointed by this object. 52 std::string GetID() const; 53 54 // Returns the entry currently pointed by this object. 55 const ResourceEntry& GetValue() const; 56 57 // Gets the cache entry which corresponds to |entry_| if available. 58 bool GetCacheEntry(FileCacheEntry* cache_entry); 59 60 // Advances to the next entry. 61 void Advance(); 62 63 // Returns true if this object has encountered any error. 64 bool HasError() const; 65 66 private: 67 ResourceEntry entry_; 68 scoped_ptr<leveldb::Iterator> it_; 69 70 DISALLOW_COPY_AND_ASSIGN(Iterator); 71 }; 72 73 // Object to iterate over cache entries stored in this storage. 74 class CacheEntryIterator { 75 public: 76 explicit CacheEntryIterator(scoped_ptr<leveldb::Iterator> it); 77 ~CacheEntryIterator(); 78 79 // Returns true if this iterator cannot advance any more and does not point 80 // to a valid entry. GetID(), GetValue() and Advance() should not be called 81 // in such cases. 82 bool IsAtEnd() const; 83 84 // Returns the ID of the entry currently pointed by this object. 85 const std::string& GetID() const; 86 87 // Returns the value of the entry currently pointed by this object. 88 const FileCacheEntry& GetValue() const; 89 90 // Advances to the next entry. 91 void Advance(); 92 93 // Returns true if this object has encountered any error. 94 bool HasError() const; 95 96 private: 97 // Used to implement Advance(). 98 void AdvanceInternal(); 99 100 scoped_ptr<leveldb::Iterator> it_; 101 std::string id_; 102 FileCacheEntry entry_; 103 104 DISALLOW_COPY_AND_ASSIGN(CacheEntryIterator); 105 }; 106 107 ResourceMetadataStorage(const base::FilePath& directory_path, 108 base::SequencedTaskRunner* blocking_task_runner); 109 110 const base::FilePath& directory_path() const { return directory_path_; } 111 112 // Returns true when cache entries were not loaded to the DB during 113 // initialization. 114 bool cache_file_scan_is_needed() const { return cache_file_scan_is_needed_; } 115 116 // Destroys this object. 117 void Destroy(); 118 119 // Initializes this object. 120 bool Initialize(); 121 122 // Sets the largest changestamp. 123 bool SetLargestChangestamp(int64 largest_changestamp); 124 125 // Gets the largest changestamp. 126 int64 GetLargestChangestamp(); 127 128 // Puts the entry to this storage. 129 bool PutEntry(const ResourceEntry& entry); 130 131 // Gets an entry stored in this storage. 132 bool GetEntry(const std::string& id, ResourceEntry* out_entry); 133 134 // Removes an entry from this storage. 135 bool RemoveEntry(const std::string& id); 136 137 // Returns an object to iterate over entries stored in this storage. 138 scoped_ptr<Iterator> GetIterator(); 139 140 // Returns the ID of the parent's child. 141 std::string GetChild(const std::string& parent_id, 142 const std::string& child_name); 143 144 // Returns the IDs of the parent's children. 145 void GetChildren(const std::string& parent_id, 146 std::vector<std::string>* children); 147 148 // Puts the cache entry to this storage. 149 bool PutCacheEntry(const std::string& id, const FileCacheEntry& entry); 150 151 // Gets a cache entry stored in this storage. 152 bool GetCacheEntry(const std::string& id, FileCacheEntry* out_entry); 153 154 // Removes a cache entry from this storage. 155 bool RemoveCacheEntry(const std::string& id); 156 157 // Returns an object to iterate over cache entries stored in this storage. 158 scoped_ptr<CacheEntryIterator> GetCacheEntryIterator(); 159 160 // Returns the local ID associated with the given resource ID. 161 bool GetIdByResourceId(const std::string& resource_id, std::string* out_id); 162 163 private: 164 friend class ResourceMetadataStorageTest; 165 166 // To destruct this object, use Destroy(). 167 ~ResourceMetadataStorage(); 168 169 // Used to implement Destroy(). 170 void DestroyOnBlockingPool(); 171 172 // Returns a string to be used as a key for child entry. 173 static std::string GetChildEntryKey(const std::string& parent_id, 174 const std::string& child_name); 175 176 // Puts header. 177 bool PutHeader(const ResourceMetadataHeader& header); 178 179 // Gets header. 180 bool GetHeader(ResourceMetadataHeader* out_header); 181 182 // Checks validity of the data. 183 bool CheckValidity(); 184 185 // Path to the directory where the data is stored. 186 base::FilePath directory_path_; 187 188 bool cache_file_scan_is_needed_; 189 190 // Entries stored in this storage. 191 scoped_ptr<leveldb::DB> resource_map_; 192 193 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; 194 195 DISALLOW_COPY_AND_ASSIGN(ResourceMetadataStorage); 196}; 197 198} // namespace internal 199} // namespace drive 200 201#endif // CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_ 202