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

Unified 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: not found vs error 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/leveldb_proto/proto_database_impl.h
diff --git a/components/leveldb_proto/proto_database_impl.h b/components/leveldb_proto/proto_database_impl.h
index 4de21e9d4ff805232cfd2b3f33ad0aaa66602c5a..da02eed13876f266f1a45fc883aeae95c061039f 100644
--- a/components/leveldb_proto/proto_database_impl.h
+++ b/components/leveldb_proto/proto_database_impl.h
@@ -53,6 +53,9 @@ class ProtoDatabaseImpl : public ProtoDatabase<T> {
const typename ProtoDatabase<T>::UpdateCallback& callback) override;
void LoadEntries(
const typename ProtoDatabase<T>::LoadCallback& callback) override;
+ void GetEntry(
+ const std::string& key,
+ const typename ProtoDatabase<T>::GetCallback& callback) override;
void Destroy(
const typename ProtoDatabase<T>::DestroyCallback& callback) override;
@@ -97,6 +100,14 @@ void RunLoadCallback(const typename ProtoDatabase<T>::LoadCallback& callback,
}
template <typename T>
+void RunGetCallback(const typename ProtoDatabase<T>::GetCallback& callback,
+ const bool* success,
+ const bool* found,
+ std::unique_ptr<T> entry) {
+ callback.Run(*success, *found ? std::move(entry) : nullptr);
+}
+
+template <typename T>
void RunDestroyCallback(
const typename ProtoDatabase<T>::DestroyCallback& callback,
const bool* success) {
@@ -160,6 +171,26 @@ void LoadEntriesFromTaskRunner(LevelDB* database,
}
}
+template <typename T>
+void GetEntryFromTaskRunner(LevelDB* database,
+ const std::string& key,
+ T* entry,
+ bool* found,
+ bool* success) {
+ DCHECK(success);
+ DCHECK(found);
+ DCHECK(entry);
+
+ std::string serialized_entry;
+ *success = database->Get(key, found, &serialized_entry);
+
+ if (success && !entry->ParseFromString(serialized_entry)) {
+ *found = false;
+ DLOG(WARNING) << "Unable to parse leveldb_proto entry";
+ // TODO(cjhopman): Decide what to do about un-parseable entries.
+ }
+}
+
} // namespace
template <typename T>
@@ -254,6 +285,26 @@ void ProtoDatabaseImpl<T>::LoadEntries(
base::Passed(&entries)));
}
+template <typename T>
+void ProtoDatabaseImpl<T>::GetEntry(
+ const std::string& key,
+ const typename ProtoDatabase<T>::GetCallback& callback) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ bool* success = new bool(false);
+ bool* found = new bool(false);
+
+ std::unique_ptr<T> entry(new T());
+ // Get this pointer before entry is base::Passed() so we can use it below.
+ T* entry_ptr = entry.get();
+
+ task_runner_->PostTaskAndReply(
+ FROM_HERE,
+ base::Bind(GetEntryFromTaskRunner<T>, base::Unretained(db_.get()), key,
+ entry_ptr, found, success),
+ base::Bind(RunGetCallback<T>, callback, base::Owned(success),
+ base::Owned(found), base::Passed(&entry)));
+}
+
} // namespace leveldb_proto
#endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_
« 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