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

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

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

Powered by Google App Engine
This is Rietveld 408576698