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

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: 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 side-by-side diff with in-line comments
Download patch
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..a062270dd780dcf6e3c055f7e3d726c1def1d9e6 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,13 @@ void RunLoadCallback(const typename ProtoDatabase<T>::LoadCallback& callback,
}
template <typename T>
+void RunGetCallback(const typename ProtoDatabase<T>::GetCallback& callback,
+ const bool* success,
+ std::unique_ptr<T> entry) {
+ callback.Run(*success, *success ? std::move(entry) : nullptr);
+}
+
+template <typename T>
void RunDestroyCallback(
const typename ProtoDatabase<T>::DestroyCallback& callback,
const bool* success) {
@@ -160,6 +170,24 @@ void LoadEntriesFromTaskRunner(LevelDB* database,
}
}
+template <typename T>
+void GetEntryFromTaskRunner(LevelDB* database,
+ const std::string& key,
+ T* entry,
+ bool* success) {
+ DCHECK(success);
+ DCHECK(entry);
+
+ std::string serialized_entry;
+ *success = database->Get(key, &serialized_entry);
+
+ if (success && !entry->ParseFromString(serialized_entry)) {
+ *success = 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 +282,25 @@ 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);
+
+ 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, success),
+ base::Bind(RunGetCallback<T>, callback, base::Owned(success),
+ base::Passed(&entry)));
+}
+
} // namespace leveldb_proto
#endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_

Powered by Google App Engine
This is Rietveld 408576698