resource_metadata_storage.h revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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
18namespace base {
19class SequencedTaskRunner;
20}
21
22namespace leveldb {
23class DB;
24class Iterator;
25}
26
27namespace drive {
28
29class ResourceEntry;
30class ResourceMetadataHeader;
31
32namespace internal {
33
34// Storage for ResourceMetadata which is responsible to manage resource
35// entries and child-parent relationships between entries.
36class ResourceMetadataStorage {
37 public:
38  // This should be incremented when incompatibility change is made to DB
39  // format.
40  static const int kDBVersion = 11;
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  // Returns true if the DB was successfully upgraded to the newest version.
109  static bool UpgradeOldDB(const base::FilePath& directory_path,
110                           const ResourceIdCanonicalizer& id_canonicalizer);
111
112  ResourceMetadataStorage(const base::FilePath& directory_path,
113                          base::SequencedTaskRunner* blocking_task_runner);
114
115  const base::FilePath& directory_path() const { return directory_path_; }
116
117  // Returns true when cache entries were not loaded to the DB during
118  // initialization.
119  bool cache_file_scan_is_needed() const { return cache_file_scan_is_needed_; }
120
121  // Destroys this object.
122  void Destroy();
123
124  // Initializes this object.
125  bool Initialize();
126
127  // Collects FileCacheEntry from trashed resource map DB.
128  void RecoverCacheEntriesFromTrashedResourceMap(
129      std::map<std::string, FileCacheEntry>* out_entries);
130
131  // Sets the largest changestamp.
132  bool SetLargestChangestamp(int64 largest_changestamp);
133
134  // Gets the largest changestamp.
135  int64 GetLargestChangestamp();
136
137  // Puts the entry to this storage.
138  bool PutEntry(const ResourceEntry& entry);
139
140  // Gets an entry stored in this storage.
141  bool GetEntry(const std::string& id, ResourceEntry* out_entry);
142
143  // Removes an entry from this storage.
144  bool RemoveEntry(const std::string& id);
145
146  // Returns an object to iterate over entries stored in this storage.
147  scoped_ptr<Iterator> GetIterator();
148
149  // Returns the ID of the parent's child.
150  std::string GetChild(const std::string& parent_id,
151                       const std::string& child_name);
152
153  // Returns the IDs of the parent's children.
154  void GetChildren(const std::string& parent_id,
155                   std::vector<std::string>* children);
156
157  // Puts the cache entry to this storage.
158  bool PutCacheEntry(const std::string& id, const FileCacheEntry& entry);
159
160  // Gets a cache entry stored in this storage.
161  bool GetCacheEntry(const std::string& id, FileCacheEntry* out_entry);
162
163  // Removes a cache entry from this storage.
164  bool RemoveCacheEntry(const std::string& id);
165
166  // Returns an object to iterate over cache entries stored in this storage.
167  scoped_ptr<CacheEntryIterator> GetCacheEntryIterator();
168
169  // Returns the local ID associated with the given resource ID.
170  bool GetIdByResourceId(const std::string& resource_id, std::string* out_id);
171
172 private:
173  friend class ResourceMetadataStorageTest;
174
175  // To destruct this object, use Destroy().
176  ~ResourceMetadataStorage();
177
178  // Used to implement Destroy().
179  void DestroyOnBlockingPool();
180
181  // Returns a string to be used as a key for child entry.
182  static std::string GetChildEntryKey(const std::string& parent_id,
183                                      const std::string& child_name);
184
185  // Puts header.
186  bool PutHeader(const ResourceMetadataHeader& header);
187
188  // Gets header.
189  bool GetHeader(ResourceMetadataHeader* out_header);
190
191  // Checks validity of the data.
192  bool CheckValidity();
193
194  // Path to the directory where the data is stored.
195  base::FilePath directory_path_;
196
197  bool cache_file_scan_is_needed_;
198
199  // Entries stored in this storage.
200  scoped_ptr<leveldb::DB> resource_map_;
201
202  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
203
204  DISALLOW_COPY_AND_ASSIGN(ResourceMetadataStorage);
205};
206
207}  // namespace internal
208}  // namespace drive
209
210#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_
211