Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ | 5 #ifndef COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ |
| 6 #define COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ | 6 #define COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 void Init(const char* client_name, | 46 void Init(const char* client_name, |
| 47 const base::FilePath& database_dir, | 47 const base::FilePath& database_dir, |
| 48 const typename ProtoDatabase<T>::InitCallback& callback) override; | 48 const typename ProtoDatabase<T>::InitCallback& callback) override; |
| 49 void UpdateEntries( | 49 void UpdateEntries( |
| 50 std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> | 50 std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> |
| 51 entries_to_save, | 51 entries_to_save, |
| 52 std::unique_ptr<KeyVector> keys_to_remove, | 52 std::unique_ptr<KeyVector> keys_to_remove, |
| 53 const typename ProtoDatabase<T>::UpdateCallback& callback) override; | 53 const typename ProtoDatabase<T>::UpdateCallback& callback) override; |
| 54 void LoadEntries( | 54 void LoadEntries( |
| 55 const typename ProtoDatabase<T>::LoadCallback& callback) override; | 55 const typename ProtoDatabase<T>::LoadCallback& callback) override; |
| 56 void LoadKeys( | |
| 57 const typename ProtoDatabase<T>::LoadKeysCallback& callback) override; | |
| 56 void GetEntry( | 58 void GetEntry( |
| 57 const std::string& key, | 59 const std::string& key, |
| 58 const typename ProtoDatabase<T>::GetCallback& callback) override; | 60 const typename ProtoDatabase<T>::GetCallback& callback) override; |
| 59 void Destroy( | 61 void Destroy( |
| 60 const typename ProtoDatabase<T>::DestroyCallback& callback) override; | 62 const typename ProtoDatabase<T>::DestroyCallback& callback) override; |
| 61 | 63 |
| 62 // Allow callers to provide their own Database implementation. | 64 // Allow callers to provide their own Database implementation. |
| 63 void InitWithDatabase( | 65 void InitWithDatabase( |
| 64 std::unique_ptr<LevelDB> database, | 66 std::unique_ptr<LevelDB> database, |
| 65 const base::FilePath& database_dir, | 67 const base::FilePath& database_dir, |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 87 | 89 |
| 88 template <typename T> | 90 template <typename T> |
| 89 void RunUpdateCallback( | 91 void RunUpdateCallback( |
| 90 const typename ProtoDatabase<T>::UpdateCallback& callback, | 92 const typename ProtoDatabase<T>::UpdateCallback& callback, |
| 91 const bool* success) { | 93 const bool* success) { |
| 92 callback.Run(*success); | 94 callback.Run(*success); |
| 93 } | 95 } |
| 94 | 96 |
| 95 template <typename T> | 97 template <typename T> |
| 96 void RunLoadCallback(const typename ProtoDatabase<T>::LoadCallback& callback, | 98 void RunLoadCallback(const typename ProtoDatabase<T>::LoadCallback& callback, |
| 97 const bool* success, | 99 bool* success, |
| 98 std::unique_ptr<std::vector<T>> entries) { | 100 std::unique_ptr<std::vector<T>> entries) { |
| 99 callback.Run(*success, std::move(entries)); | 101 callback.Run(*success, std::move(entries)); |
| 100 } | 102 } |
| 101 | 103 |
| 102 template <typename T> | 104 template <typename T> |
| 105 void RunLoadKeysCallback( | |
| 106 const typename ProtoDatabase<T>::LoadKeysCallback& callback, | |
| 107 std::unique_ptr<bool> success, | |
| 108 std::unique_ptr<std::vector<std::string>> keys) { | |
| 109 callback.Run(*success, std::move(keys)); | |
| 110 } | |
| 111 | |
| 112 template <typename T> | |
| 103 void RunGetCallback(const typename ProtoDatabase<T>::GetCallback& callback, | 113 void RunGetCallback(const typename ProtoDatabase<T>::GetCallback& callback, |
| 104 const bool* success, | 114 const bool* success, |
| 105 const bool* found, | 115 const bool* found, |
| 106 std::unique_ptr<T> entry) { | 116 std::unique_ptr<T> entry) { |
| 107 callback.Run(*success, *found ? std::move(entry) : nullptr); | 117 callback.Run(*success, *found ? std::move(entry) : nullptr); |
| 108 } | 118 } |
| 109 | 119 |
| 110 template <typename T> | 120 template <typename T> |
| 111 void RunDestroyCallback( | 121 void RunDestroyCallback( |
| 112 const typename ProtoDatabase<T>::DestroyCallback& callback, | 122 const typename ProtoDatabase<T>::DestroyCallback& callback, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 T entry; | 174 T entry; |
| 165 if (!entry.ParseFromString(serialized_entry)) { | 175 if (!entry.ParseFromString(serialized_entry)) { |
| 166 DLOG(WARNING) << "Unable to parse leveldb_proto entry"; | 176 DLOG(WARNING) << "Unable to parse leveldb_proto entry"; |
| 167 // TODO(cjhopman): Decide what to do about un-parseable entries. | 177 // TODO(cjhopman): Decide what to do about un-parseable entries. |
| 168 } | 178 } |
| 169 | 179 |
| 170 entries->push_back(entry); | 180 entries->push_back(entry); |
| 171 } | 181 } |
| 172 } | 182 } |
| 173 | 183 |
| 184 void LoadKeysFromTaskRunner(LevelDB* database, | |
| 185 std::vector<std::string>* keys, | |
| 186 bool* success) { | |
| 187 DCHECK(success); | |
| 188 DCHECK(keys); | |
| 189 keys->clear(); | |
| 190 *success = database->LoadKeys(keys); | |
| 191 } | |
| 192 | |
| 174 template <typename T> | 193 template <typename T> |
| 175 void GetEntryFromTaskRunner(LevelDB* database, | 194 void GetEntryFromTaskRunner(LevelDB* database, |
| 176 const std::string& key, | 195 const std::string& key, |
| 177 T* entry, | 196 T* entry, |
| 178 bool* found, | 197 bool* found, |
| 179 bool* success) { | 198 bool* success) { |
| 180 DCHECK(success); | 199 DCHECK(success); |
| 181 DCHECK(found); | 200 DCHECK(found); |
| 182 DCHECK(entry); | 201 DCHECK(entry); |
| 183 | 202 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 279 std::vector<T>* entries_ptr = entries.get(); | 298 std::vector<T>* entries_ptr = entries.get(); |
| 280 | 299 |
| 281 task_runner_->PostTaskAndReply( | 300 task_runner_->PostTaskAndReply( |
| 282 FROM_HERE, base::Bind(LoadEntriesFromTaskRunner<T>, | 301 FROM_HERE, base::Bind(LoadEntriesFromTaskRunner<T>, |
| 283 base::Unretained(db_.get()), entries_ptr, success), | 302 base::Unretained(db_.get()), entries_ptr, success), |
| 284 base::Bind(RunLoadCallback<T>, callback, base::Owned(success), | 303 base::Bind(RunLoadCallback<T>, callback, base::Owned(success), |
| 285 base::Passed(&entries))); | 304 base::Passed(&entries))); |
| 286 } | 305 } |
| 287 | 306 |
| 288 template <typename T> | 307 template <typename T> |
| 308 void ProtoDatabaseImpl<T>::LoadKeys( | |
| 309 const typename ProtoDatabase<T>::LoadKeysCallback& callback) { | |
| 310 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 311 std::unique_ptr<bool> success(new bool(false)); | |
| 312 std::unique_ptr<std::vector<std::string>> keys( | |
| 313 new std::vector<std::string>()); | |
|
nyquist
2016/10/03 21:46:42
Nit: Can these not use MakeUnique?
tschumann
2016/10/04 09:41:38
Changed to not repeat the types anymore. I think i
nyquist
2016/10/04 14:22:45
Agreed. I think this looks very clean.
| |
| 314 auto load_task = | |
| 315 base::Bind(LoadKeysFromTaskRunner, base::Unretained(db_.get()), | |
| 316 keys.get(), success.get()); | |
| 317 task_runner_->PostTaskAndReply( | |
| 318 FROM_HERE, load_task, | |
| 319 base::Bind(RunLoadKeysCallback<T>, callback, base::Passed(&success), | |
| 320 base::Passed(&keys))); | |
| 321 } | |
| 322 | |
| 323 template <typename T> | |
| 289 void ProtoDatabaseImpl<T>::GetEntry( | 324 void ProtoDatabaseImpl<T>::GetEntry( |
| 290 const std::string& key, | 325 const std::string& key, |
| 291 const typename ProtoDatabase<T>::GetCallback& callback) { | 326 const typename ProtoDatabase<T>::GetCallback& callback) { |
| 292 DCHECK(thread_checker_.CalledOnValidThread()); | 327 DCHECK(thread_checker_.CalledOnValidThread()); |
| 293 bool* success = new bool(false); | 328 bool* success = new bool(false); |
| 294 bool* found = new bool(false); | 329 bool* found = new bool(false); |
| 295 | 330 |
| 296 std::unique_ptr<T> entry(new T()); | 331 std::unique_ptr<T> entry(new T()); |
| 297 // Get this pointer before entry is base::Passed() so we can use it below. | 332 // Get this pointer before entry is base::Passed() so we can use it below. |
| 298 T* entry_ptr = entry.get(); | 333 T* entry_ptr = entry.get(); |
| 299 | 334 |
| 300 task_runner_->PostTaskAndReply( | 335 task_runner_->PostTaskAndReply( |
| 301 FROM_HERE, | 336 FROM_HERE, |
| 302 base::Bind(GetEntryFromTaskRunner<T>, base::Unretained(db_.get()), key, | 337 base::Bind(GetEntryFromTaskRunner<T>, base::Unretained(db_.get()), key, |
| 303 entry_ptr, found, success), | 338 entry_ptr, found, success), |
| 304 base::Bind(RunGetCallback<T>, callback, base::Owned(success), | 339 base::Bind(RunGetCallback<T>, callback, base::Owned(success), |
| 305 base::Owned(found), base::Passed(&entry))); | 340 base::Owned(found), base::Passed(&entry))); |
| 306 } | 341 } |
| 307 | 342 |
| 308 } // namespace leveldb_proto | 343 } // namespace leveldb_proto |
| 309 | 344 |
| 310 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ | 345 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ |
| OLD | NEW |