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

Side by Side Diff: components/leveldb_proto/testing/fake_db.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 unified diff | Download patch
« no previous file with comments | « components/leveldb_proto/proto_database_impl_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_TESTING_FAKE_DB_H_ 5 #ifndef COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_
6 #define COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ 6 #define COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 21 matching lines...) Expand all
32 void Init(const char* client_name, 32 void Init(const char* client_name,
33 const base::FilePath& database_dir, 33 const base::FilePath& database_dir,
34 const typename ProtoDatabase<T>::InitCallback& callback) override; 34 const typename ProtoDatabase<T>::InitCallback& callback) override;
35 void UpdateEntries( 35 void UpdateEntries(
36 std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> 36 std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector>
37 entries_to_save, 37 entries_to_save,
38 std::unique_ptr<std::vector<std::string>> keys_to_remove, 38 std::unique_ptr<std::vector<std::string>> keys_to_remove,
39 const typename ProtoDatabase<T>::UpdateCallback& callback) override; 39 const typename ProtoDatabase<T>::UpdateCallback& callback) override;
40 void LoadEntries( 40 void LoadEntries(
41 const typename ProtoDatabase<T>::LoadCallback& callback) override; 41 const typename ProtoDatabase<T>::LoadCallback& callback) override;
42 void GetEntry(
43 const std::string& key,
44 const typename ProtoDatabase<T>::GetCallback& callback) override;
42 void Destroy( 45 void Destroy(
43 const typename ProtoDatabase<T>::DestroyCallback& callback) override; 46 const typename ProtoDatabase<T>::DestroyCallback& callback) override;
44 47
45 base::FilePath& GetDirectory(); 48 base::FilePath& GetDirectory();
46 49
47 void InitCallback(bool success); 50 void InitCallback(bool success);
48 51
49 void LoadCallback(bool success); 52 void LoadCallback(bool success);
50 53
54 void GetCallback(bool success);
55
51 void UpdateCallback(bool success); 56 void UpdateCallback(bool success);
52 57
53 static base::FilePath DirectoryForTestDB(); 58 static base::FilePath DirectoryForTestDB();
54 59
55 private: 60 private:
56 static void RunLoadCallback( 61 static void RunLoadCallback(
57 const typename ProtoDatabase<T>::LoadCallback& callback, 62 const typename ProtoDatabase<T>::LoadCallback& callback,
58 std::unique_ptr<typename std::vector<T>> entries, 63 std::unique_ptr<typename std::vector<T>> entries,
59 bool success); 64 bool success);
60 65
66 static void RunGetCallback(
67 const typename ProtoDatabase<T>::GetCallback& callback,
68 std::unique_ptr<T> entry,
69 bool success);
70
61 base::FilePath dir_; 71 base::FilePath dir_;
62 EntryMap* db_; 72 EntryMap* db_;
63 73
64 Callback init_callback_; 74 Callback init_callback_;
65 Callback load_callback_; 75 Callback load_callback_;
76 Callback get_callback_;
66 Callback update_callback_; 77 Callback update_callback_;
67 }; 78 };
68 79
69 template <typename T> 80 template <typename T>
70 FakeDB<T>::FakeDB(EntryMap* db) 81 FakeDB<T>::FakeDB(EntryMap* db)
71 : db_(db) {} 82 : db_(db) {}
72 83
73 template <typename T> 84 template <typename T>
74 FakeDB<T>::~FakeDB() {} 85 FakeDB<T>::~FakeDB() {}
75 86
(...skipping 24 matching lines...) Expand all
100 const typename ProtoDatabase<T>::LoadCallback& callback) { 111 const typename ProtoDatabase<T>::LoadCallback& callback) {
101 std::unique_ptr<std::vector<T>> entries(new std::vector<T>()); 112 std::unique_ptr<std::vector<T>> entries(new std::vector<T>());
102 for (const auto& pair : *db_) 113 for (const auto& pair : *db_)
103 entries->push_back(pair.second); 114 entries->push_back(pair.second);
104 115
105 load_callback_ = 116 load_callback_ =
106 base::Bind(RunLoadCallback, callback, base::Passed(&entries)); 117 base::Bind(RunLoadCallback, callback, base::Passed(&entries));
107 } 118 }
108 119
109 template <typename T> 120 template <typename T>
121 void FakeDB<T>::GetEntry(
122 const std::string& key,
123 const typename ProtoDatabase<T>::GetCallback& callback) {
124 std::unique_ptr<T> entry;
125 auto it = db_->find(key);
126 if (it != db_->end())
127 entry.reset(new T(it->second));
128
129 get_callback_ = base::Bind(RunGetCallback, callback, base::Passed(&entry));
130 }
131
132 template <typename T>
110 void FakeDB<T>::Destroy( 133 void FakeDB<T>::Destroy(
111 const typename ProtoDatabase<T>::DestroyCallback& callback) { 134 const typename ProtoDatabase<T>::DestroyCallback& callback) {
112 } 135 }
113 136
114 template <typename T> 137 template <typename T>
115 base::FilePath& FakeDB<T>::GetDirectory() { 138 base::FilePath& FakeDB<T>::GetDirectory() {
116 return dir_; 139 return dir_;
117 } 140 }
118 141
119 template <typename T> 142 template <typename T>
120 void FakeDB<T>::InitCallback(bool success) { 143 void FakeDB<T>::InitCallback(bool success) {
121 init_callback_.Run(success); 144 init_callback_.Run(success);
122 init_callback_.Reset(); 145 init_callback_.Reset();
123 } 146 }
124 147
125 template <typename T> 148 template <typename T>
126 void FakeDB<T>::LoadCallback(bool success) { 149 void FakeDB<T>::LoadCallback(bool success) {
127 load_callback_.Run(success); 150 load_callback_.Run(success);
128 load_callback_.Reset(); 151 load_callback_.Reset();
129 } 152 }
130 153
131 template <typename T> 154 template <typename T>
155 void FakeDB<T>::GetCallback(bool success) {
156 get_callback_.Run(success);
157 get_callback_.Reset();
158 }
159
160 template <typename T>
132 void FakeDB<T>::UpdateCallback(bool success) { 161 void FakeDB<T>::UpdateCallback(bool success) {
133 update_callback_.Run(success); 162 update_callback_.Run(success);
134 update_callback_.Reset(); 163 update_callback_.Reset();
135 } 164 }
136 165
137 // static 166 // static
138 template <typename T> 167 template <typename T>
139 void FakeDB<T>::RunLoadCallback( 168 void FakeDB<T>::RunLoadCallback(
140 const typename ProtoDatabase<T>::LoadCallback& callback, 169 const typename ProtoDatabase<T>::LoadCallback& callback,
141 std::unique_ptr<typename std::vector<T>> entries, 170 std::unique_ptr<typename std::vector<T>> entries,
142 bool success) { 171 bool success) {
143 callback.Run(success, std::move(entries)); 172 callback.Run(success, std::move(entries));
144 } 173 }
145 174
146 // static 175 // static
147 template <typename T> 176 template <typename T>
177 void FakeDB<T>::RunGetCallback(
178 const typename ProtoDatabase<T>::GetCallback& callback,
179 std::unique_ptr<T> entry,
180 bool success) {
181 callback.Run(success, std::move(entry));
182 }
183
184 // static
185 template <typename T>
148 base::FilePath FakeDB<T>::DirectoryForTestDB() { 186 base::FilePath FakeDB<T>::DirectoryForTestDB() {
149 return base::FilePath(FILE_PATH_LITERAL("/fake/path")); 187 return base::FilePath(FILE_PATH_LITERAL("/fake/path"));
150 } 188 }
151 189
152 } // namespace test 190 } // namespace test
153 } // namespace leveldb_proto 191 } // namespace leveldb_proto
154 192
155 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ 193 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_
OLDNEW
« no previous file with comments | « components/leveldb_proto/proto_database_impl_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698