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

Side by Side Diff: chrome/browser/predictors/glowplug_key_value_table.h

Issue 2851473002: predictors: Extract sql key-value tables into separate class. (Closed)
Patch Set: Created 3 years, 7 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
(Empty)
1 // Copyright 2017 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.
4
5 #ifndef CHROME_BROWSER_PREDICTORS_GLOWPLUG_KEY_VALUE_TABLE_H_
6 #define CHROME_BROWSER_PREDICTORS_GLOWPLUG_KEY_VALUE_TABLE_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/strings/stringprintf.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "sql/statement.h"
15
16 namespace predictors {
17
18 namespace internal {
19
20 template <typename T>
21 void BindDataToStatement(const std::string& key,
Benoit L 2017/04/27 15:49:02 Does this need to be a template?
alexilin 2017/04/28 14:54:52 Alternatives? I could use protobuf::MessageLite in
Benoit L 2017/04/28 15:12:34 Thanks, that's better this way. The function is in
22 const T& data,
23 sql::Statement* statement) {
24 int size = data.ByteSize();
25 DCHECK_GT(size, 0);
26 std::vector<char> proto_buffer(size);
27 data.SerializeToArray(&proto_buffer[0], size);
28
29 statement->BindString(0, key);
30 statement->BindBlob(1, &proto_buffer[0], size);
31 }
32
33 } // namespace internal
34
35 template <typename T>
36 class GlowplugKeyValueTable {
37 public:
38 GlowplugKeyValueTable(const std::string& table_name, sql::Connection* db);
39 virtual void GetAllData(std::map<std::string, T>* data_map) const;
Benoit L 2017/04/27 15:49:02 nit: Why virtual?
alexilin 2017/04/28 14:54:52 Eventually, for testing. Added a comment, thanks!
40 virtual void UpdateData(const std::string& key, const T& data);
41 virtual void DeleteData(const std::vector<std::string>& keys);
42 virtual void DeleteAllData();
43
44 private:
45 std::string GetSelectAllSql() const {
Benoit L 2017/04/27 15:49:02 Can these methods not be templates? I mean, can th
alexilin 2017/04/28 14:54:52 Then I have to move the implementation of these me
46 return base::StringPrintf("SELECT * FROM %s", table_name_.c_str());
47 }
48 std::string GetInsertSql() const {
49 return base::StringPrintf("INSERT INTO %s (key, proto) VALUES (?,?)",
50 table_name_.c_str());
51 }
52 std::string GetDeleteSql() const {
53 return base::StringPrintf("DELETE FROM %s WHERE key=?",
54 table_name_.c_str());
55 }
56 std::string GetDeleteAllSql() const {
57 return base::StringPrintf("DELETE FROM %s", table_name_.c_str());
58 }
59
60 const std::string table_name_;
61 sql::Connection* db_;
62
63 DISALLOW_COPY_AND_ASSIGN(GlowplugKeyValueTable);
64 };
65
66 template <typename T>
67 GlowplugKeyValueTable<T>::GlowplugKeyValueTable(const std::string& table_name,
68 sql::Connection* db)
69 : table_name_(table_name), db_(db) {}
70
71 template <typename T>
72 void GlowplugKeyValueTable<T>::GetAllData(
73 std::map<std::string, T>* data_map) const {
74 sql::Statement reader(db_->GetUniqueStatement(GetSelectAllSql().c_str()));
75 while (reader.Step()) {
76 auto it = data_map->emplace(reader.ColumnString(0), T()).first;
77 int size = reader.ColumnByteLength(1);
78 const void* blob = reader.ColumnBlob(1);
79 DCHECK(blob);
80 it->second.ParseFromArray(blob, size);
81 }
82 }
83
84 template <typename T>
85 void GlowplugKeyValueTable<T>::UpdateData(const std::string& key,
86 const T& data) {
87 db_->BeginTransaction();
88
89 // Delete the older data from the table.
Benoit L 2017/04/27 15:49:02 Thinking about it, why do we delete and then inser
alexilin 2017/04/28 16:27:25 Done.
90 sql::Statement deleter(db_->GetUniqueStatement(GetDeleteSql().c_str()));
91 deleter.BindString(0, key);
92 bool success = deleter.Run();
93
94 if (success) {
95 // Add the new data to the table.
96 sql::Statement inserter(db_->GetUniqueStatement(GetInsertSql().c_str()));
97 ::predictors::internal::BindDataToStatement(key, data, &inserter);
98 success = inserter.Run();
99 }
100
101 if (!success)
102 db_->RollbackTransaction();
103 else
104 db_->CommitTransaction();
105 }
106
107 template <typename T>
108 void GlowplugKeyValueTable<T>::DeleteData(
109 const std::vector<std::string>& keys) {
110 for (const auto& key : keys) {
111 sql::Statement deleter(db_->GetUniqueStatement(GetDeleteSql().c_str()));
Benoit L 2017/04/27 15:49:03 nit: put the statement outside the loop? Otherwise
alexilin 2017/04/28 14:54:52 Put the statement outside the loop - done.
112 deleter.BindString(0, key);
113 deleter.Run();
114 }
115 }
116
117 template <typename T>
118 void GlowplugKeyValueTable<T>::DeleteAllData() {
119 sql::Statement deleter(db_->GetUniqueStatement(GetDeleteAllSql().c_str()));
120 deleter.Run();
121 }
122
123 } // namespace predictors
124
125 #endif // CHROME_BROWSER_PREDICTORS_GLOWPLUG_KEY_VALUE_TABLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698