1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_DRIVE_RESOURCE_METADATA_STORAGE_H_
#define COMPONENTS_DRIVE_RESOURCE_METADATA_STORAGE_H_
#include <stdint.h>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "components/drive/drive.pb.h"
#include "components/drive/drive_export.h"
#include "components/drive/file_errors.h"
namespace base {
class SequencedTaskRunner;
}
namespace leveldb {
class DB;
class Iterator;
}
namespace drive {
class ResourceEntry;
class ResourceMetadataHeader;
namespace internal {
// Storage for ResourceMetadata which is responsible to manage resource
// entries and child-parent relationships between entries.
class COMPONENTS_DRIVE_EXPORT ResourceMetadataStorage {
public:
// This should be incremented when incompatibility change is made to DB
// format.
static constexpr int kDBVersion = 19;
// Object to iterate over entries stored in this storage.
class COMPONENTS_DRIVE_EXPORT Iterator {
public:
explicit Iterator(std::unique_ptr<leveldb::Iterator> it);
Iterator(const Iterator&) = delete;
Iterator& operator=(const Iterator&) = delete;
~Iterator();
// Returns true if this iterator cannot advance any more and does not point
// to a valid entry. Get() and Advance() should not be called in such cases.
bool IsAtEnd() const;
// Returns the ID of the entry currently pointed by this object.
std::string GetID() const;
// Returns the entry currently pointed by this object.
const ResourceEntry& GetValue() const;
// Advances to the next entry.
void Advance();
// Returns true if this object has encountered any error.
bool HasError() const;
private:
ResourceEntry entry_;
std::unique_ptr<leveldb::Iterator> it_;
};
// Cache information recovered from trashed DB.
struct COMPONENTS_DRIVE_EXPORT RecoveredCacheInfo {
RecoveredCacheInfo();
~RecoveredCacheInfo();
bool is_dirty;
std::string md5;
std::string title;
};
using RecoveredCacheInfoMap = std::map<std::string, RecoveredCacheInfo>;
// Returns true if the DB was successfully upgraded to the newest version.
static bool UpgradeOldDB(const base::FilePath& directory_path);
ResourceMetadataStorage(const base::FilePath& directory_path,
base::SequencedTaskRunner* blocking_task_runner);
ResourceMetadataStorage(const ResourceMetadataStorage&) = delete;
ResourceMetadataStorage& operator=(const ResourceMetadataStorage&) = delete;
const base::FilePath& directory_path() const { return directory_path_; }
// Returns true when cache entries were not loaded to the DB during
// initialization.
bool cache_file_scan_is_needed() const { return cache_file_scan_is_needed_; }
// Initializes this object.
bool Initialize();
// Destroys this object.
void Destroy();
// Collects cache info from trashed resource map DB.
void RecoverCacheInfoFromTrashedResourceMap(RecoveredCacheInfoMap* out_info);
// Sets the largest changestamp.
FileError SetLargestChangestamp(int64_t largest_changestamp);
// Gets the largest changestamp.
FileError GetLargestChangestamp(int64_t* largest_changestamp);
FileError GetStartPageToken(std::string* out_value);
FileError SetStartPageToken(const std::string& value);
// Puts the entry to this storage.
FileError PutEntry(const ResourceEntry& entry);
// Gets an entry stored in this storage.
FileError GetEntry(const std::string& id, ResourceEntry* out_entry);
// Removes an entry from this storage.
FileError RemoveEntry(const std::string& id);
// Returns an object to iterate over entries stored in this storage.
std::unique_ptr<Iterator> GetIterator();
// Returns the ID of the parent's child.
FileError GetChild(const std::string& parent_id,
const std::string& child_name,
std::string* child_id) const;
// Returns the IDs of the parent's children.
FileError GetChildren(const std::string& parent_id,
std::vector<std::string>* children) const;
// Returns the local ID associated with the given resource ID.
FileError GetIdByResourceId(const std::string& resource_id,
std::string* out_id) const;
private:
friend class ResourceMetadataStorageTest;
// To destruct this object, use Destroy().
~ResourceMetadataStorage();
// Used to implement Destroy().
void DestroyOnBlockingPool();
// Returns a string to be used as a key for child entry.
// Exposed in the header for unit testing.
static std::string GetChildEntryKey(const std::string& parent_id,
const std::string& child_name);
// Puts header.
FileError PutHeader(const ResourceMetadataHeader& header);
// Gets header.
FileError GetHeader(ResourceMetadataHeader* out_header) const;
// Checks validity of the data.
bool CheckValidity();
// Path to the directory where the data is stored.
const base::FilePath directory_path_;
bool cache_file_scan_is_needed_;
// Entries stored in this storage.
std::unique_ptr<leveldb::DB> resource_map_;
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
};
} // namespace internal
} // namespace drive
#endif // COMPONENTS_DRIVE_RESOURCE_METADATA_STORAGE_H_
|