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 COMPONENTS_DRIVE_RESOURCE_METADATA_STORAGE_H_
6 #define COMPONENTS_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 "components/drive/drive.pb.h"
16 #include "components/drive/file_errors.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
= 13;
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 // Advances to the next entry.
61 // Returns true if this object has encountered any error.
62 bool HasError() const;
66 scoped_ptr
<leveldb::Iterator
> it_
;
68 DISALLOW_COPY_AND_ASSIGN(Iterator
);
71 // Cache information recovered from trashed DB.
72 struct RecoveredCacheInfo
{
74 ~RecoveredCacheInfo();
80 typedef std::map
<std::string
, RecoveredCacheInfo
> RecoveredCacheInfoMap
;
82 // Returns true if the DB was successfully upgraded to the newest version.
83 static bool UpgradeOldDB(const base::FilePath
& directory_path
);
85 ResourceMetadataStorage(const base::FilePath
& directory_path
,
86 base::SequencedTaskRunner
* blocking_task_runner
);
88 const base::FilePath
& directory_path() const { return directory_path_
; }
90 // Returns true when cache entries were not loaded to the DB during
92 bool cache_file_scan_is_needed() const { return cache_file_scan_is_needed_
; }
94 // Destroys this object.
97 // Initializes this object.
100 // Collects cache info from trashed resource map DB.
101 void RecoverCacheInfoFromTrashedResourceMap(RecoveredCacheInfoMap
* out_info
);
103 // Sets the largest changestamp.
104 FileError
SetLargestChangestamp(int64 largest_changestamp
);
106 // Gets the largest changestamp.
107 FileError
GetLargestChangestamp(int64
* largest_changestamp
);
109 // Puts the entry to this storage.
110 FileError
PutEntry(const ResourceEntry
& entry
);
112 // Gets an entry stored in this storage.
113 FileError
GetEntry(const std::string
& id
, ResourceEntry
* out_entry
);
115 // Removes an entry from this storage.
116 FileError
RemoveEntry(const std::string
& id
);
118 // Returns an object to iterate over entries stored in this storage.
119 scoped_ptr
<Iterator
> GetIterator();
121 // Returns the ID of the parent's child.
122 FileError
GetChild(const std::string
& parent_id
,
123 const std::string
& child_name
,
124 std::string
* child_id
);
126 // Returns the IDs of the parent's children.
127 FileError
GetChildren(const std::string
& parent_id
,
128 std::vector
<std::string
>* children
);
130 // Returns the local ID associated with the given resource ID.
131 FileError
GetIdByResourceId(const std::string
& resource_id
,
132 std::string
* out_id
);
135 friend class ResourceMetadataStorageTest
;
137 // To destruct this object, use Destroy().
138 ~ResourceMetadataStorage();
140 // Used to implement Destroy().
141 void DestroyOnBlockingPool();
143 // Returns a string to be used as a key for child entry.
144 static std::string
GetChildEntryKey(const std::string
& parent_id
,
145 const std::string
& child_name
);
148 FileError
PutHeader(const ResourceMetadataHeader
& header
);
151 FileError
GetHeader(ResourceMetadataHeader
* out_header
);
153 // Checks validity of the data.
154 bool CheckValidity();
156 // Path to the directory where the data is stored.
157 base::FilePath directory_path_
;
159 bool cache_file_scan_is_needed_
;
161 // Entries stored in this storage.
162 scoped_ptr
<leveldb::DB
> resource_map_
;
164 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
166 DISALLOW_COPY_AND_ASSIGN(ResourceMetadataStorage
);
169 } // namespace internal
172 #endif // COMPONENTS_DRIVE_RESOURCE_METADATA_STORAGE_H_