OLD | NEW |
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 CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
7 | 7 |
8 #include <cstddef> | 8 #include <cstddef> |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "chrome/browser/predictors/glowplug_key_value_table.h" |
16 #include "chrome/browser/predictors/predictor_table_base.h" | 17 #include "chrome/browser/predictors/predictor_table_base.h" |
17 #include "chrome/browser/predictors/resource_prefetch_common.h" | 18 #include "chrome/browser/predictors/resource_prefetch_common.h" |
18 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" | 19 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" |
19 #include "components/precache/core/proto/precache.pb.h" | 20 #include "components/precache/core/proto/precache.pb.h" |
20 | 21 |
21 namespace sql { | |
22 class Statement; | |
23 } | |
24 | |
25 namespace predictors { | 22 namespace predictors { |
26 | 23 |
27 // From resource_prefetch_predictor.proto. | 24 // From resource_prefetch_predictor.proto. |
28 using RedirectStat = RedirectData_RedirectStat; | 25 using RedirectStat = RedirectData_RedirectStat; |
29 | 26 |
30 // Interface for database tables used by the ResourcePrefetchPredictor. | 27 // Interface for database tables used by the ResourcePrefetchPredictor. |
31 // All methods except the constructor and destructor need to be called on the DB | 28 // All methods except the constructor and destructor need to be called on the DB |
32 // thread. | 29 // thread. |
33 // | 30 // |
34 // Currently manages: | 31 // Currently manages: |
35 // - UrlResourceTable - resources per Urls. | 32 // - UrlResourceTable - key: url, value: PrefetchData |
36 // - UrlRedirectTable - redirects per Urls. | 33 // - UrlRedirectTable - key: url, value: RedirectData |
37 // - HostResourceTable - resources per host. | 34 // - HostResourceTable - key: host, value: PrefetchData |
38 // - HostRedirectTable - redirects per host. | 35 // - HostRedirectTable - key: host, value: RedirectData |
| 36 // - ManifestTable - key: host with stripped "www." prefix, |
| 37 // value: precache::PrecacheManifest |
| 38 // - OriginTable - key: host, value: OriginData |
39 class ResourcePrefetchPredictorTables : public PredictorTableBase { | 39 class ResourcePrefetchPredictorTables : public PredictorTableBase { |
40 public: | 40 public: |
41 typedef std::map<std::string, PrefetchData> PrefetchDataMap; | 41 typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
42 typedef std::map<std::string, RedirectData> RedirectDataMap; | 42 typedef std::map<std::string, RedirectData> RedirectDataMap; |
43 typedef std::map<std::string, precache::PrecacheManifest> ManifestDataMap; | 43 typedef std::map<std::string, precache::PrecacheManifest> ManifestDataMap; |
44 typedef std::map<std::string, OriginData> OriginDataMap; | 44 typedef std::map<std::string, OriginData> OriginDataMap; |
45 | 45 |
46 // Returns data for all Urls and Hosts. | 46 // Returns data for all Urls and Hosts. |
47 virtual void GetAllData(PrefetchDataMap* url_data_map, | 47 virtual void GetAllData(PrefetchDataMap* url_data_map, |
48 PrefetchDataMap* host_data_map, | 48 PrefetchDataMap* host_data_map, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 // The maximum length of the string that can be stored in the DB. | 123 // The maximum length of the string that can be stored in the DB. |
124 static constexpr size_t kMaxStringLength = 1024; | 124 static constexpr size_t kMaxStringLength = 1024; |
125 | 125 |
126 protected: | 126 protected: |
127 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() | 127 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() |
128 // instead of this constructor. | 128 // instead of this constructor. |
129 ResourcePrefetchPredictorTables(); | 129 ResourcePrefetchPredictorTables(); |
130 ~ResourcePrefetchPredictorTables() override; | 130 ~ResourcePrefetchPredictorTables() override; |
131 | 131 |
132 private: | 132 private: |
133 // Represents the type of information that is stored in prefetch database. | |
134 enum class PrefetchDataType { RESOURCE, REDIRECT, MANIFEST, ORIGIN }; | |
135 | |
136 enum class TableOperationType { INSERT, REMOVE }; | |
137 | |
138 friend class PredictorDatabaseInternal; | 133 friend class PredictorDatabaseInternal; |
139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 134 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
140 DatabaseVersionIsSet); | 135 DatabaseVersionIsSet); |
141 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 136 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
142 DatabaseIsResetWhenIncompatible); | 137 DatabaseIsResetWhenIncompatible); |
143 | 138 |
144 // Database version. Always increment it when any change is made to the data | 139 // Database version. Always increment it when any change is made to the data |
145 // schema (including the .proto). | 140 // schema (including the .proto). |
146 static constexpr int kDatabaseVersion = 7; | 141 static constexpr int kDatabaseVersion = 7; |
147 | 142 |
148 // Helper functions below help perform functions on the Url and host table | |
149 // using the same code. | |
150 void GetAllResourceDataHelper(PrefetchKeyType key_type, | |
151 PrefetchDataMap* data_map); | |
152 void GetAllRedirectDataHelper(PrefetchKeyType key_type, | |
153 RedirectDataMap* redirect_map); | |
154 void GetAllManifestDataHelper(ManifestDataMap* manifest_map); | |
155 void GetAllOriginDataHelper(OriginDataMap* manifest_map); | |
156 | |
157 void UpdateDataHelper(PrefetchKeyType key_type, | |
158 PrefetchDataType data_type, | |
159 const std::string& key, | |
160 const google::protobuf::MessageLite& data); | |
161 void DeleteDataHelper(PrefetchKeyType key_type, | |
162 PrefetchDataType data_type, | |
163 const std::vector<std::string>& keys); | |
164 | |
165 // PredictorTableBase: | 143 // PredictorTableBase: |
166 void CreateTableIfNonExistent() override; | 144 void CreateTableIfNonExistent() override; |
167 void LogDatabaseStats() override; | 145 void LogDatabaseStats() override; |
168 | 146 |
169 static bool DropTablesIfOutdated(sql::Connection* db); | 147 static bool DropTablesIfOutdated(sql::Connection* db); |
170 static int GetDatabaseVersion(sql::Connection* db); | 148 static int GetDatabaseVersion(sql::Connection* db); |
171 static bool SetDatabaseVersion(sql::Connection* db, int version); | 149 static bool SetDatabaseVersion(sql::Connection* db, int version); |
172 | 150 |
173 // Helper to return cached Statements. | 151 std::unique_ptr<GlowplugKeyValueTable<PrefetchData>> url_resource_table_; |
174 std::unique_ptr<sql::Statement> GetTableUpdateStatement( | 152 std::unique_ptr<GlowplugKeyValueTable<RedirectData>> url_redirect_table_; |
175 PrefetchKeyType key_type, | 153 std::unique_ptr<GlowplugKeyValueTable<PrefetchData>> host_resource_table_; |
176 PrefetchDataType data_type, | 154 std::unique_ptr<GlowplugKeyValueTable<RedirectData>> host_redirect_table_; |
177 TableOperationType op_type); | 155 std::unique_ptr<GlowplugKeyValueTable<precache::PrecacheManifest>> |
178 | 156 manifest_table_; |
179 static const char* GetTableName(PrefetchKeyType key_type, | 157 std::unique_ptr<GlowplugKeyValueTable<OriginData>> origin_table_; |
180 PrefetchDataType data_type); | |
181 | 158 |
182 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 159 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
183 }; | 160 }; |
184 | 161 |
185 } // namespace predictors | 162 } // namespace predictors |
186 | 163 |
187 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 164 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
OLD | NEW |