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

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

Issue 2049573003: Implement ProtoDatabase::GetEntry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add tests Created 4 years, 6 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 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
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 std::unique_ptr<T> entry) {
106 callback.Run(*success, *success ? std::move(entry) : nullptr);
107 }
108
109 template <typename T>
100 void RunDestroyCallback( 110 void RunDestroyCallback(
101 const typename ProtoDatabase<T>::DestroyCallback& callback, 111 const typename ProtoDatabase<T>::DestroyCallback& callback,
102 const bool* success) { 112 const bool* success) {
103 callback.Run(*success); 113 callback.Run(*success);
104 } 114 }
105 115
106 inline void InitFromTaskRunner(LevelDB* database, 116 inline void InitFromTaskRunner(LevelDB* database,
107 const base::FilePath& database_dir, 117 const base::FilePath& database_dir,
108 bool* success) { 118 bool* success) {
109 DCHECK(success); 119 DCHECK(success);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 T entry; 163 T entry;
154 if (!entry.ParseFromString(serialized_entry)) { 164 if (!entry.ParseFromString(serialized_entry)) {
155 DLOG(WARNING) << "Unable to parse leveldb_proto entry"; 165 DLOG(WARNING) << "Unable to parse leveldb_proto entry";
156 // TODO(cjhopman): Decide what to do about un-parseable entries. 166 // TODO(cjhopman): Decide what to do about un-parseable entries.
157 } 167 }
158 168
159 entries->push_back(entry); 169 entries->push_back(entry);
160 } 170 }
161 } 171 }
162 172
173 template <typename T>
174 void GetEntryFromTaskRunner(LevelDB* database,
175 const std::string& key,
176 T* entry,
177 bool* success) {
178 DCHECK(success);
179 DCHECK(entry);
180
181 std::string serialized_entry;
182 *success = database->Get(key, &serialized_entry);
183
184 if (success && !entry->ParseFromString(serialized_entry)) {
185 *success = false;
186 DLOG(WARNING) << "Unable to parse leveldb_proto entry";
187 // TODO(cjhopman): Decide what to do about un-parseable entries.
188 }
189 }
190
163 } // namespace 191 } // namespace
164 192
165 template <typename T> 193 template <typename T>
166 ProtoDatabaseImpl<T>::ProtoDatabaseImpl( 194 ProtoDatabaseImpl<T>::ProtoDatabaseImpl(
167 const scoped_refptr<base::SequencedTaskRunner>& task_runner) 195 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
168 : task_runner_(task_runner) {} 196 : task_runner_(task_runner) {}
169 197
170 template <typename T> 198 template <typename T>
171 ProtoDatabaseImpl<T>::~ProtoDatabaseImpl() { 199 ProtoDatabaseImpl<T>::~ProtoDatabaseImpl() {
172 DCHECK(thread_checker_.CalledOnValidThread()); 200 DCHECK(thread_checker_.CalledOnValidThread());
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 // Get this pointer before entries is base::Passed() so we can use it below. 275 // Get this pointer before entries is base::Passed() so we can use it below.
248 std::vector<T>* entries_ptr = entries.get(); 276 std::vector<T>* entries_ptr = entries.get();
249 277
250 task_runner_->PostTaskAndReply( 278 task_runner_->PostTaskAndReply(
251 FROM_HERE, base::Bind(LoadEntriesFromTaskRunner<T>, 279 FROM_HERE, base::Bind(LoadEntriesFromTaskRunner<T>,
252 base::Unretained(db_.get()), entries_ptr, success), 280 base::Unretained(db_.get()), entries_ptr, success),
253 base::Bind(RunLoadCallback<T>, callback, base::Owned(success), 281 base::Bind(RunLoadCallback<T>, callback, base::Owned(success),
254 base::Passed(&entries))); 282 base::Passed(&entries)));
255 } 283 }
256 284
285 template <typename T>
286 void ProtoDatabaseImpl<T>::GetEntry(
287 const std::string& key,
288 const typename ProtoDatabase<T>::GetCallback& callback) {
289 DCHECK(thread_checker_.CalledOnValidThread());
290 bool* success = new bool(false);
291
292 std::unique_ptr<T> entry(new T());
293 // Get this pointer before entry is base::Passed() so we can use it below.
294 T* entry_ptr = entry.get();
295
296 task_runner_->PostTaskAndReply(
297 FROM_HERE,
298 base::Bind(GetEntryFromTaskRunner<T>,
299 base::Unretained(db_.get()), key, entry_ptr, success),
300 base::Bind(RunGetCallback<T>, callback, base::Owned(success),
301 base::Passed(&entry)));
302 }
303
257 } // namespace leveldb_proto 304 } // namespace leveldb_proto
258 305
259 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ 306 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698