Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: components/leveldb_proto/proto_database_impl.h

Issue 2379113002: Extended the ProtoDatabase to provide LoadKeys() functionality. (Closed)
Patch Set: Garbage Collect orphaned images on service start-up. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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
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 auto success = base::MakeUnique<bool>(false);
312 auto keys = base::MakeUnique<std::vector<std::string>>();
313 auto load_task =
314 base::Bind(LoadKeysFromTaskRunner, base::Unretained(db_.get()),
315 keys.get(), success.get());
316 task_runner_->PostTaskAndReply(
317 FROM_HERE, load_task,
318 base::Bind(RunLoadKeysCallback<T>, callback, base::Passed(&success),
319 base::Passed(&keys)));
320 }
321
322 template <typename T>
289 void ProtoDatabaseImpl<T>::GetEntry( 323 void ProtoDatabaseImpl<T>::GetEntry(
290 const std::string& key, 324 const std::string& key,
291 const typename ProtoDatabase<T>::GetCallback& callback) { 325 const typename ProtoDatabase<T>::GetCallback& callback) {
292 DCHECK(thread_checker_.CalledOnValidThread()); 326 DCHECK(thread_checker_.CalledOnValidThread());
293 bool* success = new bool(false); 327 bool* success = new bool(false);
294 bool* found = new bool(false); 328 bool* found = new bool(false);
295 329
296 std::unique_ptr<T> entry(new T()); 330 std::unique_ptr<T> entry(new T());
297 // Get this pointer before entry is base::Passed() so we can use it below. 331 // Get this pointer before entry is base::Passed() so we can use it below.
298 T* entry_ptr = entry.get(); 332 T* entry_ptr = entry.get();
299 333
300 task_runner_->PostTaskAndReply( 334 task_runner_->PostTaskAndReply(
301 FROM_HERE, 335 FROM_HERE,
302 base::Bind(GetEntryFromTaskRunner<T>, base::Unretained(db_.get()), key, 336 base::Bind(GetEntryFromTaskRunner<T>, base::Unretained(db_.get()), key,
303 entry_ptr, found, success), 337 entry_ptr, found, success),
304 base::Bind(RunGetCallback<T>, callback, base::Owned(success), 338 base::Bind(RunGetCallback<T>, callback, base::Owned(success),
305 base::Owned(found), base::Passed(&entry))); 339 base::Owned(found), base::Passed(&entry)));
306 } 340 }
307 341
308 } // namespace leveldb_proto 342 } // namespace leveldb_proto
309 343
310 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ 344 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_
OLDNEW
« no previous file with comments | « components/leveldb_proto/proto_database.h ('k') | components/leveldb_proto/proto_database_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698