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