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

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

Issue 2355273002: Redirect handling in the resource_prefetch_predictor. (Closed)
Patch Set: More nits. Created 4 years, 2 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
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 <stddef.h> 8 #include <stddef.h>
9 9
10 #include <map> 10 #include <map>
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 "base/time/time.h" 16 #include "base/time/time.h"
17 #include "chrome/browser/predictors/predictor_table_base.h" 17 #include "chrome/browser/predictors/predictor_table_base.h"
18 #include "chrome/browser/predictors/resource_prefetch_common.h" 18 #include "chrome/browser/predictors/resource_prefetch_common.h"
19 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" 19 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h"
20 #include "content/public/common/resource_type.h" 20 #include "content/public/common/resource_type.h"
21 #include "net/base/request_priority.h" 21 #include "net/base/request_priority.h"
22 #include "url/gurl.h" 22 #include "url/gurl.h"
23 23
24 namespace sql { 24 namespace sql {
25 class Statement; 25 class Statement;
26 } 26 }
27 27
28 namespace predictors { 28 namespace predictors {
29 29
30 // From resource_prefetch_predictor.proto.
30 using chrome_browser_predictors::ResourceData; 31 using chrome_browser_predictors::ResourceData;
32 using chrome_browser_predictors::RedirectData;
33 using RedirectStat = chrome_browser_predictors::RedirectData::RedirectStat;
31 34
32 // Interface for database tables used by the ResourcePrefetchPredictor. 35 // Interface for database tables used by the ResourcePrefetchPredictor.
33 // All methods except the constructor and destructor need to be called on the DB 36 // All methods except the constructor and destructor need to be called on the DB
34 // thread. 37 // thread.
35 // 38 //
36 // Currently manages: 39 // Currently manages:
37 // - UrlResourceTable - resources per Urls. 40 // - UrlResourceTable - resources per Urls.
38 // - UrlMetadataTable - misc data for Urls (like last visit time). 41 // - UrlMetadataTable - misc data for Urls (like last visit time).
42 // - UrlRedirectTable - redirects per Urls.
39 // - HostResourceTable - resources per host. 43 // - HostResourceTable - resources per host.
40 // - HostMetadataTable - misc data for hosts. 44 // - HostMetadataTable - misc data for hosts.
45 // - HostRedirectTable - redirects per host.
41 class ResourcePrefetchPredictorTables : public PredictorTableBase { 46 class ResourcePrefetchPredictorTables : public PredictorTableBase {
42 public: 47 public:
43 // Sorts the resources by score, decreasing.
44 static void SortResources(std::vector<ResourceData>* resources);
45
46 // Aggregated data for a Url or Host. Although the data differs slightly, we 48 // Aggregated data for a Url or Host. Although the data differs slightly, we
47 // store them in the same structure, because most of the fields are common and 49 // store them in the same structure, because most of the fields are common and
48 // it allows us to use the same functions. 50 // it allows us to use the same functions.
49 struct PrefetchData { 51 struct PrefetchData {
50 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); 52 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key);
51 PrefetchData(const PrefetchData& other); 53 PrefetchData(const PrefetchData& other);
52 ~PrefetchData(); 54 ~PrefetchData();
53 55
54 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } 56 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; }
55 57
56 // Is the data a host as opposed to a Url? 58 // Is the data a host as opposed to a Url?
57 PrefetchKeyType key_type; // Not const to be able to assign. 59 PrefetchKeyType key_type; // Not const to be able to assign.
58 std::string primary_key; // is_host() ? host : main frame url. 60 std::string primary_key; // is_host() ? host : main frame url.
59 61
60 base::Time last_visit; 62 base::Time last_visit;
61 std::vector<ResourceData> resources; 63 std::vector<ResourceData> resources;
62 }; 64 };
63 // Map from primary key to PrefetchData for the key. 65 // Map from primary key to PrefetchData for the key.
64 typedef std::map<std::string, PrefetchData> PrefetchDataMap; 66 typedef std::map<std::string, PrefetchData> PrefetchDataMap;
65 67
68 // Map from primary key to RedirectData for the key.
69 typedef std::map<std::string, RedirectData> RedirectDataMap;
70
66 // Returns data for all Urls and Hosts. 71 // Returns data for all Urls and Hosts.
67 virtual void GetAllData(PrefetchDataMap* url_data_map, 72 virtual void GetAllData(PrefetchDataMap* url_data_map,
68 PrefetchDataMap* host_data_map); 73 PrefetchDataMap* host_data_map,
74 RedirectDataMap* url_redirect_data_map,
75 RedirectDataMap* host_redirect_data_map);
69 76
70 // Updates data for a Url and a host. If either of the |url_data| or 77 // Updates data for a Url and a host. If either of the |url_data| or
71 // |host_data| has an empty primary key, it will be ignored. 78 // |host_data| or |url_redirect_data| or |host_redirect_data| has an empty
72 // Note that the Urls and primary key in |url_data| and |host_data| should be 79 // primary key, it will be ignored.
73 // less than |kMaxStringLength| in length. 80 // Note that the Urls and primary key in |url_data|, |host_data|,
81 // |url_redirect_data| and |host_redirect_data| should be less than
82 // |kMaxStringLength| in length.
74 virtual void UpdateData(const PrefetchData& url_data, 83 virtual void UpdateData(const PrefetchData& url_data,
75 const PrefetchData& host_data); 84 const PrefetchData& host_data,
85 const RedirectData& url_redirect_data,
86 const RedirectData& host_redirect_data);
76 87
77 // Delete data for the input |urls| and |hosts|. 88 // Delete data for the input |urls| and |hosts|.
78 virtual void DeleteData(const std::vector<std::string>& urls, 89 virtual void DeleteResourceData(const std::vector<std::string>& urls,
79 const std::vector<std::string>& hosts); 90 const std::vector<std::string>& hosts);
80 91
81 // Wrapper over DeleteData for convenience. 92 // Wrapper over DeleteResourceData for convenience.
82 virtual void DeleteSingleDataPoint(const std::string& key, 93 virtual void DeleteSingleResourceDataPoint(const std::string& key,
83 PrefetchKeyType key_type); 94 PrefetchKeyType key_type);
95
96 // Delete data for the input |urls| and |hosts|.
97 virtual void DeleteRedirectData(const std::vector<std::string>& urls,
98 const std::vector<std::string>& hosts);
99
100 // Wrapper over DeleteRedirectData for convenience.
101 virtual void DeleteSingleRedirectDataPoint(const std::string& key,
102 PrefetchKeyType key_type);
84 103
85 // Deletes all data in all the tables. 104 // Deletes all data in all the tables.
86 virtual void DeleteAllData(); 105 virtual void DeleteAllData();
87 106
107 // Sorts the resources by score, decreasing.
108 static void SortResources(std::vector<ResourceData>* resources);
109
110 // Sorts the redirects by score, decreasing.
111 static void SortRedirects(std::vector<RedirectStat>* redirects);
112
88 // The maximum length of the string that can be stored in the DB. 113 // The maximum length of the string that can be stored in the DB.
89 static constexpr size_t kMaxStringLength = 1024; 114 static constexpr size_t kMaxStringLength = 1024;
90 115
91 private: 116 private:
117 // Represents the type of information that is stored in prefetch database.
118 enum PrefetchDataType {
119 PREFETCH_DATA_TYPE_RESOURCE,
120 PREFETCH_DATA_TYPE_REDIRECT
121 };
122
92 friend class PredictorDatabaseInternal; 123 friend class PredictorDatabaseInternal;
93 friend class MockResourcePrefetchPredictorTables; 124 friend class MockResourcePrefetchPredictorTables;
94 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, 125 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest,
95 DatabaseVersionIsSet); 126 DatabaseVersionIsSet);
96 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, 127 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest,
97 DatabaseIsResetWhenIncompatible); 128 DatabaseIsResetWhenIncompatible);
98 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, ComputeScore); 129 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest,
130 ComputeResourceScore);
99 131
100 ResourcePrefetchPredictorTables(); 132 ResourcePrefetchPredictorTables();
101 ~ResourcePrefetchPredictorTables() override; 133 ~ResourcePrefetchPredictorTables() override;
102 134
103 // Helper functions below help perform functions on the Url and host table 135 // Helper functions below help perform functions on the Url and host table
104 // using the same code. 136 // using the same code.
105 void GetAllDataHelper(PrefetchKeyType key_type, 137 void GetAllResourceDataHelper(PrefetchKeyType key_type,
106 PrefetchDataMap* data_map, 138 PrefetchDataMap* data_map,
107 std::vector<std::string>* to_delete); 139 std::vector<std::string>* to_delete);
108 bool UpdateDataHelper(const PrefetchData& data); 140 void GetAllRedirectDataHelper(PrefetchKeyType key_type,
141 RedirectDataMap* redirect_map);
142 bool UpdateResourceDataHelper(PrefetchKeyType key_type,
143 const PrefetchData& data);
144 bool UpdateRedirectDataHelper(PrefetchKeyType key_type,
145 const RedirectData& data);
109 void DeleteDataHelper(PrefetchKeyType key_type, 146 void DeleteDataHelper(PrefetchKeyType key_type,
147 PrefetchDataType data_type,
110 const std::vector<std::string>& keys); 148 const std::vector<std::string>& keys);
111 149
112 // Returns true if the strings in the |data| are less than |kMaxStringLength| 150 // Returns true if the strings in the |data| are less than |kMaxStringLength|
113 // in length. 151 // in length.
114 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); 152 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data);
153 static bool StringsAreSmallerThanDBLimit(const RedirectData& data);
115 154
116 // Computes score of |data|. 155 // Computes score of |data|.
117 static float ComputeScore(const ResourceData& data); 156 static float ComputeResourceScore(const ResourceData& data);
157 static float ComputeRedirectScore(const RedirectStat& data);
118 158
119 // PredictorTableBase methods. 159 // PredictorTableBase methods.
120 void CreateTableIfNonExistent() override; 160 void CreateTableIfNonExistent() override;
121 void LogDatabaseStats() override; 161 void LogDatabaseStats() override;
122 162
123 // Database version. Always increment it when any change is made to the data 163 // Database version. Always increment it when any change is made to the data
124 // schema (including the .proto). 164 // schema (including the .proto).
125 static constexpr int kDatabaseVersion = 2; 165 static constexpr int kDatabaseVersion = 3;
126 166
127 static bool DropTablesIfOutdated(sql::Connection* db); 167 static bool DropTablesIfOutdated(sql::Connection* db);
128 static int GetDatabaseVersion(sql::Connection* db); 168 static int GetDatabaseVersion(sql::Connection* db);
129 static bool SetDatabaseVersion(sql::Connection* db, int version); 169 static bool SetDatabaseVersion(sql::Connection* db, int version);
130 170
131 // Helpers to return Statements for cached Statements. The caller must take 171 // Helpers to return Statements for cached Statements. The caller must take
132 // ownership of the return Statements. 172 // ownership of the return Statements.
133 sql::Statement* GetUrlResourceDeleteStatement(); 173 sql::Statement* GetUrlResourceDeleteStatement();
134 sql::Statement* GetUrlResourceUpdateStatement(); 174 sql::Statement* GetUrlResourceUpdateStatement();
135 sql::Statement* GetUrlMetadataDeleteStatement(); 175 sql::Statement* GetUrlMetadataDeleteStatement();
136 sql::Statement* GetUrlMetadataUpdateStatement(); 176 sql::Statement* GetUrlMetadataUpdateStatement();
177 sql::Statement* GetUrlRedirectDeleteStatement();
178 sql::Statement* GetUrlRedirectUpdateStatement();
137 179
138 sql::Statement* GetHostResourceDeleteStatement(); 180 sql::Statement* GetHostResourceDeleteStatement();
139 sql::Statement* GetHostResourceUpdateStatement(); 181 sql::Statement* GetHostResourceUpdateStatement();
140 sql::Statement* GetHostMetadataDeleteStatement(); 182 sql::Statement* GetHostMetadataDeleteStatement();
141 sql::Statement* GetHostMetadataUpdateStatement(); 183 sql::Statement* GetHostMetadataUpdateStatement();
184 sql::Statement* GetHostRedirectDeleteStatement();
185 sql::Statement* GetHostRedirectUpdateStatement();
142 186
143 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); 187 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables);
144 }; 188 };
145 189
146 } // namespace predictors 190 } // namespace predictors
147 191
148 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ 192 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698