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.
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_
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"
19 class SequencedTaskRunner
;
30 class ResourceMetadataHeader
;
34 // Storage for ResourceMetadata which is responsible to manage resource
35 // entries and child-parent relationships between entries.
36 class ResourceMetadataStorage
{
38 // This should be incremented when incompatibility change is made to DB
40 static const int kDBVersion
= 12;
42 // Object to iterate over entries stored in this storage.
45 explicit Iterator(scoped_ptr
<leveldb::Iterator
> it
);
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.
52 // Returns the ID of the entry currently pointed by this object.
53 std::string
GetID() const;
55 // Returns the entry currently pointed by this object.
56 const ResourceEntry
& GetValue() const;
58 // Gets the cache entry which corresponds to |entry_| if available.
59 bool GetCacheEntry(FileCacheEntry
* cache_entry
);
61 // Advances to the next entry.
64 // Returns true if this object has encountered any error.
65 bool HasError() const;
69 scoped_ptr
<leveldb::Iterator
> it_
;
71 DISALLOW_COPY_AND_ASSIGN(Iterator
);
74 // Object to iterate over cache entries stored in this storage.
75 class CacheEntryIterator
{
77 explicit CacheEntryIterator(scoped_ptr
<leveldb::Iterator
> it
);
78 ~CacheEntryIterator();
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
85 // Returns the ID of the entry currently pointed by this object.
86 const std::string
& GetID() const;
88 // Returns the value of the entry currently pointed by this object.
89 const FileCacheEntry
& GetValue() const;
91 // Advances to the next entry.
94 // Returns true if this object has encountered any error.
95 bool HasError() const;
98 // Used to implement Advance().
99 void AdvanceInternal();
101 scoped_ptr
<leveldb::Iterator
> it_
;
103 FileCacheEntry entry_
;
105 DISALLOW_COPY_AND_ASSIGN(CacheEntryIterator
);
108 // Cache information recovered from trashed DB.
109 struct RecoveredCacheInfo
{
110 RecoveredCacheInfo();
111 ~RecoveredCacheInfo();
117 typedef std::map
<std::string
, RecoveredCacheInfo
> RecoveredCacheInfoMap
;
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
);
123 ResourceMetadataStorage(const base::FilePath
& directory_path
,
124 base::SequencedTaskRunner
* blocking_task_runner
);
126 const base::FilePath
& directory_path() const { return directory_path_
; }
128 // Returns true when cache entries were not loaded to the DB during
130 bool cache_file_scan_is_needed() const { return cache_file_scan_is_needed_
; }
132 // Destroys this object.
135 // Initializes this object.
138 // Collects cache info from trashed resource map DB.
139 void RecoverCacheInfoFromTrashedResourceMap(RecoveredCacheInfoMap
* out_info
);
141 // Sets the largest changestamp.
142 bool SetLargestChangestamp(int64 largest_changestamp
);
144 // Gets the largest changestamp.
145 int64
GetLargestChangestamp();
147 // Puts the entry to this storage.
148 bool PutEntry(const ResourceEntry
& entry
);
150 // Gets an entry stored in this storage.
151 bool GetEntry(const std::string
& id
, ResourceEntry
* out_entry
);
153 // Removes an entry from this storage.
154 bool RemoveEntry(const std::string
& id
);
156 // Returns an object to iterate over entries stored in this storage.
157 scoped_ptr
<Iterator
> GetIterator();
159 // Returns the ID of the parent's child.
160 std::string
GetChild(const std::string
& parent_id
,
161 const std::string
& child_name
);
163 // Returns the IDs of the parent's children.
164 void GetChildren(const std::string
& parent_id
,
165 std::vector
<std::string
>* children
);
167 // Puts the cache entry to this storage.
168 bool PutCacheEntry(const std::string
& id
, const FileCacheEntry
& entry
);
170 // Gets a cache entry stored in this storage.
171 bool GetCacheEntry(const std::string
& id
, FileCacheEntry
* out_entry
);
173 // Removes a cache entry from this storage.
174 bool RemoveCacheEntry(const std::string
& id
);
176 // Returns an object to iterate over cache entries stored in this storage.
177 scoped_ptr
<CacheEntryIterator
> GetCacheEntryIterator();
179 // Returns the local ID associated with the given resource ID.
180 bool GetIdByResourceId(const std::string
& resource_id
, std::string
* out_id
);
183 friend class ResourceMetadataStorageTest
;
185 // To destruct this object, use Destroy().
186 ~ResourceMetadataStorage();
188 // Used to implement Destroy().
189 void DestroyOnBlockingPool();
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
);
196 bool PutHeader(const ResourceMetadataHeader
& header
);
199 bool GetHeader(ResourceMetadataHeader
* out_header
);
201 // Checks validity of the data.
202 bool CheckValidity();
204 // Path to the directory where the data is stored.
205 base::FilePath directory_path_
;
207 bool cache_file_scan_is_needed_
;
209 // Entries stored in this storage.
210 scoped_ptr
<leveldb::DB
> resource_map_
;
212 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
214 DISALLOW_COPY_AND_ASSIGN(ResourceMetadataStorage
);
217 } // namespace internal
220 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_