Make castv2 performance test work.
[chromium-blink-merge.git] / components / leveldb_proto / testing / fake_db.h
blob3bcc70e7b593d592ba75bde14ea6fdfc9c1c4785
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_
6 #define COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "components/leveldb_proto/proto_database.h"
17 namespace leveldb_proto {
18 namespace test {
20 template <typename T>
21 class FakeDB : public ProtoDatabase<T> {
22 typedef base::Callback<void(bool)> Callback;
24 public:
25 typedef typename base::hash_map<std::string, T> EntryMap;
27 explicit FakeDB(EntryMap* db);
28 virtual ~FakeDB();
30 virtual void Init(const base::FilePath& database_dir,
31 typename ProtoDatabase<T>::InitCallback callback)
32 override;
34 virtual void UpdateEntries(
35 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save,
36 scoped_ptr<std::vector<std::string> > keys_to_remove,
37 typename ProtoDatabase<T>::UpdateCallback callback) override;
39 virtual void LoadEntries(typename ProtoDatabase<T>::LoadCallback callback)
40 override;
41 base::FilePath& GetDirectory();
43 void InitCallback(bool success);
45 void LoadCallback(bool success);
47 void UpdateCallback(bool success);
49 static base::FilePath DirectoryForTestDB();
51 private:
52 static void RunLoadCallback(
53 typename ProtoDatabase<T>::LoadCallback callback,
54 scoped_ptr<typename std::vector<T> > entries,
55 bool success);
57 base::FilePath dir_;
58 EntryMap* db_;
60 Callback init_callback_;
61 Callback load_callback_;
62 Callback update_callback_;
65 template <typename T>
66 FakeDB<T>::FakeDB(EntryMap* db)
67 : db_(db) {}
69 template <typename T>
70 FakeDB<T>::~FakeDB() {}
72 template <typename T>
73 void FakeDB<T>::Init(const base::FilePath& database_dir,
74 typename ProtoDatabase<T>::InitCallback callback) {
75 dir_ = database_dir;
76 init_callback_ = callback;
79 template <typename T>
80 void FakeDB<T>::UpdateEntries(
81 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save,
82 scoped_ptr<std::vector<std::string> > keys_to_remove,
83 typename ProtoDatabase<T>::UpdateCallback callback) {
84 for (typename ProtoDatabase<T>::KeyEntryVector::iterator it =
85 entries_to_save->begin();
86 it != entries_to_save->end(); ++it) {
87 (*db_)[it->first] = it->second;
89 for (std::vector<std::string>::iterator it = keys_to_remove->begin();
90 it != keys_to_remove->end(); ++it) {
91 (*db_).erase(*it);
93 update_callback_ = callback;
96 template <typename T>
97 void FakeDB<T>::LoadEntries(typename ProtoDatabase<T>::LoadCallback callback) {
98 scoped_ptr<std::vector<T> > entries(new std::vector<T>());
99 for (typename EntryMap::iterator it = db_->begin(); it != db_->end(); ++it) {
100 entries->push_back(it->second);
102 load_callback_ =
103 base::Bind(RunLoadCallback, callback, base::Passed(&entries));
106 template <typename T>
107 base::FilePath& FakeDB<T>::GetDirectory() {
108 return dir_;
111 template <typename T>
112 void FakeDB<T>::InitCallback(bool success) {
113 init_callback_.Run(success);
114 init_callback_.Reset();
117 template <typename T>
118 void FakeDB<T>::LoadCallback(bool success) {
119 load_callback_.Run(success);
120 load_callback_.Reset();
123 template <typename T>
124 void FakeDB<T>::UpdateCallback(bool success) {
125 update_callback_.Run(success);
126 update_callback_.Reset();
129 // static
130 template <typename T>
131 void FakeDB<T>::RunLoadCallback(
132 typename ProtoDatabase<T>::LoadCallback callback,
133 scoped_ptr<typename std::vector<T> > entries, bool success) {
134 callback.Run(success, entries.Pass());
137 // static
138 template <typename T>
139 base::FilePath FakeDB<T>::DirectoryForTestDB() {
140 return base::FilePath(FILE_PATH_LITERAL("/fake/path"));
143 } // namespace test
144 } // namespace leveldb_proto
146 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_