Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 COMPONENTS_LEVELDB_PROTO_CORE_PROTO_DATABASE_IMPL_H_ | |
| 6 #define COMPONENTS_LEVELDB_PROTO_CORE_PROTO_DATABASE_IMPL_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/file_util.h" | |
| 13 #include "base/files/file_path.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/message_loop/message_loop.h" | |
| 16 #include "base/sequenced_task_runner.h" | |
| 17 #include "base/strings/string_util.h" | |
| 18 #include "base/threading/sequenced_worker_pool.h" | |
| 19 #include "base/threading/thread_checker.h" | |
| 20 #include "base/threading/thread_collision_warner.h" | |
| 21 #include "components/leveldb_proto/core/proto_database.h" | |
| 22 #include "third_party/leveldatabase/src/include/leveldb/db.h" | |
| 23 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | |
| 24 #include "third_party/leveldatabase/src/include/leveldb/options.h" | |
| 25 #include "third_party/leveldatabase/src/include/leveldb/slice.h" | |
| 26 #include "third_party/leveldatabase/src/include/leveldb/status.h" | |
| 27 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | |
| 28 | |
| 29 namespace base { | |
| 30 class SequencedTaskRunner; | |
|
cjhopman
2014/06/13 19:41:05
Don't need these forward declarations anymore.
Mathieu
2014/06/13 22:07:07
Done.
| |
| 31 class MessageLoop; | |
| 32 } | |
| 33 | |
| 34 namespace leveldb { | |
| 35 class DB; | |
|
cjhopman
2014/06/13 19:41:05
Don't need this one either.
Mathieu
2014/06/13 22:07:07
Done.
| |
| 36 } | |
| 37 | |
| 38 namespace leveldb_proto { | |
| 39 | |
| 40 // When the ProtoDatabaseImpl instance is deleted, in-progress asynchronous | |
| 41 // operations will be completed and the corresponding callbacks will be called. | |
|
cjhopman
2014/06/13 19:41:05
Maybe point out that construction/calls/destructio
Mathieu
2014/06/13 22:07:07
Done.
| |
| 42 template <typename T> | |
| 43 class ProtoDatabaseImpl : public ProtoDatabase<T> { | |
| 44 public: | |
| 45 // The underlying database. Calls to this type may be blocking. | |
| 46 class Database { | |
|
cjhopman
2014/06/13 19:41:05
I think that the SerializeAsString() and ParseFrom
Mathieu
2014/06/13 22:07:07
Gotcha. The Database no longer depends on proto. T
| |
| 47 public: | |
| 48 virtual bool Init(const base::FilePath& database_dir) = 0; | |
| 49 virtual bool Save( | |
| 50 const typename ProtoDatabase<T>::KeyEntryVector& entries_to_save, | |
| 51 const std::vector<std::string>& keys_to_remove) = 0; | |
| 52 virtual bool Load(std::vector<T>* entries) = 0; | |
| 53 virtual ~Database() {} | |
| 54 }; | |
| 55 | |
| 56 // Once constructed, function calls and destruction should all occur on the | |
| 57 // same thread (not necessarily the same as the constructor). | |
| 58 class LevelDB : public Database { | |
| 59 public: | |
| 60 LevelDB(); | |
| 61 virtual ~LevelDB(); | |
| 62 virtual bool Init(const base::FilePath& database_dir) OVERRIDE; | |
| 63 virtual bool Save( | |
| 64 const typename ProtoDatabase<T>::KeyEntryVector& entries_to_save, | |
| 65 const std::vector<std::string>& keys_to_remove) OVERRIDE; | |
| 66 virtual bool Load(std::vector<T>* entries) OVERRIDE; | |
| 67 | |
| 68 private: | |
| 69 DFAKE_MUTEX(thread_checker_); | |
| 70 scoped_ptr<leveldb::DB> db_; | |
| 71 }; | |
| 72 | |
| 73 explicit ProtoDatabaseImpl( | |
| 74 scoped_refptr<base::SequencedTaskRunner> task_runner); | |
| 75 | |
| 76 virtual ~ProtoDatabaseImpl(); | |
| 77 | |
| 78 // ProtoDatabase implementation. | |
| 79 virtual void Init(const base::FilePath& database_dir, | |
|
cjhopman
2014/06/13 19:41:05
I don't think I like that this Init() is exposed t
Mathieu
2014/06/13 22:07:07
Ack. Added a TODO with you as point of contact.
| |
| 80 typename ProtoDatabase<T>::InitCallback callback) OVERRIDE; | |
| 81 virtual void UpdateEntries( | |
| 82 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, | |
| 83 scoped_ptr<std::vector<std::string> > keys_to_remove, | |
| 84 typename ProtoDatabase<T>::UpdateCallback callback) OVERRIDE; | |
| 85 virtual void LoadEntries( | |
| 86 typename ProtoDatabase<T>::LoadCallback callback) OVERRIDE; | |
| 87 | |
| 88 // Allow callers to provide their own Database implementation. | |
| 89 void InitWithDatabase(scoped_ptr<Database> database, | |
| 90 const base::FilePath& database_dir, | |
| 91 typename ProtoDatabase<T>::InitCallback callback); | |
| 92 | |
| 93 private: | |
| 94 base::ThreadChecker thread_checker_; | |
| 95 | |
| 96 // Used to run blocking tasks in-order. | |
| 97 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 98 | |
| 99 scoped_ptr<Database> db_; | |
| 100 | |
| 101 DISALLOW_COPY_AND_ASSIGN(ProtoDatabaseImpl); | |
| 102 }; | |
| 103 | |
| 104 using base::MessageLoop; | |
|
cjhopman
2014/06/13 19:41:05
Remove these usings
Mathieu
2014/06/13 22:07:07
Done.
| |
| 105 using base::SequencedTaskRunner; | |
| 106 | |
| 107 template <typename T> | |
| 108 ProtoDatabaseImpl<T>::LevelDB::LevelDB() {} | |
| 109 | |
| 110 template <typename T> | |
| 111 ProtoDatabaseImpl<T>::LevelDB::~LevelDB() { | |
| 112 DFAKE_SCOPED_LOCK(thread_checker_); | |
| 113 } | |
| 114 | |
| 115 template <typename T> | |
| 116 bool ProtoDatabaseImpl<T>::LevelDB::Init(const base::FilePath& database_dir) { | |
| 117 DFAKE_SCOPED_LOCK(thread_checker_); | |
| 118 | |
| 119 leveldb::Options options; | |
| 120 options.create_if_missing = true; | |
| 121 options.max_open_files = 0; // Use minimum. | |
| 122 | |
| 123 std::string path = database_dir.AsUTF8Unsafe(); | |
| 124 | |
| 125 leveldb::DB* db = NULL; | |
| 126 leveldb::Status status = leveldb::DB::Open(options, path, &db); | |
| 127 if (status.IsCorruption()) { | |
| 128 base::DeleteFile(database_dir, true); | |
| 129 status = leveldb::DB::Open(options, path, &db); | |
| 130 } | |
| 131 | |
| 132 if (status.ok()) { | |
| 133 CHECK(db); | |
| 134 db_.reset(db); | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 LOG(WARNING) << "Unable to open " << database_dir.value() << ": " | |
| 139 << status.ToString(); | |
| 140 return false; | |
| 141 } | |
| 142 | |
| 143 template <typename T> | |
| 144 bool ProtoDatabaseImpl<T>::LevelDB::Save( | |
| 145 const typename ProtoDatabase<T>::KeyEntryVector& entries_to_save, | |
| 146 const std::vector<std::string>& keys_to_remove) { | |
| 147 DFAKE_SCOPED_LOCK(thread_checker_); | |
| 148 | |
| 149 leveldb::WriteBatch updates; | |
| 150 for (typename ProtoDatabase<T>::KeyEntryVector::const_iterator it = | |
| 151 entries_to_save.begin(); | |
| 152 it != entries_to_save.end(); ++it) { | |
| 153 updates.Put(leveldb::Slice(it->first), | |
| 154 leveldb::Slice(it->second.SerializeAsString())); | |
| 155 } | |
| 156 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin(); | |
| 157 it != keys_to_remove.end(); ++it) { | |
| 158 updates.Delete(leveldb::Slice(*it)); | |
| 159 } | |
| 160 | |
| 161 leveldb::WriteOptions options; | |
| 162 options.sync = true; | |
| 163 leveldb::Status status = db_->Write(options, &updates); | |
| 164 if (status.ok()) return true; | |
| 165 | |
| 166 DLOG(WARNING) << "Failed writing leveldb_proto entries: " | |
| 167 << status.ToString(); | |
| 168 return false; | |
| 169 } | |
| 170 | |
| 171 template <typename T> | |
| 172 bool ProtoDatabaseImpl<T>::LevelDB::Load(std::vector<T>* entries) { | |
| 173 DFAKE_SCOPED_LOCK(thread_checker_); | |
| 174 | |
| 175 leveldb::ReadOptions options; | |
| 176 scoped_ptr<leveldb::Iterator> db_iterator(db_->NewIterator(options)); | |
| 177 for (db_iterator->SeekToFirst(); db_iterator->Valid(); db_iterator->Next()) { | |
| 178 leveldb::Slice value_slice = db_iterator->value(); | |
| 179 | |
| 180 T entry; | |
| 181 if (!entry.ParseFromArray(value_slice.data(), value_slice.size())) { | |
| 182 DLOG(WARNING) << "Unable to parse leveldb_proto entry " | |
| 183 << db_iterator->key().ToString(); | |
| 184 // TODO(cjhopman): Decide what to do about un-parseable entries. | |
| 185 } | |
| 186 entries->push_back(entry); | |
| 187 } | |
| 188 return true; | |
| 189 } | |
| 190 | |
| 191 namespace { | |
| 192 | |
| 193 template <typename T> | |
| 194 void RunInitCallback(typename ProtoDatabase<T>::InitCallback callback, | |
| 195 const bool* success) { | |
| 196 callback.Run(*success); | |
| 197 } | |
| 198 | |
| 199 template <typename T> | |
| 200 void RunUpdateCallback(typename ProtoDatabase<T>::UpdateCallback callback, | |
| 201 const bool* success) { | |
| 202 callback.Run(*success); | |
| 203 } | |
| 204 | |
| 205 template <typename T> | |
| 206 void RunLoadCallback(typename ProtoDatabase<T>::LoadCallback callback, | |
| 207 const bool* success, scoped_ptr<std::vector<T> > entries) { | |
| 208 callback.Run(*success, entries.Pass()); | |
| 209 } | |
| 210 | |
| 211 template <typename T> | |
| 212 void InitFromTaskRunner(typename ProtoDatabaseImpl<T>::Database* database, | |
| 213 const base::FilePath& database_dir, bool* success) { | |
| 214 DCHECK(success); | |
| 215 | |
| 216 // TODO(cjhopman): Histogram for database size. | |
| 217 *success = database->Init(database_dir); | |
| 218 } | |
| 219 | |
| 220 template <typename T> | |
| 221 void UpdateEntriesFromTaskRunner( | |
| 222 typename ProtoDatabaseImpl<T>::Database* database, | |
| 223 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, | |
| 224 scoped_ptr<std::vector<std::string> > keys_to_remove, bool* success) { | |
| 225 DCHECK(success); | |
| 226 *success = database->Save(*entries_to_save, *keys_to_remove); | |
| 227 } | |
| 228 | |
| 229 template <typename T> | |
| 230 void LoadEntriesFromTaskRunner( | |
| 231 typename ProtoDatabaseImpl<T>::Database* database, std::vector<T>* entries, | |
| 232 bool* success) { | |
| 233 DCHECK(success); | |
| 234 DCHECK(entries); | |
| 235 | |
| 236 entries->clear(); | |
| 237 *success = database->Load(entries); | |
| 238 } | |
| 239 | |
| 240 } // namespace | |
| 241 | |
| 242 template <typename T> | |
| 243 ProtoDatabaseImpl<T>::ProtoDatabaseImpl( | |
| 244 scoped_refptr<base::SequencedTaskRunner> task_runner) | |
| 245 : task_runner_(task_runner) {} | |
| 246 | |
| 247 template <typename T> | |
| 248 ProtoDatabaseImpl<T>::~ProtoDatabaseImpl() { | |
| 249 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 250 if (!task_runner_->DeleteSoon(FROM_HERE, db_.release())) { | |
| 251 DLOG(WARNING) << "DOM distiller database will not be deleted."; | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 template <typename T> | |
| 256 void ProtoDatabaseImpl<T>::Init( | |
| 257 const base::FilePath& database_dir, | |
| 258 typename ProtoDatabase<T>::InitCallback callback) { | |
| 259 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 260 InitWithDatabase(scoped_ptr<Database>(new LevelDB()), database_dir, callback); | |
| 261 } | |
| 262 | |
| 263 template <typename T> | |
| 264 void ProtoDatabaseImpl<T>::InitWithDatabase( | |
| 265 scoped_ptr<Database> database, const base::FilePath& database_dir, | |
| 266 typename ProtoDatabase<T>::InitCallback callback) { | |
| 267 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 268 DCHECK(!db_); | |
| 269 DCHECK(database); | |
| 270 db_.reset(database.release()); | |
| 271 bool* success = new bool(false); | |
| 272 task_runner_->PostTaskAndReply( | |
| 273 FROM_HERE, base::Bind(InitFromTaskRunner<T>, base::Unretained(db_.get()), | |
| 274 database_dir, success), | |
| 275 base::Bind(RunInitCallback<T>, callback, base::Owned(success))); | |
| 276 } | |
| 277 | |
| 278 template <typename T> | |
| 279 void ProtoDatabaseImpl<T>::UpdateEntries( | |
| 280 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, | |
| 281 scoped_ptr<std::vector<std::string> > keys_to_remove, | |
| 282 typename ProtoDatabase<T>::UpdateCallback callback) { | |
| 283 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 284 bool* success = new bool(false); | |
| 285 task_runner_->PostTaskAndReply( | |
| 286 FROM_HERE, | |
| 287 base::Bind(UpdateEntriesFromTaskRunner<T>, base::Unretained(db_.get()), | |
| 288 base::Passed(&entries_to_save), base::Passed(&keys_to_remove), | |
| 289 success), | |
| 290 base::Bind(RunUpdateCallback<T>, callback, base::Owned(success))); | |
| 291 } | |
| 292 | |
| 293 template <typename T> | |
| 294 void ProtoDatabaseImpl<T>::LoadEntries( | |
| 295 typename ProtoDatabase<T>::LoadCallback callback) { | |
| 296 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 297 bool* success = new bool(false); | |
| 298 | |
| 299 scoped_ptr<std::vector<T> > entries(new std::vector<T>()); | |
| 300 // Get this pointer before entries is base::Passed() so we can use it below. | |
| 301 std::vector<T>* entries_ptr = entries.get(); | |
| 302 | |
| 303 task_runner_->PostTaskAndReply( | |
| 304 FROM_HERE, base::Bind(LoadEntriesFromTaskRunner<T>, | |
| 305 base::Unretained(db_.get()), entries_ptr, success), | |
| 306 base::Bind(RunLoadCallback<T>, callback, base::Owned(success), | |
| 307 base::Passed(&entries))); | |
| 308 } | |
| 309 | |
| 310 } // namespace leveldb_proto | |
| 311 | |
| 312 #endif // COMPONENTS_LEVELDB_PROTO_CORE_PROTO_DATABASE_IMPL_H_ | |
| OLD | NEW |