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

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: Untemplate some code. 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
« no previous file with comments | « chrome/browser/BUILD.gn ('k') | chrome/browser/predictors/glowplug_key_value_table.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "sql/statement.h"
14
15 namespace google {
16 namespace protobuf {
17 class MessageLite;
18 }
19 }
20
21 namespace predictors {
22
23 namespace internal {
24
25 void BindDataToStatement(const std::string& key,
26 const google::protobuf::MessageLite& data,
27 sql::Statement* statement);
28
29 std::string GetSelectAllSql(const std::string& table_name);
30 std::string GetInsertSql(const std::string& table_name);
31 std::string GetDeleteSql(const std::string& table_name);
32 std::string GetDeleteAllSql(const std::string& table_name);
33
34 } // namespace internal
35
36 template <typename T>
37 class GlowplugKeyValueTable {
38 public:
39 GlowplugKeyValueTable(const std::string& table_name, sql::Connection* db);
40 // Virtual for testing.
41 virtual void GetAllData(std::map<std::string, T>* data_map) const;
42 virtual void UpdateData(const std::string& key, const T& data);
43 virtual void DeleteData(const std::vector<std::string>& keys);
44 virtual void DeleteAllData();
45
46 private:
47 const std::string table_name_;
48 sql::Connection* db_;
49
50 DISALLOW_COPY_AND_ASSIGN(GlowplugKeyValueTable);
51 };
52
53 template <typename T>
54 GlowplugKeyValueTable<T>::GlowplugKeyValueTable(const std::string& table_name,
55 sql::Connection* db)
56 : table_name_(table_name), db_(db) {}
57
58 template <typename T>
59 void GlowplugKeyValueTable<T>::GetAllData(
60 std::map<std::string, T>* data_map) const {
61 sql::Statement reader(db_->GetUniqueStatement(
62 ::predictors::internal::GetSelectAllSql(table_name_).c_str()));
63 while (reader.Step()) {
64 auto it = data_map->emplace(reader.ColumnString(0), T()).first;
Benoit L 2017/04/28 15:12:34 That's quick, this was allowed only 4 days ago :-)
alexilin 2017/04/28 16:27:26 It's a pleasure to be the one of the first users.
65 int size = reader.ColumnByteLength(1);
66 const void* blob = reader.ColumnBlob(1);
67 DCHECK(blob);
68 it->second.ParseFromArray(blob, size);
69 }
70 }
71
72 template <typename T>
73 void GlowplugKeyValueTable<T>::UpdateData(const std::string& key,
74 const T& data) {
75 db_->BeginTransaction();
76
77 // Delete the older data from the table.
78 sql::Statement deleter(db_->GetUniqueStatement(
79 ::predictors::internal::GetDeleteSql(table_name_).c_str()));
80 deleter.BindString(0, key);
81 bool success = deleter.Run();
82
83 if (success) {
84 // Add the new data to the table.
85 sql::Statement inserter(db_->GetUniqueStatement(
86 ::predictors::internal::GetInsertSql(table_name_).c_str()));
87 ::predictors::internal::BindDataToStatement(key, data, &inserter);
88 success = inserter.Run();
89 }
90
91 if (!success)
92 db_->RollbackTransaction();
93 else
94 db_->CommitTransaction();
95 }
96
97 template <typename T>
98 void GlowplugKeyValueTable<T>::DeleteData(
99 const std::vector<std::string>& keys) {
100 auto statement = db_->GetUniqueStatement(
101 ::predictors::internal::GetDeleteSql(table_name_).c_str());
102 for (const auto& key : keys) {
103 sql::Statement deleter(statement);
104 deleter.BindString(0, key);
105 deleter.Run();
106 }
107 }
108
109 template <typename T>
110 void GlowplugKeyValueTable<T>::DeleteAllData() {
111 sql::Statement deleter(db_->GetUniqueStatement(
112 ::predictors::internal::GetDeleteAllSql(table_name_).c_str()));
113 deleter.Run();
114 }
115
116 } // namespace predictors
117
118 #endif // CHROME_BROWSER_PREDICTORS_GLOWPLUG_KEY_VALUE_TABLE_H_
OLDNEW
« no previous file with comments | « chrome/browser/BUILD.gn ('k') | chrome/browser/predictors/glowplug_key_value_table.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698