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/predictor_table_base.h" | 16 #include "chrome/browser/predictors/predictor_table_base.h" |
17 #include "chrome/browser/predictors/resource_prefetch_common.h" | 17 #include "chrome/browser/predictors/resource_prefetch_common.h" |
18 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" | 18 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" |
| 19 #include "components/precache/core/proto/precache.pb.h" |
19 | 20 |
20 namespace sql { | 21 namespace sql { |
21 class Statement; | 22 class Statement; |
22 } | 23 } |
23 | 24 |
24 namespace predictors { | 25 namespace predictors { |
25 | 26 |
26 // From resource_prefetch_predictor.proto. | 27 // From resource_prefetch_predictor.proto. |
27 using RedirectStat = RedirectData_RedirectStat; | 28 using RedirectStat = RedirectData_RedirectStat; |
28 | 29 |
29 // Interface for database tables used by the ResourcePrefetchPredictor. | 30 // Interface for database tables used by the ResourcePrefetchPredictor. |
30 // All methods except the constructor and destructor need to be called on the DB | 31 // All methods except the constructor and destructor need to be called on the DB |
31 // thread. | 32 // thread. |
32 // | 33 // |
33 // Currently manages: | 34 // Currently manages: |
34 // - UrlResourceTable - resources per Urls. | 35 // - UrlResourceTable - resources per Urls. |
35 // - UrlRedirectTable - redirects per Urls. | 36 // - UrlRedirectTable - redirects per Urls. |
36 // - HostResourceTable - resources per host. | 37 // - HostResourceTable - resources per host. |
37 // - HostRedirectTable - redirects per host. | 38 // - HostRedirectTable - redirects per host. |
38 class ResourcePrefetchPredictorTables : public PredictorTableBase { | 39 class ResourcePrefetchPredictorTables : public PredictorTableBase { |
39 public: | 40 public: |
40 // Map from primary key to PrefetchData for the key. | |
41 typedef std::map<std::string, PrefetchData> PrefetchDataMap; | 41 typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
42 | |
43 // Map from primary key to RedirectData for the key. | |
44 typedef std::map<std::string, RedirectData> RedirectDataMap; | 42 typedef std::map<std::string, RedirectData> RedirectDataMap; |
| 43 typedef std::map<std::string, precache::PrecacheManifest> ManifestDataMap; |
45 | 44 |
46 // Returns data for all Urls and Hosts. | 45 // Returns data for all Urls and Hosts. |
47 virtual void GetAllData(PrefetchDataMap* url_data_map, | 46 virtual void GetAllData(PrefetchDataMap* url_data_map, |
48 PrefetchDataMap* host_data_map, | 47 PrefetchDataMap* host_data_map, |
49 RedirectDataMap* url_redirect_data_map, | 48 RedirectDataMap* url_redirect_data_map, |
50 RedirectDataMap* host_redirect_data_map); | 49 RedirectDataMap* host_redirect_data_map, |
| 50 ManifestDataMap* manifest_map); |
51 | 51 |
52 // Updates data for a Url and a host. If either of the |url_data| or | 52 // Updates data for a Url and a host. If either of the |url_data| or |
53 // |host_data| or |url_redirect_data| or |host_redirect_data| has an empty | 53 // |host_data| or |url_redirect_data| or |host_redirect_data| has an empty |
54 // primary key, it will be ignored. | 54 // primary key, it will be ignored. |
55 // Note that the Urls and primary key in |url_data|, |host_data|, | 55 // Note that the Urls and primary key in |url_data|, |host_data|, |
56 // |url_redirect_data| and |host_redirect_data| should be less than | 56 // |url_redirect_data| and |host_redirect_data| should be less than |
57 // |kMaxStringLength| in length. | 57 // |kMaxStringLength| in length. |
58 virtual void UpdateData(const PrefetchData& url_data, | 58 virtual void UpdateData(const PrefetchData& url_data, |
59 const PrefetchData& host_data, | 59 const PrefetchData& host_data, |
60 const RedirectData& url_redirect_data, | 60 const RedirectData& url_redirect_data, |
61 const RedirectData& host_redirect_data); | 61 const RedirectData& host_redirect_data); |
62 | 62 |
| 63 // Updates manifest data for the input |host|. |
| 64 virtual void UpdateManifestData( |
| 65 const std::string& host, |
| 66 const precache::PrecacheManifest& manifest_data); |
| 67 |
63 // Delete data for the input |urls| and |hosts|. | 68 // Delete data for the input |urls| and |hosts|. |
64 virtual void DeleteResourceData(const std::vector<std::string>& urls, | 69 virtual void DeleteResourceData(const std::vector<std::string>& urls, |
65 const std::vector<std::string>& hosts); | 70 const std::vector<std::string>& hosts); |
66 | 71 |
67 // Wrapper over DeleteResourceData for convenience. | 72 // Wrapper over DeleteResourceData for convenience. |
68 virtual void DeleteSingleResourceDataPoint(const std::string& key, | 73 virtual void DeleteSingleResourceDataPoint(const std::string& key, |
69 PrefetchKeyType key_type); | 74 PrefetchKeyType key_type); |
70 | 75 |
71 // Delete data for the input |urls| and |hosts|. | 76 // Delete data for the input |urls| and |hosts|. |
72 virtual void DeleteRedirectData(const std::vector<std::string>& urls, | 77 virtual void DeleteRedirectData(const std::vector<std::string>& urls, |
73 const std::vector<std::string>& hosts); | 78 const std::vector<std::string>& hosts); |
74 | 79 |
75 // Wrapper over DeleteRedirectData for convenience. | 80 // Wrapper over DeleteRedirectData for convenience. |
76 virtual void DeleteSingleRedirectDataPoint(const std::string& key, | 81 virtual void DeleteSingleRedirectDataPoint(const std::string& key, |
77 PrefetchKeyType key_type); | 82 PrefetchKeyType key_type); |
78 | 83 |
| 84 // Delete data for the input |hosts|. |
| 85 virtual void DeleteManifestData(const std::vector<std::string>& hosts); |
| 86 |
79 // Deletes all data in all the tables. | 87 // Deletes all data in all the tables. |
80 virtual void DeleteAllData(); | 88 virtual void DeleteAllData(); |
81 | 89 |
82 // Removes the resources with more than |max_consecutive_misses| consecutive | 90 // Removes the resources with more than |max_consecutive_misses| consecutive |
83 // misses from |data|. | 91 // misses from |data|. |
84 static void TrimResources(PrefetchData* data, size_t max_consecutive_misses); | 92 static void TrimResources(PrefetchData* data, size_t max_consecutive_misses); |
85 | 93 |
86 // Sorts the resources by score, decreasing. | 94 // Sorts the resources by score, decreasing. |
87 static void SortResources(PrefetchData* data); | 95 static void SortResources(PrefetchData* data); |
88 | 96 |
89 // Computes score of |data|. | 97 // Computes score of |data|. |
90 static float ComputeResourceScore(const ResourceData& data); | 98 static float ComputeResourceScore(const ResourceData& data); |
91 | 99 |
92 // Removes the redirects with more than |max_consecutive_misses| consecutive | 100 // Removes the redirects with more than |max_consecutive_misses| consecutive |
93 // misses from |data|. | 101 // misses from |data|. |
94 static void TrimRedirects(RedirectData* data, size_t max_consecutive_misses); | 102 static void TrimRedirects(RedirectData* data, size_t max_consecutive_misses); |
95 | 103 |
96 // The maximum length of the string that can be stored in the DB. | 104 // The maximum length of the string that can be stored in the DB. |
97 static constexpr size_t kMaxStringLength = 1024; | 105 static constexpr size_t kMaxStringLength = 1024; |
98 | 106 |
99 protected: | 107 protected: |
100 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() | 108 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() |
101 // instead of this constructor. | 109 // instead of this constructor. |
102 ResourcePrefetchPredictorTables(); | 110 ResourcePrefetchPredictorTables(); |
103 ~ResourcePrefetchPredictorTables() override; | 111 ~ResourcePrefetchPredictorTables() override; |
104 | 112 |
105 private: | 113 private: |
106 // Represents the type of information that is stored in prefetch database. | 114 // Represents the type of information that is stored in prefetch database. |
107 enum class PrefetchDataType { RESOURCE, REDIRECT }; | 115 enum class PrefetchDataType { RESOURCE, REDIRECT, MANIFEST }; |
108 | 116 |
109 enum class TableOperationType { INSERT, REMOVE }; | 117 enum class TableOperationType { INSERT, REMOVE }; |
110 | 118 |
111 friend class PredictorDatabaseInternal; | 119 friend class PredictorDatabaseInternal; |
112 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 120 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
113 DatabaseVersionIsSet); | 121 DatabaseVersionIsSet); |
114 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 122 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
115 DatabaseIsResetWhenIncompatible); | 123 DatabaseIsResetWhenIncompatible); |
116 | 124 |
117 // Database version. Always increment it when any change is made to the data | 125 // Database version. Always increment it when any change is made to the data |
118 // schema (including the .proto). | 126 // schema (including the .proto). |
119 static constexpr int kDatabaseVersion = 5; | 127 static constexpr int kDatabaseVersion = 6; |
120 | 128 |
121 // Helper functions below help perform functions on the Url and host table | 129 // Helper functions below help perform functions on the Url and host table |
122 // using the same code. | 130 // using the same code. |
123 void GetAllResourceDataHelper(PrefetchKeyType key_type, | 131 void GetAllResourceDataHelper(PrefetchKeyType key_type, |
124 PrefetchDataMap* data_map); | 132 PrefetchDataMap* data_map); |
125 void GetAllRedirectDataHelper(PrefetchKeyType key_type, | 133 void GetAllRedirectDataHelper(PrefetchKeyType key_type, |
126 RedirectDataMap* redirect_map); | 134 RedirectDataMap* redirect_map); |
| 135 void GetAllManifestDataHelper(ManifestDataMap* manifest_map); |
| 136 |
127 bool UpdateDataHelper(PrefetchKeyType key_type, | 137 bool UpdateDataHelper(PrefetchKeyType key_type, |
128 PrefetchDataType data_type, | 138 PrefetchDataType data_type, |
129 const std::string& key, | 139 const std::string& key, |
130 const google::protobuf::MessageLite& data); | 140 const google::protobuf::MessageLite& data); |
131 void DeleteDataHelper(PrefetchKeyType key_type, | 141 void DeleteDataHelper(PrefetchKeyType key_type, |
132 PrefetchDataType data_type, | 142 PrefetchDataType data_type, |
133 const std::vector<std::string>& keys); | 143 const std::vector<std::string>& keys); |
134 | 144 |
135 // PredictorTableBase: | 145 // PredictorTableBase: |
136 void CreateTableIfNonExistent() override; | 146 void CreateTableIfNonExistent() override; |
(...skipping 11 matching lines...) Expand all Loading... |
148 | 158 |
149 static const char* GetTableName(PrefetchKeyType key_type, | 159 static const char* GetTableName(PrefetchKeyType key_type, |
150 PrefetchDataType data_type); | 160 PrefetchDataType data_type); |
151 | 161 |
152 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 162 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
153 }; | 163 }; |
154 | 164 |
155 } // namespace predictors | 165 } // namespace predictors |
156 | 166 |
157 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 167 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
OLD | NEW |