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 <string> | 9 #include <string> |
9 #include <utility> | 10 #include <utility> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/bind.h" | 13 #include "base/bind.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/ptr_util.h" |
16 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
17 #include "base/sequenced_task_runner.h" | 18 #include "base/sequenced_task_runner.h" |
18 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
20 #include "base/threading/sequenced_worker_pool.h" | 21 #include "base/threading/sequenced_worker_pool.h" |
21 #include "base/threading/thread_checker.h" | 22 #include "base/threading/thread_checker.h" |
22 #include "components/leveldb_proto/leveldb_database.h" | 23 #include "components/leveldb_proto/leveldb_database.h" |
23 #include "components/leveldb_proto/proto_database.h" | 24 #include "components/leveldb_proto/proto_database.h" |
24 | 25 |
25 namespace leveldb_proto { | 26 namespace leveldb_proto { |
(...skipping 13 matching lines...) Expand all Loading... |
39 | 40 |
40 ~ProtoDatabaseImpl() override; | 41 ~ProtoDatabaseImpl() override; |
41 | 42 |
42 // ProtoDatabase implementation. | 43 // ProtoDatabase implementation. |
43 // TODO(cjhopman): Perhaps Init() shouldn't be exposed to users and not just | 44 // TODO(cjhopman): Perhaps Init() shouldn't be exposed to users and not just |
44 // part of the constructor | 45 // part of the constructor |
45 void Init(const char* client_name, | 46 void Init(const char* client_name, |
46 const base::FilePath& database_dir, | 47 const base::FilePath& database_dir, |
47 const typename ProtoDatabase<T>::InitCallback& callback) override; | 48 const typename ProtoDatabase<T>::InitCallback& callback) override; |
48 void UpdateEntries( | 49 void UpdateEntries( |
49 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, | 50 std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> |
50 scoped_ptr<KeyVector> keys_to_remove, | 51 entries_to_save, |
| 52 std::unique_ptr<KeyVector> keys_to_remove, |
51 const typename ProtoDatabase<T>::UpdateCallback& callback) override; | 53 const typename ProtoDatabase<T>::UpdateCallback& callback) override; |
52 void LoadEntries( | 54 void LoadEntries( |
53 const typename ProtoDatabase<T>::LoadCallback& callback) override; | 55 const typename ProtoDatabase<T>::LoadCallback& callback) override; |
54 void Destroy( | 56 void Destroy( |
55 const typename ProtoDatabase<T>::DestroyCallback& callback) override; | 57 const typename ProtoDatabase<T>::DestroyCallback& callback) override; |
56 | 58 |
57 // Allow callers to provide their own Database implementation. | 59 // Allow callers to provide their own Database implementation. |
58 void InitWithDatabase( | 60 void InitWithDatabase( |
59 scoped_ptr<LevelDB> database, | 61 std::unique_ptr<LevelDB> database, |
60 const base::FilePath& database_dir, | 62 const base::FilePath& database_dir, |
61 const typename ProtoDatabase<T>::InitCallback& callback); | 63 const typename ProtoDatabase<T>::InitCallback& callback); |
62 | 64 |
63 private: | 65 private: |
64 base::ThreadChecker thread_checker_; | 66 base::ThreadChecker thread_checker_; |
65 | 67 |
66 // Used to run blocking tasks in-order. | 68 // Used to run blocking tasks in-order. |
67 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 69 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
68 | 70 |
69 scoped_ptr<LevelDB> db_; | 71 std::unique_ptr<LevelDB> db_; |
70 base::FilePath database_dir_; | 72 base::FilePath database_dir_; |
71 | 73 |
72 DISALLOW_COPY_AND_ASSIGN(ProtoDatabaseImpl); | 74 DISALLOW_COPY_AND_ASSIGN(ProtoDatabaseImpl); |
73 }; | 75 }; |
74 | 76 |
75 namespace { | 77 namespace { |
76 | 78 |
77 template <typename T> | 79 template <typename T> |
78 void RunInitCallback(const typename ProtoDatabase<T>::InitCallback& callback, | 80 void RunInitCallback(const typename ProtoDatabase<T>::InitCallback& callback, |
79 const bool* success) { | 81 const bool* success) { |
80 callback.Run(*success); | 82 callback.Run(*success); |
81 } | 83 } |
82 | 84 |
83 template <typename T> | 85 template <typename T> |
84 void RunUpdateCallback( | 86 void RunUpdateCallback( |
85 const typename ProtoDatabase<T>::UpdateCallback& callback, | 87 const typename ProtoDatabase<T>::UpdateCallback& callback, |
86 const bool* success) { | 88 const bool* success) { |
87 callback.Run(*success); | 89 callback.Run(*success); |
88 } | 90 } |
89 | 91 |
90 template <typename T> | 92 template <typename T> |
91 void RunLoadCallback(const typename ProtoDatabase<T>::LoadCallback& callback, | 93 void RunLoadCallback(const typename ProtoDatabase<T>::LoadCallback& callback, |
92 const bool* success, | 94 const bool* success, |
93 scoped_ptr<std::vector<T>> entries) { | 95 std::unique_ptr<std::vector<T>> entries) { |
94 callback.Run(*success, std::move(entries)); | 96 callback.Run(*success, std::move(entries)); |
95 } | 97 } |
96 | 98 |
97 template <typename T> | 99 template <typename T> |
98 void RunDestroyCallback( | 100 void RunDestroyCallback( |
99 const typename ProtoDatabase<T>::DestroyCallback& callback, | 101 const typename ProtoDatabase<T>::DestroyCallback& callback, |
100 const bool* success) { | 102 const bool* success) { |
101 callback.Run(*success); | 103 callback.Run(*success); |
102 } | 104 } |
103 | 105 |
104 inline void InitFromTaskRunner(LevelDB* database, | 106 inline void InitFromTaskRunner(LevelDB* database, |
105 const base::FilePath& database_dir, | 107 const base::FilePath& database_dir, |
106 bool* success) { | 108 bool* success) { |
107 DCHECK(success); | 109 DCHECK(success); |
108 | 110 |
109 // TODO(cjhopman): Histogram for database size. | 111 // TODO(cjhopman): Histogram for database size. |
110 *success = database->Init(database_dir); | 112 *success = database->Init(database_dir); |
111 } | 113 } |
112 | 114 |
113 inline void DestroyFromTaskRunner(const base::FilePath& database_dir, | 115 inline void DestroyFromTaskRunner(const base::FilePath& database_dir, |
114 bool* success) { | 116 bool* success) { |
115 CHECK(success); | 117 CHECK(success); |
116 | 118 |
117 *success = LevelDB::Destroy(database_dir); | 119 *success = LevelDB::Destroy(database_dir); |
118 } | 120 } |
119 | 121 |
120 template <typename T> | 122 template <typename T> |
121 void UpdateEntriesFromTaskRunner( | 123 void UpdateEntriesFromTaskRunner( |
122 LevelDB* database, | 124 LevelDB* database, |
123 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, | 125 std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, |
124 scoped_ptr<KeyVector> keys_to_remove, | 126 std::unique_ptr<KeyVector> keys_to_remove, |
125 bool* success) { | 127 bool* success) { |
126 DCHECK(success); | 128 DCHECK(success); |
127 | 129 |
128 // Serialize the values from Proto to string before passing on to database. | 130 // Serialize the values from Proto to string before passing on to database. |
129 KeyValueVector pairs_to_save; | 131 KeyValueVector pairs_to_save; |
130 for (const auto& pair : *entries_to_save) { | 132 for (const auto& pair : *entries_to_save) { |
131 pairs_to_save.push_back( | 133 pairs_to_save.push_back( |
132 std::make_pair(pair.first, pair.second.SerializeAsString())); | 134 std::make_pair(pair.first, pair.second.SerializeAsString())); |
133 } | 135 } |
134 | 136 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 DLOG(WARNING) << "Proto database will not be deleted."; | 174 DLOG(WARNING) << "Proto database will not be deleted."; |
173 } | 175 } |
174 | 176 |
175 template <typename T> | 177 template <typename T> |
176 void ProtoDatabaseImpl<T>::Init( | 178 void ProtoDatabaseImpl<T>::Init( |
177 const char* client_name, | 179 const char* client_name, |
178 const base::FilePath& database_dir, | 180 const base::FilePath& database_dir, |
179 const typename ProtoDatabase<T>::InitCallback& callback) { | 181 const typename ProtoDatabase<T>::InitCallback& callback) { |
180 DCHECK(thread_checker_.CalledOnValidThread()); | 182 DCHECK(thread_checker_.CalledOnValidThread()); |
181 database_dir_ = database_dir; | 183 database_dir_ = database_dir; |
182 InitWithDatabase(make_scoped_ptr(new LevelDB(client_name)), database_dir, | 184 InitWithDatabase(base::WrapUnique(new LevelDB(client_name)), database_dir, |
183 callback); | 185 callback); |
184 } | 186 } |
185 | 187 |
186 template <typename T> | 188 template <typename T> |
187 void ProtoDatabaseImpl<T>::Destroy( | 189 void ProtoDatabaseImpl<T>::Destroy( |
188 const typename ProtoDatabase<T>::DestroyCallback& callback) { | 190 const typename ProtoDatabase<T>::DestroyCallback& callback) { |
189 DCHECK(thread_checker_.CalledOnValidThread()); | 191 DCHECK(thread_checker_.CalledOnValidThread()); |
190 DCHECK(db_); | 192 DCHECK(db_); |
191 DCHECK(!database_dir_.empty()); | 193 DCHECK(!database_dir_.empty()); |
192 | 194 |
193 // Note that |db_| should be released from task runner. | 195 // Note that |db_| should be released from task runner. |
194 if (!task_runner_->DeleteSoon(FROM_HERE, db_.release())) { | 196 if (!task_runner_->DeleteSoon(FROM_HERE, db_.release())) { |
195 DLOG(WARNING) << "Proto database will not be deleted."; | 197 DLOG(WARNING) << "Proto database will not be deleted."; |
196 callback.Run(false); | 198 callback.Run(false); |
197 return; | 199 return; |
198 } | 200 } |
199 | 201 |
200 // After |db_| is released, we can now wipe out the database directory. | 202 // After |db_| is released, we can now wipe out the database directory. |
201 bool* success = new bool(false); | 203 bool* success = new bool(false); |
202 task_runner_->PostTaskAndReply( | 204 task_runner_->PostTaskAndReply( |
203 FROM_HERE, base::Bind(DestroyFromTaskRunner, database_dir_, success), | 205 FROM_HERE, base::Bind(DestroyFromTaskRunner, database_dir_, success), |
204 base::Bind(RunDestroyCallback<T>, callback, base::Owned(success))); | 206 base::Bind(RunDestroyCallback<T>, callback, base::Owned(success))); |
205 } | 207 } |
206 | 208 |
207 template <typename T> | 209 template <typename T> |
208 void ProtoDatabaseImpl<T>::InitWithDatabase( | 210 void ProtoDatabaseImpl<T>::InitWithDatabase( |
209 scoped_ptr<LevelDB> database, | 211 std::unique_ptr<LevelDB> database, |
210 const base::FilePath& database_dir, | 212 const base::FilePath& database_dir, |
211 const typename ProtoDatabase<T>::InitCallback& callback) { | 213 const typename ProtoDatabase<T>::InitCallback& callback) { |
212 DCHECK(thread_checker_.CalledOnValidThread()); | 214 DCHECK(thread_checker_.CalledOnValidThread()); |
213 DCHECK(!db_); | 215 DCHECK(!db_); |
214 DCHECK(database); | 216 DCHECK(database); |
215 db_.reset(database.release()); | 217 db_.reset(database.release()); |
216 bool* success = new bool(false); | 218 bool* success = new bool(false); |
217 task_runner_->PostTaskAndReply( | 219 task_runner_->PostTaskAndReply( |
218 FROM_HERE, base::Bind(InitFromTaskRunner, base::Unretained(db_.get()), | 220 FROM_HERE, base::Bind(InitFromTaskRunner, base::Unretained(db_.get()), |
219 database_dir, success), | 221 database_dir, success), |
220 base::Bind(RunInitCallback<T>, callback, base::Owned(success))); | 222 base::Bind(RunInitCallback<T>, callback, base::Owned(success))); |
221 } | 223 } |
222 | 224 |
223 template <typename T> | 225 template <typename T> |
224 void ProtoDatabaseImpl<T>::UpdateEntries( | 226 void ProtoDatabaseImpl<T>::UpdateEntries( |
225 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, | 227 std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, |
226 scoped_ptr<KeyVector> keys_to_remove, | 228 std::unique_ptr<KeyVector> keys_to_remove, |
227 const typename ProtoDatabase<T>::UpdateCallback& callback) { | 229 const typename ProtoDatabase<T>::UpdateCallback& callback) { |
228 DCHECK(thread_checker_.CalledOnValidThread()); | 230 DCHECK(thread_checker_.CalledOnValidThread()); |
229 bool* success = new bool(false); | 231 bool* success = new bool(false); |
230 task_runner_->PostTaskAndReply( | 232 task_runner_->PostTaskAndReply( |
231 FROM_HERE, | 233 FROM_HERE, |
232 base::Bind(UpdateEntriesFromTaskRunner<T>, base::Unretained(db_.get()), | 234 base::Bind(UpdateEntriesFromTaskRunner<T>, base::Unretained(db_.get()), |
233 base::Passed(&entries_to_save), base::Passed(&keys_to_remove), | 235 base::Passed(&entries_to_save), base::Passed(&keys_to_remove), |
234 success), | 236 success), |
235 base::Bind(RunUpdateCallback<T>, callback, base::Owned(success))); | 237 base::Bind(RunUpdateCallback<T>, callback, base::Owned(success))); |
236 } | 238 } |
237 | 239 |
238 template <typename T> | 240 template <typename T> |
239 void ProtoDatabaseImpl<T>::LoadEntries( | 241 void ProtoDatabaseImpl<T>::LoadEntries( |
240 const typename ProtoDatabase<T>::LoadCallback& callback) { | 242 const typename ProtoDatabase<T>::LoadCallback& callback) { |
241 DCHECK(thread_checker_.CalledOnValidThread()); | 243 DCHECK(thread_checker_.CalledOnValidThread()); |
242 bool* success = new bool(false); | 244 bool* success = new bool(false); |
243 | 245 |
244 scoped_ptr<std::vector<T> > entries(new std::vector<T>()); | 246 std::unique_ptr<std::vector<T>> entries(new std::vector<T>()); |
245 // Get this pointer before entries is base::Passed() so we can use it below. | 247 // Get this pointer before entries is base::Passed() so we can use it below. |
246 std::vector<T>* entries_ptr = entries.get(); | 248 std::vector<T>* entries_ptr = entries.get(); |
247 | 249 |
248 task_runner_->PostTaskAndReply( | 250 task_runner_->PostTaskAndReply( |
249 FROM_HERE, base::Bind(LoadEntriesFromTaskRunner<T>, | 251 FROM_HERE, base::Bind(LoadEntriesFromTaskRunner<T>, |
250 base::Unretained(db_.get()), entries_ptr, success), | 252 base::Unretained(db_.get()), entries_ptr, success), |
251 base::Bind(RunLoadCallback<T>, callback, base::Owned(success), | 253 base::Bind(RunLoadCallback<T>, callback, base::Owned(success), |
252 base::Passed(&entries))); | 254 base::Passed(&entries))); |
253 } | 255 } |
254 | 256 |
255 } // namespace leveldb_proto | 257 } // namespace leveldb_proto |
256 | 258 |
257 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ | 259 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ |
OLD | NEW |