| 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 GetEntry( |
| 57 const std::string& key, |
| 58 const typename ProtoDatabase<T>::GetCallback& callback) override; |
| 56 void Destroy( | 59 void Destroy( |
| 57 const typename ProtoDatabase<T>::DestroyCallback& callback) override; | 60 const typename ProtoDatabase<T>::DestroyCallback& callback) override; |
| 58 | 61 |
| 59 // Allow callers to provide their own Database implementation. | 62 // Allow callers to provide their own Database implementation. |
| 60 void InitWithDatabase( | 63 void InitWithDatabase( |
| 61 std::unique_ptr<LevelDB> database, | 64 std::unique_ptr<LevelDB> database, |
| 62 const base::FilePath& database_dir, | 65 const base::FilePath& database_dir, |
| 63 const typename ProtoDatabase<T>::InitCallback& callback); | 66 const typename ProtoDatabase<T>::InitCallback& callback); |
| 64 | 67 |
| 65 private: | 68 private: |
| (...skipping 24 matching lines...) Expand all Loading... |
| 90 } | 93 } |
| 91 | 94 |
| 92 template <typename T> | 95 template <typename T> |
| 93 void RunLoadCallback(const typename ProtoDatabase<T>::LoadCallback& callback, | 96 void RunLoadCallback(const typename ProtoDatabase<T>::LoadCallback& callback, |
| 94 const bool* success, | 97 const bool* success, |
| 95 std::unique_ptr<std::vector<T>> entries) { | 98 std::unique_ptr<std::vector<T>> entries) { |
| 96 callback.Run(*success, std::move(entries)); | 99 callback.Run(*success, std::move(entries)); |
| 97 } | 100 } |
| 98 | 101 |
| 99 template <typename T> | 102 template <typename T> |
| 103 void RunGetCallback(const typename ProtoDatabase<T>::GetCallback& callback, |
| 104 const bool* success, |
| 105 const bool* found, |
| 106 std::unique_ptr<T> entry) { |
| 107 callback.Run(*success, *found ? std::move(entry) : nullptr); |
| 108 } |
| 109 |
| 110 template <typename T> |
| 100 void RunDestroyCallback( | 111 void RunDestroyCallback( |
| 101 const typename ProtoDatabase<T>::DestroyCallback& callback, | 112 const typename ProtoDatabase<T>::DestroyCallback& callback, |
| 102 const bool* success) { | 113 const bool* success) { |
| 103 callback.Run(*success); | 114 callback.Run(*success); |
| 104 } | 115 } |
| 105 | 116 |
| 106 inline void InitFromTaskRunner(LevelDB* database, | 117 inline void InitFromTaskRunner(LevelDB* database, |
| 107 const base::FilePath& database_dir, | 118 const base::FilePath& database_dir, |
| 108 bool* success) { | 119 bool* success) { |
| 109 DCHECK(success); | 120 DCHECK(success); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 T entry; | 164 T entry; |
| 154 if (!entry.ParseFromString(serialized_entry)) { | 165 if (!entry.ParseFromString(serialized_entry)) { |
| 155 DLOG(WARNING) << "Unable to parse leveldb_proto entry"; | 166 DLOG(WARNING) << "Unable to parse leveldb_proto entry"; |
| 156 // TODO(cjhopman): Decide what to do about un-parseable entries. | 167 // TODO(cjhopman): Decide what to do about un-parseable entries. |
| 157 } | 168 } |
| 158 | 169 |
| 159 entries->push_back(entry); | 170 entries->push_back(entry); |
| 160 } | 171 } |
| 161 } | 172 } |
| 162 | 173 |
| 174 template <typename T> |
| 175 void GetEntryFromTaskRunner(LevelDB* database, |
| 176 const std::string& key, |
| 177 T* entry, |
| 178 bool* found, |
| 179 bool* success) { |
| 180 DCHECK(success); |
| 181 DCHECK(found); |
| 182 DCHECK(entry); |
| 183 |
| 184 std::string serialized_entry; |
| 185 *success = database->Get(key, found, &serialized_entry); |
| 186 |
| 187 if (success && !entry->ParseFromString(serialized_entry)) { |
| 188 *found = false; |
| 189 DLOG(WARNING) << "Unable to parse leveldb_proto entry"; |
| 190 // TODO(cjhopman): Decide what to do about un-parseable entries. |
| 191 } |
| 192 } |
| 193 |
| 163 } // namespace | 194 } // namespace |
| 164 | 195 |
| 165 template <typename T> | 196 template <typename T> |
| 166 ProtoDatabaseImpl<T>::ProtoDatabaseImpl( | 197 ProtoDatabaseImpl<T>::ProtoDatabaseImpl( |
| 167 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 198 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 168 : task_runner_(task_runner) {} | 199 : task_runner_(task_runner) {} |
| 169 | 200 |
| 170 template <typename T> | 201 template <typename T> |
| 171 ProtoDatabaseImpl<T>::~ProtoDatabaseImpl() { | 202 ProtoDatabaseImpl<T>::~ProtoDatabaseImpl() { |
| 172 DCHECK(thread_checker_.CalledOnValidThread()); | 203 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 // Get this pointer before entries is base::Passed() so we can use it below. | 278 // Get this pointer before entries is base::Passed() so we can use it below. |
| 248 std::vector<T>* entries_ptr = entries.get(); | 279 std::vector<T>* entries_ptr = entries.get(); |
| 249 | 280 |
| 250 task_runner_->PostTaskAndReply( | 281 task_runner_->PostTaskAndReply( |
| 251 FROM_HERE, base::Bind(LoadEntriesFromTaskRunner<T>, | 282 FROM_HERE, base::Bind(LoadEntriesFromTaskRunner<T>, |
| 252 base::Unretained(db_.get()), entries_ptr, success), | 283 base::Unretained(db_.get()), entries_ptr, success), |
| 253 base::Bind(RunLoadCallback<T>, callback, base::Owned(success), | 284 base::Bind(RunLoadCallback<T>, callback, base::Owned(success), |
| 254 base::Passed(&entries))); | 285 base::Passed(&entries))); |
| 255 } | 286 } |
| 256 | 287 |
| 288 template <typename T> |
| 289 void ProtoDatabaseImpl<T>::GetEntry( |
| 290 const std::string& key, |
| 291 const typename ProtoDatabase<T>::GetCallback& callback) { |
| 292 DCHECK(thread_checker_.CalledOnValidThread()); |
| 293 bool* success = new bool(false); |
| 294 bool* found = new bool(false); |
| 295 |
| 296 std::unique_ptr<T> entry(new T()); |
| 297 // Get this pointer before entry is base::Passed() so we can use it below. |
| 298 T* entry_ptr = entry.get(); |
| 299 |
| 300 task_runner_->PostTaskAndReply( |
| 301 FROM_HERE, |
| 302 base::Bind(GetEntryFromTaskRunner<T>, base::Unretained(db_.get()), key, |
| 303 entry_ptr, found, success), |
| 304 base::Bind(RunGetCallback<T>, callback, base::Owned(success), |
| 305 base::Owned(found), base::Passed(&entry))); |
| 306 } |
| 307 |
| 257 } // namespace leveldb_proto | 308 } // namespace leveldb_proto |
| 258 | 309 |
| 259 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ | 310 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ |
| OLD | NEW |