alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 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 | |
David Van Cleve | a86bcb2 | 2020-01-21 17:25:44 | [diff] [blame] | 5 | #ifndef COMPONENTS_SQLITE_PROTO_KEY_VALUE_TABLE_H_ |
| 6 | #define COMPONENTS_SQLITE_PROTO_KEY_VALUE_TABLE_H_ |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 12 | #include "sql/statement.h" |
| 13 | |
| 14 | namespace google { |
| 15 | namespace protobuf { |
| 16 | class MessageLite; |
| 17 | } |
Alex Ilin | dc57769 | 2019-05-29 09:27:29 | [diff] [blame] | 18 | } // namespace google |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 19 | |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 20 | namespace sqlite_proto { |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 21 | |
| 22 | namespace internal { |
| 23 | |
| 24 | void BindDataToStatement(const std::string& key, |
| 25 | const google::protobuf::MessageLite& data, |
| 26 | sql::Statement* statement); |
| 27 | |
| 28 | std::string GetSelectAllSql(const std::string& table_name); |
| 29 | std::string GetReplaceSql(const std::string& table_name); |
| 30 | std::string GetDeleteSql(const std::string& table_name); |
| 31 | std::string GetDeleteAllSql(const std::string& table_name); |
| 32 | |
| 33 | } // namespace internal |
| 34 | |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 35 | // The backend class helps perform database operations on a single table. The |
| 36 | // table name is passed as a constructor argument. The table schema is fixed: it |
| 37 | // always consists of two columns, TEXT type "key" and BLOB type "proto". The |
| 38 | // class doesn't manage the creation and the deletion of the table. |
| 39 | // |
Alexandr Ilin | 9a16ee7 | 2017-08-16 13:13:34 | [diff] [blame] | 40 | // All the functions except of the constructor must be called on a DB sequence |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 41 | // of the corresponding TableManager. The preferred way to call the methods of |
| 42 | // this class is passing the method to TableManager::ScheduleDBTask(). |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 43 | // |
| 44 | // Example: |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 45 | // manager_->ScheduleDBTask( |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 46 | // FROM_HERE, |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 47 | // base::BindOnce(&KeyValueTable<PrefetchData>::UpdateData, |
David Van Cleve | 1b47a51 | 2020-08-13 22:13:39 | [diff] [blame] | 48 | // table_->AsWeakPtr(), key, data)); |
| 49 | // |
| 50 | // TODO(crbug.com/1115398): Supporting weak pointers is a temporary measure |
| 51 | // mitigating a crash caused by complex lifetime requirements for KeyValueTable |
| 52 | // relative to the related classes. Making KeyValueTable<T> stateless instead |
| 53 | // could be a better way to resolve these lifetime issues in the long run. |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 54 | template <typename T> |
David Van Cleve | 1b47a51 | 2020-08-13 22:13:39 | [diff] [blame] | 55 | class KeyValueTable : public base::SupportsWeakPtr<KeyValueTable<T>> { |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 56 | public: |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 57 | explicit KeyValueTable(const std::string& table_name); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 58 | // Virtual for testing. |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 59 | virtual ~KeyValueTable() = default; |
| 60 | |
| 61 | KeyValueTable(const KeyValueTable&) = delete; |
| 62 | KeyValueTable& operator=(const KeyValueTable&) = delete; |
| 63 | |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 64 | virtual void GetAllData(std::map<std::string, T>* data_map, |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 65 | sql::Database* db) const; |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 66 | virtual void UpdateData(const std::string& key, |
| 67 | const T& data, |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 68 | sql::Database* db); |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 69 | virtual void DeleteData(const std::vector<std::string>& keys, |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 70 | sql::Database* db); |
| 71 | virtual void DeleteAllData(sql::Database* db); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | const std::string table_name_; |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | template <typename T> |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 78 | KeyValueTable<T>::KeyValueTable(const std::string& table_name) |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 79 | : table_name_(table_name) {} |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 80 | |
| 81 | template <typename T> |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 82 | void KeyValueTable<T>::GetAllData(std::map<std::string, T>* data_map, |
| 83 | sql::Database* db) const { |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 84 | sql::Statement reader(db->GetUniqueStatement( |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 85 | ::sqlite_proto::internal::GetSelectAllSql(table_name_).c_str())); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 86 | while (reader.Step()) { |
| 87 | auto it = data_map->emplace(reader.ColumnString(0), T()).first; |
Victor Costan | 1268a999 | 2021-07-16 17:16:39 | [diff] [blame] | 88 | base::span<const uint8_t> blob = reader.ColumnBlob(1); |
| 89 | it->second.ParseFromArray(blob.data(), blob.size()); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | template <typename T> |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 94 | void KeyValueTable<T>::UpdateData(const std::string& key, |
| 95 | const T& data, |
| 96 | sql::Database* db) { |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 97 | sql::Statement inserter(db->GetUniqueStatement( |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 98 | ::sqlite_proto::internal::GetReplaceSql(table_name_).c_str())); |
| 99 | ::sqlite_proto::internal::BindDataToStatement(key, data, &inserter); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 100 | inserter.Run(); |
| 101 | } |
| 102 | |
| 103 | template <typename T> |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 104 | void KeyValueTable<T>::DeleteData(const std::vector<std::string>& keys, |
| 105 | sql::Database* db) { |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 106 | sql::Statement deleter(db->GetUniqueStatement( |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 107 | ::sqlite_proto::internal::GetDeleteSql(table_name_).c_str())); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 108 | for (const auto& key : keys) { |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 109 | deleter.BindString(0, key); |
| 110 | deleter.Run(); |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 111 | deleter.Reset(true); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | template <typename T> |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 116 | void KeyValueTable<T>::DeleteAllData(sql::Database* db) { |
Alexandr Ilin | eb0f08a | 2017-05-30 10:55:34 | [diff] [blame] | 117 | sql::Statement deleter(db->GetUniqueStatement( |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 118 | ::sqlite_proto::internal::GetDeleteAllSql(table_name_).c_str())); |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 119 | deleter.Run(); |
| 120 | } |
| 121 | |
David Van Cleve | a4e3ee1 | 2020-01-21 20:07:01 | [diff] [blame] | 122 | } // namespace sqlite_proto |
alexilin | 6175b0a | 2017-04-28 17:34:40 | [diff] [blame] | 123 | |
David Van Cleve | a86bcb2 | 2020-01-21 17:25:44 | [diff] [blame] | 124 | #endif // COMPONENTS_SQLITE_PROTO_KEY_VALUE_TABLE_H_ |